使用Python实现一个简单的接口服务

使用Python实现一个简单的接口服务,可以通过get、post方法请求该接口,拿到响应数据。创建一个dnschange_api.py文件,添加代码如下:

import flask, json
from flask import request
import dealdns

'''
flask: web框架,通过flask提供的装饰器@server.route()将普通函数转换为服务
登录接口,需要传url、username、passwd
'''
# 创建一个服务,把当前这个python文件当做一个服务
server = flask.Flask(__name__)
# server.config['JSON_AS_ASCII'] = False
# @server.route()可以将普通函数转变为服务 登录接口的路径、请求方式
@server.route('/operate',methods=['get', 'post'])
def operate():
    domaintype = request.values.get('domaintype')
    if domaintype == "static":
        domainname = request.values.get('domianname')
        action = request.values.get('action')
        if action == "add":
            toip=request.values.get('toip')
            status_code, rescode = dealdns.online_domain_add_ip(domainname,toip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '增加成功'}
                return json.dumps(resu, ensure_ascii=False)    # 将字典转换为json串, json是字符串
            else:
                resu = {'code': -2, 'message': '增加失败'}
                return json.dumps(resu, ensure_ascii=False)

        elif action == "change":
            fromip=request.values.get('fromip')
            toip=request.values.get('toip')
            status_code, rescode = dealdns.online_domain_change_ip(domainname,fromip,toip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '修改成功'}
                return json.dumps(resu, ensure_ascii=False)
            else:
                resu = {'code': -3, 'message': '修改失败'}
                return json.dumps(resu, ensure_ascii=False)

        elif action == "delete":
            fromip=request.values.get('fromip')
            status_code, rescode = dealdns.online_domain_delete_ip(domainname,fromip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '删除成功'}
                return json.dumps(resu, ensure_ascii=False)
            else:
                resu = {'code': -4, 'message': '删除失败'}
                return json.dumps(resu, ensure_ascii=False)
        else:
            res = {'code': -1, 'message': '输入action有误'}
            return json.dumps(resu, ensure_ascii=False)

    elif domaintype == "dynamic" :
        domainname = request.values.get('domianname')
        dtdomainname = request.values.get('dtdomainname')
        action = request.values.get('action')
        if action == "add":
            toip=request.values.get('toip')
            status_code, rescode = dealdns.online_dtdomain_add_ip(domainname,dtdomainname,toip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '增加成功'}
                return json.dumps(resu, ensure_ascii=False)
            else:
                resu = {'code': -5, 'message': '增加失败'}
                return json.dumps(resu, ensure_ascii=False)

        elif action == "change":
            fromip=request.values.get('fromip')
            toip=request.values.get('toip')
            status_code, rescode = dealdns.online_dtdomain_change_ip(domainname,dtdomainname,fromip,toip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '修改成功'}
                return json.dumps(resu, ensure_ascii=False)
            else:
                resu = {'code': -6, 'message': '修改失败'}
                return json.dumps(resu, ensure_ascii=False)

        elif action == "delete":
            fromip=request.values.get('fromip')
            status_code, rescode = dealdns.online_dtdomain_delete_ip(domainname,dtdomainname,fromip)
            print(status_code, rescode)
            if str(status_code) == "200" and str(rescode) == "0":
                resu = {'code': 0, 'message': '删除成功'}
                return json.dumps(resu, ensure_ascii=False)
            else:
                resu = {'code': -7, 'message': '删除失败'}
                return json.dumps(resu, ensure_ascii=False)
        else:
            resu = {'code': -8, 'message': '输入action有误'}
            return json.dumps(resu, ensure_ascii=False)

    else:
        resu = {'code': -9, 'message': '输入domaintype有误'}
        return json.dumps(resu, ensure_ascii=False)


if __name__ == '__main__':
    server.run(debug=True, port=8888, host='0.0.0.0')# 指定端口、host,0.0.0.0代表不管几个网卡,任何ip都可以访问

推荐阅读更多精彩内容