forked from pinokiocomputer/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
27 lines (24 loc) · 697 Bytes
/
server.py
File metadata and controls
27 lines (24 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from fastapi import FastAPI, Body
from typing import List
from pydantic import BaseModel
import sys
import os
import importlib
class Params(BaseModel):
values: List[str]
app = FastAPI()
#@app.get("/")
#async def root():
# return {"greeting":"Hello world"}
# { name: "pip.py", method: "list", params: [ ] }
@app.post("/call")
async def call(item: dict = Body(...)):
print(f"item={item}")
sys.path.insert(0, os.path.dirname(item["path"]))
#async def call(path: str, method: str, params: Params):
mod = importlib.import_module(item["name"], item["path"])
print(f"Mod = {mod}")
fun = getattr(mod, item["method"])
print(f"Fun = {fun}")
result = fun(*item["params"])
return result