我不知道,如果這個主題已經回答與否,如果是不好意思:Python客戶端/服務器發送的大尺寸文件
我有一個簡單的Python腳本發送一個文件夾中的所有文件:
客戶:
import os,sys, socket, time
def Send(sok,data,end="292929"):
sok.sendall(data + end);
def SendFolder(sok,folder):
if(os.path.isdir(folder)):
files = os.listdir(folder);
os.chdir(folder);
for file_ in files:
Send(sok,file_);#Send the file name to the server
f = open(file_, "rb");
while(True):
d = f.read();#read file
if(d == ""):
break;
Send(sok, d, "");#Send file data
f.close();
time.sleep(0.8);#Problem here!!!!!!!!!!!
Send(sok,"");#Send termination to the server
time.sleep(1);#Wait the server to write the file
os.chdir("..");
Send(sok,"endfile");#let the server know that we finish sending files
else:
Send("endfile")#If not folder send termination
try:
sok1 = socket.socket();
sok1.connect(("192.168.1.121",4444))#local ip
time.sleep(1);
while(True):
Send(sok1,"Enter folder name to download: ");
r = sok1.recv(1024);
SendFolder(sok1,r);
time.sleep(0.5);
except BaseException, e:
print "Error: " + str(e);
os._exit(1);
服務器:
import sys,os,socket,time
# receive data
def Receive(sock, end="292929"):
data = "";
while(True):
if(data.endswith(end)):
break;
else:
data = sock.recv(1024);
return data[:-len(end)];#return data less termination
def FolderReceive(sok):
while(True):
r = Receive(sok);# recv filename or folder termination("endfile")
if(r == "endfolder"):
print "Folder receive complete.";
break;
else:
filename = r;#file name
filedata = Receive(sok);# receive file data
f = open(filename,"wb");
f.write(filedata);
f.close();#finish to write the file
print "Received: " + filename;
try:
sok1 = socket.socket();
sok1.bind(("0.0.0.0",4444));
sok1.listen(5);
cl , addr = sok1.accept();#accepts connection
while(True):
r = Receive(cl);
sys.stdout.write("\n" + r);
next = raw_input();
cl.sendall(next);#send folder name to the client
FolderReceive(cl);
except BaseException, e:
print "Error: " + str(e);
os._exit(1);
我知道這有史以來最好的不是服務器...但什麼 我知道。這隻適用於文件夾較小的文件夾,因爲如果我發送大文件(如5mb ...),它會崩潰,因爲客戶端等待服務器的時間是不夠的。
所以我的問題是我怎麼能發送文件到服務器,而無需客戶端需要等待?或確切地知道客戶端需要等待服務器接收文件多少時間?一些相同的代碼,但處理任何文件大小,有什麼幫助?
是的我知道python不需要「;」和「()」,但我喜歡這樣做! – lazyy001
嗯,它讓你的代碼難以閱讀,因爲有經驗的Python開發人員 - 你想在這裏幫助你的人。 – abarnert
同時,'if(data.endswith(end)):'不會起作用,因爲結束標記可能來自數據包中的_anywhere_,而不僅僅是數據包的末尾。 (如果你想發送的文件中出現「292929」字符串,例如,如果你嘗試發送一個包含這個腳本的目錄,那麼這也將是一個大問題。) – abarnert