Want to turn your Android phone into a simple local web server? With Termux and Python, you can quickly start a local HTTP server and access files from a web browser on the same device or, when your network setup allows it, from another device on your local network.
This guide explains how to install Python, start a local HTTP server, choose a custom port, share files on your local network, stop the server, and troubleshoot common problems.
Table of Contents
- Requirements
- Install Python in Termux
- Create a Server Folder
- Start the HTTP Server
- Open the Server in a Browser
- Use a Custom Port
- Host a Simple HTML Website
- Access from Another Device
- Stop the Server
- Run the Server in the Background
- Troubleshooting
- Security Tips
- FAQ
Requirements
Before starting, make sure you have:
- An Android device
- Termux installed
- A stable network connection if you plan to access the server from another device
- Basic knowledge of Termux commands
You will also need Python, which can be installed directly through the Termux package manager.
Step 1: Update Termux and Install Python
Open Termux and update the installed packages:
pkg update && pkg upgrade -y
Now install Python:
pkg install python -y
Verify the installation:
python --version
If Python is installed correctly, Termux will display the installed Python version.
Step 2: Create a Folder for Your Local Server
Create a directory where you want to store the files that will be served by the HTTP server.
mkdir myserver
Move into the directory:
cd myserver
You can check your current directory with:
pwd
Every file inside this directory can be made available through the local HTTP server.
Step 3: Start the Local Python HTTP Server
Python includes a simple HTTP server module. From inside your chosen directory, run:
python -m http.server
By default, the server uses port 8000.
You may see output similar to:
Serving HTTP on 0.0.0.0 port 8000 ...
Your local HTTP server is now running.
Step 4: Open the Server in Your Browser
On the same Android device, open a browser and visit:
http://localhost:8000
You can also try:
http://127.0.0.1:8000
The browser should display a directory listing or your website's index.html file if one exists.
Use a Custom Port
You can choose a different port number.
For example, to use port 8080:
python -m http.server 8080
Then open:
http://localhost:8080
You can use another available port if needed:
python -m http.server 3000
Then visit:
http://localhost:3000
Host a Simple HTML Website
Create an HTML file:
nano index.html
Add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Termux Server</title>
</head>
<body>
<h1>Hello from Hacker World!</h1>
<p>This website is running locally from Termux using Python.</p>
</body>
</html>
Save the file and start the server:
python -m http.server 8000
Open:
http://localhost:8000
Python will automatically serve index.html when the directory is opened.
Access the Server from Another Device
If your Android device and another device are connected to the same local network, you may be able to access the server using the Android device's local IP address.
Start the server:
python -m http.server 8000
Check network information using:
ip addr
Look for a local IPv4 address associated with your active network interface.
For example, if your phone's local address is:
192.168.1.10
Another device on the same network may be able to open:
http://192.168.1.10:8000
Note: Local network access depends on your Wi-Fi configuration, Android networking behavior, device settings, and firewall or hotspot restrictions.
Serve Files from Android Storage
If you want to serve files stored in your Android shared storage, first enable storage access in Termux:
termux-setup-storage
Allow the storage permission when Android asks.
Then move to the shared storage directory:
cd ~/storage/shared
For example, create a folder:
mkdir website
cd website
Start the server:
python -m http.server 8000
Files inside the folder can now be accessed through your local HTTP server.
Start the Server for a Specific Directory
You do not always need to change into the directory first.
Use the --directory option:
python -m http.server 8000 --directory ~/myserver
This starts the server on port 8000 and serves files from the specified directory.
Test the Local Server Using cURL
You can test your local server directly from Termux using cURL.
Install cURL if needed:
pkg install curl -y
Then run:
curl http://localhost:8000
To check only the response headers:
curl -I http://localhost:8000
This is useful for confirming that the server is responding correctly.
How to Stop the Python HTTP Server
When the server is running in the Termux terminal, press:
CTRL + C
This stops the Python HTTP server and returns you to the Termux command prompt.
Run the Server in the Background
You can start the server as a background process:
python -m http.server 8000 &
Check running Python processes:
ps -ef | grep python
If you identify the server process, you can stop it using:
kill PROCESS_ID
Replace PROCESS_ID with the actual process ID.
Save Server Logs
You can redirect output to a log file:
python -m http.server 8000 > server.log 2>&1 &
View the log:
cat server.log
To monitor updates:
tail -f server.log
Useful Python HTTP Server Commands
| Command | Description |
|---|---|
python -m http.server |
Start server on the default port |
python -m http.server 8000 |
Start server on port 8000 |
python -m http.server 8080 |
Start server on port 8080 |
python -m http.server --directory ~/myserver |
Serve a specific directory |
curl http://localhost:8000 |
Test the local server |
python -m http.server 8000 & |
Run the server in the background |
Common Problems and Fixes
1. Python Command Not Found
If you see:
python: command not found
Install Python:
pkg install python -y
Then verify:
python --version
2. Address Already in Use
If you see an error similar to:
OSError: [Errno 98] Address already in use
Another application or process may already be using that port.
Try a different port:
python -m http.server 8080
3. Browser Cannot Open localhost
Make sure the Python server is still running in Termux.
Try both:
http://localhost:8000
and:
http://127.0.0.1:8000
You can also test directly in Termux:
curl http://localhost:8000
4. Another Device Cannot Access the Server
Check that:
- Both devices are connected to the same local network.
- You are using the correct local IP address.
- The Python server is running.
- Your Wi-Fi or hotspot does not isolate connected devices.
You may also try starting the server explicitly bound to all available interfaces:
python -m http.server 8000 --bind 0.0.0.0
5. Permission Problems With Android Storage
Run:
termux-setup-storage
Then grant storage permission and access files through the ~/storage directory.
Security Tips
Python's built-in HTTP server is useful for local development, testing, and simple file sharing, but it should not be treated as a production-grade web server.
- Do not expose sensitive files.
- Avoid sharing private directories.
- Do not expose the development server directly to the public internet.
- Use a proper web server and security configuration for production websites.
- Stop the server when you no longer need it.
Quick Setup Commands
# Update Termux
pkg update && pkg upgrade -y
# Install Python
pkg install python -y
# Create a project folder
mkdir myserver
cd myserver
# Create a simple HTML file
nano index.html
# Start the server
python -m http.server 8000
Then open:
http://localhost:8000
Frequently Asked Questions
Can I run a Python HTTP server on Android?
Yes. You can install Python in Termux and use Python's built-in http.server module to serve files locally.
What command starts a Python HTTP server?
python -m http.server
What is the default Python HTTP server port?
The default port is usually 8000.
How do I use a different port?
python -m http.server 8080
How do I stop the server?
Press:
CTRL + C
Can another device access my Termux server?
Potentially, yes. If both devices are on the same local network and the network allows device-to-device communication, you can try accessing the server using your Android device's local IP address and port.
Is Python's built-in HTTP server suitable for production?
No. It is intended for simple development, testing, and local file-serving use cases. Use a properly configured production web server for public or production deployments.
Conclusion
Setting up a local Python HTTP server in Termux is one of the easiest ways to experiment with web development directly on Android. With only a few commands, you can serve HTML files, test websites, access files through your browser, and experiment with local networking.
The main command you need to remember is:
python -m http.server 8000
This creates a simple local server that can be opened from:
http://localhost:8000
For more Termux tutorials, programming guides, Android tools, and technology resources, visit Hacker World.







0 comments:
Post a Comment