from flask import Flask, jsonify
from flask_cors import CORS
import requests
import json
import pymysql
from flask import request
from urllib.parse import urlparse
from urllib import parse
app = Flask(__name__)
CORS(app)
list=[]
# def page(page,size):
# for i in range(page) :
# headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36'}
# rep=requests.get('https://club.jd.com/comment/productPageComments.action?callback=fn&productId=100014352499&score=0&sortType=5&page='+str(i)+'&pageSize='+str(size)+'&isShadowSku=0&fold=1',headers=headers)
# r=rep.text.replace('fn(','').replace(');','')
# # print(str(r))
# data = json.loads(r)
# a = data['comments']
# if a:list.extend(a)
# page(1,10)
@app.route('/hello')
def hello():
return jsonify(data)
@app.route('/login',methods=['POST'])
def login():
postdata = request.get_json()
print(postdata)
username=postdata.get('username')
password=postdata.get('password')
if len(username)==0 | len(password)==0:
return {'data':None,'message':'请输入账号密码','code':0}
else:
sql = "select * from user where username='%s' and password='%s'"%(username,password)
print(sql)
data = getDB(sql)
if len(data)==1:
return {'data':None,'message':'登录成功','code':200}
else:
return {'data':None,'message':'账号或密码错误','code':0}
def getDB(sql):
db = pymysql.connect(host='localhost',port=3306,user='root',password='123456',database='app',charset='utf8')
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
cursor.execute(sql)
data=cursor.fetchall()
cursor.close()
db.close()
return data
@app.route('/robot',methods=['POST'])
def robot():
data = request.get_json()
content = data.get('content')
print(content)
rep = requests.get('http://api.qingyunke.com/api.php?key=free&appid=0&msg='+content)
data = json.loads(rep.text)
return {'data':data['content'],'message':'成功','code':200}
# @app.route('/robot',methods=['GET'])
# def robot():
# url = urlparse(request.url)
# content = parse.parse_qs(url.query).get('content')[0]
# print(content)
# rep = requests.get('http://api.qingyunke.com/api.php?key=free&appid=0&msg='+content)
# data = json.loads(rep.text)
# return {'data':data['content'],'message':'成功','code':200}
if __name__ == '__main__':
app.run('localhost',8000)
如何用python写接口
最新推荐文章于 2024-07-15 19:26:33 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)