Use Python module to run a basic HTTP server and use a web browser on mobile phone to access the localhost PC via USB tethering.
Overview of setup and usage
A mobile phone is connected to localhost PC via a USB cable.
- Setup on mobile phone (USB tethering is enabled)
- Setup on localhost PC (see Part A.)
- Access from mobile phone (see Part B.)
When user has finished access, go to localhost PC and press Ctrl+C to stop the HTTP server that is running in the terminal. Or simply closing the terminal may terminate the process as well.
Part A: Setup on localhost PC
Ensure that Python is installed on the localhost PC (Run python --version
in a terminal to check the version, which will affect step 3).
Open a terminal
First, check the IP address of localhost PC using ip
or ifconfig
command (whichever works). The following example output shows 192.168.XX.YYY
is the IP address that has been assigned to a localhost PC via USB tethering.
$ ip addr show usb0
3: usb0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
link/ether **:**:**:**:**:** brd ff:ff:ff:ff:ff:ff
inet 192.168.XX.YYY/** brd 192.168.XX.ZZZ scope global usb0
valid_lft forever preferred_lft forever
inet6 ****::****:****:****:****/** scope link
valid_lft forever preferred_lft forever
$ ifconfig usb0
usb0 Link encap:Ethernet HWaddr **:**:**:**:**:**
inet addr:192.168.XX.YYY Bcast:192.168.XX.ZZZ Mask:255.255.255.0
inet6 addr: ****::****:****:****:****/** Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
...
Then, start an HTTP server using Python module with any available port number i.e. 31415 (Easy to remember as 'pi'). The following example shows http.server
module is used for Python 3, otherwise use SimpleHTTPServer
module for Python 2.
$ python -m http.server <port_number>
Serving HTTP on 0.0.0.0 port ***** ...
Additionally, if localhost PC has enabled firewall, add a new rule to allow incoming connection from the port number and IP address (This would be another set of question and answer). Else temporarily disable firewall would just work for simplicity in exchange of low security.
Part B: Access from mobile phone
Ensure that USB tethering remain enabled; No further configuration.
Open a web browser
Go to http://192.168.XX.YYY:<port_number>
in which 192.168.XX.YYY
is the IP address used by localhost PC and <port_number>
is any available port number on localhost PC.
The web browser will show Directory listing of current working directory, which by default is Home directory of current user.
User can now access any files and directories in the current working directory for browsing and download purpose.
Limitation and compatibility
An HTTP server will allow read-only access, which is good enough for browsing and download purpose. User will not be able to modify files on the localhost PC. Should user need both read and write access, then use other method (Perhaps using SFTP or SSH, which I do not know).
The HTTP server method will work as long as USB tethering is enabled (IP address is known), regardless of Airplane Mode or Cellular Data is turned on or off. Likely work for mobile phones with any Android version (not sure for other mobile operating systems).
Tested using Python 2.7 and Python 3.4, with UFW enabled and disabled on Xubuntu 14.04 and USB tethering enabled on Android 7.1.1.
References
Related posts on SE network