2014-01-20 18:35:18 +00:00
|
|
|
try:
|
2016-04-02 15:28:42 +01:00
|
|
|
import usocket as socket
|
2014-01-20 18:35:18 +00:00
|
|
|
except:
|
2016-04-02 15:28:42 +01:00
|
|
|
import socket
|
2014-01-20 18:35:18 +00:00
|
|
|
|
|
|
|
|
2016-04-02 17:13:39 +01:00
|
|
|
def main(use_stream=False):
|
|
|
|
s = socket.socket()
|
2014-01-18 21:49:06 +00:00
|
|
|
|
2016-04-02 17:13:39 +01:00
|
|
|
ai = socket.getaddrinfo("google.com", 80)
|
|
|
|
print("Address infos:", ai)
|
2016-05-02 22:45:37 +01:00
|
|
|
addr = ai[0][-1]
|
2014-01-18 21:49:06 +00:00
|
|
|
|
2016-04-02 17:13:39 +01:00
|
|
|
print("Connect address:", addr)
|
|
|
|
s.connect(addr)
|
2014-01-18 21:49:06 +00:00
|
|
|
|
2016-04-02 17:13:39 +01: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()
|