zoukankan      html  css  js  c++  java
  • ForkingMixIn 示例

    import os 
    import socket
    import threading
    import SocketServer
    
    SERVER_HOST = 'localhost'
    SERVER_PORT = 0 #tells the kernel to pick up a port dynamically
    BUF_SIZE = 1024
    ECHO_MSG = 'Hello echo server!'
    
    class ForkingClient():
    	'''A client to test forking server'''
    	def __init__(self, ip, port):
    		#Create a socket
    		self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    		#Connect to the server
    		self.sock.connect((ip, port))
    
    	def run(self):
    		'''Send the data to the server'''
    		current_process_id = os.getpid()
    		print "PID %s Sending echo message to the server: '%s' "  %(current_process_id, ECHO_MSG)
    		send_data_length = self.sock.send(ECHO_MSG)
    		print "Sent: %d characters, so far..." %send_data_length
    
    		#Display server response
    		response = self.sock.recv(BUF_SIZE)
    		print "PID %s received: %s" %(current_process_id, response[5:])
    
    	def shutdown(self):
    		'''Clean up the client socket'''
    		self.sock.close()
    
    
    class ForkingServerRequestHandler(SocketServer.BaseRequestHandler):
    	def handler(self):
    		#Send the echo back to the client
    		data = self.request.recv(BUF_SIZE)
    		current_process_id = os.getpid()
    		reponse = '%s: %s' %(current_process_id, data)
    		print "Server sending response [current_process_id: data] = [%s]" %response
    		self.request.send(response)
    		return
    
    
    class ForkingServer(SocketServer.ForkingMixIn, SocketServer.TCPServer):
    	'''Nothing to add here, inherited everything necessary from parents'''
    	pass
    
    def main():
    	#Launch the server
    	server = ForkingServer((SERVER_HOST, SERVER_PORT), ForkingServerRequestHandler)
    	ip, port =server.server_address  #Retrieve the port number
    	server_thread = threading.Thread(target=server.serve_forever)
    	server_thread.setDaemon(True)  #Don't hang on exit
    	server_thread.start()
    	print "Server loop running PID: %s"  %os.getpid()
    
    	#Launch the client(s)
    	client1 = ForkingClient(ip, port)
    	client1.run()
    
    	client2 = ForkingClient(ip, port)
    	client2.run()
    
    	#Clean them up
    	server.shutdown()
    	client1.shutdown()
    	client2.shutdown()
    	server.socket.close()
    
    
    if __name__=='__main__':
    	main()
    

      

  • 相关阅读:
    RFID之linux下利用PN532对M1卡(饭卡,
    Ubuntu server配置远程VNC服务
    如何在Linux命令行中剪裁图像
    CentOS 6.3 yum安装LAMP(Apache+MySQL+PHP)
    ubuntu16.04 安装Opencv 3.1.0 import cv2 报错ImportError: No module named hdf5
    ubuntu16.04 安装Opencv 3.1.0 fatal error: hdf5.h: 没有那个文件或目录
    通过子网掩码确定主机地址
    单调数据结构
    利用Python分析羊车门问题
    Welcome To My Blog!!!
  • 原文地址:https://www.cnblogs.com/arhatlohan/p/4733159.html
Copyright © 2011-2022 走看看