Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write a web service using flask in python

Shulou Source: shulou.com Published: 2022-06-02 05:54:04 09月24日 Update

This article will explain in detail how to use flask to write a web service in python. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Flask is a lightweight Web application framework written in Python. Its WSGI toolbox uses Werkzeug, and the template engine uses Jinja2. Flask uses BSD authorization.

Flask is also called "microframework" because it uses a simple core and adds other functions with extension. Flask does not have database and form validation tools that are used by default.

The above is from Baidu Encyclopedia.

Flask's official website: http://flask.pocoo.org/

Please search for the package that installs flask in python

Here is a simple web service code sample for flask:

#-*-coding: UTF-8-*-

# introduce the required development package

Import configparser

From flask import Flask

From flask import request

From flask import Response

Import os

Import json

# initialize a flask object

App = Flask (_ _ name__)

# define the route and implementation method of a service, and return a hello world string

@ app.route ('/ route1/route2')

Def simplesample ():

Return 'hello world'

# the above services can be accessed by entering the http:// host address: Port number / route1/route2 in the browser

# the following route is to create a route with a variable parameter in the path, with route2 as the parameter, which can be passed in on the client.

@ app.route ('/ route1/')

Def simplesample1 (route2):

Return route2

# say hello to the route with parameters after it. When defining it, you don't need to write the parameters in the route, just get it in the code

# http://localhost:2018/route1/route3?param1=param1

@ app.route ('/ route1/route3')

Def simplesample2 ():

Param1 = request.args.get ('param1',')

# corresponding actions

Return param1# executes the operation of app and sets the server address and port number that the service listens to.

If _ _ name__ = ='_ _ main__':

App.run (host='localhost', port='2018')

On how to use flask in python to write a web service is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Services routing parameters code content slogans addresses tools articles more knowledge articles yes hosts functions just available in characters strings official websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Xiaomi OPPO Reno Redmi