2014-01-20 13:35:18 -05:00
|
|
|
try:
|
2016-04-02 10:28:42 -04:00
|
|
|
import usocket as socket
|
2014-01-20 13:35:18 -05:00
|
|
|
except:
|
2016-04-02 10:28:42 -04:00
|
|
|
import socket
|
2014-01-20 13:35:18 -05:00
|
|
|
|
|
|
|
|
2016-04-02 12:13:39 -04:00
|
|
|
def main(use_stream=False):
|
|
|
|
s = socket.socket()
|
2014-01-18 16:49:06 -05:00
|
|
|
|
2016-04-02 12:13:39 -04:00
|
|
|
ai = socket.getaddrinfo("google.com", 80)
|
|
|
|
print("Address infos:", ai)
|
|
|
|
addr = ai[0][4]
|
2014-01-18 16:49:06 -05:00
|
|
|
|
2016-04-02 12:13:39 -04:00
|
|
|
print("Connect address:", addr)
|
|
|
|
s.connect(addr)
|
2014-01-18 16:49:06 -05:00
|
|
|
|
2016-04-02 12:13:39 -04:00
|
|
|
if use_stream:
|
|
|
|
# MicroPython socket objects support stream (aka file) interface
|
|
|
|
# directly, but the line below is needed for CPython.
|
|
|
|
s = s.makefile("rwb", 0)
|
|
|
|
s.write(b"GET / HTTP/1.0\n\n")
|
|
|
|
print(s.readall())
|
|
|
|
else:
|
|
|
|
s.send(b"GET / HTTP/1.0\n\n")
|
|
|
|
print(s.recv(4096))
|
|
|
|
|
|
|
|
|
|
|
|
main()
|