CODE WITH MARTIN

Flask Web Service


Introduction

This article will show you very quickly how to use Python as a web server using the 3rd party Flask framework.

Flask is a very easy to use web application framework for writing web servers with Python. It's much easier and offers more features than the native HTTPServer module in Python. It's highly recommended if you plan on writing back end web services or hosting web sites with Python.

You can see how much easier this is compared with the article 'HTTP Web Service'.

The Code

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello from flask!'

app.run(host='localhost', port=81)

When you run, you will see in the terminal the server running message. While the server is running, open your browser and browse to "http://localhost:81".

You should see a response in your browser that came from your server on line 7.

Next Steps

Explore the Flask framework over at https://flask.palletsprojects.com/en/2.2.x/. I suggest going through the "Quickstart" sections to see what Flask can offer you.