Tuesday, 28 March 2017

How to transfer a file between two connected computers in python?



I don't know if this has been answered before(i looked online but couldn't find one), but how can i send a file (.exe if possible) over a network to another computer that is connected to the network? I tried sockets but i could only send strings and i've tried to learn ftplib but i don't understand it at all or if ftp is even what i am looking for, so i am at a complete standstill. Any input is appreciated (even more so if someone can explain FTP, is it like socket? All the examples i've seen don't have a server program where the client can connect to.)


Answer



Some simplistic example code for the sending side:



if os.path.exists(df):

with open(df, 'rb') as f:
packet = f.read(blocksize)

while packet != '':
conn.send(packet)

packet = f.read(blocksize)


Where:




df = 'path/to/data/file'
blocksize = 8192 # or some other size packet you want to transmit.
# Powers of 2 are good.
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...