I'm trying to make a program in Python that implements sockets, each client sends a PDF file and the server receives it and the title is changed to "file_number.pdf" (i.e.: file_1.pdf). The problem presented is that only a client can send a file successfully. When a second client tries to send the program does crash. What am I doing wrong and how can I solve my code to allow N clients (with N <20) to connect to the server and transfer files?
我尝试在Python中创建一个实现套接字的程序,每个客户机发送一个PDF文件,服务器接收它,然后将标题改为“file_number”。pdf”(即。:file_1.pdf)。出现的问题是只有客户机才能成功发送文件。当第二个客户机试图发送程序时,它会崩溃。我做错了什么,我该如何解决我的代码让N个客户端(有N <20)连接到服务器和传输文件?
Here's the server code:
这里的服务器代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.
sc, address = s.accept()
print address
i=1
f = open('file_'+ str(i)+".pdf",'wb') # Open in binary
i=i+1
while (True):
# Recibimos y escribimos en el fichero
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
Here's the client code:
客户端代码:
import socket
import sys
s = socket.socket()
s.connect(("localhost",9999))
f = open ("libroR.pdf", "rb")
l = f.read(1024)
while (l):
s.send(l)
l = f.read(1024)
s.close()
To simplify my code I always use a book with file name "libroR.pdf", but in the full code it is chosen by a graphical user interface.
为了简化我的代码,我总是使用一个名为“libroR”的书。但在完整的代码中,它是由图形用户界面选择的。
3 个解决方案
#1
13
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.
您必须将所有的代码从sc, address = .accept() upto sc.close()到另一个循环,或者在收到第一个文件后,服务器将终止。它不会崩溃,脚本刚刚完成。
[EDIT] Here is the revised code:
[编辑]这是修改后的代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.
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):
# recibimos y escribimos en el fichero
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
Note that s.listen(10)
means "set maximum accept rate to 10 connections", not "stop after 10 connections".
注意。listen(10)的意思是“将最大接受率设置为10个连接”,而不是“在10个连接后停止”。
#2
3
Your code is getting stuck in the second while loop.
你的代码在循环中被卡住了。
See:
看到的:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10)
i=1
while True:
sc, address = s.accept()
print address
f = open('file_'+str(i)+".pdf",'wb') #open in binary
i=i+1
print(i)
l = 1
while(l):
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
#3
1
You are closing the server socket (s
in your code) after handling the first client connection. Thus only one client is ever handled by your server. Make a loop around accept
and reading from the sc
.
在处理第一个客户端连接后,您正在关闭服务器套接字(在您的代码中)。因此,服务器只处理一个客户机。从sc中对接收和读取进行循环。
#1
13
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.
您必须将所有的代码从sc, address = .accept() upto sc.close()到另一个循环,或者在收到第一个文件后,服务器将终止。它不会崩溃,脚本刚刚完成。
[EDIT] Here is the revised code:
[编辑]这是修改后的代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.
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):
# recibimos y escribimos en el fichero
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
Note that s.listen(10)
means "set maximum accept rate to 10 connections", not "stop after 10 connections".
注意。listen(10)的意思是“将最大接受率设置为10个连接”,而不是“在10个连接后停止”。
#2
3
Your code is getting stuck in the second while loop.
你的代码在循环中被卡住了。
See:
看到的:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10)
i=1
while True:
sc, address = s.accept()
print address
f = open('file_'+str(i)+".pdf",'wb') #open in binary
i=i+1
print(i)
l = 1
while(l):
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
#3
1
You are closing the server socket (s
in your code) after handling the first client connection. Thus only one client is ever handled by your server. Make a loop around accept
and reading from the sc
.
在处理第一个客户端连接后,您正在关闭服务器套接字(在您的代码中)。因此,服务器只处理一个客户机。从sc中对接收和读取进行循环。