fastapi接口(在服务器上生效)
本文最后更新于 171 天前,其中的信息可能已经有所发展或是发生改变。

第一步:在本地编写代码

import pymysql
import json,MySQLdb
​
import uvicorn
from fastapi import FastAPI
app = FastAPI()
@app.get("/data/{item_id}")
async def root(item_id: int):
    if item_id == 1:
        page_start = 0
        josndata = TableToJson(page_start)
​
​
    else:
        page_start = (item_id-1)*100 - 1
        josndata = TableToJson(page_start)
    return josndata
​
​
​
def TableToJson(num1):
        try:
            # 本地数据库
            # 服务器名,账户,密码,数据库名称
            connect = pymysql.connect(host='你的服务器ip',
                                  user='MySQL用户名',
                                  password='密码',
                                  db='你要链接的数据库',
                                  charset='utf8')
            cur = connect.cursor()
​
            create_sqli = "select * from user_log limit {},500;".format(num1)
            print(create_sqli)
            print("已经在收搜索了")
            cur.execute(create_sqli)
            data = cur.fetchall()
            # 关闭游标
            cur.close()
            # 关闭数据库连接
            connect.close()
            jsonData = []
            # 循环读取元组数据
            # 将元组数据转换为列表类型,每个条数据元素为字典类型:[{'字段1':'字段1的值','字段2':'字段2的值',...,'字段N:字段N的值'},{第二条数据},...,{第N条数据}]
            for row in data:
                result = {}
                result['user_id'] = row[0]
                result['item_id'] = row[1]
                result['cat_id'] = row[2]
                result['merchant_id'] = str(row[3])
                result['brand_id'] = str(row[4])
                result['month'] = str(row[5])
                result['day'] = row[6]
                result['action'] = row[7]
                result['age_range'] = row[8]
                result['gender'] = str(row[9])
                result['province'] = str(row[10])
                jsonData.append(result)
                jsondatar = json.dumps(jsonData, ensure_ascii=False)
            return jsondatar[1:len(jsondatar) - 1]
        except Exception as e:
            print("连接失败:", e)
            return None
if __name__ == '__main__':
    uvicorn.run("你的项目名称:app", host="127.0.0.1", port=8000
                , log_level="info", reload=True)

第二步:在你的服务器上运行

http://t.zoukankan.com/sea-stream-p-14210645.html

让你设置的端口生效,在你的本地可以访问到,就需要将防火墙端口生效

# 防火墙端口生效
firewall-cmd --zone=public --add-port=你设置的端口/tcp --permanent
# 使其生效
service firewalld restart
image-20221112002853614

在你的虚拟主机上执行下面代码:

# 安装需要的包uvicorn
pip install uvicorn
# 安装需要的包FastApi
pip install FastApi
# 安装需要的包pymysql
pip install pymysql
image-20221112001325218

之后直接执行

image-20221112001855999

完成上述操作后,所有准备工作准备待续,执行下面代码即可

# 在linux下启动python文件
python3 FirstApi.py
image-20221112002427480

有的需要等待一段时间,有的可以直接就访问,在你本地的浏览器访问

http://:9000/1

即可显示

image-20221112003503207

第三步:使用dataease连接api

进入数据源,添加api数据源

image-20221112004034282

进入后

image-20221112004314833

进行api接口创建

image-20221112004617538

api连接成功后

image-20221112004841796
image-20221112005115467

成功后,进入数据集进行连接查看

image-20221112005454009
image-20221112005620646
image-20221112005707219

成功创建api接口数据连接。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇