Python network programming, problem solving for sticky packs and subcontracting

Source: Internet
Author: User
Tags pack

Solving the problem of sticky packet and subcontracting in TCP programming:
Ref: 52096465

Service side:

#!/bin/env python#-*-coding:utf-8-*-import socketimport timeimport structimport jsonimport socketimport sysclass SockP Ackbody (): Def __init__ (self, data_buffer = bytes (), header_size = 4): Self.data_buffer = Data_buffer s Elf.header_size = Header_size Self.header = None Self.body_size = 0 Self.body = none def packbody ( Self, sock): while True: ' One receive loop ' recv_pkg = SOCK.RECV (1024x768) if not recv_pkg                and Len (self.data_buffer) = = 0: "Connect closed, and no data in buffer."                return None Self.data_buffer + = recv_pkg If Len (Self.data_buffer) < self.header_size: The packet received is less than self.headersize, stating that the header did not accept the complete ' Continue Self.header = Struct.unpack ("!1i", self . data_buffer[:self.header_size]) self.body_size = self.header[0] #分包 If Len (self.data_buf FER) < Self.header_size + Self.body_size: "The packet received is less than self.header_size + Self.body_size, indicating that the entire packet did not accept the complete ' continue SE Lf.body = self.data_buffer[self.header_size:self.header_size + self.body_size] #粘包 self.data_buffer  = Self.data_buffer[self.header_size + self.body_size:] return self.bodyif __name__ = = ' __main__ ': Sock_pkger = Sockpackbody () sock = Socket.socket (socket.af_inet, socket. Sock_stream) Sock.bind ((' 0.0.0.0 ', 6688)) Sock.listen (5) agent_sock, agent_addr = Sock.accept () n = 1 whi Le True: "one connection (session) loop" ' Recv_data = Sock_pkger. Packbody (Agent_sock) if not recv_data:agent_sock.close () break print Recv_data

Client:

Import socketimport timeimport structimport jsonimport sysreload (SYS) sys.setdefaultencoding (' utf-8 ') Serve_host = " localhost "server_port = 6688class Pkgbuildheader (object): Def __init__ (self, body_size,): Self.pack_header = St Ruct.pack ("!1i", body_size) if __name__ = = ' __main__ ': client = Socket.socket () client.connect (Serve_host, Server_po RT) # normal packet definition BODY = ' Link encap:ethernet HWaddr 00:0c:29:ef:84:a3 inet addr:192.168.31.140 bcast:192 .168.31.255 mask:255.255.255.0 Inet6 addr:fe80::20c:29ff:feef:84a3/64 scope:link up broadcast RUNNING          Multicast mtu:1500 metric:1 RX packets:236844 errors:0 dropped:0 overruns:0 frame:0 TX packets:233949 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1076587 102.6 MiB TX bytes:41874253 (39.9 mib) "header = Pkgbuildheader (len)) sendData1 = Header.pack_h Eader + Body.encode () # sub-package data definition Header =Pkgbuildheader (len (body)) Senddata2_1 = Header.pack_header+body[:20].encode () senddata2_2 = Body[20:].encode () # Sticky packet data definition body1 = "Link encap:ethernet HWaddr 00:0c:29:ef:84:a3" header1 = Pkgbuildheader (len (body1)) Body2 = "RX b ytes:107658740 (102.6 MiB) TX bytes:41874253 (39.9 mib) "Header2 = Pkgbuildheader (len (body2)) sendData3 = HEADER1.P Ack_header+body1.encode () +header2.pack_header+body2.encode () # Normal packet client.send (SENDDATA1) Time.sleep (30) # Package Test Client.send (senddata2_1) time.sleep (1) client.send (senddata2_2) Time.sleep (3) # Sticky pack Test Client.send (Sen DDATA3) Time.sleep (3) Client.close ()

Python network programming, problem solving for sticky packs and subcontracting

Related Article

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.