|
import requests |
|
import importlib.util |
|
import sys |
|
|
|
def load_and_run_from_txt(txt_url: str): |
|
"""同步方式下载txt并执行""" |
|
try: |
|
|
|
response = requests.get(txt_url) |
|
response.raise_for_status() |
|
|
|
code_content = response.text |
|
print(f"下载成功,代码长度: {len(code_content)}") |
|
|
|
|
|
spec = importlib.util.spec_from_loader("dynamic_api", loader=None) |
|
module = importlib.util.module_from_spec(spec) |
|
|
|
exec(code_content, module.__dict__) |
|
sys.modules["dynamic_api"] = module |
|
|
|
|
|
if hasattr(module, 'app'): |
|
import uvicorn |
|
uvicorn.run(module.app, host="0.0.0.0", port=7860) |
|
else: |
|
print("未找到FastAPI应用") |
|
|
|
except Exception as e: |
|
print(f"执行失败: {e}") |
|
|
|
if __name__ == "__main__": |
|
|
|
load_and_run_from_txt("http://192.168.1.100:9000/download/api.txt") |