main.py 831 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from fastapi import FastAPI
  2. from pydantic import BaseModel
  3. from utils import dmQuery
  4. from utils.shellUtil import execute_command_on_linux
  5. app = FastAPI() # 创建API实例
  6. @app.get("/")
  7. async def root():
  8. return {"message": "Hello World"}
  9. # 将数据模型定义为继承BaseModel的类
  10. class Item(BaseModel):
  11. sql: str
  12. @app.put("/exsql")
  13. async def create_item( item: Item, q: str = None):
  14. result = {**item.dict()}
  15. results = dmQuery.execute_query(item.sql)
  16. return results
  17. @app.put("/exshell")
  18. async def create_item( item: Item, q: str = None):
  19. # 配置 Linux 服务器信息
  20. host = '119.29.146.254'
  21. port = 22
  22. username = 'root'
  23. password = 'zrd1236987!'
  24. # 要执行的命令
  25. result = execute_command_on_linux(host, port, username, password, item.sql)
  26. return result