[英]How to send data from client socket to server socket using Python?
[英]How can I convert binary data received from C++ client socket to python Server socket
我正在嘗試解碼從人類可讀形式收到的字節。 如果我使用decode('utf-8')
python 給出錯誤
utf-8' 編解碼器無法解碼 position 257-258 中的字節:無效的繼續字節
如果我使用decode('iso-8859-1')
它會顯示垃圾值
***�1234567 "PROTOCOL-ICMP Address Mask Request undefined code"¸y»_Äý
bb"*
)#FªPVúEÀ¨~`v¸y»_Iw
�**
python 代碼: udpUnixServer.py
#! /usr/bin/env python3
import socket
import os, os.path
import struct
if os.path.exists("snort_alert"):
os.remove("snort_alert")
server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM,0)
server.bind("snort_alert")
while True:
print("listening")
datagram,addr = server.recvfrom(1024)
print(datagram)
print("Unpack")
print(struct.unpack_from('sssssssssssssssssssssssssssl', datagram, 0))
#x=bytearray(datagram)
#print(x)
server.close()
os.remove("snort_alert")
print("Done")
Output 無解碼:
b'"PROTOCOL-ICMP Address Mask Request undefined code"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00[\x12\xbe_\x9a\xb8\n\x00b\x00\x00\x00b\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00"\x00\x00\x00*\x00\x00\x00\x00\x00\x00\x00\x00PV\xfa\x82\xe2\x00\x0c)#F\xaa\x08\x00E\x00\x00T#p@\x00@\x01\xd4\x07\xc0\xa8~\x83\x01\x02\x03\x04\x08\x00z\x1a\x00\x0c\x1b\xdc[\x12\xbe_\x00\x00\x00\x00\x7f\xb8\n\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
我不確定如何使用struct.pack
或struct.unpack
將上述字節轉換為字符串。
請指導。
我剛剛看了你的結構。 你的struct Alertpkt
是 65864 字節,主要是因為非常大的uint8_t pkt[65535];
大批。 也只有uint8_t alertmsg[256];
可能代表一個字符串(不一定是 256 字節長,可以更短)。
struct sf_timeval32
{
uint32_t tv_sec;
uint32_t tv_usec;
};
struct pcap_pkthdr32
{
struct sf_timeval32 ts;
uint32_t caplen;
uint32_t pktlen;
};
struct Alertpkt
{
uint8_t alertmsg[256];
struct pcap_pkthdr32 pkth;
uint32_t dlthdr;
uint32_t nethdr;
uint32_t transhdr;
uint32_t data;
uint32_t val;
#define NOPACKET_STRUCT 0x1
#define NO_TRANSHDR 0x2
uint8_t pkt[65535];
uint32_t gid;
uint32_t sid;
uint32_t rev;
uint32_t class_id;
uint32_t priority;
uint32_t event_id;
uint32_t event_ref;
struct sf_timeval32 ref_time;
};
在您在 Python 端收到的示例數據中(僅 1024 字節)
>>> datagram[0:51]
b'"PROTOCOL-ICMP Address Mask Request undefined code"'
表示您收到的人類可讀消息,您可以將其視為 Python 字符串。 然后datagram[51:256]
只是填充零,你應該跳過它們。
接下來的 16 個字節
>>> datagram[256:256+16]
b'[\x12\xbe_\x9a\xb8\n\x00b\x00\x00\x00b\x00\x00\x0
是: tv_sec
、 tv_usec
、 caplen
、 pktlen
。 所有這些都是 4 字節無符號整數,因此您可以讀取它們的值,例如:
>>> ctypes.c_uint32.from_buffer_copy(datagram[256:256+4])
c_uint(1606292059)
>>> ctypes.c_uint32.from_buffer_copy(datagram[256+4:256+8])
c_uint(702618)
>>> ctypes.c_uint32.from_buffer_copy(datagram[256+8:256+12])
c_uint(98)
>>> ctypes.c_uint32.from_buffer_copy(datagram[256+12:256+16])
c_uint(98)
其余部分數據以此類推...我認為這些值是合理的,因為tv_sec
代表“2020 年 11 月 25 日星期三 8:14:19 AM”的紀元值。 請記住,1024 字節不是一個完整的結構。 也可能你應該定義偏移量,或者編寫一個很好的解碼 function,而不是直接使用幻數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.