A guide to transferring files using Python HTTP server and client scripts
This guide explains how to transfer files from a host PC (IP: 192.168.1.20) to a Linux Virtual Machine (VM, IP: 192.168.1.22)
using Python scripts.
The VM runs a server (`file_server.py`) to receive files, and the host runs a client (`file_client.py`) to send them over HTTP on port 8000.
Wireless LAN adapter Wi-Fi:
IPv4 Address: 192.168.1.20
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.1.1
sudo apt install python3
on Debian-based VM).requests
library on the host (pip install requests
).lsof
on the VM for port management (sudo apt install lsof
).sudo ufw allow 8000
).file_server.py
script to a directory (e.g., /home/YourName
)./tmp/uploads
) has write permissions.sudo apt install python3 lsof
.sudo ufw allow 8000
.file_client.py
script to a directory.requests
: pip install requests
.server_url
to http://192.168.1.22:8000
and file_path
to your file's path.sudo ufw allow 8000
?The command sudo ufw allow 8000
configures the firewall to permit incoming connections on port 8000:
Here's a breakdown of the command:
ufw
: A command-line tool for managing firewall rules on Debian-based systems like Ubuntu.allow 8000
: Adds a rule to permit TCP connections on port 8000.sudo
: Required for administrative privileges to modify firewall rules.Confirm that port 8000 is open and the server is reachable:
sudo ufw status
Look for 8000 ALLOW Anywhere
.
curl http://192.168.1.22:8000
Or ping the VM:
ping 192.168.1.22
If curl
fails with “Connection refused,” ensure file_server.py
is running and port 8000 is open.
sudo ufw allow 8000
opens port 8000 to all connections. For better security, restrict to the host's IP:
sudo ufw allow from 192.168.252.1 to any port 8000
sudo ufw delete allow 8000
file_server.py
and file_client.py
(e.g., to 8080)
and allow the new port:
sudo ufw allow 8080
curl http://192.168.1.22:8000
If you see Connection refused or Connection timed out, the server is not accessible.sudo iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
ps aux | grep file_server.py
. If not, start it with python3 file_server.py
.file_client.py
(e.g., http://192.168.1.22:8000
).ip addr
. If it shows a different IP (e.g., 172.20.10.4
), update file_client.py
accordingly.sudo iptables -L
to ensure the rules are applied correctly./tmp/uploads
)./tmp/uploads
) has the correct permissions (e.g., chmod 755 /tmp/uploads
).ufw
is enabled:
sudo ufw enable
ps aux | grep file_server.py
This will show if the server script is active. If not, start it with:
python3 file_server.py
sudo lsof -i :8000
If a process is using it, you can kill it with:
sudo kill -9 <pid>
Replace <pid>
with the process ID from the previous command.
ufw
is enabled:
sudo ufw enable
sudo lsof -i :8000
curl -v http://:8000
sudo iptables -I OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8000 -j ACCEPT
ping