从客户端到服务器接收大文件时遇到麻烦,同时我可以轻松接收小文件。我已经看到几乎每个有关在Python中接收文件的堆栈溢出线程,但无法弄清楚。如果我尝试传输二进制文件(大文件),则数据无法完全接收。一半的文件将被接收,或者服务器端将在客户端已发送数据的同时继续监听数据。
C ++发送方(客户端)
else if(command.find("sendme") != std::string::npos)
{
memset(file_commands,'\0',5);
strsplit(toSplit,file_commands);
std::string trigger = "savethis=" + std::string(file_commands[1]);
respond(trigger.c_str());
respondF(sendFile(file_commands[1]).c_str()); // respondF(char* data); sends data without encryption in my networking app
}
Python接收端(服务器)
# Decrypt the data
response = xor(reply)
if(response.startswith("savethis")):
print("[+] Incoming file request..")
try:
f = response.split("=")
with open(f[1],"wb") as received_file:
data = csocket.recv(1024)
print("[*] Receiving file..")
received_file.write(data)
print("[+] Got file '"+f[1] +"'.")
except IndexError:
print("Error.")
else:
print(Style.RESET_ALL + "(New Message) "+ip+":"+port+" - " + str(response),flush=True)