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.
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