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

      

  • 相关阅读:
    The network bridge on device VMnet0 is not running
    QuickContactBadge去掉三角
    在Android Studio中调用so中的方法
    Android Studio动态调试smali代码
    用AndroidStudio创建so
    Android逆向 破解第一个Android程序
    Java配置----JDK开发环境搭建及环境变量配置
    AndroidKiller报.smali文件丢失问题解决(关闭Android Studio的Instant Run)
    Android逆向 Android平台虚拟机
    Android逆向 APK文件组成
  • 原文地址:https://www.cnblogs.com/arhatlohan/p/4733159.html
Copyright © 2011-2022 走看看