(Python 个套接字),没有收到所有东西
(Python sockets), Does not receive everything
我试图让 pc 的树将它发送到主机并将其保存到文件中,但是,我一直收到树的一小部分,而它确实打印出了 'users' 端的所有内容。
服务器:
conn.send(command.encode(self.FORMAT))
print("[COMMAND SENT] Waiting for execution")
msg_length = conn.recv(self.HEADER).decode(self.FORMAT)
final_rcv = conn.recv(int(msg_length)).decode(self.FORMAT)
with open(str(adres[0]) + "_" + str(adres[1]) + ".txt", "w", encoding="utf-8") as f:
f.write(final_rcv)
f.close()
客户:
string_ = ''
for line in tree(Path.home() / ''):
string_ += line
string_ += "\n"
msg_length = str(len(string_.encode(self.FORMAT)))
self.s.send(msg_length.encode(self.FORMAT))
self.s.send(string_.encode(self.FORMAT))
print(string_)
print(msg_length)
感谢您的宝贵时间,希望您能用上这就是未来。
编辑:当我 运行 代码时没有错误。
There are no errors when I run the code.
虽然可能没有错误,但您调用的函数有 return 个值。但是你忽略了这些。具体来说send
会return实际发送的字节数,不一定和要求发送的数据大小一样。来自 the documentation:
... Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. ...
正如清楚记录的那样,程序需要检查 send
的 return 值以确定是否还有未写入的数据,然后 send
这些数据(并且只有这些)稍后.你的程序没有。或者你可以只使用 sendall:
... Unlike send(), this method continues to send data from bytes until either all data has been sent or an error occurs ...
请注意,您在调用 recv
时也有同样的错误假设。与您的预期相反,此函数不能保证读取完整数据,但可能只读取一部分。您必须检查实际读取了多少数据,并在必要时再次(可能再次)调用 recv
以读取其余数据。来自 the documentation:
... The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize.
我试图让 pc 的树将它发送到主机并将其保存到文件中,但是,我一直收到树的一小部分,而它确实打印出了 'users' 端的所有内容。
服务器:
conn.send(command.encode(self.FORMAT))
print("[COMMAND SENT] Waiting for execution")
msg_length = conn.recv(self.HEADER).decode(self.FORMAT)
final_rcv = conn.recv(int(msg_length)).decode(self.FORMAT)
with open(str(adres[0]) + "_" + str(adres[1]) + ".txt", "w", encoding="utf-8") as f:
f.write(final_rcv)
f.close()
客户:
string_ = ''
for line in tree(Path.home() / ''):
string_ += line
string_ += "\n"
msg_length = str(len(string_.encode(self.FORMAT)))
self.s.send(msg_length.encode(self.FORMAT))
self.s.send(string_.encode(self.FORMAT))
print(string_)
print(msg_length)
感谢您的宝贵时间,希望您能用上这就是未来。 编辑:当我 运行 代码时没有错误。
There are no errors when I run the code.
虽然可能没有错误,但您调用的函数有 return 个值。但是你忽略了这些。具体来说send
会return实际发送的字节数,不一定和要求发送的数据大小一样。来自 the documentation:
... Returns the number of bytes sent. Applications are responsible for checking that all data has been sent; if only some of the data was transmitted, the application needs to attempt delivery of the remaining data. ...
正如清楚记录的那样,程序需要检查 send
的 return 值以确定是否还有未写入的数据,然后 send
这些数据(并且只有这些)稍后.你的程序没有。或者你可以只使用 sendall:
... Unlike send(), this method continues to send data from bytes until either all data has been sent or an error occurs ...
请注意,您在调用 recv
时也有同样的错误假设。与您的预期相反,此函数不能保证读取完整数据,但可能只读取一部分。您必须检查实际读取了多少数据,并在必要时再次(可能再次)调用 recv
以读取其余数据。来自 the documentation:
... The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize.