[英]Python Send known bytes
I want to send a stream of known bytes
over the network, such as 0020ff00
, but I want to use other method beside concatenation of char
symbols. 我想通过网络发送一个已知
bytes
流,例如0020ff00
,但是我想使用除串联char
符号之外的其他方法。 Can you provide me with another, more elegant method? 您能为我提供另一种更优雅的方法吗? Thanks
谢谢
def send_connect_request(sock):
print('Sending connect request...')
sock.send('' + chr(2) + chr(0) + chr(0) + chr(0) + chr(255) + chr(0))
# sock.shutdown(1)
change to 改成
def send_connect_request(sock):
print('Sending connect request...')
sock.send(0x0002ff00)
# sock.shutdown(1)
If you know the bytes, use a bytes
literal: 如果您知道字节,请使用
bytes
文字:
sock.send(b'\x02\x00\x00\x00\xff\x00')
If you really don't want to type out all the escape-sequences, use the bytes
constructor with a list of int
s: 如果您真的不想键入所有转义序列,请使用带有
int
列表的bytes
构造函数:
sock.send(bytes([2,0,0,0,255,0]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.