可以实现哪些方法来加快python get请求的速度?异步



在下面的代码中,我可以获得每个请求并将响应保存到一个文件中。2000条线路的搜索花了12个多小时才完成。如何加快此过程?实现asynchio这样的东西有效吗?

import requests
with open('file.txt', 'r') as f:
urls = f.readlines()
for url in urls:
try:
data = requests.get(url)
except:
printf(url + " failed")
continue   #moves on to the next url as nothing to write to file
with open('file_complete.txt', 'a+') as f:   #change to mode "a+" to append
f.write(data.text + "n")

有一个库,我曾使用过类似的用例。它的调用速度比请求更快,您可以将URL作为列表传递,然后让它完成其余的

根据URL上的响应类型,可以更改方法。以下是保存响应主体的示例

import faster_than_requests as requests
result = requests.get2str2(["https://github.com", "https://facebook.com"], threads = True)

使用Session,以便所有请求都通过单个TCP连接进行,而不必为每个URL重新打开新的连接。

import requests
with open('file.txt', 'r') as f, 
open('file_complete.txt', 'a') as out, 
requests.Session() as s:
for url in f:
try:
data = s.get(url)
except Exception:
print(f'{url} failed')
continue
print(data.text, file=out)

在这里,我在循环之前打开file_complete.txt并保持打开状态,但每次重新打开文件的开销可能很小,尤其是与get实际完成所需的时间相比。

除了库和多线程之外,另一种可能性是在没有TLS的情况下发出请求,即使用http://endpoints而不是https://。

这将跳过每个调用的SSL握手(您和服务器之间的一些请求(。

在成千上万的电话中,效果可能会累积起来。

当然,你会让自己暴露在这样一种可能性中,即你可能正在与伪装成预定服务器的人进行通信。

你还将暴露你的交通,这样沿途的每个人都可以像明信片一样阅读它。顺便说一句,电子邮件也有同样的安全漏洞。

最新更新