How to Set Up a Server in Python?

Estimated read time 2 min read

To set up a server in Python, you can use the built-in http.server module. Here are the steps to follow:

  1. Open your command prompt or terminal.
  2. Navigate to the directory where you want to serve files from.
  3. Create a new Python file with the following code:
import http.server
import socketserver

PORT = 8000

Handler = http.server.SimpleHTTPRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
  1. Save the file with a .py extension (e.g.
  2. Run the server by typing python in the command prompt or terminal.
  3. Open your web browser and navigate to http://localhost:8000 (replace 8000 with the port number you specified in the code). You should see a list of files in the directory.

You can now serve files from this directory by simply placing them in the directory and accessing them through the browser. You can also modify the Handler variable to use a custom request handler class if you need to customize the behavior of the server.

Note that this is a very basic example of setting up a server in Python. If you need more advanced functionality, such as handling specific types of requests or integrating with other APIs, you may need to use a more advanced web framework such as Flask or Django.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply