The Python 3 Equivalent of SimpleHTTPServer

Introduction

In this article, we'll explore Python's built-in HTTP servers. We will discuss the SimpleHTTPServer module, its Python 3 equivalent, and how to run these servers via the command line. This knowledge is crucial for developers who need to quickly set up a server for testing or sharing files.

What is the SimpleHTTPServer?

The SimpleHTTPServer module is a Python 2.x built-in module that allows you to create a simple HTTP server. This server can serve files from the directory it's run in, making it a nice tool for testing web pages or even sharing files.

# Python 2.x
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

The http.server in Python 3

With the advent of Python 3, the SimpleHTTPServer was replaced by the http.server module. The http.server module provides similar functionality to the SimpleHTTPServer but is updated to work with Python 3.

The http.server module also includes a more robust HTTP request handler than SimpleHTTPServer, offering more control over HTTP responses.

# Python 3.x
$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 ...

Running the Server via Command Line

Running the server via the command line is straightforward. In Python 2.x, you would use the SimpleHTTPServer module like so:

$ python -m SimpleHTTPServer

In Python 3.x, you would use the http.server module instead:

$ python3 -m http.server

Both commands will start a server on port 8000, serving files from the current directory. If you want to specify a different port, you can do so by adding the port number at the end of the command:

$ python3 -m http.server 8080

Running a Basic HTTP Server in Python 3

With Python 3, running a basic HTTP Server is as simple as using the http.server module. This module is a straightforward and efficient way of serving up files and directories on your machine. Here's how you can do it:

$ python3 -m http.server

Once you run this command, you should see something like this:

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

This means your HTTP server is up and running. You can then access it by going to http://localhost:8000 in your web browser. By default, the server is set to port 8000, but you can specify a different port by appending it to the command:

$ python3 -m http.server 8080

This command will start the server on port 8080.

CGI Handling in Python 3

In earlier versions of Python, the CGIHTTPServer module was commonly used to handle CGI (Common Gateway Interface) scripts. Starting a CGI server was as simple as running the command:

$ python -m CGIHTTPServer

However, starting with Python 3.3, the CGIHTTPServer module was removed, and its functionality was incorporated into the http.server module. This was done in order to simplify the HTTP server capabilities in the standard library.

The modern equivalent for starting a CGI server in Python 3.3 and later versions is:

$ python3 -m http.server --cgi

By using the --cgi option with the http.server module, you can enable the same CGI handling functionality that was available with CGIHTTPServer. This should make migrating over to Python 3 much easier.

Differences Between SimpleHTTPServer and http.server

While SimpleHTTPServer and http.server essentially perform the same function, there are a few key differences between them. The most significant difference is that SimpleHTTPServer is only available in Python 2, while http.server is available in Python 3.

Another notable difference is that http.server is more secure than SimpleHTTPServer. The http.server module does not execute or interpret any code, making it safer to use. On the other hand, SimpleHTTPServer can potentially execute arbitrary Python code present in the web directory. Because of this, I'd highly recommend you use http.server when possible.

Be careful! Always be cautious when serving files and directories from your personal or development computer, especially when doing so over a network. Never serve sensitive information or allow server access to untrusted individuals.

Conclusion

Python 3's http.server module is a simple and effective tool for running an HTTP server. It's a more secure and updated version of Python 2's SimpleHTTPServer, and it's just as easy to use. Whether you're testing a website, sharing files over a network, or just playing around, http.server is a great tool to have at your disposal.

Last Updated: August 24th, 2023
Was this helpful?

Ā© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms