Send a file through sockets in Python

2019-01-11 07:05发布

问题:

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?

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.

回答1:

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.

[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".



回答2:

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:

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.



回答4:

Using this code you can send files multiple time using the same client.py

Server.py

import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.

i = 1
while True:
    sc, address = s.accept()

    print address
    f = open('file_'+ str(i)+".wav",'wb') #open in binary
    i=i+1
    while (True):
        # recibimos y escribimos en el fichero
        l = sc.recv(1024)
        f.write(l)

        if not l:
            break

    f.close()
    sc.close()
    print('copied the file.')

s.close()