Python socket file transfer not working only with pdf files

213 Views Asked by At

I'm writing a file transfer server and client that transfers csv and pdf files, and checks a hash for integrity, and saves it to a local directory. Everything works fine on the csv, but when I try to send a pdf from client to server, the file will not be written and the server just saves the title in a 0kB pdf file. Not sure what to do from here, recv(bytes_read) keeps coming up empty.

Client send file function (Ignore the gross indentation, this is first stack overflow post)

def putFile(filename, serverIP, port, BUFFER_SIZE,s): #sends file to a server
filesize= os.path.getsize(filename)

#make and send hash
hexhash=getHash(filename)#make hash
s.send(hexhash.encode())#send hash 
print("Hash sent",hexhash)
received=s.recv(BUFFER_SIZE).decode()#receive response from server 
print(received)#print response

#send file
s.send(f"{filename}[SEPARATOR]{filesize}".encode())#send file name and size

with open(filename, "rb") as f: #open as read in binary to read in chunks
    while True:
       
        bytes_read = f.read(BUFFER_SIZE)#.encode() # read the bytes from the file in 4096B
        if not bytes_read:  # file transmitting is done
            print("Sent")
            #s.sendall(bytes_read)
            #s.send(("").encode())
            break
        s.sendall(bytes_read) #sendall assures transimission in busy networks 
        print(bytes_read)

print("waiting")
received=s.recv(BUFFER_SIZE).decode()#receive response from server about hash
print(received)#print response
if received== "Good hash":
    print("File stored")
elif received=="Bad Hash":
    print("File sent does not match file received")
    
s.close()
return 

#END SEND FILE

server function

storeFile(fileheader, cli, hexhash): #eceives a file from a client

filename, filedata=fileheader.split("[SEPARATOR]")

filename=os.path.basename(filename)#just the file name
print("File:",filename)   
#filesize=int(filesize)
cli.setblocking(False)   
with open(filename, "wb") as f: #save file
    while True:
        print("loop")
        try:
            print("trying")
            bytes_read = cli.recv(BUFFER_SIZE)# read up to BUFFSIZE bytes from the client
            print("reading:",bytes_read)
        except socket.error as e:
            print("except")
            err = e.args[0]
            if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                sleep(1)
                print("File Saved")
                break
            else:
                # a "real" error occurred
                print(e)
                sys.exit(1)
        else:
            # got a message, do something :)
            print("write")
            f.write(bytes_read)  # write to the file the bytes we just received)

       
#cli.close()
cli.setblocking(True)
#receive hash
check=checkHash(hexhash,filename)#check the hash given and the file hash, hash and file are given
if check: #hashes match
    cli.send(("Good hash").encode())#respond to clent that hashes match
    #print(received.split(SEPARATOR))
    
    #s.close()
    #END FILE RECEIPT"""
elif not check:
    cli.send("Bad hash".encode())
#cli.close()
return
0

There are 0 best solutions below