Author Topic: [python]Share files the easy way  (Read 2135 times)

0 Members and 4 Guests are viewing this topic.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
[python]Share files the easy way
« on: July 11, 2012, 05:20:30 am »
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 server that will host the folder/sub-folders it is run in.


Code: (python) [Select]

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:
**Linux:
in a terminal
Code: [Select]
ifconfigLook for wlan if your on wifi, if you have a hardline look for eth0 or eth* under IP. There will also be subnets showing that look like 255.255.255.0, which is the most common subnet for soho (small office home office) networks, which is class C. But don't stress over that. Your IP will look something like 192.168.1.52, also class C, but I digress. If you need more info on IP's use google, this isn't hard to find, and if your on this forum you should know about it anyway.


**Windows:
in a cmd (command prompt)
Code: [Select]
ipconfig
or use the GUI in the network connections.
The rest of the information regarding IP's with linux apply.


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
Code: [Select]
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 say's 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:
Code: (python) [Select]

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.
« Last Edit: July 11, 2012, 05:24:29 am by techb »
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [python]Share files the easy way
« Reply #1 on: July 11, 2012, 08:32:03 am »
Oh dear lord! This is amazing.
I always used SMB (Simple File Sharing) to transfer stuff from my phone and a VM. ASTRO file manager with an SMB module provides access to shared folders.
This looks much better ^^

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [python]Share files the easy way
« Reply #2 on: July 11, 2012, 09:18:22 am »
Didn't know ASTRO had that feature.


Glad it could help someone out. I know it has helped me more than a few times.
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [python]Share files the easy way
« Reply #3 on: July 11, 2012, 01:43:58 pm »
It doesn't on its own. You need a module for that.

Offline LeXeL

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 1
    • View Profile
Re: [python]Share files the easy way
« Reply #4 on: July 11, 2012, 09:36:29 pm »
The same thing but one line
Code: [Select]
python -m SimpleHTTPServer 8000

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [python]Share files the easy way
« Reply #5 on: July 11, 2012, 10:14:22 pm »
The same thing but one line
Code: [Select]
python -m SimpleHTTPServer 8000


But where is it going to ask you what directory to serve?
>>>import this
-----------------------------

Offline LeXeL

  • /dev/null
  • *
  • Posts: 12
  • Cookies: 1
    • View Profile
Re: [python]Share files the easy way
« Reply #6 on: July 11, 2012, 10:21:28 pm »
Execute the command when you are in the directory you want...


Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [python]Share files the easy way
« Reply #7 on: July 11, 2012, 10:38:06 pm »
Execute the command when you are in the directory you want...


I don't think you get the point of having a script, but it's okay.


Also, that command wont work on Android. Even with su.
« Last Edit: July 11, 2012, 10:46:05 pm by techb »
>>>import this
-----------------------------

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: [python]Share files the easy way
« Reply #8 on: July 12, 2012, 07:41:04 am »

Here is a function to grab your IP on your android device and display for easier use.


Code: [Select]
import os

def curip():
  ip = os.popen("netcfg")
  l = [i.split()[2] for i in ip.readline() if "UP" in i and "lo" not in i]
  print "Current IP, not including loopback:"
  for i in l:
    print i, " on port 58080"
>>>import this
-----------------------------