Python脚本批量并发登录获取token
import requests import concurrent.futures import urllib3 urllib3.disable_warnings() def login_user(username,password): login_url = 'xxx/v1/user/login' data = { "phone":username, "code": password, "rid": "202307131845143e01f4be72da26d1ae" } headers = { 'Host': 'xxx.xxx.cn', 'Connection': 'keep-alive', 'Content-Length': '184', 'X-App-fr': 'default', 'sec-ch-ua': '"Not=A?Brand";v="99","Chromium";v="118"', 'Version': '0.0.1', 'sec-ch-ua-mobile': '?0', 'User-Agent': 'Mozilla/5.0(WindowsNT10.0)AppleWebKit/537.36(KHTML,likeGecko)Chrome/118.0.1.2Safari/537.36JiSu/118.0.1.2', 'App-Name': 'chatglm', 'Content-Type': 'application/json;charset=UTF-8', 'Accept': 'application/json,text/plain,*/*' } response = requests.post(login_url,headers=headers, json=data, verify=False) myRes = response.json() if myRes['status'] == 0: return myRes['result']['access_token'] else: return None def getMyRespose(): myFile = "D:/xxx/xxx/xxxx/testAccount.csv" write_file = "D:/xxx/xxx/xxx/myToken1.txt" # 用于存储access_token的列表 access_tokens = [] #使用线程池来并发执行登录 with concurrent.futures.ThreadPoolExecutor(max_workers=500) as executor: futures = [] with open(myFile, 'r') as f: for line in f: username, password = line.strip().split(',') future = executor.submit(login_user, username, password) futures.append(future) # 收集结果 for future in concurrent.futures.as_completed(futures): access_token = future.result() if access_token: access_tokens.append(access_token) # 将access_token写入文件 with open(write_file, 'w') as wf: for token in access_tokens: wf.write(token + '\n') if __name__ == '__main__': getMyRespose()