kopia lustrzana https://github.com/micropython/micropython
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).pull/1958/head
rodzic
a5d07c3aba
commit
fd2b71f972
|
@ -4,6 +4,7 @@ except:
|
|||
import socket
|
||||
|
||||
|
||||
def main(use_stream=False):
|
||||
s = socket.socket()
|
||||
|
||||
ai = socket.getaddrinfo("google.com", 80)
|
||||
|
@ -13,7 +14,7 @@ addr = ai[0][4]
|
|||
print("Connect address:", addr)
|
||||
s.connect(addr)
|
||||
|
||||
if 0:
|
||||
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)
|
||||
|
@ -22,3 +23,6 @@ if 0:
|
|||
else:
|
||||
s.send(b"GET / HTTP/1.0\n\n")
|
||||
print(s.recv(4096))
|
||||
|
||||
|
||||
main()
|
||||
|
|
Ładowanie…
Reference in New Issue