struct.pack Python中的4sL格式是什么

时间:2022-06-02 18:18:19

I am new to python and my first program is to write a code for receiving multicast packets.

我是python的新手,我的第一个程序是编写接收组播数据包的代码。

I got this code for example and similar codes everywhere.

我得到了这个代码,例如到处都是类似的代码。

import socket
import struct

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', 4242))
mreq = struct.pack("=4sl", socket.inet_aton("224.51.105.104"), socket.INADDR_ANY)

sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) 
while True:
   print sock.recv(10240)

I am not able to figure out this code. especially the 4sl part

我无法弄清楚这段代码。特别是4sl部分

2 个解决方案

#1


6  

Read the struct module documentation.

阅读struct module文档。

= means native byte order, standard sizes, and no alignment. 4s means "four-letter string" (four char joined together into a bytestring), l means "signed long", in this case a four-byte int.

=表示本机字节顺序,标准大小,无对齐。 4s表示“四个字母的字符串”(四个字符串连接成一个字节串),l表示“signed long”,在本例中是一个四字节的int。

As for the rest of the code, this is setting up a multicast udp listener bound to port 4242. mreq is this structure (C code):

至于其余的代码,这是设置一个绑定到端口4242的多播udp监听器.mreq是这个结构(C代码):

struct ip_mreq {
    struct in_addr imr_multiaddr;   /* IP multicast address of group */
    struct in_addr imr_interface;   /* local IP address of interface */
};

This code stores that data in mreq, which is passed to setsockopt to inform the OS that you are interested in data from that multicast address.

此代码将该数据存储在mreq中,该数据传递给setsockopt以通知操​​作系统您对该多播地址中的数据感兴趣。

A google search revealed this tutorial on IP multicast.

谷歌搜索发布了关于IP多播的本教程。

#2


2  

From struct.pack format specification:

从struct.pack格式规范:

  • = means "use platform endianess"
  • =表示“使用平台endianess”
  • 4s means "first thing is a string of four chars"
  • 4s意味着“第一件事是一串四个字符”
  • l means "a long integer follows"
  • l表示“跟随长整数”

So you are packing (writing to a byte vector) four characters followed by a long integer. Endianess matters for the latter.

所以你打包(写入一个字节向量)四个字符后跟一个长整数。 Endianess对后者很重要。

#1


6  

Read the struct module documentation.

阅读struct module文档。

= means native byte order, standard sizes, and no alignment. 4s means "four-letter string" (four char joined together into a bytestring), l means "signed long", in this case a four-byte int.

=表示本机字节顺序,标准大小,无对齐。 4s表示“四个字母的字符串”(四个字符串连接成一个字节串),l表示“signed long”,在本例中是一个四字节的int。

As for the rest of the code, this is setting up a multicast udp listener bound to port 4242. mreq is this structure (C code):

至于其余的代码,这是设置一个绑定到端口4242的多播udp监听器.mreq是这个结构(C代码):

struct ip_mreq {
    struct in_addr imr_multiaddr;   /* IP multicast address of group */
    struct in_addr imr_interface;   /* local IP address of interface */
};

This code stores that data in mreq, which is passed to setsockopt to inform the OS that you are interested in data from that multicast address.

此代码将该数据存储在mreq中,该数据传递给setsockopt以通知操​​作系统您对该多播地址中的数据感兴趣。

A google search revealed this tutorial on IP multicast.

谷歌搜索发布了关于IP多播的本教程。

#2


2  

From struct.pack format specification:

从struct.pack格式规范:

  • = means "use platform endianess"
  • =表示“使用平台endianess”
  • 4s means "first thing is a string of four chars"
  • 4s意味着“第一件事是一串四个字符”
  • l means "a long integer follows"
  • l表示“跟随长整数”

So you are packing (writing to a byte vector) four characters followed by a long integer. Endianess matters for the latter.

所以你打包(写入一个字节向量)四个字符后跟一个长整数。 Endianess对后者很重要。