-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreadedTCPServer.py
73 lines (59 loc) · 2.34 KB
/
ThreadedTCPServer.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from SocketServer import ThreadingMixIn
from Queue import Queue
import time
import socket
import logging
import threading
import SocketServer
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s '
'[%(levelname)s] (%(threadName)-10s) %(message)s',)
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
file = open('test.log', 'wb')
data = self.request.recv(1024)
cur_thread = threading.current_thread()
response = "{} {}: {}\n".format(time.ctime(), cur_thread.name, data)
file.write(response)
self.request.sendall(response)
file.close()
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# use a thread pool instead of a new thread on every request
numThreads = 50
allow_reuse_address = True
def serve_forever(self):
# Handle one request at a time until doomsday.
self.requests = Queue(self.numThreads)
for x in range(self.numThreads):
t = threading.Thread(target=self.process_request_thread)
t.setDaemon(1)
t.start()
# server main loop
while True:
self.handle_request()
self.server_close()
def process_request_thread(self):
# obtain request from queue instead of directly from server socket
while True:
ThreadingMixIn.process_request_thread(self, *self.requests.get())
def handle_request(self):
# simply collect requests and put them on the queue for the workers.
try:
request, client_address = self.get_request()
except socket.error:
return
if self.verify_request(request, client_address):
self.requests.put((request, client_address))
if __name__ == "__main__":
HOST, PORT = "localhost", 12345
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
ip, port = server.server_address
# Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
try:
# logging.debug("server loop running in thread:", server_thread.name)
server.serve_forever()
except KeyboardInterrupt:
pass