Skip to content

Commit

Permalink
examples/http_client.py: Improve CPython compatibility in stream mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfalcon committed Apr 2, 2016
1 parent a5d2af7 commit a5d07c3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions examples/network/http_client.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
try:
import usocket as _socket
import usocket as socket
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)
addr = ai[0][4]

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

if 0:
# MicroPython rawsocket module supports file interface directly
s.write("GET / HTTP/1.0\n\n")
# 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")
Expand Down

0 comments on commit a5d07c3

Please sign in to comment.