0% found this document useful (0 votes)
9 views14 pages

Python HTTP Server Setup Guide

Python is a programming language that includes a built-in module, http.server, allowing users to quickly set up a simple web server for local testing without configuration. The server can be started using the command 'python3 -m http.server' and can be accessed via a web browser. Differences between WSL 1 and WSL 2 include IP address sharing and the need for port forwarding in WSL 2 to access the server externally.

Uploaded by

David Sofita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views14 pages

Python HTTP Server Setup Guide

Python is a programming language that includes a built-in module, http.server, allowing users to quickly set up a simple web server for local testing without configuration. The server can be started using the command 'python3 -m http.server' and can be accessed via a web browser. Differences between WSL 1 and WSL 2 include IP address sharing and the need for port forwarding in WSL 2 to access the server externally.

Uploaded by

David Sofita
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

What is Python

 Python is a programming language — and it’s already included in most Linux


distributions (including those used in WSL).
 You run it using:
 python3

(The 3 means it’s version 3 of Python — version 2 is obsolete.)

2️⃣ The -m option

 The -m flag tells Python:


“Run this built-in module as a script.”
 In our case, the module is [Link], which is part of Python’s standard library — it
comes preinstalled.

3️⃣ The [Link] module

 It’s a tiny web server included in Python for quick local testing.
 It doesn’t require any coding or configuration — you just run it from the folder you want
to share.

So:

python3 -m [Link]

means:
➡ “Start a web server in this directory, serving its contents over HTTP.”

the server starts on port 8080, because you explicitly wrote it.

4️⃣ Port and binding

You can add:

python3 -m [Link] 8080 --bind [Link]

where:

 8080 = port number


 --bind [Link] = listen on all interfaces (so it’s reachable from Windows or LAN)

If you omit them:

python3 -m [Link]

it will:

 Use port 8000 by default


 Bind to all interfaces by default

5️⃣ Example in practice

1. Go into a folder you want to share:


2. cd /mnt/c/Users/Veronica/Desktop
3. Start the server:
4. python3 -m [Link] 8080
5. Open your browser and go to:
6. [Link]
7. You’ll see your Desktop files listed in the browser!

What happens when you run:


python3 -m [Link] 8080

1. Python starts a temporary HTTP server (a very simple one).


2. It uses the current folder (where you ran the command) as the web root.

3. It checks inside that folder:

o If there’s a file called [Link] or [Link], it will serve that file


automatically as the default page.

o If there’s no index file, it generates a directory listing — showing all files and
subfolders with links, like this:

4. Directory listing for /mnt/c/Users/Veronica/Desktop


5. -----------------------------------------------
6. [Link]
7. [Link]
8. [Link]
9. ...
So:

Situation What appears in the browser

Folder contains [Link] That HTML page is shown automatically

No [Link] present A file/folder listing is generated automatically

6️⃣ Why it’s useful

This built-in Python command is often used for:

 Quickly sharing files between systems


 Testing web apps or APIs
 Checking network connectivity
 Demonstrating how “binding” works

✅ Summary

Element Meaning
python3 Runs the Python interpreter
-m Run a module as a program
[Link] Built-in module that acts as a simple web server
8080 Port number
--bind [Link] Listen on all network interfaces

Why Python’s [Link] became popular

 Built-in in Python (installed by default in most Linux distros and WSL).


 No installation needed, just one command:

 python3 -m [Link]
 Serves the current folder immediately.

 Lightweight and perfect for quick tests or file sharing.

1️⃣ Apache HTTP Server

 Apache is a full-featured, production-ready web server.


 It has been widely used for web hosting, intranet servers, and production
environments for decades.

 Features:

o Handles multiple websites, virtual hosts

o Supports PHP, SSL, CGI, modules, etc.

 ✅ Still heavily used in production environments, especially on Linux servers.

2️⃣ Python’s built-in [Link]

 This is not a replacement for Apache.


 It’s a very simple, temporary HTTP server:

o Serves files from a folder

o Useful for testing, development, quick file sharing

 Pros:

o Zero configuration

o Pre-installed in Python

 Cons:

o Not secure for production

o Does not handle heavy load or multiple sites


3️⃣ Why you see Python used now

 On WSL, local testing, or tutorials, Python is convenient because:


o No installation needed

o Runs in one command

o Perfect for quick experiments or demonstrations

 Apache is still used in real servers, but for local WSL testing, it’s overkill.

4️⃣ Summary / Comparison

Feature Apache Python [Link]

Purpose Production web server Development / testing

Configuration Complex Minimal, almost zero

Security Full support None — not for production

Use today Yes, widely Yes, for quick tests

Installation Must install Built-in Python module

💡 Key point:

 Apache did not disappear — it’s still used in production Linux servers.
 Python’s [Link] is just a lightweight tool for local testing, WSL demos, or
teaching.

 Seeing Python examples in WSL tutorials doesn’t mean Apache is obsolete — it’s just
not needed for small local tests.

1️⃣ Python [Link] is “temporary”

 Temporary does not mean the Python module is uninstalled or deleted.


 It means that the HTTP server only runs while you run the command, for example:
 python3 -m [Link] 8080
o As soon as you stop the command (Ctrl+C in terminal) or close the terminal,
the server stops running.
 Unlike Apache, which runs as a background service/daemon continuously, Python’s
server is manual and session-based.

2️⃣ Why it feels “permanent”

 The module itself ([Link]) is part of Python — it’s installed permanently.


 You can run it any time, in any folder.
 But the server instance you start is only alive while the command runs.

3 Key difference with Apache


3️⃣

Feature Python [Link] Apache


Installed Permanently (as Python module) Permanently (service installed)
Running Only while command is executed Runs continuously as a system service
Startup Manual (one command) Automatic (starts with OS)
Production use ❌ Not recommended ✅ Production-ready

✅ Summary

 “Temporary” = server runs only for the current session, not that the software
disappears.
 You can start it any time, but you must run the command each time.
 Apache, in contrast, runs always in the background until you stop it.

1️⃣ Multiple Python HTTP servers on the same machine


 Each instance of python3 -m [Link] runs independently in its own terminal.
 Important: Each server must use a different port, because only one process can bind
to a specific port at a time.
Example:
 # Terminal 1
 python3 -m [Link] 8080

 # Terminal 2
 python3 -m [Link] 8081

 # Terminal 3
 python3 -m [Link] 5000
 Each terminal now serves the folder you ran it in, accessible at
[Link]

2️⃣ Notes / limitations

 Ports must differ — otherwise the second server will fail with “address already in use.”
 Each server serves the current folder only — different terminals can serve different
directories.
 Performance is not optimized for production, so multiple heavy servers may slow down
the system.

3️⃣Summary

Feature Python [Link] Notes


Multiple servers ✅ Yes Must use different ports
Running simultaneously ✅ Yes Each in its own terminal
Directory served Current folder Can be different per server
Production-ready ❌ No Only for testing / local use

💡 Key point:
You can run multiple Python web servers on the same machine at the same time, just make
sure each uses a unique port and serves the folder you want.

1️⃣ Same folder, multiple servers

 You can open two or more terminals, cd into the same folder, and run:
# Terminal 1
python3 -m [Link] 8080

# Terminal 2
python3 -m [Link] 8081
 Both servers serve the exact same files, but on different ports.

2️⃣ How it works

 Each Python HTTP server instance is independent:


o Reads the folder contents at startup.

o Handles requests on its own port.


 Changes to files in the folder are reflected immediately for both servers.

3️⃣ Notes / limitations

 Useful for testing multiple clients or ports.


 No isolation — both servers see the same folder and files.
 Performance depends on the number of clients and server load — Python’s server is not
designed for heavy traffic.

4️⃣Summary

Scenario Result
Same folder, different ports ✅ Works fine
Different folders, different ports ✅ Works fine
Same port, different terminals ❌ Error (port already in use)
Heavy production traffic ❌ Not recommended

💡 Key point:
You can absolutely run multiple servers on the same folder simultaneously — just use
different ports. Each server will serve the same files independently.
Step by step for Windows Server 2025 WSL, showing how to make a Python
HTTP server accessible from another machine, and explain the differences
between WSL 1 and WSL 2.

1️⃣ Prepare the folder and server

1. Open your WSL terminal (Ubuntu).


2. Create a folder for your web files:

mkdir ~/webtest
cd ~/webtest

3. Add a test file:

echo "Hello from Linux server" > [Link]

4. Start Python server:

python3 -m [Link] 8080

 At this point, the server is running locally in WSL, accessible in WSL via:

[Link]

2️⃣ Access from Windows host

WSL 1 — Step by Step

1️⃣ Go to your Linux folder


cd ~/webtest

2️⃣ Start Python server bound to all interfaces

 This ensures the server listens on all network interfaces, not just localhost:

python3 -m [Link] 8080 --bind [Link]

 --bind [Link] → “listen on all interfaces”


 Now your server is running inside WSL 1.
3️⃣ Find your Windows IP address

 Open PowerShell or Command Prompt:

ipconfig

 Look for your active network adapter, e.g., Ethernet or Wi-Fi.


 Note the IPv4 Address, e.g., [Link].

4️⃣ Allow Windows Firewall for the port

 PowerShell as admin:

New-NetFirewallRule -DisplayName "Python HTTP 8080" -Direction


Inbound -Protocol TCP -LocalPort 8080 -Action Allow

 This opens port 8080 for external devices on the network.

5️⃣ Access the server from another device

 On another computer on the same network, open a browser:

[Link]

 You should see the list of files from ~/webtest, or [Link] if it exists.

✅ Notes / Tips

 No extra port forwarding needed — WSL 1 shares Windows IP.


 Binding [Link] is essential — otherwise only localhost is accessible.

 You can run multiple servers on different ports:

python3 -m [Link] 8081 --bind [Link]

 Each terminal can serve the same folder or different folders.


WSL 2 — Step by Step

1️⃣ Go to your Linux folder


cd ~/webtest

2️⃣ Start Python server bound to all interfaces


python3 -m [Link] 8080 --bind [Link]

 --bind [Link] ensures the server listens on all interfaces inside WSL 2.

3️⃣ Find the WSL 2 IP address

 In WSL terminal, run:

ip addr show eth0

 Look for the line starting with inet:

inet [Link]/20

 The IP [Link] is your WSL 2 instance’s IP.


 Note: This IP changes every time WSL restarts.

4️⃣ Test locally in Windows host

 Open a browser in Windows and go to:

[Link]

 You should see your web files.


 If it doesn’t work, check Windows firewall (port 8080 must be allowed).

5️⃣ Make WSL 2 server accessible externally


 WSL 2 uses NAT, so external devices cannot reach WSL IP directly.
 You need to forward the port from Windows host to WSL 2.

1. Open PowerShell as admin.

2. Run:

# Replace <Windows_IP> with your host IP, <WSL_IP> with your WSL 2
IP
netsh interface portproxy add v4tov4 listenport=8080
listenaddress=<Windows_IP> connectport=8080 connectaddress=<WSL_IP>

Example:

netsh interface portproxy add v4tov4 listenport=8080


listenaddress=[Link] connectport=8080
connectaddress=[Link]

6️⃣ Allow firewall traffic


New-NetFirewallRule -DisplayName "Python HTTP 8080" -Direction
Inbound -Protocol TCP -LocalPort 8080 -Action Allow

7️⃣ Access from external client

 On another machine in the same network, go to:

[Link]

 Your Python HTTP server running in WSL 2 is now accessible.

✅ Notes / Tips

Step WSL 1 WSL 2

External Shares Windows IP, Separate IP, requires port


access direct forwarding

Bind
--bind [Link] --bind [Link]
command

Firewall Allow port Allow port

Restart No IP change WSL 2 IP changes → update


Step WSL 1 WSL 2

portproxy

💡 Key point:

 WSL 2 requires port forwarding because it runs in a virtualized network.


 Use [Link] binding to allow WSL to listen on all interfaces.

 Remember to update portproxy if WSL 2 restarts and the IP changes.

Differences summary: WSL 1 vs WSL 2


Feature WSL 1 WSL 2

IP address Shared with Windows Virtualized, separate

Access from other devices Direct, just allow firewall Requires port forwarding or NAT setup

GUI support ❌ Not supported ✅ Supported via WSLg

Recommended for external HTTP server ✅ Easier ⚠ Extra configuration needed

5️⃣ Notes / Best Practices

 For quick tests, WSL 1 is simpler for external access.


 For production-like testing, WSL 2 gives full Linux kernel compatibility.

 Always ensure Windows firewall allows inbound traffic on your chosen port.

 Using [Link] as the bind address ensures the server listens on all interfaces:

 python3 -m [Link] 8080 --bind [Link]

💡 Key point:
 WSL 1: External access is almost immediate, uses Windows IP.
 WSL 2: External access requires port forwarding due to NAT.

 --bind [Link] is essential to allow connections from outside the WSL instance.

You might also like