unpack 需要一个长度为 92 python 的字符串参数
unpack requires a string argument of length 92 python
我需要你的帮助来解决我的代码中的一个问题,我不知道是什么原因导致的,我在客户端上遇到的错误是:
unpack_string = struct.unpack(fmt, data)
struct.error: unpack requires a string argument of length 92
如果你能帮助我理解为什么会发生这种情况以及我该如何解决它,我会很高兴
服务器代码 -
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()
当服务器returns他的输出到客户端时出现问题。
在服务器代码中,准备了 pack_string
,但发送了 pack
:
pack_string = struct.pack(fmt, out[1])
client_soc.send(pack)
我需要你的帮助来解决我的代码中的一个问题,我不知道是什么原因导致的,我在客户端上遇到的错误是:
unpack_string = struct.unpack(fmt, data) struct.error: unpack requires a string argument of length 92
如果你能帮助我理解为什么会发生这种情况以及我该如何解决它,我会很高兴
服务器代码 -
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()
当服务器returns他的输出到客户端时出现问题。
在服务器代码中,准备了 pack_string
,但发送了 pack
:
pack_string = struct.pack(fmt, out[1])
client_soc.send(pack)