Python SimpleHTTPServer Tutorial With Example | HTTP request Handler
Python SimpleHTTPServer Tutorial With Example | HTTP request Handler is today’s topic. Python SimpleHTTPServer module is a convenient tool. You can use Python SimpleHTTPServer to turn any directory into a simple HTTP web server. The SimpleHTTPServer module that comes with Python is a simple HTTP server that provides standard GET and HEAD request handlers. An advantage with the built-in HTTP server is that you don’t have to install and configure anything. The only thing that you need is to have Python installed.
Python SimpleHTTPServer Tutorial
Python SimpleHTTPServer supports only two HTTP methods – GET and HEAD. So it’s an excellent tool to share files over the network. Python SimpleHTTPServer has been migrated to python http.server module in Python 3, we will learn about both of these modules today and see how easy it is to work with them. If you want to start a server on default port 8000, then type the following command.
python -m SimpleHTTPServer
You can change the default port by passing the port in the argument in the above command.
python -m SimpleHTTPServer [port]
We have started the Python SimpleHTTPServer on port 8000.
Now, if you will go to the browser, then it will show all the files and folders list in the output of a webpage.
No module named SimpleHTTPServer
If you are using Python3 and if you try to start the SimpleHTTPServer then you will get the error like No module named SimpleHTTPServer. It is because it is merged with http.server module. You can use below command to run python http server in Python 3.
python3 -m http.server 9000
Now, create a simple index.html file inside that server directory where you have started the server and write the following code inside the index.html file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h3>Python SimpleHTTPServer</h3> <p style="margin-left: 30px; color: rebeccapurple">Python 3 Web Server</p> </body> </html>
Now, go to the browser and type this URL: http://localhost:9000 and see the output.
Python 3 http server example
Python 3 http server module defines classes for implementing HTTP servers or Web servers. The http.server is not recommended for production. It only implements necessary security checks.
We can write the server code to create a web server.
Write the following code inside the app.py file.
# app.py import http.server import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()
Okay, now run the file. So go to the terminal and type the following command.
If you have an index.html file in your directory, then the server will respond with that file.
You can find more about Python 3 http.server on this link.
Finally, Python SimpleHTTPServer Tutorial With Example | HTTP request Handler in Python is over.