-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·48 lines (34 loc) · 1.1 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
#!/usr/bin/env python3
"""
TiTs isn't Telegram, sorry.
This file is a basic implementation of a TiTs server
"""
HOST, PORT = "localhost", 60007
import socketserver
import threading
import time
class MyHandler (socketserver.BaseRequestHandler):
"""
the message server
"""
def handle (self):
client_ip, client_port = self.client_address
print("ip, port => {} : {}".format(client_ip, client_port))
cur_thread = threading.current_thread()
data = self.request.recv(1024)
while data:
print("T: {}".format(cur_thread.name))
print(time.time())
print(data.decode())
data = self.request.recv(1024)
class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
pass
if __name__ == "__main__":
print("server Runs on {}:{}".format(HOST, PORT))
server = ThreadedTCPServer((HOST, PORT), MyHandler)
try:
server_thread = threading.Thread(target=server.serve_forever)
server_thread.start()
except KeyboardInterrupt:
server.shutdown()
print("server shutdown")