Setting up a html server on a WiPy 3.0

Hey Everybody,
I’ve been trying to create an HTML server on the WiPy 3,0 but have come up short. Running the code

> import socket
> 
> html = open("index.html").read()
> html = str.encode(html)
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind(('0.0.0.0', 80))
> s.listen()
> light = False
> lock = False
> hasMoved = False
> while True:
>     conn, addr = s.accept()
>     try:
>         request = conn.recv(1024)
>         lines = request.split(b'\r\n')
>         method, uri, version = lines[0].split(b' ')
>         if uri == b'/':
>             conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
>             html = open("index.html").read()
>             html = str.encode(html)
>             conn.send(html)
>         elif uri == b'/favicon.ico':
>             conn.send(b'HTTP/1.1 404 Not Found\r\n\r\n')
>         elif uri == b'/toggleLight':
>             conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
>             light = not light
>         elif uri == b'/toggleLock':
>             conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
>             lock = not lock
>         elif uri == b'/testMove':
>             conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
>             hasMoved = not hasMoved
>         elif uri == b'/status':
>             conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
>             conn.send(str.encode('{}:{}:{}'.format(light, lock, hasMoved)))
>     except Exception as e:
>         print(e)
>         conn.send(b'HTTP/1.1 500 Internal Server Error')
>     conn.close()

I can run it normally but when I try to run it on the WiPy 3.0 it doesn’t seem to work. I can’t tell if the server is running or of I just can’t connect to it. When I try and connect to the server, 192.168.4.1, it just says Refused to Connect.
25%20PM
If anyone knows how to improve the code or just some other way to run a server off the WiPy 3.0.

All it has to do is toggle 2 variables (Light & Lock) and read 1 variable (hasMoved), all on and off.

Thanks
Jordan Whittaker

1 Like

Jordan, you should verify the WiPy is listening using the ‘netstat’ tool. Linux, MacOS and Windows have this built in to the command line/prompt/terminal. On Windows, try the below line.

netstat 192.168.4.1 80

That should tell you if it’s listening on port 80. If it’s not responding, try changing the port to something else (e.g. 8080) since 80 might need admin privileges to access. Verify that runs through this code.

netstat 192.168.4.1 8080

The other alternative is to modify your code to the below, which should get a very basic server running;

import socket

HOST = '0.0.0.0'
PORT = 8080        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

Source: Socket Programming in Python (Guide) – Real Python

1 Like