小編給大家分享一下運(yùn)維系列之FastAPI接口服務(wù)怎么用,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元上街做網(wǎng)站,已為上家服務(wù),為上街各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792
第一部分:FastAPI接口部分
from fastapi import FastAPI, Path
from com.fy.fastapi.monitor.shell.fabric.FabricLinux import FabricLinux
app = FastAPI()
#執(zhí)行shell命令;
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/cmd/{cmd}")
def runCommand(cmd, host, userName, password):
fabric = FabricLinux(host , userName , password)
result = fabric.runCommand(cmd)
fabric.closeClient()
return {"res": result }
#上傳文件;
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/upload/{srcFile}")
def upload(srcFile, targetDir, host, userName, password):
fabric = FabricLinux(host , userName , password)
result = fabric.upload(srcFile, targetDir)
fabric.closeClient()
return {"res": result }
#下載文件;
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/download/{srcFile}")
def download(srcFile, targetDir, host, userName, password):
#fabricu.download("/home/Crawler/WeChatSouGouMain.tar.gz", "D:\\txt\\WeChatSouGouMain.tar.gz")
fabric = FabricLinux(host , userName , password)
result = fabric.download(srcFile, targetDir)
fabric.closeClient()
return {"res": result }
#刪除文件
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/delete/{targetPath}")
def delete(targetPath:"必須是全路徑", host, userName, password):
fabric = FabricLinux(host , userName , password)
result = fabric.runCommand("rm -rf " + targetPath)
fabric.closeClient()
return {"res": result }
#解壓.tar.gz文件
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/delete/{targetPath}")
def decomTarGz(srcPath, tarPath, host, userName, password):
fabric = FabricLinux(host , userName , password)
result = fabric.decomTarGz(srcPath, tarPath)
fabric.closeClient()
return {"res": result }
#根據(jù)PID關(guān)閉相關(guān)的服務(wù);
#調(diào)用示例:http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95
@app.get("/kill/{pid}")
def stop(pid: int=Path(..., gt=0, le=32767), host, userName, password):
''' gt: 大于; le: 小于等于 '''
fabric = FabricLinux(host , userName , password)
result = fabric.runCommand("kill -9 " + pid)
fabric.closeClient()
return {"res": result }
#根據(jù)Python命令符,以及啟動(dòng)文件路徑,啟動(dòng)相應(yīng)的服務(wù);
#調(diào)用示例:http://127.0.0.1:8000/start/python?startFile=%E6%B5%8B%E8%AF%95
@app.get("/start/{pycmd}")
def start(pycmd , startFile , host, userName, password):
fabric = FabricLinux(host , userName , password)
result = fabric.runCommand("nohup " + pycmd + " " + startFile + " &")
fabric.closeClient()
return {"res": result }
第二部分:fabric接口部分,主要用來執(zhí)行命令行
from fabric import Connection
import traceback, os
class FabricLinux:
看完了這篇文章,相信你對(duì)“運(yùn)維系列之FastAPI接口服務(wù)怎么用”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!