|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
Connected by ('127.0.0.1', 59565)
Exception in thread Thread-1:
Traceback (most recent call last):
File "E:\python\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "E:\python\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "c:/Users/ykl/Desktop/毕设/s1/Project/server_no_ssl.py", line 32, in conn_thread
header_json = str(struct.unpack('1024s', buf)[0], encoding='utf-8').strip('\00')
struct.error: unpack requires a buffer of 1024 bytes
问题代码如下
- def conn_thread(self,connection):
- while True:
- try:
- connection.settimeout(60)
- fileinfo_size = struct.calcsize('1024s')
- buf = connection.recv(fileinfo_size)
- if buf: # 如果不加这个if,第一个文件传输完成后会自动走到下一句
- header_json = str(struct.unpack('1024s', buf)[0], encoding='utf-8').strip('\00')
- #print(header_json)
- header = json.loads(header_json)
- Command = header['Command']
复制代码
因为你不一定可以收到 1024 个字节,
struct.unpack('1024s', buf) 需要 buf 必须要有 1024 个字节才行,
故对你的代码修改如下:
- def conn_thread(self,connection):
- while True:
- try:
- connection.settimeout(60)
- # fileinfo_size = struct.calcsize('1024s') 这一行不要
- buf = connection.recv(1024) # 这里直接用 1024 即可
- if buf: # 如果不加这个if,第一个文件传输完成后会自动走到下一句
- header_json = str(struct.unpack(f'{len(buf)}s', buf)[0], encoding='utf-8').strip('\00') # 这里把 1024 改为 {len(buf)},且在字符串前面加上了 f
- #print(header_json)
- header = json.loads(header_json)
- Command = header['Command']
复制代码
|
|