-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (50 loc) · 2.11 KB
/
Copy pathmain.py
File metadata and controls
62 lines (50 loc) · 2.11 KB
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
import time
import sys
from pychat.server.TCPServer import TCPServer
from pychat.server.RequestHandler import RequestHandler
# def client(ip, port, message):
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# sock.connect((ip, port))
# sock.sendall(bytes(message, 'utf8'))
# response = str(sock.recv(1024), 'utf8')
# print('Received from server: {}'.format(response))
# client(ip, port, "Hello World, my name is Rubén Arrebola")
# client(ip, port, "This is another message, sended from my machine")
# client(ip, port, "Another interesting message sended from the moon, hey dude I have cookies here!")
def print_instructions():
print("Instructions:\n-> python main.py -h [HOST] -p [PORT]")
def get_args(args, default_host, default_port, bad_args = False):
result = [bad_args, default_host, default_port]
saved_parameter = ''
if len(args) > 1:
for index, cur_parameter in enumerate(args):
if saved_parameter == '-h':
result[1] = cur_parameter
elif saved_parameter == '-p':
result[2] = int(cur_parameter)
if cur_parameter == '-h':
saved_parameter = cur_parameter
elif cur_parameter == '-p':
saved_parameter = cur_parameter
else:
saved_parameter = ''
if saved_parameter != '' and index + 1 >= len(args):
result[0] = True
print("Bad arguments...\n")
print_instructions()
break
return result
if __name__ == "__main__":
HOST = 'localhost'
PORT = 3000
EXIT = False
# Get arguments passed on the command-line and assigns them to Host, Port and exit
EXIT, HOST, PORT = get_args(sys.argv, default_host=HOST, default_port=PORT, bad_args=EXIT)
# If there is not a bad argument, initialize the server
if EXIT == False:
server = TCPServer((HOST, PORT))
server.start()
while(True):
# Sleep the thread to avoid hard cpu work
time.sleep(60)
server.shutdown()