Skip to content

Commit fd2b71f

Browse files
committed
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).
1 parent a5d07c3 commit fd2b71f

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

examples/network/http_client.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
import socket
55

66

7-
s = socket.socket()
7+
def main(use_stream=False):
8+
s = socket.socket()
89

9-
ai = socket.getaddrinfo("google.com", 80)
10-
print("Address infos:", ai)
11-
addr = ai[0][4]
10+
ai = socket.getaddrinfo("google.com", 80)
11+
print("Address infos:", ai)
12+
addr = ai[0][4]
1213

13-
print("Connect address:", addr)
14-
s.connect(addr)
14+
print("Connect address:", addr)
15+
s.connect(addr)
1516

16-
if 0:
17-
# MicroPython socket objects support stream (aka file) interface
18-
# directly, but the line below is needed for CPython.
19-
s = s.makefile("rwb", 0)
20-
s.write(b"GET / HTTP/1.0\n\n")
21-
print(s.readall())
22-
else:
23-
s.send(b"GET / HTTP/1.0\n\n")
24-
print(s.recv(4096))
17+
if use_stream:
18+
# MicroPython socket objects support stream (aka file) interface
19+
# directly, but the line below is needed for CPython.
20+
s = s.makefile("rwb", 0)
21+
s.write(b"GET / HTTP/1.0\n\n")
22+
print(s.readall())
23+
else:
24+
s.send(b"GET / HTTP/1.0\n\n")
25+
print(s.recv(4096))
26+
27+
28+
main()

0 commit comments

Comments
 (0)