-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04-unusual-database.py
51 lines (40 loc) · 1.33 KB
/
04-unusual-database.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
import asyncio
store = {"version": "UDP KV Store v1.0"}
class UnusualDatabaseProtocol(asyncio.DatagramProtocol):
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, client_address):
request = data.decode()
print(client_address, "sent", request)
result = parse_request(request)
if result:
# Send the value for the given key
self.transport.sendto(result.encode("utf-8"), client_address)
def parse_request(message):
split = message.split("=", 1)
if len(split) == 1:
# It is a query for a key
key = split[0]
return f'{key}={store.get(key,"")}'
else:
# Insert the key
# If the client tries to modify the version key, ignore it
key, value = split
if key != "version":
store[key] = value
return None
def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
transport, protocol = loop.run_until_complete(
loop.create_datagram_endpoint(
lambda: UnusualDatabaseProtocol(), local_addr=("0.0.0.0", 8000)
)
)
try:
loop.run_forever()
finally:
transport.close()
loop.close()
if __name__ == "__main__":
main()