标签:python 网络编程
python 2.7中客户端向服务器发送数据demo
ser = socket.socket()
ser.connect((ghost,gport))
lenstr = len(str)
lendata = struct.pack(‘BB‘,lenstr/256,lenstr%256)
ser.send(lendata)
ser.send(str)
recvlen = ser.recv(2)
recvlen = struct.unpack(‘BB‘,recvlen)
totallen = recvlen[0]*256 + recvlen[1]%256
str = ser.recv(totallen)
ser.close()
python 3.4中客户端向服务器发送数据demo
lenstr = ‘%s%s‘ % (lenstr,len(str))
str = "188888888222050%4s%s" % (lenstr,str)
if termtype == 1:
writeloginfo(6,‘[request]::‘+str)
elif termtype == 2:
writeloginfo(5,‘[request]::‘+str)
lenstr = len(str)
lendata = struct.pack(‘!BB‘,0,lenstr)
ser.send(lendata)
ser.send(str.encode())
recvlen = ser.recv(2)
recvlen = struct.unpack(‘!BB‘,recvlen)
totallen = recvlen[0]*256 + recvlen[1]%256
str = ser.recv(totallen)
str = repr(str)
python2.7和python3.4网络编程处理二进制数据区别
标签:python 网络编程
原文地址:http://blog.csdn.net/hsy12342611/article/details/45200535