我試圖通過套接字傳輸文件,如果我立即關閉連接立即關閉連接 現在我想繼續發送命令到服務器上傳完成後,但服務器只是忽略他們,並認爲有更多的線路來對文件Python通過套接字傳輸文件
這裏是我到目前爲止的代碼 客戶:
def client_sender():
global upload
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print target
print port
client.connect((target, port))
if upload:
with open(upload_destination, "rb") as f:
for line in f:
client.send(line)
f.close()
client.send("DONE\r\n")
upload = False
print client.recv(1024)
buffer = ""
buffer = sys.stdin.read()
#... some code for sending commands and receiving a response
服務器:
def handle_client(client_socket):
global upload
print "Client connected"
if upload:
file_buffer = ""
while True:
data = client_socket.recv(1024)
if data.rstrip() == "DONE":
break
file_buffer += data
try:
file_descriptor = open(upload_destination, 'wb')
file_descriptor.write(file_buffer)
file_descriptor.close()
client_socket.send("Successfully placed the file in %s" %upload_destination)
except:
client_socket.send("Failed writing to the file")
upload = False
#... same as client, just some more code for commands
如果文件包含「DONE」,會發生什麼情況會很有趣。 – mhawke
這是爲了傳輸我編寫的已編譯的C程序,所以沒有辦法可以適得其反 – Aginu