Python Write a send and receive data with the socket, their own test concurrency can reach 10k+, with a 20-minute one, not much direct code
#!/usr/bin/python#coding:utf-8# author:51reboot.comimport socket,selectimport os,sys,copyhost = ' 0.0.0.0 ' Port = 8089s = socket.socket (socket.af_inet, socket. SOCK_STREAM) s.setsockopt (socket. Sol_socket, socket. SO_REUSEADDR, 1) # release Port s.setblocking (0) # set to non-blocking s.bind ((Host,port)) S.listen (Ten) # Waiting on column # listen: accept -> read -> process -> write -> closing# accpet: read -> process -> wriet -> closing# state machine Template state_m = { -1:{ # sock ' s ': ' Read ', # sock status ' R ':{ # read information ' have ' :0, # number of bytes read ' Need ':10, # number of bytes to read }, ' W ':{ # write 's Data ' have ':0, # data that has been write ' Need ':0, # data needed for write }, ' d ': ', # For storing data },}statE= {}def state_machine (sock): if sock == s: # Monitoring socket conn,addr = s.accept () s.setblocking (0) # set to non-blocking state[conn] = copy.deepcopy (STATE_M[-1]) # assign an initial value to this connection r_list.append (conn) # add sock to list of required r_list information else: # already accept the Socket stat = state[sock][' s '] # save sock Status information if stat == ' read ': #如果状态为read, you need to read the operation if state[sock][' R ' [' Need '] == 0: # If the client sent all of the data read all over r_list.remove (sock) # sock The list that needs to be read remove state[sock][' s '] = ' process ' # Modify the status of sock state_machine (sock) # make a callback else:      ONE_READ = SOCK.RECV (state[sock][' r ' [' Need ']) # reads the specified number of bytes state[sock][' d '] += one_read # Update bytes Read # needs to rearrange the data that has been read and the data that needs to be read state[sock][' R ' [' Have '] += Len (one_read) state[sock][' R ' [' Need '] -= len (one_read) if state[sock][' R ' [' Have '] == 10: # Read the header message and start reading the main content state[ sock][' R ' [' Need '] += int (state[sock][' d ']) # Modify the number of bytes to be read Next state[sock][' d '] = ' # Empty The data you've received elif stat == ' Process ': # after reading the data sent by the client, start returning the data to the client response = state[sock][' d '][::-1] # Invert characters state[sock][' d '] = "%010d%s" % (Len (response), response) # Update received data state[sock][' W ' [' Need '] = Len (state[sock][' d ']) # Update the number of bytes to emit state[ sock][' s '] = ' write ' # Modify the status of sock to write w_list.Append (sock) # Add sock to W_LIST elif stat == ' Write ': print "Wirite", state # If the status is write, send data to the client last_have_send = state[sock][' W ' [' Have '] # Save the number of bytes sent have_send = sock.send (STATE[SOCk][' d '][last_have_send:]) # send data to the client, returning the number of bytes returned state[sock][' W ' [' Have '] += have_ send # Modify emitted bytes state[sock][' W ' [' Need '] -= have_send # Modify the bytes that need to be emitted if state[sock][' W ' [' Need '] == 0 and state[sock][' W '] [' have '] != 0: # Close sock & If the data has been sent outnbsp; state[sock][' s '] = ' closing ' elif stat == ' closing ': print "Closing", state # Close Connection State.pop (sock) # Remove sock from status try: w_list.remove (sock) # Remove sock from W_list except valueerror: Pass sock.close () # Close Connection R_list = [s ] # -readable sock queue w_list = [] # Writable sock Queue Non_stop = truewhile true : try: r_socks,w_socks,err_socks = select.select (r_list,w_list,[]) except socket.error,e: print e for sock in r_socks: state_machine (sock) for sock in w_socks: state_machine (sock)
There are more exchanges please add QQ Group 365534424 reboot operation and maintenance development practice
This article from "Reboot Operation Development" blog, reproduced please contact the author!
Python asynchronous transceiver Data Socket combat