import httpx from httpx import BasicAuth, Timeout def login(): """登录WorldQuant Brain API""" try: # 从nacos获取账号密码 with httpx.Client(timeout=10.0) as temp_client: nacos_resp = temp_client.get( 'http://192.168.31.41:30848/nacos/v1/cs/configs?dataId=wq_account&group=quantify' ) if nacos_resp.status_code != 200: print('获取账号密码失败') return False config = nacos_resp.json() username = config.get('user_name') password = config.get('password') if not username or not password: print('账号密码不完整') return False print(f"正在登录账户: {username}") # 创建客户端并设置超时(关键修复) # 设置更长的超时时间:连接30秒,读取60秒 timeout = Timeout(connect=30.0, read=60.0, write=30.0, pool=30.0) client = httpx.Client( auth=BasicAuth(username, password), timeout=timeout ) # 发送登录请求 response = client.post( 'https://api.worldquantbrain.com/authentication') if response.status_code == 201: print("登录成功!") # 登录成功后将client对象返回出来 return client else: print( f"登录失败: {response.status_code} - {response.text}") client.close() return False except Exception as e: print(f"登录异常: {e}") return False def fetch_alpha_by_id(alpha_id: str, client: httpx.Client): """根据Alpha ID获取Alpha详情""" url = f"https://api.worldquantbrain.com/alphas/{alpha_id}" response = client.get(url) if response.status_code == 200: return response.json() else: return {} if __name__ == '__main__': client = login() if not client: exit(1) alpha = fetch_alpha_by_id('O0mnm8zd', client) print(alpha)