Fastest way of sharing files using a Python one-liner

Python-friendlyPython has a module named SimpleHTTPServer that implements the 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 Mozilla Firefox.

In Python 2.x:

$ python -m SimpleHTTPServer [port]
Serving HTTP on 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 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, in the Browser of any PC that has connectivity.

Directory listing for /

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)

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.

If you’re using Internet Explorer 11, know how to overcome the Bing Search behavior in its address bar.

About Deepak Devanand

Seeker of knowledge
This entry was posted in Python and tagged , , , . Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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