-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
40 lines (29 loc) · 875 Bytes
/
Copy pathserver.py
File metadata and controls
40 lines (29 loc) · 875 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
38
39
40
import asyncio
from importlib import import_module
import sqlalchemy
from constants import HOST, PORT
def execute(user, command):
args = command.split()
try:
module = import_module(f'commands.{args[0]}')
function = getattr(module, args[0])
result = function(user, args[1:])
except Exception as e:
result = "Invalid Command"
print(e)
return result
async def callback(reader, writer):
user = [None]
while True:
command = await reader.read(512)
command = command.decode()
if command == 'quit':
break
result = execute(user, command)
writer.write(result.encode())
async def server():
srvr = await asyncio.start_server(callback, HOST, PORT)
async with srvr:
await srvr.serve_forever()
if __name__ == '__main__':
asyncio.run(server())