-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webserver3.py
71 lines (59 loc) · 2.07 KB
/
Webserver3.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
import network
import ujson
import time
async def serve_client(reader, writer):
print("Reading...")
data = await reader.readline()
print("Read")
print(f"received {data}")
writer.write(b'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 2\r\n\r\n\r\n')
writer.drain()
print("drained")
#1. If we do this the connection gets closed seemingly before we sent the response
# But we drain so that should be impossible
await writer.wait_closed()
#2 Connection never closes, curl request never completes
#writer.close()
async def do_connect():
try:
fp = open('secrets.json')
secretsString = fp.read()
secrets = ujson.loads(secretsString)
except OSError as e:
print("Error reading secret: ", e)
sys.exit()
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(secrets['wifi']['ssid'], secrets['wifi']['password'])
while station.isconnected() == False:
print(f"Waiting for connection to wifi...")
await asyncio.sleep(1)
pass
print(f"Connection successful: {station.ifconfig()}")
async def heartbeat():
while True:
print("heartbeat")
await asyncio.sleep(1)
async def main():
print(f"Starting...")
await do_connect()
#loop = asyncio.get_event_loop()
#loop.create_task(asyncio.start_server(serve_client, "0.0.0.0", 80))
#loop.run_forever()
asyncio.create_task(heartbeat())
asyncio.run(asyncio.start_server(serve_client, "0.0.0.0", 80))
#server = await asyncio.start_server(serve_client, "0.0.0.0", 80)
#await asyncio.gather(server)
#while True:
#print("heartbeat")
#await asyncio.sleep(1) # This make it never return ... but we are in an async method...confused
#time.sleep(1) # With time.sleep it heartbeats but never responds to requests
print("Starting main")
asyncio.run(main())
loop = asyncio.get_event_loop()
try:
loop.run_forever()
except KeyboardInterrupt:
loop.close()
print("Finished")