Tutorial: Using Python http.server and netsh for File Access Between Devices
4 min read
Problem Statement
The other day I had a situation where I had two Windows machines from work that I wanted to transfer some big files (Bigger than allowed to send via email) and which I have no admin rights, so I could not really install programs and had no access to some websites to upload the files like Google, Dropbox and so on. I have Python installed though…
Solution
In this tutorial, we'll explore how I used Python's http.server
module to transfer files from devices running on the same network and also expand by adding a 3rd element that will port forward data.
To facilitate understanding, here's a simplified diagram of the setup:
Tools and Setup
Python: We'll use Python's built-in
http.server
module to serve files over HTTP.Windows Command Prompt:
netsh
will be used to set up port forwarding.WSL: We'll utilize WSL (Ubuntu in this case) to receive and access the files.
Step-by-Step Tutorial
Step 1: Set Up Python http.server
in WSL
Navigate to the directory containing the files you want to access:
cd /path/to/your/files
Start the Python HTTP server on a chosen port (e.g.,
8000
):python3 -m http.server 8000
This command starts an HTTP server on port
8000
, serving files from the current directory.
Step 2: Find the IP Address of Target
Find the IP address of your Target instance:
#Linux ifconfig ip addr show eth0 #Windows ipconfig
Step 3: Set Up Port Forwarding
— Windows
Make sure to disable the Windows firewall or create a rule to permit TCP connection on the listening port (
8888
).Open Command Prompt as Administrator on Windows.
Set up port forwarding using
netsh
. For example, to forward port8888
on Windows to8000
on WSL (172.18.47.94
):netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8888 connectaddress=172.18.47.94 connectport=8000
listenaddress=0.0.0.0
: Listens on all interfaces.listenport=8888
: Listens port8888
on Windows.connectaddress=172.18.47.94
: Forwards to Target address.connectport=8000
: Forwards to port8000
on Target.
To verify that the port forwarding was successful:
netsh interface portproxy show all
To remove the forwarding rule:
netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=8888
Step 4: Access Files from Any Machine on this Network
Open a web browser on another machine (can even be your phone).
Navigate to
http://<Windows-IP>:8888
.This URL will access the files served by Python
http.server
running in Target through the port forwarding set up bynetsh
.
Here is an image of my cellphone accessing the shared files from target.
— Linux (For Reference)
Basic Port Forwarding with netcat
Suppose you want to forward traffic from port 8888
on your Linux machine to port 8000
on a remote machine (e.g., your WSL instance with IP 172.18.47.94
).
Open a terminal on your Linux machine.
Set up port forwarding using
netcat
:nc -l -p 8888 -c "nc 172.18.47.94 8000"
-l
: Tellsnetcat
to listen for incoming connections.-p 8080
: Specifies that it should listen on port8080
.-c "nc 172.18.47.94 8888"
: When a connection is received, this runs anothernetcat
command that forwards the traffic to the remote host (172.18.47.94
) on port8000
.
Step 2: Explanation
Any connection to your Linux machine on port
8888
will be forwarded bynetcat
to port8888
on the target machine (172.18.47.94
), which is your WSL instance in this case.Note: This setup is relatively simple but has limitations. It's most useful for basic forwarding in scenarios where
netcat
works reliably. For more complex routing, tools likeiptables
orsocat
offer greater control.
Step 3: Persistent Netcat Forwarding with -k
To keep the listener running for multiple connections (persistent mode), you can add the -k
option:
nc -lk -p 8888 -c "nc 172.18.47.94 8000"
The -k
flag tells netcat
to continue listening for connections after the current one closes.
— SSH
Port Forwarding on Linux Using SSH (Alternative)
For simpler setups or remote access, SSH port forwarding is often easier than configuring nc
. If SSH is set up, you can use this command:
Run this on your Linux machine to forward port
8080
to8000
:ssh -L 8888:localhost:8000 user@172.18.47.94
This forwards local port
8080
to the Target machine's port8000
over SSH.
Conclusion
This tutorial demonstrated how to leverage Python's http.server
module along with netsh
on Windows to securely transfer files from a Windows host machine to a Target instance. We created a simple yet effective setup for accessing files across different environments within the same local network by using HTTP for file transfer and port forwarding.
Following these steps, you can overcome some limiting scenarios and set up file sharing and access between machines, enhancing your workflow in mixed environment.