python请求很慢

sca*_*ous 4 python urllib download urllib2 python-requests

我正在开发一个下载管理器.使用python中的请求模块来检查有效链接(并希望破坏链接).我检查以下链接的代码:

    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"
Run Code Online (Sandbox Code Playgroud)

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

注意:如果我替换url为实际的URL" http://pyscripter.googlecode.com/files/PyScripter-v2.5.3-Setup.exe ",这需要一秒钟,因此它似乎是请求的问题.

mic*_*yer 11

并非所有主机都支持head请求.您可以使用此代替:

r = requests.get(url, stream=True)
Run Code Online (Sandbox Code Playgroud)

这实际上只下载标题,而不是响应内容.此外,如果想要在以后获取文件,则不必再提出其他请求.

这里获取更多相关信息.


Jon*_*nts 9

不要使用get实际检索文件,使用:

r = requests.head(url,allow_redirects=False)
Run Code Online (Sandbox Code Playgroud)

从我的机器上的6.9secs到0.4secs