Pickle file and send via socket

UTKARSH PANDEY u13051995p at gmail.com
Thu Feb 17 23:58:16 EST 2022


On Wednesday, August 8, 2012 at 8:37:33 PM UTC+5:30, lipska the kat wrote:
> On 08/08/12 14:50, S.B wrote: 
> > On Wednesday, August 8, 2012 3:48:43 PM UTC+3, lipska the kat wrote: 
> >> On 06/08/12 14:32, S.B wrote: 
> >>
> [snip]
> > Thank you so much ! 
> > The examples are very helpful. 
> > What happens if I have a regular text file I want to send via the network. 
> > Do I need to read the file and then dump it into the "stargate" file object?
> Well according to the documentation at 
> 
> http://docs.python.org/py3k/tutorial/inputoutput.html#reading-and-writing-files 
> 
> it should be straightforward to read and write pickled files 
> Not sure why you want to pickle a text file over the network when you 
> could just stream it between ports ! 
> 
> however ... 
> 
> I'm currently getting a Unicode decode error on the first byte in the 
> stream when it gets to the other end, no idea why so I guess I have to 
> continue searching, read the documentation above and see if you can 
> figure it out, that's what I'm doing.
> lipska 
> 
> -- 
> Lipska the Kat: Troll hunter, sandbox destroyer 
> and farscape dreamer of Aeryn Sun



Directly read bytes from file and send it over the socket object from client side in while loop until all content from file is read.

Something like this.

Client side
import socket

s = socket.socket()

PORT = 9898

s.connect(("192.168.0.101",PORT))

file = open("turnover.csv","rb")
SendData = file.read(1024)

while SendData:
    s.send(SendData)
    SendData = file.read(1024)


s.close()

Server side 


import socket
s = socket.socket()
PORT =9898
print("Server is listening on port :",PORT,"\n")

s.bind(("192.168.0.101",PORT))

s.listen(10)

file = open("recv.csv","wb")
print("\n Copied file name will be recv.txt at server side\n")

while True:
    conn,addr = s.accept()
    RecvData = conn.recv(1024)
    while RecvData:
        file.write(RecvData)
        RecvData = conn.recv(1024)

    file.close()
    print("\n File has been copied successfully \n")

    conn.close()
    print("\n Server is closing the connection \n")


    break


More information about the Python-list mailing list