Handy Python One-liner for Sharing Files Quickly

Python-friendly

Python has a module named SimpleHTTPServer that implements the basic HTTP Server. This module can be used to share files quickly and easily. Executing the following command shares files and folders underneath the current directory via the HTTP server. The files so shared can then be downloaded from a web browser such as Firefox or Chrome.

In Python 2.x:

$ python -m SimpleHTTPServer [port]
Serving HTTP on 0.0.0.0 port 8000 ...

By default SimpleHTTPServer listens on the port number 8000. Providing a port_number as the second argument makes the SimpleHTTPServer to listen on that particular port.

$ python -m SimpleHTTPServer 4841
Serving HTTP on 0.0.0.0 port 4841 ...

 

In Python 3.x:

python3 -m http.server [port]

 

To download the files that are shared, enter http://IP_of_host:port ( For example, http://192.168.1.10:8000) in the Browser of any PC that has connectivity.

Directory listing for /
Pictures/
Projects/
Public/
Python/
Templates/
Videos/

 

If you’re not comfortable working in the command-line, run the following script inside the folder to share all the files and folders inside of it.

import SimpleHTTPServer
import SocketServer

PORT = 1234

handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("",PORT), handler))

print('Serving at port', PORT)
httpd.serve_forever()

Keyboard Ctrl + C to abort the HTTP server.

I find this method extremely handy way of copying files between systems, since Python is in all Linux distributions by default and oftentimes there’s no GUI available in servers. For Windows you need to get the Python installer and install it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s