How to embed a web server in your Python3 app

0 min read 88 words

This is not the first time that I created a Python3 application that spat out some output and required access to it via an HTTP server.

While there are numerous ways to achieve this, a really simple way is to embed an HTTP server directly in your Python3 application, and have it serve your output directly when called.

A sample web server in Python

from http.server import HTTPServer, BaseHTTPRequestHandler


class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        # Add additional output here
        self.wfile.write(b'Hello, world!')


httpd = HTTPServer(('', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags