Python 原始套接字更改符号

Python raw socket changing symbols

我有 2 个程序通过以太网相互通信。发送一个是在将其作为以太网帧发送之前使用 scapy 对端口、ip 和有效负载进行编码。我的问题是,在有效负载中,我正在发送计数器,而当接收到它时,它有时会更改为符号。

\x00\x00\x00\x00\x00\x00\x00\x07
\x00\x00\x00\x00\x00\x00\x00\x08 is fine but next
\x00\x00\x00\x00\x00\x00\x00\t
\x00\x00\x00\x00\x00\x00\x00\n
\x00\x00\x00\x00\x00\x00\x00\x0b its fine again

稍后它们被更改为下一个 asci 符号

我的问题是如何停止将字节转换为 asci?

sender.py

import socket
from scapy.all import *

PADDING_VALUE = b'\xd1'
ETH_P_ALL = 3
DST_IP = "127.0.0.12"
IFACE = "lo"
SRC_IP = "127.0.0.11"

class FpgaMockup:
    def __init__(self, setup_iface):
        self.setup_sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
        self.setup_sock.bind((setup_iface, 0))
        self.padding = 16

    def send(self, pkt):
        self.setup_sock.send(pkt)

if __name__ == "__main__":
    testing_fpga = FpgaMockup(IFACE)
    
    for i in range(100):
        packet = IP(dst=DST_IP, src=SRC_IP)/UDP(sport=12666, dport=12666)/Raw(load=int(i).to_bytes(8, "big")+PADDING_VALUE*testing_fpga.padding)
        pkt = Ether(packet)
        testing_fpga.send(raw(pkt))
    print("Finished sending.")

reciever.py

import socket

ETH_P_ALL = 3

s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(ETH_P_ALL))
s.bind(("lo", 0))

while(True):
    pkt = s.recv(4096)
    print(pkt)

这些都没有“改变”。 \x09\t完全相同,\x0a\n相同。这些只是印刷不同但仍然相同:

>>> print(b'\x08\x09\x0a\x0b')
b'\x08\t\n\x0b'
>>> b'\x08\x09\x0a\x0b' == b'\x08\t\n\x0b'
True

有关详细信息,请参阅 String and Bytes literals 语法的文档。

如果您不想进行此对话,只需强制将其写入十六进制序列而不是字符即可:

>>> b'\x08\t\n\x0b'.hex()
'08090a0b'