Skip to content

Commit a5d07c3

Browse files
committed
examples/http_client.py: Improve CPython compatibility in stream mode.
1 parent a5d2af7 commit a5d07c3

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

examples/network/http_client.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
try:
2-
import usocket as _socket
2+
import usocket as socket
33
except:
4-
import _socket
4+
import socket
55

66

7-
s = _socket.socket()
7+
s = socket.socket()
88

9-
ai = _socket.getaddrinfo("google.com", 80)
9+
ai = socket.getaddrinfo("google.com", 80)
1010
print("Address infos:", ai)
1111
addr = ai[0][4]
1212

1313
print("Connect address:", addr)
1414
s.connect(addr)
1515

1616
if 0:
17-
# MicroPython rawsocket module supports file interface directly
18-
s.write("GET / HTTP/1.0\n\n")
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")
1921
print(s.readall())
2022
else:
2123
s.send(b"GET / HTTP/1.0\n\n")

0 commit comments

Comments
 (0)