40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
import httpx
|
|
from httpx import BasicAuth
|
|
import os
|
|
|
|
def login(credentials_file='account.txt'):
|
|
"""登录WorldQuant Brain API"""
|
|
# 读取本地账号密码
|
|
if not os.path.exists(credentials_file):
|
|
print("未找到 account.txt 文件")
|
|
with open(credentials_file, 'w') as f: f.write("")
|
|
print("account.txt 文件已创建,请填写账号密码, 格式: ['username', 'password]")
|
|
exit(1)
|
|
|
|
with open(credentials_file) as f:
|
|
credentials = eval(f.read())
|
|
username, password = credentials[0], credentials[1]
|
|
|
|
print(f"正在登录账户: {username}")
|
|
|
|
# 创建客户端并认证
|
|
client = httpx.Client(auth=BasicAuth(username, password))
|
|
|
|
# 发送登录请求
|
|
response = client.post('https://api.worldquantbrain.com/authentication')
|
|
print(f"登录状态: {response.status_code}")
|
|
|
|
if response.status_code == 201:
|
|
print("登录成功!")
|
|
print(response.json())
|
|
return client
|
|
else:
|
|
print(f"登录失败: {response.json()}")
|
|
client.close()
|
|
return None
|
|
|
|
# 使用示例
|
|
if __name__ == "__main__":
|
|
client = login()
|
|
if client:
|
|
client.close() |