python请求很慢 [英] python requests is slow

查看:41
本文介绍了python请求很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个下载管理器.使用 python 中的 requests 模块检查有效链接(以及可能已损坏的链接).我用于检查以下链接的代码:

I am developing a download manager. Using the requests module in python to check for a valid link (and hopefully broken links). My code for checking link below:

url = 'http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe'
r = requests.get(url, allow_redirects=False) # this line takes 40 seconds
if r.status_code==200:
    print("link valid")
else:
    print("link invalid")

现在,问题是执行此检查需要大约 40 秒,这是巨大的.我的问题是如何使用 urllib2 或其他方法加快速度?

Now, the issue is this takes approximately 40 seconds to perform this check, which is huge. My question is how can I speed this up maybe using urllib2 or something??

注意:另外,如果我将 url 替换为http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe"的实际 URL,这需要一个第二个,所以它似乎是请求的问题.

Note: Also if I replace url with the actual URL which is 'http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe', this takes one second so it appears to be an issue with requests.

推荐答案

并非所有主机都支持 head 请求.你可以改用这个:

Not all hosts support head requests. You can use this instead:

r = requests.get(url, stream=True)

这实际上只下载标题,而不是响应内容.此外,如果您的想法是事后获取文件,则不必再提出请求.

This actually only download the headers, not the response content. Moreover, if the idea is to get the file afterwards, you don't have to make another request.

请参阅此处了解更多信息.

这篇关于python请求很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆