Leif160519的blog Leif160519的blog

——————

目录
python socket 发送16进制数据
/  

python socket 发送16进制数据

本文摘自: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进制数据
作  者Leif160519
出  处https://github.icu/articles/2019/10/08/1570525401617.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!