Python, create a Web (HTTP) server

Python makes it super easy to create an HTTP server, via the http module of the standard library.

In particular, the http.server object is the thing we’re going to use.

First, I want to mention one quick way to run an HTTP server from any folder, without writing any code:

python -m http.server --cgi 8000

This will run an HTTP server on port 8000, serving the files in the current folder. Of course it’s not an HTTP server fully featured like Nginx or Apache, but that’s often good enough for prototypes, or for your own test projects.

Let’s now use that module in our code to make it programmatically serve an “Hello, World!” string to anyone connecting on port 8000:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World!"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

After running this program locally you can try connecting using a Web browser to port http://localhost:8000.

This serves the same Hello, World! string to anyone visiting the site on http://localhost:8000, on GET requests to any URL, complete with a 200 response and a Content-type: text/html header.

We write to wfile, which contains the output stream for writing a response back to the client.

It works on GET requests because we implemented the handler do_GET method.

You can also implement do_HEAD(), do_POST() and any other HTTP method:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a GET response"
        self.wfile.write(bytes(message, "utf8"))
    def do_POST(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a POST response"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

Python has lots of different libraries we can use to handle applications built on top a Web server, including the very popular Flask and Django.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

Python makes it super easy to create an HTTP server, via the http module of the standard library.

In particular, the http.server object is the thing we’re going to use.

First, I want to mention one quick way to run an HTTP server from any folder, without writing any code:

python -m http.server --cgi 8000

This will run an HTTP server on port 8000, serving the files in the current folder. Of course it’s not an HTTP server fully featured like Nginx or Apache, but that’s often good enough for prototypes, or for your own test projects.

Let’s now use that module in our code to make it programmatically serve an “Hello, World!” string to anyone connecting on port 8000:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World!"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

After running this program locally you can try connecting using a Web browser to port http://localhost:8000.

This serves the same Hello, World! string to anyone visiting the site on http://localhost:8000, on GET requests to any URL, complete with a 200 response and a Content-type: text/html header.

We write to wfile, which contains the output stream for writing a response back to the client.

It works on GET requests because we implemented the handler do_GET method.

You can also implement do_HEAD(), do_POST() and any other HTTP method:

from http.server import BaseHTTPRequestHandler, HTTPServer

class handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a GET response"
        self.wfile.write(bytes(message, "utf8"))
    def do_POST(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()

        message = "Hello, World! Here is a POST response"
        self.wfile.write(bytes(message, "utf8"))

with HTTPServer(('', 8000), handler) as server:
    server.serve_forever()

Python has lots of different libraries we can use to handle applications built on top a Web server, including the very popular Flask and Django.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-02-10T05:00:00+00:00) Python, create a Web (HTTP) server. Retrieved from https://www.scien.cx/2021/02/10/python-create-a-web-http-server/

MLA
" » Python, create a Web (HTTP) server." flaviocopes.com | Sciencx - Wednesday February 10, 2021, https://www.scien.cx/2021/02/10/python-create-a-web-http-server/
HARVARD
flaviocopes.com | Sciencx Wednesday February 10, 2021 » Python, create a Web (HTTP) server., viewed ,<https://www.scien.cx/2021/02/10/python-create-a-web-http-server/>
VANCOUVER
flaviocopes.com | Sciencx - » Python, create a Web (HTTP) server. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/10/python-create-a-web-http-server/
CHICAGO
" » Python, create a Web (HTTP) server." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/02/10/python-create-a-web-http-server/
IEEE
" » Python, create a Web (HTTP) server." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/02/10/python-create-a-web-http-server/. [Accessed: ]
rf:citation
» Python, create a Web (HTTP) server | flaviocopes.com | Sciencx | https://www.scien.cx/2021/02/10/python-create-a-web-http-server/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.