-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPysocketserver.py
More file actions
64 lines (51 loc) · 1.86 KB
/
Copy pathPysocketserver.py
File metadata and controls
64 lines (51 loc) · 1.86 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
import socket
import os
import threading
import socketserver
SERVER_HOST = 'localhost'
SERVER_PORT = 0
BUF_SIZE = 2048
ECHO_MSG = 'Hello echo Server'
class ForkedClient():
def __init__(self, ip, port):
self.sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
self.sock.connect((ip, port))
def run(self):
current_process_id = os.getpid()
sent_data_len = self.sock.send(bytes(ECHO_MSG, 'utf-8'))
response = self.sock.recv(BUF_SIZE)
print("PID %s received: %s" % (current_process_id, response[5:]))
def shutdown(self):
self.sock.close()
class ForkingServerRequestHandler(socketserver.BaseRequestHandler):
def handle(self):
#Send the echo back to the client
#received = str(sock.recv(1024), "utf-8")
data = str(self.request.recv(BUF_SIZE), 'utf-8')
current_process_id = os.getpid()
response = '%s: %s' % (current_process_id, data)
print ("Server sending response [current_process_id: data] = [%s]" % response)
self.request.send(bytes(response, 'utf-8'))
return
class ForkingServer(socketserver.ForkingMixIn, socketserver.TCPServer, ):
pass
def main():
server = ForkingServer((SERVER_HOST, SERVER_PORT),ForkingServerRequestHandler)
ip, port = server.server_address
server_thread = threading.Thread(target=server.serve_forever)
server_thread.setDaemon(True)
server_thread.start()
print("Server loop running PID: %s" % os.getpid())
client1 = ForkedClient(ip, port)
client1.run()
print("First client running")
client2 = ForkedClient(ip, port)
client2.run()
print("Second client running")
# Clean them up
server.shutdown()
client1.shutdown()
client2.shutdown()
server.socket.close()
if __name__ == '__main__':
main()