examples/http_client.py: Introduce main() function.

Allows to re-run code if it was imported as a module (e.g., on bare-metal
ports).
This commit is contained in:
Paul Sokolovsky 2016-04-02 19:13:39 +03:00
parent a5d07c3aba
commit fd2b71f972
1 changed files with 19 additions and 15 deletions

View File

@ -4,21 +4,25 @@ except:
import socket
s = socket.socket()
def main(use_stream=False):
s = socket.socket()
ai = socket.getaddrinfo("google.com", 80)
print("Address infos:", ai)
addr = ai[0][4]
ai = socket.getaddrinfo("google.com", 80)
print("Address infos:", ai)
addr = ai[0][4]
print("Connect address:", addr)
s.connect(addr)
print("Connect address:", addr)
s.connect(addr)
if 0:
# 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))
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()