diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-04-02 19:13:39 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-04-02 19:13:39 +0300 |
commit | fd2b71f972d29496768892467c7b2024af25f157 (patch) | |
tree | 1f5e3c6b6b1e17c7f02c83296e067f694c8997e4 /examples/network/http_client.py | |
parent | a5d07c3aba1cb3f02489777cb52ddd1908602150 (diff) | |
download | micropython-fd2b71f972d29496768892467c7b2024af25f157.tar.gz micropython-fd2b71f972d29496768892467c7b2024af25f157.zip |
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).
Diffstat (limited to 'examples/network/http_client.py')
-rw-r--r-- | examples/network/http_client.py | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/examples/network/http_client.py b/examples/network/http_client.py index 548756bbf4..b74daeb08d 100644 --- a/examples/network/http_client.py +++ b/examples/network/http_client.py @@ -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() |