python-flask interface development framework for web service instance

A, flask

flask is a lightweight framework written in python, you can use it to implement a website or web service. In this paper, to develop an interface with a flask.

II: mounting frame

flask need to install another reference. pip install flask

Successful installation see screenshot

 

 

 Three: flask Development Interface Processes

Development interface with flask process:

#__name__ represents the current python file. The current python file as a service to start

1, a server is defined

server=flask.Flask(__name__) 

2, then the difference between the definition of the interface functions, interface functions and general function that is defined as above special function interface to add:

# The first parameter is the path, the second argument to support the request mode, the default is to not write the words get;

# / Index / url interface is

@server.route('/index',methods=['get','post']) 

3, let the program run

app.run (host = '0.0.0.0', port = 50009) ah, wherein the host may be used provided it 0.0.0.0 public network access ip, port is the port number, port 5000 is not filled, then the default

IV: Examples of interfaces get request

the Flask Import
Import json
# First app = Flask (__ name__) which is a part of the initialization process; __ name__ represents the current python file. The current python file as a service to start
Server = flask.Flask (name__ __)
@ server.route ( "/ the Test /")
# This is a case get access to the machine http://0.0.0.0:7777/index response information. " msg ":" this is a response message "," msg_code ":" 0000 "
 @ server.route ( '/ index', Methods = [ 'GET', 'POST'])
 DEF index ():
     RES = {" MSG " : "this is a response message", "msg_code": "0000"}
     return json.dumps (RES, ensure_ascii = False)
 server.run (Port = 7777, Debug = True, Host = '0.0.0.0')
 
V: Examples of post request Interface
# This is an example post request, defined interface has only two parameters 'username', 'password', there is no business check, neither argument is null then the service returns an interface call is successful, the service returns parameter is null interface calls fail
@ server.route ( '/ index', methods = [ 'POST'])
DEF index ():
    # so if you want to accept incoming parameters, the following methods
    username = flask.request.values.get ( 'username')
    = flask.request.values.get password ( 'password')
    IF username and password:
        RES = { "MSG": "interface call success", "msg_code": "0000"}
        return json.dumps (RES, ensure_ascii = False )
    the else:
        RES = { "MSG": "Interface call failed, as required empty", "msg_code": "9999"}
        return json.dumps (RES, ensure_ascii = False)
server.run (Port = 7777, Debug = True, host = '0.0.0.0' )
 
The figure below is a test jmeter local service

 

 

 

 

to sum up:

Thus a simple python web service instance has been completed.

Guess you like

Origin www.cnblogs.com/qiaoli0726/p/12206520.html