Hello, I'm Arthur. When I started learning Python, I used to think that building a website required a lot of complicated tools and frameworks.
But one of the easiest ways to understand how web servers work is to build a small one yourself.
In this tutorial, we'll create a simple Python web server, understand how it handles requests, and learn how to make it accessible from another computer.
1. Create a Simple Python Web Server
Python includes a built-in HTTP server that you can use to serve files from a directory.
First, create a folder for your project.
Inside that folder, create a file named index.html:
<!DOCTYPE html>
<html>
<head>
<title>My First Server</title>
</head>
<body>
<h1>Hello from my Python server!</h1>
<p>This page is being served using Python.</p>
</body>
</html>
Now open your terminal in the same folder and run:
python -m http.server 8000
Your server will start listening on port 8000.
Open your browser and visit:
http://localhost:8000
You should see the HTML page you created.
This is a simple way to understand how a web server delivers files to a browser.
2. Understand What Happens When You Open a Website
When you open a website, your browser sends an HTTP request to a server.
The server receives that request and sends back a response.
The response may contain HTML, JSON, images, or other data.
In our example, Python receives the browser's request and returns the files from the current directory.
This is the basic idea behind many web applications, even when they use more advanced frameworks.
3. Make Your Server Accessible From Another Computer
By default, Python's HTTP server is mainly intended for local testing.
If you want to access it from another computer on the same network, you can bind it to all available network interfaces:
python -m http.server 8000 --bind 0.0.0.0
Then use your computer's local IP address from another device:
http://YOUR_LOCAL_IP:8000
Make sure both devices are connected to the same network and that your firewall allows the connection.
Do not expose this basic server directly to the public internet. It is intended for simple testing, not production hosting.
4. When Should You Use a VPS?
A local server is useful for learning and testing, but it stops being accessible when your computer shuts down or loses its network connection.
If you want to run a web application continuously, a virtual private server can be a practical option.
A VPS gives you a remote environment where you can install Python, configure your application, and keep it running independently of your personal computer.
For example, Seimaxim's VPS hosting services are worth exploring when comparing server options for Python projects.
Before choosing a VPS, consider the amount of RAM, CPU resources, storage, and network requirements your application needs.
5. Keep Your Python Project Organized
As your application grows, keeping files organized becomes important.
A simple project structure could look like this:
my-python-project/
├── app.py
├── requirements.txt
├── index.html
└── README.md
The app.py file contains your Python code, while requirements.txt lists the libraries your project needs.
You can create a requirements file using:
pip freeze > requirements.txt
This makes it easier to recreate your development environment on another computer or server.
Final Thoughts
Building a simple Python web server is a useful exercise for anyone learning web development.
It helps you understand HTTP requests, browser communication, and the role of a server in delivering a website.
Once you are comfortable with the basics, you can move on to frameworks such as Flask or Django and learn how to deploy a real application.
If you are planning to host a Python project online, you can also explore Seimaxim to compare VPS hosting options for your next project.
Top comments (0)