Quick question about http_server_simplistic.py at examples folder

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ctrlf
Posts: 5
Joined: Mon Oct 10, 2016 1:18 pm

Quick question about http_server_simplistic.py at examples folder

Post by ctrlf » Mon Oct 10, 2016 9:27 pm

I copy and pasted using Ctrl-E this example found here https://github.com/micropython/micropyt ... plistic.py but although I receive a 200 code the page is empty. Am i doing something wrong?

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: Quick question about http_server_simplistic.py at examples folder

Post by chrisgp » Tue Oct 11, 2016 4:53 am

That example didn't work for me either when pasted with the REPL...it's important the HTTP response have \r\n as the newline characters and those might be getting lost by pasting it in.

The following version of it worked when pasting it in. The only change I made was to explicitly use \r\n for the newlines in the HTTP response rather than relying on whatever the newlines ended up being.

Code: Select all

try:
    import usocket as socket
except:
    import socket

CONTENT = b"""\
HTTP/1.0 200 OK\r\n\
\r\n\
Hello #%d from MicroPython!
"""

def main():
    s = socket.socket()
    ai = socket.getaddrinfo("0.0.0.0", 8080)
    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_s = res[0]
        client_addr = res[1]
        req = client_s.recv(4096)
        print("Request:")
        print(req)
        client_s.send(CONTENT % counter)
        client_s.close()
        counter += 1
        print()

main()

Post Reply