File size: 1,035 Bytes
309ccba
6a41820
309ccba
6a41820
309ccba
 
 
 
 
 
6a41820
309ccba
 
6a41820
309ccba
 
 
 
 
 
 
 
 
 
 
 
 
6a41820
309ccba
 
6a41820
 
309ccba
 
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
28
29
30
31
32
33
34
import requests
import importlib.util
import sys

def load_and_run_from_txt(txt_url: str):
    """同步方式下载txt并执行"""
    try:
        # 下载txt文件
        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
        
        # 启动FastAPI
        if hasattr(module, 'app'):
            import uvicorn
            uvicorn.run(module.app, host="0.0.0.0", port=8000)
        else:
            print("未找到FastAPI应用")
            
    except Exception as e:
        print(f"执行失败: {e}")

if __name__ == "__main__":
    # 直接执行
    load_and_run_from_txt("https://bg5-pycode.static.hf.space/api.txt")