Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.)

like image 557
Baboon Avatar asked Jan 04 '12 04:01

Baboon


People also ask

How do I share files between two computers?

To share a file or folder over a network in File Explorer, do the following: Right-click (or long-press) a file, and then select Show more options > Give access to > Specific people. Select a user on the network to share the file with, or select Everyone to give all network users access to the file.

Can you directly connect two computers to transfer files?

Two computers can be easily connected to share the files between them or to share the internet, printer between them. The process is usually simple and can be done with a few hardware devices and a bit of software knowledge.


1 Answers

A. Python3

We can use http.server for this. From SO answer here, SimpleHTTPServer is moved to http.server in python3.

python -m http.server

Python2:

I use SimpleHTTPServer for this sometimes:

python -m SimpleHTTPServer

...which would serve the files in the current directory on port 8000. Open your web browser on the other computer and download whatever you want.

To know the IP address of your computer, you can use (in Ubuntu) ifconfig, e.g:

$ ifconfig
enp0s31f6 Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
          inet addr:10.0.0.3  Bcast:10.0.0.255  Mask:255.255.255.0

In windows it is ipconfig.

Then, in the other computer, you send the browser to: http://10.0.0.3:8000.

B. If you have SSH enabled you could use paramiko to connect and SFTP transfer whatever you want.

like image 162
zeekay Avatar answered Sep 28 '22 05:09

zeekay