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)
|
2016-10-09 17:36:04 +01:00
|
|
|
s.write(b"GET / HTTP/1.0\r\n\r\n")
|
2016-11-13 21:24:45 +00:00
|
|
|
print(s.read())
|
2016-04-02 17:13:39 +01:00
|
|
|
else:
|
2016-10-09 17:36:04 +01:00
|
|
|
s.send(b"GET / HTTP/1.0\r\n\r\n")
|
2016-04-02 17:13:39 +01:00
|
|
|
print(s.recv(4096))
|
|
|
|
|
2016-07-22 22:41:19 +01:00
|
|
|
s.close()
|
|
|
|
|
2016-04-02 17:13:39 +01:00
|
|
|
|
|
|
|
main()
|