-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
53 lines (48 loc) · 2.16 KB
/
server.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
# This file should be run first so that the client has something to attach to
import socket
from time import ctime
from re import split
from sys import argv as args
import serverTools
host = ""
# optionally takes port from command line
port = args[2] if len(args) >= 3 else 21567
bufSize = 1024
addr = (host, port)
# Load tools into dictionary
# Read the exports list in serverTools and create a string -> function dictionary from it
tools = {t:getattr(serverTools, t) for t in serverTools.exports}
while True:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv:
serv.bind(addr)
serv.listen(5)
print("Waiting for connection...")
with serv.accept()[0] as client:
print("Client connected")
# helper function to save typing
send = lambda data: client.send(bytes(data, 'utf-8'))
while True:
data = client.recv(bufSize).decode('utf-8')
# disconnect after receiving empty string
# and wait for a new connection
if not data:
print('Client disconnecting')
break
# split into pieces by whitespace to get command and parameters
commands = split(r'\s+', data.strip())
try:
if commands[0] in tools:
send(tools[commands[0]](*commands[1:]))
else: # not valid command, send timestamp and data back
send("{}: Received '{}'".format(ctime(), data))
# covers incorrect parameters, either number given or type
except (TypeError, ValueError):
send("Error: Invalid parameters '{}' for command '{}'".format(" ".join(commands[1:]), commands[0]))
# exit the server
# sockets are closed automatically by with statements
except serverTools.ExitServerException:
print("Exiting Server")
quit()
# catch all errors so it doesn't crash if input is bad
except:
send("Error running '{}'".format(data))