How Do I Run a Web Server With Python?

//

Heather Bennett

Running a web server with Python is a fundamental skill for any web developer. Whether you’re building a small personal website or a large-scale web application, knowing how to set up and run a web server can greatly enhance your development process. In this tutorial, I will guide you through the steps of running a web server using Python.

Prerequisites

Before we dive into the steps, make sure you have the following:

  • Python installed: You can download and install Python from the official website at python.org/downloads.
  • A text editor: Choose any text editor or integrated development environment (IDE) that you’re comfortable with.

Step 1: Create a Simple Python Web Server

To get started, open your favorite text editor and create a new file called server.py. In this file, we’ll write our Python code to set up the web server.

Note: Make sure to save the file with a .py extension to ensure it is recognized as a Python script.

In your server.py file, enter the following code:


import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("Server started at localhost:" + str(PORT))
    httpd.serve_forever()

This code imports the required modules and sets up a basic HTTP request handler. It then creates an instance of the TCPServer class from the socketserver module and binds it to port 8000. Finally, it starts the server and listens for incoming connections.

Step 2: Run the Web Server

Now that we have our Python web server code ready, let’s run it. Open a terminal or command prompt, navigate to the directory where you saved server.py, and enter the following command:


python server.py

You should see a message indicating that the server has started at localhost:8000.

Note:

If port 8000 is already in use on your machine, you can choose a different port number by modifying the PORT variable in server.

Step 3: Access Your Web Server in a Browser

To verify that your web server is running correctly, open a web browser and enter the following URL:


http://localhost:8000

You should see a directory listing of files and folders in the directory where server.py is located. This means your web server is up and running!

Congratulations!

You have successfully set up and run a basic web server using Python. This simple web server can serve static files such as HTML, CSS, JavaScript, images, etc. You can now start building dynamic websites or test your web applications locally.

Troubleshooting Tips:

  • If you see an error “python is not recognized as an internal or external command”: Make sure Python is added to your system’s PATH environment variable. You can find instructions on how to do this on the Python website.
  • If you encounter a permission error: Try running the command as an administrator or use a different port number.
  • If you make changes to the server code: Remember to stop the server by pressing Ctrl+C in the terminal and then re-run it with the updated code.

That’s it! You’re now ready to run a web server with Python. Happy coding!