[英]Receiving binary data through network sockets in python 3
In my networking class a lab i have is to make a client to receive 5 familiar 32-bit integers in big endian order and to intemperate them. 在我的网络课程中,我要做的一个实验是使一个客户端按大端序接收5个熟悉的32位整数并使它们变活。 I decided to use python and everything works well enough but i am receiving strange hex code. 我决定使用python,一切正常,但是我收到了奇怪的十六进制代码。
\\x00\\x00\\x00o\\x00\\x00\\x00\\xe4\\x00\\x00\\x01\\xb3\\x00\\x00\\x01\\xdb\\x00\\x00\\x01\\xec \\ X00 \\ X00 \\ x00o \\ X00 \\ X00 \\ X00 \\ XE4 \\ X00 \\ X00 \\ X01 \\ XB3 \\ X00 \\ X00 \\ X01 \\ XDB \\ X00 \\ X00 \\ X01 \\ XEC
I can convert most of it easily but the x00o is really confusing me, 228 435 475 492 where the 4 after that I believe. 我可以轻松转换大多数,但x00o确实让我感到困惑,228 435 475 492,在那之后我相信这4个。 Can you help me intemperate the server message? 您能帮我取消服务器消息吗?
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Failed to create socket')
sys.exit()
print('Socket Created')
host = 'localhost'
port = 5071
try:
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
#could not resolve
print('Hostname could not be resolved. Exiting')
sys.exit()
s.connect((remote_ip , port))
print('Socket Connected to ' + host + ' on ip ' + remote_ip)
reply = s.recv(4096)
print(reply)
code.py : code.py :
import sys
import struct
def convert_data_to_ints(data, big_endian=True):
int_count = len(data) // 4 # Assuming uint is 4 bytes long !!!
fmt = ">" if big_endian else "<"
fmt += "I" * int_count
return struct.unpack(fmt, data[:int_count * 4])
def main():
print("Python {} on {}\n".format(sys.version, sys.platform))
data = b"\x00\x00\x00o\x00\x00\x00\xe4\x00\x00\x01\xb3\x00\x00\x01\xdb\x00\x00\x01\xec"
ints_be = convert_data_to_ints(data)
print("Big endian: {}".format(ints_be))
ints_le = convert_data_to_ints(data, big_endian=False)
print("Little endian: {}".format(ints_le))
if __name__ == "__main__":
main()
Notes : 注意事项 :
convert_data_to_ints
: convert_data_to_ints
:
"I"
format). 转换为unsigned int ( "I"
格式)。 Check the " Format Strings " section in on the (1 st ) above page 检查网页上面在第(1 日 )在“ 格式字符串 ”节 "IIIII"
, wanted to make it more general). 依赖于int为4字节长的事实(我不想对"IIIII"
进行硬编码, "IIIII"
想使其更加通用)。 If the string length is not a multiple of 4 (an integral number of int s), the incomplete int at the end (at most 3 bytes) is discarded (of course, a nicer way to pad the string and also convert the "incomplete" data, but that's outside the question scope) 如果字符串长度不是4的整数倍( int的整数 ),则末尾不完整的int (最多3个字节)将被丢弃(当然,更好的方式是填充字符串并转换“ incomplete数据,但这不在问题范围内) main
: main
:
socket.recv
returns it in this form). 数据是[Python]:字节对象实例(我认为socket.recv
以这种形式返回它)。 If (I'm wrong and) it returns the data as a string, just use [Python]: str. 如果(我错了)并且它以字符串形式返回数据,则只需使用[Python]:str。 encode ( encoding="utf-8", errors="strict" ) 编码 ( encoding =“ utf-8”,errors =“ strict” ) Output : 输出 :
E:\\Work\\Dev\\StackOverflow\\q048508018>"c:\\install\\Python\\3.4.3\\x86\\python.exe" code.py Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Big endian: (111, 228, 435, 475, 492) Little endian: (1862270976, 3825205248, 3003187200, 3674275840, 3959488512)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.