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)
|
2016-05-02 17:45:37 -04:00
|
|
|
addr = ai[0][-1]
|
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)
|
2016-10-09 12:36:04 -04:00
|
|
|
s.write(b"GET / HTTP/1.0\r\n\r\n")
|
2016-11-13 16:24:45 -05:00
|
|
|
print(s.read())
|
2016-04-02 12:13:39 -04:00
|
|
|
else:
|
2016-10-09 12:36:04 -04:00
|
|
|
s.send(b"GET / HTTP/1.0\r\n\r\n")
|
2016-04-02 12:13:39 -04:00
|
|
|
print(s.recv(4096))
|
|
|
|
|
2016-07-22 17:41:19 -04:00
|
|
|
s.close()
|
|
|
|
|
2016-04-02 12:13:39 -04:00
|
|
|
|
|
|
|
main()
|