Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the FastAPI interface service of operation and maintenance series

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the FastAPI interface service of the operation and maintenance series. I hope you will get something after reading this article. Let's discuss it together.

Part one: FastAPI interface part

From fastapi import FastAPI, Path

From com.fy.fastapi.monitor.shell.fabric.FabricLinux import FabricLinux

App = FastAPI ()

# execute shell command

# example of calling: 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}

# upload files

# example of calling: 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}

# download files

# example of calling: 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}

# Delete files

# example of calling: http://127.0.0.1:8000/items/3?q=%E6%B5%8B%E8%AF%95

@ app.get ("/ delete/ {targetPath}")

Def delete (targetPath: "must be full path", host, userName, password):

Fabric = FabricLinux (host, userName, password)

Result = fabric.runCommand ("rm-rf" + targetPath)

Fabric.closeClient ()

Return {"res": result}

# extract the .tar.gz file

# example of calling: 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}

# shut down related services according to PID

# example of calling: 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: greater than; le: less than or equal to''

Fabric = FabricLinux (host, userName, password)

Result = fabric.runCommand ("kill-9" + pid)

Fabric.closeClient ()

Return {"res": result}

# start the corresponding service according to the Python command and the startup file path

# example of calling: 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}

The second part: fabric interface part, which is mainly used to execute the command line.

From fabric import Connection

Import traceback, os

Class FabricLinux:

Def _ _ init__ (self, host: "server IP", userName: "user name", password: "password"):

Self.host = host

Self.userName = userName

Self.password = password

Print (self.userName + "@" + self.host, {"password": self.password})

Self.initClient () # initialize the link

# initialize ssh link object

Def initClient (self):

# if the server is configured with SSH password-free login, connect_kwargs is not required to specify the password.

Self.con = Connection (self.userName + "@" + self.host, connect_kwargs= {"password": self.password})

# close the fabric Operand

Def closeClient (self):

Self.con.close ()

# execute shell command

Def runCommand (self, sshCommand: "Linux command line statement"):

# top command has not been tested and passed

# if the command line contains a path, it is best to use an absolute path

Try:

# Syntax: run ('remote command')

Result = self.con.run (sshCommand, hide=True)

If result.return_code = = 0Rank # error code, 0: correct execution, 1: error

Return True, result.stdout

Return result.failed, result.stdout

Except:

Exp = traceback.format_exc ()

If "mkdir" in exp and 'File exists' in exp:

Print ("directory [", sshCommand, "] already exists")

Else:

Print (exp)

Return False, exp

# execute the shell command in a specific directory

Def runDir (self, sshCommand: "Linux Command Line statement", dir):

If not os.path.isdir (dir):

Return "wrong destination path, must be a directory"

With self.cd (dir): # with indicates that the code for the with block is executed under the dir directory

Return self.runCommand (sshCommand)

# extract the .tar.gz file

Def decomTarGz (self, srcPath, tarPath):

If os.path.isdir (srcPath):

Return "path with decompression, must be file type"

If not os.path.isdir (tarPath):

Return "wrong destination path, must be a directory"

Return fabricu.runCommand ("tar-zxvf" + srcPath + "- C" + tarPath)

# switch to a directory (context incoherent)

Def cd (self, dir):

# Syntax: cd ('remote directory')

Self.con.cd (dir)

# upload local files to a remote host

Def upload (self, src: "the full path of the file to be uploaded. It is best not to have spaces in the path", target: "directory saved to the server"):

If not os.path.isdir (target):

Return "full path of files to be uploaded"

Elif "" in src:

Return "Special characters such as spaces, (,) are not allowed in the path"

# Syntax: put ('local file', 'remote directory')

Else:return self.con.put (src, target)

# download files from the remote host to the local

Def download (self, src: "full path of remote file", target: "local file path (full path of file name must be included)"):

# Syntax: get ('remote file', 'local file path')

# example: fabricu.download ("/ home/Crawler/WeChatSouGouMain.tar.gz", "D:\\ txt\\ WeChatSouGouMain.tar.gz")

If not os.path.isdir (target):

Return self.con.get (src, target)

Else:return "wrong destination path, must be a full path containing the file name"

After reading this article, I believe you have a certain understanding of "how to use the FastAPI interface service of operation and maintenance series". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report