WeChat applet quick interface writing

 

Interface, what is an interface? This is recorded on Baidu Encyclopedia:

Interface generally refers to an abstraction (which can be another entity) that an entity provides itself to the outside world to separate external communication methods from internal operations so that it can be modified internally without affecting the way other external entities interact with it.

The applet displays data, and the data changes from time to time. It needs to call the interface to ensure that the data is what the program wants. Then let's take a look at the preparation of a small program interface today.

 

Our goal

Our goal is to write an interface for the front end of the applet to call. This time we mainly use two interface call methods, get and post.

Then we can use these two ways to write the interface at will. Don't say anything, just look at the code!

 

Before we start, we need to install a few modules,

flask

flask_restful

 

​It will be more convenient for us to write.

Get interface writing method

 

# coding=utf-8
import sys
import importlib
importlib.reload(sys)
from flask import *
import flask_restful

app = Flask(__name__)
api = flask_restful.Api(app)


class HelloWorld(flask_restful.Resource):
    def get(slef):
        x=request.args['x']#获取参数中的值
          y=request.args['y']
        return {'hello':y,'donghu':x}#接口返回值

api.add_resource(HelloWorld, '/login',methods=['GET'])#页面路径


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80)#请求地址,以及端口

 

Then a get interface is written, run, and enter in the browser

http://127.0.0.1/login

If the value can be returned normally, ​that means there is no problem.

Post interface writing method

 

# coding=utf-8
import sys
import importlib
importlib.reload(sys)
from flask import *
import flask_restful

app = Flask(__name__)
api = flask_restful.Api(app)

class HelloWorld(flask_restful.Resource):
    def post(self):
        x = request.form['x']#获取参数
        y=request.form['y']
        return {'hello':y,'donghu':x}

api.add_resource(HelloWorld, '/login2',methods=['POST'])

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=80)

The post interface and the get interface are written in the same way, except that the way of receiving parameters is slightly adjusted.

Run, and then type in the browser, http://127.0.0.1/login2​, to see if it can be accessed normally.

Very good, after completing these, you can be a teacher, and you can already write interfaces.

 

O ^ ~ ^ O

 

Follow us to learn more!

 

How to automatically send text messages to girlfriend

Build your own voice chat robot

These pictures are too beautiful, want a good slow a download, how to break online, urgent! ! ! !

 

 

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/89606333