参考:http://m.blog.csdn.net/blog/JasonQue/8256424
具体的struct模块的解释可以参照上面的链接。
struct.pack也就是将数据按照二进制的格式进行传输
#!/usr/bin/env python #coding:utf-8 __author__ = ‘ferraborghini‘ from socket import * import struct #将16进制数据当做字节流传递 def dataSwitch(data): str1 = ‘‘ str2 = ‘‘ while data: str1 = data[0:2] s = int(str1,16) str2 += struct.pack(‘B‘,s) data = data[2:] return str2 if __name__ == "__main__": HOST = ‘localhost‘ PORT = 21567 BUFSIZE = 1024 ADDR = (HOST,PORT) tcpCliSock = socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) while True: data = raw_input(‘>‘) if not data: break tcpCliSock.send(dataSwitch(data)) data = tcpCliSock.recv(BUFSIZE) if not data: break print data tcpCliSock.close()
解析报文,这时候读入的其实相当于二进制流,我们要做的就将二进制流转化为16进制就行
#!/usr/bin/env python #coding:utf-8 from socket import * from time import ctime if __name__ == "__main__": HOST = ‘‘ #此处为空代表可以绑定所有有效地址 PORT = 21567 BUFSIZE = 1024 ADDR = (HOST,PORT) tcpSerSocket = socket(AF_INET,SOCK_STREAM) tcpSerSocket.bind(ADDR) tcpSerSocket.listen(5) #最多可以有5个连接同时进入 while True: print ‘waiting for connection...‘ tcpCliSock,addr = tcpSerSocket.accept() print ‘...connected from:‘,addr while True: data = tcpCliSock.recv(BUFSIZE) print data.encode(‘hex‘) if not data: break tcpCliSock.send(‘[%s] %s‘%(ctime(),data)) # tcpCliSock.close() #如果接收完,就断开的话,下次再发送就会报错,书本上有问题 tcpSerSocket.close()
时间: 2024-10-10 16:54:27