Python 解包需要长度为92的字符串参数
我需要您的帮助来解决我的代码中的一个问题,我不知道是什么导致了这个问题。我在我的客户机上遇到的错误是: 如果您能帮助我理解为什么会发生这种情况,以及我如何解决它,我将非常高兴 服务器代码-Python 解包需要长度为92的字符串参数,python,client-server,pack,Python,Client Server,Pack,我需要您的帮助来解决我的代码中的一个问题,我不知道是什么导致了这个问题。我在我的客户机上遇到的错误是: 如果您能帮助我理解为什么会发生这种情况,以及我如何解决它,我将非常高兴 服务器代码- import socket import struct import os import subprocess import commands def cmd_output(command): return commands.getstatusoutput(command) def main():
import socket
import struct
import os
import subprocess
import commands
def cmd_output(command):
return commands.getstatusoutput(command)
def main():
serv_soc = socket.socket()
serv_soc.bind(("127.0.0.1", 50111))
serv_soc.listen(1)
client_soc, client_address = serv_soc.accept()
option = 1
while(option != 5):
client_data = client_soc.recv(1024)
unpack = struct.unpack('Bh', client_data)
option = unpack[0]
print option
if(option == 1):
pass
elif(option == 2):
pass
elif(option == 3):
fmt = "%ds" % unpack[1]
new_data = client_soc.recv(1024)
command = struct.unpack(fmt, new_data)
out = cmd_output(command[0])
print len(out[1])
print out
pack = struct.pack('Bh', 3, len(out[1]))
print pack
client_soc.send(pack)
fmt = "%ds" % len(out[1])
print out[1]
pack_string = struct.pack(fmt, out[1])
client_soc.send(pack)
elif(option == 4):
pass
serv_soc.close()
if __name__ == "__main__":
main()
客户端代码-
import socket
import struct
def main():
# Open socket
my_soc = socket.socket()
my_soc.connect(("127.0.0.1", 50111))
option = input("Enter option:")
while (option != 5):
if(option == 1):
pass
elif(option == 2):
pass
elif(option == 3):
command = raw_input("Enter command: ")
pack = struct.pack('Bh', 3, len(command))
my_soc.send(pack)
fmt = "%ds" % len(command)
pack_string = struct.pack(fmt, command)
my_soc.send(pack_string)
elif(option == 4):
pass
data = my_soc.recv(1024)
unpack = struct.unpack('Bh', data)
fmt = "%ds" % unpack[1]
data = my_soc.recv(1024)
unpack_string = struct.unpack(fmt, data) # Here Is the Problem
print unpack_string
option = input("Enter option:")
my_soc.close()
if __name__ == "__main__":
main()
当服务器将其输出返回到客户端时,就会出现问题。在服务器代码中,准备了pack\u字符串,但发送了pack:
pack_string = struct.pack(fmt, out[1])
client_soc.send(pack)
pack_string = struct.pack(fmt, out[1])
client_soc.send(pack)