Python network programming: Socket and select implement pseudo concurrency

Source: Internet
Author: User

Last Address: Python network programming: Socket

Add some content first:

One, send and sendall difference send,sendall ret = send (' Safagsgdsegsdgew ') #send sent after the completion of a return value, tell the number of sent, and does not necessarily send the data all over.        Sendall: Call send internally to send all the data out. Therefore, we use the best use of Sendall two, sticky packet sticky problem needs to understand the use of recv (), we define the value of the receive time will write recv (1024) to receive 1024 bytes at a time, but sometimes receive more than 1024 bytes of data, so that the socket is not sent by default, Waiting for the next time you receive another command will continue to send the last data that has not been sent, thus forming a sticky packet problem. For sticky packets, you can determine the size of the data before sending and receiving the data, and then send the shards. The following is a CMD-like program implemented using sockets and subprocess.
#!/usr/bin/env python#_*_ coding:utf_8 _*_ImportSocketip_port=('127.0.0.1', 9002) s=Socket.socket () s.connect (Ip_port) whileTrue:send_data= Input ('>>:'). Strip ()ifLen (send_data) = = 0:Continue    ifSend_data = ='Exit': Breaks.send (bytes (send_data,encoding='Utf-8'))    #solving Sticky pack problemsReady_tag = S.RECV (1024)#charge The length of the bytes to be sentReady_tag=str (ready_tag,encoding='UTF8')    ifReady_tag.startswith (' Ready'):#Packet format is ready|9124Msg_size=int (Ready_tag.split ('|') [-1]) Start_tag='Start's.sendall (bytes (start_tag,encoding='UTF8'))#sends a message indicating the start of the transferRecv_size =0#print (msg_size)Recv_msg = b"'     whileRecv_size <Msg_size:recv_data= S.RECV (1024) recv_msg+=Recv_data recv_size+=Len (recv_data)Print(Str (recv_msg,encoding='UTF8'))    #Receive Message    #Recv_data=s.recv (1024x768)    #print (str (recv_data,encoding= ' utf-8 '))s.close ()
socket_client
#!/use/bin/env python#_*_ coding:utf_8 _*_ImportSocketImportsubprocessip_addr=('127.0.0.1', 9002) s=socket.socket ()#Creating ObjectsS.bind (IP_ADDR)#Bind connection AddressS.listen (5)#setting the number of listening hosts whiletrue:conn,addr=s.accept () I=0 whileTrue:Print(i)Try: Recv_data= CONN.RECV (1024)            Print('------0')            #print (str (recv_data,encoding= ' utf-8 '))            ifLen (recv_data) = = 0: Break            #Execute system CommandP=subprocess. Popen (str (recv_data,encoding='Utf-8'), shell=true,stdout=subprocess. Pipe,stderr=subprocess. PIPE)#Execute system command, the standard output of Windows Platform command is GBK encoded, need to convertres = P.stdout.read ()#Get standard output            ifLen (res) = = 0:#Execute Error command, Send_data is emptySend_data = str (P.stderr.read (), encoding='Utf-8')            Else: Send_data=str (res,encoding='GBK') Send_data=bytes (send_data,encoding='Utf-8')#the data to be sent            #Judging sticky bagready_tag='ready|%s'%Len (send_data) conn.send (bytes (ready_tag,encoding='UTF8'))#Send data lengthFEEDBACK=CONN.RECV (1024)#Receive confirmation InformationFeedback=str (feedback,encoding='UTF8')            ifFeedback.startswith ('Start'):                Print(Send_data) conn.sendall (send_data)#execution result of the Send command            #Conn.sendall (send_data)            Print('succeed') I+=1exceptException: Breakconn.close ()
socket cmd

Python network programming: Socket and select implement pseudo concurrency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.