summaryrefslogtreecommitdiff
path: root/examples/network/http_server.py
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-02-27 22:03:52 -0500
committerGitHub <noreply@github.com>2018-02-27 22:03:52 -0500
commit568c04e6afa8016062f685b2198c0209f62827f4 (patch)
treed07b76119f715add462983b335797adf64b61249 /examples/network/http_server.py
parent6a2379fd0bfe10017d8abc5c36ef1d470c0d9b27 (diff)
parentea633117d01d94e8d56ed94344e4b3e46b4eef03 (diff)
Merge pull request #650 from tannewt/merge_2x3.0.0-alpha.2
Merge in commits from 2.x branch.
Diffstat (limited to 'examples/network/http_server.py')
-rw-r--r--examples/network/http_server.py64
1 files changed, 0 insertions, 64 deletions
diff --git a/examples/network/http_server.py b/examples/network/http_server.py
deleted file mode 100644
index e3a66e828..000000000
--- a/examples/network/http_server.py
+++ /dev/null
@@ -1,64 +0,0 @@
-try:
- import usocket as socket
-except:
- import socket
-
-
-CONTENT = b"""\
-HTTP/1.0 200 OK
-
-Hello #%d from MicroPython!
-"""
-
-def main(micropython_optimize=False):
- s = socket.socket()
-
- # Binding to all interfaces - server will be accessible to other hosts!
- ai = socket.getaddrinfo("0.0.0.0", 8080)
- print("Bind address info:", ai)
- addr = ai[0][-1]
-
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
- s.bind(addr)
- s.listen(5)
- print("Listening, connect your browser to http://<this_host>:8080/")
-
- counter = 0
- while True:
- res = s.accept()
- client_sock = res[0]
- client_addr = res[1]
- print("Client address:", client_addr)
- print("Client socket:", client_sock)
-
- if not micropython_optimize:
- # To read line-oriented protocol (like HTTP) from a socket (and
- # avoid short read problem), it must be wrapped in a stream (aka
- # file-like) object. That's how you do it in CPython:
- client_stream = client_sock.makefile("rwb")
- else:
- # .. but MicroPython socket objects support stream interface
- # directly, so calling .makefile() method is not required. If
- # you develop application which will run only on MicroPython,
- # especially on a resource-constrained embedded device, you
- # may take this shortcut to save resources.
- client_stream = client_sock
-
- print("Request:")
- req = client_stream.readline()
- print(req)
- while True:
- h = client_stream.readline()
- if h == b"" or h == b"\r\n":
- break
- print(h)
- client_stream.write(CONTENT % counter)
-
- client_stream.close()
- if not micropython_optimize:
- client_sock.close()
- counter += 1
- print()
-
-
-main()