Implement Python socket custom TCP packet with struct module

Source: Internet
Author: User

Recently job-hopping to a robot company in Xian, our products belong to the category of educational robots, in order to enhance customer attractiveness, the introduction of a smart home company's product API interface, let the robot to operate the smart home


The company's smart home API is a custom TCP packet that writes a custom data structure directly behind the TCP header:

Client requests to download the format of the furniture database

Command Word (4 bytes, small end)

0x4c

The server returns the format of the request result

Command Word (4 bytes, small end) Payload Length (4 bytes, small end) Payload (n*1 bytes)
0x43 11262 (large size) SQLite database


The default Python socket can only send and receive strings and requires a struct to send and receive binary data


Send Request

Cmd_word = 0x4ctx_buf = Struct.pack (' <i ', Cmd_word) Sock.sendall (TX_BUF)

Tx_buf according to the document of the struct is its string generated for the input encoding, with type (TX_BUF) display is indeed <type ' str ' >,print tx_buf display letter L

But Len (tx_buf) = = 4, print repr (tx_buf) display

' L\x00\x00\x00 '

Which means len (' l\x00\x00\x00 ') = = 4

For those who turn from C, the above is an understandable failure, but it is a successful delivery.


Receive answer

fp = open (' house.db ', ' wb+ ') recv_cnt = 0while True:    rx_buf = SOCK.RECV (4096)    len_buf = Len (rx_buf)    if Len_buf ==0:        break    if recv_cnt = = 0:        cmd_word, data_len_total = Struct.unpack (Rx_buf[0:8])        buf = buffer (rx_buf , 8, Len_buf-8)        fp.write (BUF)    else:        buf = buffer (rx_buf)        fp.write (buf)    recv_cnt = recv_cnt +1

Attention:

0, receive the custom frame header with unpack, you can get the structure of each field value

1, receive custom frame content (byte stream) without unpack, because unpack returns a tuple, and write does not support tuple type

2, because it is a binary write, you must turn str into the buffer type

3, the binary data is very large, the bottom layer will be split into multiple Ethernet frames, if you sendall immediately after recv, you may only receive 1448 bytes, do not worry, this is because you recv when the kernel stack only a frame so much data, it all returned to you, to meet the standard socket programming








Implement Python socket custom TCP packet with struct module

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.