pyhton interface development [flask]

Use flask interface development

 

Language: Python3

Framework: flask

 

Before proceeding to develop first have to install the flask, before you can use. Installation can be installed using the command directly pip: pip install flask.

Use flask interface process:

1, the definition of a service server:

= flask.Flask Server ( __name__ )           # create a service, the current python file as a service starts, __ name__ represents the current python file.

2, an interface function is defined:

Defined interface functions need to add decorators "@ server.route ()" flask provided in the above-defined function to convert an ordinary function to service logon interface.

server.route @ ( ' / REGIST ' , Methods = [ ' get ' , ' POST ' ])        # The first parameter represents a path request interface, the second interface requests mode parameter indicates, if not filled, then get the default mode.

3, run the service server:

server.run (Port = 8000, debug = True, Host = ' 127.0.0.1 ' )     # Port can not conflict with the local service port, host for the IP address you want to access, debug = True, indicates that the current running state of the debug, modify finished Code program runs automatically, no need to manually re-run.

 

According to the above steps to complete a small Interface:

Example 1:

Import the Flask, json 

Server = flask.Flask ( __name__ )                # create a service, the current python file as a service starts, __ name__ represents the current python file. 

server.route @ ( ' / REGIST ' , Methods = [ ' POST ' ])        # The first parameter represents a path request interface, the second interface requests mode parameter indicates, if not filled, then get the default mode. 
DEF index (): 
    RES = { ' MSG ' : ' This is the first developed an interface I ' , ' msg_code ' : ' 0000 ' }
     return json.dumps (RES, ensure_ascii =False)  # binary-8 converted to UTF
 
server.run (Port = 8000, debug = True, Host = ' 127.0.0.1 ' )  # Port can not conflict with the local service port, host for the IP address to be accessed, debug = True, represents the current state of the debug run, finished modifying the code of the program will run automatically, no need to manually re-run.

After using postman request as follows:

Example Two:

Interface receives the request data, and received data in a certain way to verify the signature after signature by obtaining user interface information request over, to find out whether this record exists in the database, no record registration procedure.

Import the Flask, json 

Server = flask.Flask ( __name__ )           # __name__ represent the current python file. The current file as a service start python 

@ server.route ( ' / REGIST ' , Methods = [ ' POST ' ])        # interface requests and address interface requests embodiment 
DEF REGIST (): 
    jsonData = flask.request.get_json ()                      # json string is acquired requested 
    username = jsonData [ ' username ' ] IF ( ' username '  in jsonData) the else  ''                       
    = jsonData password [ ' password ' ] IF ( ' password '  in jsonData) the else  '' 
    signjudge = signMD5.signjudge ()
     IF ! signjudge.jsonsignjudge (jsonData) = jsonData [ ' Sign ' ]:
         # ####### ## signature verification fails ########## 
        return json.dumps ({ ' MSG ' : ' the signature verification failed ' , ' msg_code ' : '. 3 001 ' },ensure_ascii=False)
     the else :
         Pass 
    IF username == ''  or password == '' :
         # ######### missing required information ########## 
        return json.dumps ({ ' MSG ' : ' missing required information ' , ' msg_code ' : '. 1 001 ' }, ensure_ascii = False)
     the else :
         Pass 
    interfacejudge = interfcaejudge.interfacejudge ()
     IF interfacejudge.judgefielduser (username) =! None:
        return json.dumps({'msg': '用户已存在', 'msg_code': '2001'}, ensure_ascii=False)
    else:
        interfacejudge.insertsuerinfo(username,password)
        return json.dumps({'msg': '注册成功', 'msg_code': '0000'}, ensure_ascii=False)

server.run(port=8000,debug=True,host='127.0.0.1')

 

 

 

Guess you like

Origin www.cnblogs.com/deliaries/p/11352259.html