summaryrefslogtreecommitdiffstatshomepage
path: root/examples/network/http_client.py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-02 17:28:42 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-04-02 17:28:42 +0300
commita5d07c3aba1cb3f02489777cb52ddd1908602150 (patch)
treeea8cb0e6d3d009d4f3728f07971d0b0c5daec4ac /examples/network/http_client.py
parenta5d2af7949b2af4fd1bae8f477f2c938634aa489 (diff)
downloadmicropython-a5d07c3aba1cb3f02489777cb52ddd1908602150.tar.gz
micropython-a5d07c3aba1cb3f02489777cb52ddd1908602150.zip
examples/http_client.py: Improve CPython compatibility in stream mode.
Diffstat (limited to 'examples/network/http_client.py')
-rw-r--r--examples/network/http_client.py14
1 files changed, 8 insertions, 6 deletions
diff --git a/examples/network/http_client.py b/examples/network/http_client.py
index d71f76b65f..548756bbf4 100644
--- a/examples/network/http_client.py
+++ b/examples/network/http_client.py
@@ -1,12 +1,12 @@
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]
@@ -14,8 +14,10 @@ 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")