-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCP_ECho_Server_example.py
More file actions
72 lines (60 loc) · 2.01 KB
/
TCP_ECho_Server_example.py
File metadata and controls
72 lines (60 loc) · 2.01 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
63
64
65
66
67
68
69
70
71
72
#Example app - TCP Echo Server
#Make a TCP server in a process that handles multiple clients
#Echos back the data the client sent
#Imports
import logging
import multiprocessing
import socket
import time
import select
logging.basicConfig(format='%(levelname)s - %(asctime)s: %(message)s',datefmt='%H:%M:%S', level=logging.DEBUG)
#Server
def chatserver(ip, port):
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
logging.info(f'Binding to {ip}:{port}')
server.bind((ip,port))
server.setblocking(False)
server.listen(100)
logging.info(f'Listening on {ip}:{port}')
readers = [server]
while True:
readable, writable, errored = select.select(readers,[],[],0.5)
for s in readable:
try:
if s == server:
client, address = s.accept()
client.setblocking(False)
readers.append(client)
logging.info(f'Connection: {address}')
else:
data = s.recv(1024)
if data:
logging.info(f'Echo: {data}')
s.send(data)
else:
logging.info(f'Remove: {s}')
s.close()
readers.remove(s)
except Exception as ex:
logging.warning(ex.args)
finally:
pass
#Main
def main():
svr = multiprocessing.Process(target=chatserver,args=['localhost',2067],daemon=True,name='Server')
while True:
command = input('Enter a command (start, stop)')
if command == 'start':
logging.info('Starting the server')
svr.start()
time.sleep(10)
if command == 'stop':
logging.info('Stopping the server')
svr.terminate()
svr.join()
svr.close()
logging.info('Server stopped')
break
logging.info('Application finished')
if __name__ == "__main__":
main()