Monday, October 04, 2010

A very useful Python tool: SimpleHTTPServer

I've talked a lot in this blog about a very convenient Python tool for transferring files/folders between computers on the same LAN irrespective of which platform any of them are running.
In a terminal on the computer whose files you want to share, just type the command
python -c "import SimpleHTTPServer; SimpleHTTPServer.test();"
Then, in the client computer, open a browser and type in the url window the IP of the server at port 8000. For example, it could be 192.168.1.12:8000.
After a delay of a few seconds, the client should now show all the files and folders starting from the directory from which the server issued the above command.
From there, any file can be downloaded to the client.
I had known for some time that there is, in fact, a shorter (and more easily memorizable) command available to do exactly the same thing. This is
python -m SimpleHTTPServer
which again should be issued from the directory on the server from which the files are to be shared.
Today I did a lot of playing around with this and found that the command worked flawlessly from OS X (10.6.2), Ubuntu 10.10 Beta, Haiku R1A2 and Windows XP SP3. (all with either Python 2.6 or 2.7)
However, I had some as yet unexplained difficulties in getting it to work in Windows 7 Ultimate but, equally inexplicably, it then started to work fine (using Python 2.7).
Note that Python 3.1.2 did NOT work in Windows 7 (although I've only tried it once).
In Windows 7, I used both the command prompt and Cygwin and both functioned equally well (or badly initially).
I had no problems with a range of computers and platforms serving as client. I even included my iPad and my iPod Touch 3G.
This is a very useful tool which is very simple to use. There is nothing to download, although the server must have Python installed, and the command is easy to remember if you're away from your usual workplace.
Of course, there may be occasions when you want to upload from the client to the server. In principle this should amount to merely issuing the command from the client, which now becomes the server.
However, I came across this site which offers a python script which apparently allows uploading from the client to the server. I haven't tried it but here it is.

2 comments:

  1. whats the command in python to upload to server (in our case http server).?

    ReplyDelete
  2. The command is as I've explained in the post:

    python -m SimpleHTTPServer

    making sure you start from a relevant directory on the server.

    ReplyDelete