|
| 1 | +# |
| 2 | +# MicroPython http_server_simplistic.py example |
| 3 | +# |
| 4 | +# This example shows how to write the smallest possible HTTP |
| 5 | +# server in MicroPython. With comments and convenience code |
| 6 | +# removed, this example can be compressed literally to ten |
| 7 | +# lines. There's a catch though - read comments below for |
| 8 | +# details, and use this code only for quick hacks, preferring |
| 9 | +# http_server.py for "real thing". |
| 10 | +# |
| 11 | +try: |
| 12 | + import usocket as socket |
| 13 | +except: |
| 14 | + import socket |
| 15 | + |
| 16 | + |
| 17 | +CONTENT = b"""\ |
| 18 | +HTTP/1.0 200 OK |
| 19 | +
|
| 20 | +Hello #%d from MicroPython! |
| 21 | +""" |
| 22 | + |
| 23 | +def main(): |
| 24 | + s = socket.socket() |
| 25 | + |
| 26 | + # Bind to (allow to be connected on ) all interfaces. This means |
| 27 | + # this server will be accessible to other hosts on your local |
| 28 | + # network, and if your server has direct (non-firewalled) connection |
| 29 | + # to the Internet, then to anyone on the Internet. We bind to all |
| 30 | + # interfaces to let this example work easily on embedded MicroPython |
| 31 | + # targets, which you will likely access from another machine on your |
| 32 | + # local network. Take care when running this on an Internet-connected |
| 33 | + # machine though! Replace "0.0.0.0" with "127.0.0.1" if in doubt, to |
| 34 | + # make the server accessible only on the machine it runs on. |
| 35 | + ai = socket.getaddrinfo("0.0.0.0", 8080) |
| 36 | + print("Bind address info:", ai) |
| 37 | + addr = ai[0][-1] |
| 38 | + |
| 39 | + # A port on which a socket listened remains inactive during some time. |
| 40 | + # This means that if you run this sample, terminate it, and run again |
| 41 | + # you will likely get an error. To avoid this timeout, set SO_REUSEADDR |
| 42 | + # socket option. |
| 43 | + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 44 | + |
| 45 | + s.bind(addr) |
| 46 | + s.listen(5) |
| 47 | + print("Listening, connect your browser to http://<this_host>:8080/") |
| 48 | + |
| 49 | + counter = 0 |
| 50 | + while True: |
| 51 | + res = s.accept() |
| 52 | + client_s = res[0] |
| 53 | + client_addr = res[1] |
| 54 | + print("Client address:", client_addr) |
| 55 | + print("Client socket:", client_s) |
| 56 | + # We assume here that .recv() call will read entire HTTP request |
| 57 | + # from client. This is usually true, at least on "big OS" systems |
| 58 | + # like Linux/MacOS/Windows. But that doesn't have to be true in |
| 59 | + # all cases, in particular on embedded systems, when there can |
| 60 | + # easily be "short recv", where it returns much less than requested |
| 61 | + # data size. That's why this example is called "simplistic" - it |
| 62 | + # shows that writing a web server in Python that *usually works* is |
| 63 | + # ten lines of code, and you can use this technique for quick hacks |
| 64 | + # and experimentation. But don't do it like that in production |
| 65 | + # applications - instead, parse HTTP request properly, as shown |
| 66 | + # by http_server.py example. |
| 67 | + req = client_s.recv(4096) |
| 68 | + print("Request:") |
| 69 | + print(req) |
| 70 | + client_s.send(CONTENT % counter) |
| 71 | + client_s.close() |
| 72 | + counter += 1 |
| 73 | + print() |
| 74 | + |
| 75 | + |
| 76 | +main() |
0 commit comments