我有一个 python 脚本,它接受一些 url 的输入。我的脚本循环遍历每个 url 并从每个页面打印出 htmltext。网站会将此视为 3 个单独的 GET 请求并因此对网站进行 3 次“点击”,还是会看到套接字连接并将其视为对页面的 1 次“点击”?
我认为这是检查调试的第一个选项,如果是这样,是否可以从同一站点上的多个 URL 获取数据,但该站点只将其视为对该站点的 1 次“点击”?我可以利用 keep-alive 功能在 urllib3 中实现这一点吗?
我的脚本如下:
for u in url:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
req = urllib2.Request(u)
req.add_header('User-Agent','Mozilla/5.0')
print urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1)).open(req)
resp = opener.open(req)
htmltext = resp.read()
请您参考如下方法:
Would the website see this as 3 seperate GET requests and therefore 3 "hits" to the site or would it see the socket connection and see it as 1 "hit" to the page?
是的,即使您重用套接字连接,它仍然是 3 个不同的请求(通过一个套接字)。无论您使用了多少个连接,服务器的访问日志都会显示 3 个请求。
重用连接的好处是创建一个新的 TCP 套接字并与服务器协商握手是一个相对昂贵的过程。这样做有时可能比检索 HTTP 响应主体本身花费更多的时间。通过重用连接,您可以在第一次请求后跳过该部分。
