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()
    

      

  • 相关阅读:
    jenkins 邮件配置
    jenkins+git学习笔记
    用户定义的变量+HTTP Cookie 管理器组合实现接口关联+问题处理
    jmeter参数化实现之CSV Data Set Config
    Jmeter学习笔记
    除法应用遇到的问题-类型及小数点
    python2输出中文乱码问题
    python常见函数及方法
    数据库的基本操作
    使用eclipse搭建maven项目
  • 原文地址:https://www.cnblogs.com/arhatlohan/p/4733159.html
Copyright © 2011-2022 走看看