-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadingServer.py
More file actions
26 lines (23 loc) · 891 Bytes
/
MultiThreadingServer.py
File metadata and controls
26 lines (23 loc) · 891 Bytes
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
#!/usr/bin/python
import socketserver
class RequestHandler(SocketServer.StreamRequestHandler):
"Handles one request to mirror some text."
def handle(self):
"""Read from StreamRequestHandler's provided rfile member,
which contains the input from the client. Mirror the text
and write it to the wfile member, which contains the output
to be sent to the client."""
l = True
while l:
l = self.rfile.readline().strip()
if l:
self.wfile.write(l[::-1] + bytes('\n', ‘utf-8’))
if __name__ == '__main__':
import sys
if len(sys.argv) < 3:
print('Usage: %s [hostname] [port number]' % sys.argv[0])
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
server = socketserver.ThreadingTCPServer((hostname, port), RequestHandler)
server.serve_forever()