Python3 's socket sticky packet problem

Source: Internet
Author: User

Workaround One:

Client:

From socket import *import structimport json# ip = ' 127.0.0.1 ' ip= ' 192.168.0.105 ' port = 8080back_log = 5buffer_size = 1024t Cp_client = socket (af_inet, Sock_stream) tcp_client.connect ((IP, port)) while True:    cmd = input (' >>: '). Strip (    if CMD is none:continue    tcp_client.send (Cmd.encode (' Utf-8 '))    length = Tcp_client.recv (buffer_size)    length = Int (Length.decode (' Utf-8 '))    Tcp_client.send (b ' ready ')    recv_size = 0    recv_msg = B "    While recv_size < length:        recv_msg + = TCP_CLIENT.RECV (buffer_size)        recv_size = Len (recv_msg)    cmd_res = recv_msg    Print (Cmd_res.decode (' GBK ')) Tcp_client.close ()

Server side:

From socket import *import Subprocessip = ' Port = 8080back_log = 5buffer_size = 1024tcp_server = Socket (Af_inet, SOCK_STR EAM) tcp_server.bind ((IP, Port)) Tcp_server.listen (Back_log) while true:conn, addr = Tcp_server.accept () print (' New connection is ' , conn) print (' New link offset ', addr) while True:try:cmd = Conn.recv (buffer_size) if not cmd: Break res = subprocess. Popen (Cmd.decode (' Utf-8 '), Shell=true, stdout=subprocess. PIPE, Stdin=subprocess. PIPE, Stderr=subprocess. PIPE) Err = Res.stderr.read () If err:cmd_res = Err else:c Md_res = Res.stdout.read () length = Len (cmd_res) conn.send (str (length). Encode (' GBK ')) rea DY = Conn.recv (buffer_size) if ready = = B ' Ready ': Conn.send (cmd_res) except Exception as      E:print (e)      Break Conn.close () Tcp_server.close () 

  

Advanced Version 2:

Client:

 fromSocketImport*ImportstructImportJsonip='127.0.0.1'#ip= ' 192.168.0.105 'Port = 8080Back_log= 5buffer_size= 1024tcp_client=socket (af_inet, Sock_stream) tcp_client.connect ((IP, port)) whileTrue:cmd= Input ('>>:'). Strip ()ifCmd isNone:Continuetcp_client.send (Cmd.encode ('Utf-8')) Length= TCP_CLIENT.RECV (4) Length= Int (Length.decode ('GBK') [0]) Recv_size=0 recv_msg= b"'     whileRecv_size <length:recv_msg+=tcp_client.recv (buffer_size) recv_size=Len (recv_msg) Cmd_res=recv_msgPrint(Cmd_res.decode ('GBK') ) Tcp_client.close ()
View Code

Service side:

 fromSocketImport*ImportstructImportJsonip='127.0.0.1'#ip= ' 192.168.0.105 'Port = 8080Back_log= 5buffer_size= 1024tcp_client=socket (af_inet, Sock_stream) tcp_client.connect ((IP, port)) whileTrue:cmd= Input ('>>:'). Strip ()ifCmd isNone:Continuetcp_client.send (Cmd.encode ('Utf-8')) Length= TCP_CLIENT.RECV (4) Length= Int (Length.decode ('GBK') [0]) Recv_size=0 recv_msg= b"'     whileRecv_size <length:recv_msg+=tcp_client.recv (buffer_size) recv_size=Len (recv_msg) Cmd_res=recv_msgPrint(Cmd_res.decode ('GBK') ) Tcp_client.close ()
View Code

Advanced Version 3:

Python3 's socket sticky packet problem

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.