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)
时间: 2024-11-08 19:03:34