kopia lustrzana https://github.com/micropython/micropython
examples/http_client.py: Improve CPython compatibility in stream mode.
rodzic
a5d2af7949
commit
a5d07c3aba
|
@ -1,12 +1,12 @@
|
||||||
try:
|
try:
|
||||||
import usocket as _socket
|
import usocket as socket
|
||||||
except:
|
except:
|
||||||
import _socket
|
import socket
|
||||||
|
|
||||||
|
|
||||||
s = _socket.socket()
|
s = socket.socket()
|
||||||
|
|
||||||
ai = _socket.getaddrinfo("google.com", 80)
|
ai = socket.getaddrinfo("google.com", 80)
|
||||||
print("Address infos:", ai)
|
print("Address infos:", ai)
|
||||||
addr = ai[0][4]
|
addr = ai[0][4]
|
||||||
|
|
||||||
|
@ -14,8 +14,10 @@ print("Connect address:", addr)
|
||||||
s.connect(addr)
|
s.connect(addr)
|
||||||
|
|
||||||
if 0:
|
if 0:
|
||||||
# MicroPython rawsocket module supports file interface directly
|
# MicroPython socket objects support stream (aka file) interface
|
||||||
s.write("GET / HTTP/1.0\n\n")
|
# 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())
|
print(s.readall())
|
||||||
else:
|
else:
|
||||||
s.send(b"GET / HTTP/1.0\n\n")
|
s.send(b"GET / HTTP/1.0\n\n")
|
||||||
|
|
Ładowanie…
Reference in New Issue