开发者社区 > 云原生 > Serverless > 正文

requests.get后返回的标头有时有Authorization这一内容,有时又没有

同样的的代码,在我自己的电脑上运行时都没有问题,每次运行都能够成功返回带有Authorization的标头。但是将我的代码部署在函数计算上之后,就会报错。不改动代码运行多次,会大概率出现返回的标头不带Authorization,location信息也明显不对。但是偶尔也会成功几次,就很奇怪。下面是相关部分代码:

url1 = 'https://ids.chd.edu.cn/authserver/login?service=http://cdjk.chd.edu.cn/healthPunch/index/login'
r1 = requests.get(url=url1,headers=headers1,allow_redirects=False)
sleep(1)
url2 = r1.headers['Location']
r2 = requests.get(url=url2,headers=headers2,allow_redirects=False)
sleep(1)
url3 = r2.headers['Location']
r3 = requests.get(url=url3,headers=headers3,allow_redirects=False)
sleep(1)
h3 = r3.headers
print(h3)
'''其中headers1传递一个CASTGC值的cookie,
headers2是一些包含User-Agent的普通内容,
headers3包括第二个requests获取的cookie,
为了界面简约就不写出来了。'''

通过我的代码,在我自己电脑上每次运行都能成功返回以下结果:(在部署到函数计算运行有时也能成功返回以下结果,但有时又不能)

{'Server': 'Server      ', 'Date': 'Fri, 26 Aug 2022 01:49:16 GMT', 'Content-Length': '0', 'Connection': 'keep-alive', 'Authorization': 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2NjE0Nzk0NTZ9.MgtsXQoleJJJLhJwq1zbQZkTJb6FVivvPORyDY7H50E', 'Location': 'https://cdjk.chd.edu.cn/#/?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2NjE0Nzk0NTZ9.MgtsXQoleJJJLhJwq1zbQZkTJb6FVivvPORyDY7H50E'}

这个结果中是包含Authorization信息的。但是在部署到函数计算上之后,有时偶尔也能成功,但同样的代码有时就会返回下面的信息,没有Authorization的内容。

{'Server': 'Server      ', 'Date': 'Fri, 26 Aug 2022 00:03:13 GMT', 'Content-Length': '0', 'Connection': 'keep-alive', 'Location': 'http://ids.chd.edu.cn/authserver/login?service=http%3A%2F%2Fcdjk.chd.edu.cn%2FhealthPunch%2Findex%2Flogin%3Bjsessionid%3DD59EFFA16259A32840F2A6CAB299A355'}

这个结果里根本没有Authorization信息,返回的location也不一样。请问大家,这是什么原因造成的呀?为什么同样的代码有时可以成功,有时又不行。 我尝试过在三次requests.get之间加sleep,1秒和5秒甚至更长都尝试过。结果都是概率性的成功或失败。 感谢大家能看到这里,如果有想法请帮帮我,感谢。或者能指点我应该往哪方面去考虑,去学习,谢谢大家。

展开
收起
Cherish丶藏 2022-08-26 10:55:59 476 0
1 条回答
写回答
取消 提交回答
  • 全栈JAVA领域创作者

    这个问题可能是由于网络延迟、服务器负载或者请求处理时间不稳定导致的。你可以尝试以下方法来解决这个问题:

    1. 增加重试次数和超时时间。在requests.get()函数中设置retriestimeout参数,以便在遇到问题时自动重试并等待更长时间。例如:
    import requests
    from time import sleep
    
    url1 = 'https://ids.chd.edu.cn/authserver/login?service=http://cdjk.chd.edu.cn/healthPunch/index/login'
    headers1 = {'CASTGC': 'your_cookie'}
    r1 = requests.get(url=url1, headers=headers1, allow_redirects=False, retries=3, timeout=10)
    sleep(1)
    url2 = r1.headers['Location']
    headers2 = {'User-Agent': 'your_user_agent'}
    r2 = requests.get(url=url2, headers=headers2, allow_redirects=False, retries=3, timeout=10)
    sleep(1)
    url3 = r2.headers['Location']
    headers3 = {'Cookie': f'{r2.cookies["CASTGC"]}; your_cookie'}
    r3 = requests.get(url=url3, headers=headers3, allow_redirects=False, retries=3, timeout=10)
    sleep(1)
    h3 = r3.headers
    print(h3)
    
    1. 使用代理。有时候服务器可能会限制某些IP地址的访问,使用代理可以绕过这些限制。你可以使用Python的requests库结合免费或付费的代理服务来实现。例如:
    import requests
    from random import choice
    from fake_proxy import ProxyList
    
    proxy_list = ProxyList()
    proxies = [random.choice(proxy_list).http] + [random.choice(proxy_list).https]
    url1 = 'https://ids.chd.edu.cn/authserver/login?service=http://cdjk.chd.edu.cn/healthPunch/index/login'
    headers1 = {'CASTGC': 'your_cookie'}
    r1 = requests.get(url=url1, headers=headers1, allow_redirects=False, proxies=proxies)
    sleep(1)
    url2 = r1.headers['Location']
    headers2 = {'User-Agent': 'your_user_agent'}
    r2 = requests.get(url=url2, headers=headers2, allow_redirects=False, proxies=proxies)
    sleep(1)
    url3 = r2.headers['Location']
    headers3 = {'Cookie': f'{r2.cookies["CASTGC"]}; your_cookie'}
    r3 = requests.get(url=url3, headers=headers3, allow_redirects=False, proxies=proxies)
    sleep(1)
    h3 = r3.headers
    print(h3)
    

    请注意,你需要安装fake_proxy库来使用代理功能。你可以使用以下命令安装:

    pip install fake-proxy
    
    2023-06-08 22:33:48
    赞同 展开评论 打赏
问答分类:
问答地址:

快速交付实现商业价值。

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载