circuitpython/examples/network/http_client.py
Paul Sokolovsky 614deb82c7 examples/http_client*: Be sure to close socket.
Otherwise, on bare-metal/RTOS systems can lead to resource leaks.
2016-07-23 00:41:19 +03:00

31 lines
625 B
Python

try:
import usocket as socket
except:
import socket
def main(use_stream=False):
s = socket.socket()
ai = socket.getaddrinfo("google.com", 80)
print("Address infos:", ai)
addr = ai[0][-1]
print("Connect address:", addr)
s.connect(addr)
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))
s.close()
main()