Python 发送已知字节

标签 python sockets

我想发送已知的流bytes通过网络,例如 0020ff00 ,但我想使用除了 char 串联之外的其他方法符号。 你能给我提供另一种更优雅的方法吗? 谢谢

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)

更改为

def send_connect_request(sock):
    print('Sending connect request...')
    sock.send(0x0002ff00)       
    # sock.shutdown(1)

最佳答案

如果您知道字节,请使用字节文字:

sock.send(b'\x02\x00\x00\x00\xff\x00')

如果您确实不想输入所有转义序列,请使用带有 int 列表的 bytes 构造函数:

sock.send(bytes([2,0,0,0,255,0]))

关于Python 发送已知字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49066266/

相关文章:

python - 从 SimpleITK 变换获取矩阵和平移

python - 用 Pandas 数据框中的空列表替换 NaN

ios - 如何在 iPhone 上用 Swift 实现 UDP 客户端和发送数据?

C++ 连接函数错误 - 参数无效

c - 用 C 检测网络事件

java - 使用 Java 1.7 处理套接字的最新技术是什么?

python - MySQLdb python 2.7

python - lxml xsi :schemaLocation namespace URI validation issue

python - 通过他们的 API 访问 Lending Club Loan 列表

javascript - Socket.io跨域问题