更新时间:2021-11-27 08:10:54
你必须把所有的代码从 sc, address = s.accept()
到 sc.close()
进入另一个循环,或者服务器在收到第一个文件后简单地终止.它没有崩溃,脚本刚刚完成.
You must put all the code from sc, address = s.accept()
upto sc.close()
into another loop or the server simply terminates after receiving the first file. It doesn't crash, the script is just finished.
这是修改后的代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Accepts up to 10 connections.
while True:
sc, address = s.accept()
print address
i=1
f = open('file_'+ str(i)+".pdf",'wb') #open in binary
i=i+1
while (True):
# receive data and write it to file
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
请注意,s.listen(10)
的意思是将最大接受率设置为 10 个连接",而不是在 10 个连接后停止".
Note that s.listen(10)
means "set maximum accept rate to 10 connections", not "stop after 10 connections".