Python - 将 uint8 和 uint16 发送到套接字
Python - send uint8 and uint16 to socket
我正在尝试使用 python 脚本将一些数据发送到 java 服务器。我使用 python 中的套接字模块来发送和接收数据。
当我发送数据时,我需要指定一个header,其中包含数据长度。 header如下:
- a
uint8
为版本号
- a
uint8
用于填充 ('reserved')
- a
uint16
为发送数据的长度
一共32位。
我可以使用 numpy 创建具有特定数据类型的数组,但问题是通过套接字发送此数据。我使用以下函数发送数据:
def send(socket, message):
r = b''
totalsent = 0
# as long as not everything has been sent ...
while totalsent < len(message):
# send it ; sent = actual sent data
sent = socket.send(message[totalsent:])
r += message[totalsent:]
# nothing sent? -> something wrong
if sent == 0:
raise RuntimeError("socket connection broken")
# update total sent
totalsent = totalsent + sent
return r
message = (something_with_numpy(VERSION_NUMBER, PADDING, len(data)))
send(socket, message)
我一直收到此函数的类型错误。这些会在 len(message)
、r += message[...]
或其他地方弹出。
我想知道是否有更好的方法来执行此操作,或者如何解决此问题以使其正常工作?
更新:这里有一些确切错误跟踪。我已经尝试了几种不同的方法,所以这些错误痕迹可能已经变得无关紧要了。
Traceback (most recent call last):
File "quick.py", line 47, in <module>
header += numpy.uint8(VERSION_NUMBER)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
header = numpy.array([VERSION_NUMBER * 255 + PADDING, len(greetData)], dtype=numpy.uint16)
Traceback (most recent call last):
File "quick.py", line 48, in <module>
print(header + greetData)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S22') dtype('S22') dtype('S22')
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.uint8(VERSION_NUMBER))
File "quick.py", line 13, in send
while totalsent < len(message):
TypeError: object of type 'numpy.uint8' has no len()
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.array([VERSION_NUMBER], dtype=numpy.uint8))
File "quick.py", line 17, in send
r += message[totalsent:]
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
在发送数据之前,您需要使用 struct
模块格式化 header。
import struct
def send_message(socket, message):
length = len(message)
version = 0 # TODO: Is this correct?
reserved = 0 # TODO: Is this correct?
header = struct.pack('!BBH', version, reserved, length)
message = header + message # So we can use the same loop w/ error checking
while ...:
socket.send(...)
我正在尝试使用 python 脚本将一些数据发送到 java 服务器。我使用 python 中的套接字模块来发送和接收数据。
当我发送数据时,我需要指定一个header,其中包含数据长度。 header如下:
- a
uint8
为版本号 - a
uint8
用于填充 ('reserved') - a
uint16
为发送数据的长度
一共32位。
我可以使用 numpy 创建具有特定数据类型的数组,但问题是通过套接字发送此数据。我使用以下函数发送数据:
def send(socket, message):
r = b''
totalsent = 0
# as long as not everything has been sent ...
while totalsent < len(message):
# send it ; sent = actual sent data
sent = socket.send(message[totalsent:])
r += message[totalsent:]
# nothing sent? -> something wrong
if sent == 0:
raise RuntimeError("socket connection broken")
# update total sent
totalsent = totalsent + sent
return r
message = (something_with_numpy(VERSION_NUMBER, PADDING, len(data)))
send(socket, message)
我一直收到此函数的类型错误。这些会在 len(message)
、r += message[...]
或其他地方弹出。
我想知道是否有更好的方法来执行此操作,或者如何解决此问题以使其正常工作?
更新:这里有一些确切错误跟踪。我已经尝试了几种不同的方法,所以这些错误痕迹可能已经变得无关紧要了。
Traceback (most recent call last):
File "quick.py", line 47, in <module>
header += numpy.uint8(VERSION_NUMBER)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
header = numpy.array([VERSION_NUMBER * 255 + PADDING, len(greetData)], dtype=numpy.uint16)
Traceback (most recent call last):
File "quick.py", line 48, in <module>
print(header + greetData)
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S22') dtype('S22') dtype('S22')
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.uint8(VERSION_NUMBER))
File "quick.py", line 13, in send
while totalsent < len(message):
TypeError: object of type 'numpy.uint8' has no len()
Traceback (most recent call last):
File "quick.py", line 47, in <module>
r = send(conn, numpy.array([VERSION_NUMBER], dtype=numpy.uint8))
File "quick.py", line 17, in send
r += message[totalsent:]
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('S3') dtype('S3') dtype('S3')
在发送数据之前,您需要使用 struct
模块格式化 header。
import struct
def send_message(socket, message):
length = len(message)
version = 0 # TODO: Is this correct?
reserved = 0 # TODO: Is this correct?
header = struct.pack('!BBH', version, reserved, length)
message = header + message # So we can use the same loop w/ error checking
while ...:
socket.send(...)