[Python] Share files the easy way with python servlet

This one was written by techb. Simple method yet interesting. Could help in some situations.

I am running VirtualBox inside Ubuntu. I have XP installed as a virtual OS and often find I want to drag-drop/copy-paste files between systems. Well you could go the route of setting up a shared folder, which would most likely be the recommended way.

You could also try installing Guest Additions so the clipboard is shared between host and guest. But it seems Guest Additions only works for text, for me anyway. If I tried to copy, say and archive like .zip, it wouldn’t transfer to the host.

We can make things really simply with a bit-o-python. I use this script on my Android device via SL4A to grab videos or images I’ve taken with my device, and figured I could use it with the situation of transferring files with VM also. So, now I use it with VM along with transferring files to any computer on my network.

So here is the code. Very simple and very short. It is an extremely small servlet that will host the folder/sub-folders it is run in.

import SimpleHTTPServer, SocketServer
 
h = SimpleHTTPServer.SimpleHTTPRequestHandler
s = SocketServer.TCPServer(("",58080), h)
s.serve_forever()

We use two built-in libs that come with the standard library. We do this because why handle all the HTTP requests or transferring our selves when we can leverage the epicness of python.

The number you see, 58080 is the port number our server is going to be running on. To access this newly found glory of file transfer on our local network, we simply open up our favorite webbrowser (mine is chrome, but any will work), and type in the servers IP followed by the port it is running on.

If you don’t know the IP, in a terminal/cmd write these:
Linux:

ifconfig

Windows:

ipconfig

or use the GUI in the network connections.
The rest of the information regarding IP’s with linux apply.
OR you could use a tool that I made for this
Linux, Windows

Now that you have the IP and your browser is open and your script is running, in the url bar type in the IP address then the port number, it should look like:

http://192.168.0.25:58080 

and hit enter/carriage return. Note the “:” between the IP and port. This is required to direct the connection to the port you want.
You should now see a page that says something like “Index of [folder your script is in]”. From there you can browse the files and sub-folders. Download archives, images, videos, scripts, etc..

If you would like to add more functionality, such as choosing the folder to server, you could import os and chdir to the folder you want, or place and run the script from the folder you want.

Here is an example:

import SimpleHTTPServer, SocketServer, os
 
sfolder = raw_input("Full path to folder to share: ")
os.chdir(sfolder)
 
h = SimpleHTTPServer.SimpleHTTPRequestHandler
s = SocketServer.TCPServer(("",58080), h)
s.serve_forever()

It is vital that you use chdir BEFORE s.serve_forever(). If you tried to do it after that, it will not work because it is serving and will block until the script is killed.

So instead of sharing the “Downloads” folder I normally do, I run the script an input “/home/techb/Desktop” and now it is serving my desktop as an “index of Desktop”. If you want full access to all files on your system you could input something like “/”. This is also going to display hidden files.

You could add support of Internet access by port forwarding, but I will highly recommend not going to the cloud with this.

3 comments

  1. crispin says:

    Hi – Sorry for resurrecting an old post …

    I’ve been trying this on KVM (windows 8.1 guest) and it works brilliantly to get files from my Ubuntu box. The problem I’m having is getting files back from windows …
    Can this be done with the same connection???

  2. Techb says:

    Script author here, no I haven’t added any two-way functionality to the script itself.

    You can run the script on your windows machine and do it that way though and it should work just the same. You might have to install Python 2.7 on your windows machine though cause I know it doesn’t come with it, but just run from what ever computer you wanna grab files from, or phone or whatever you have that can run Python.

  3. parrotsquad says:

    yah techb is right

Leave a Reply

Your email address will not be published. Required fields are marked *