本文摘自:https://blog.csdn.net/qq_40650217/article/details/79076183
给服务器发送16进制数据,客户端代码如下:
#!/usr/bin/env python
#-*- encoding: utf-8 -*-
import socket
if __name__=="__main__":
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("HostIP",port))
s.send(b"\x78\x78\x11\x01\x07\x52\x53\x36\x78\x90\x02\x42\x70\x00\x32\x01\x00\x05\x12\x79\x0D\x0A")
print(s.recv(1024))
s.close()
扩展:
import struct
import socket
a=[0x00,0x02,0x4e,0x50]
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(('127.0.0.1',8000))
data=struct.pack("%dB"%(len(a)),*a)
s.send(data)
reply=s.recv(1024)
print reply
#-*- encoding: utf-8 -*-
import json
import socket
import sys
import binascii
reload(sys)
sys.setdefaultencoding('utf-8')
if __name__=="__main__":
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect(("your_host_name", your_port))
s.send("\xab\xcd\x34\x12\x1f\x00_some_orther_data") # 前面为十六进制数据,后面可接字符串等正文
print s.recv(1024)
s.close()
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:python socket 发送16进制数据