-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhttpserver.py
More file actions
37 lines (28 loc) · 842 Bytes
/
httpserver.py
File metadata and controls
37 lines (28 loc) · 842 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import usocket
import ulogging
import config
ulogging.basicConfig(level=config.LOG_LEVEL)
logger = ulogging.getLogger("httpserver")
sock = None
def setup_http_server():
global sock
try:
# setup our HTTP server
sock = usocket.socket()
# s.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
sock.bind(usocket.getaddrinfo("0.0.0.0", 80)[0][-1])
sock.listen(2)
return True
except Exception as e:
logger.error("Got exception when setting up HTTP server")
logger.error(str(e))
return False
def client_response(conn):
response = """
{"success": true}
"""
conn.send("HTTP/1.1 200 OK\n")
conn.send("Content-Type: application/json\n")
conn.send("Connection: close\n\n")
conn.sendall(response)
conn.close()