Python stem+requests: 使用会话时不切换 circut/changing IP 地址
Python stem+requests: Not switching circut/changing IP address when using a session
查看以下脚本tortest.py
,它反复切换 TOR 电路并检查 IP 地址是否已更改:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import json
import time
# pip install requests[socks]
import requests
# pip install stem
from stem import Signal
from stem.control import Controller
http = requests.session()
proxies = {'http': 'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
def get_new_ip():
with Controller.from_port(port=9051) as controller:
controller.authenticate(password="xxx")
controller.signal(Signal.NEWNYM)
time.sleep(controller.get_newnym_wait())
for _ in xrange(5):
get_new_ip()
try:
ip1 = json.loads(requests.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies).content)["origin"]
ip2 = json.loads(http.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies).content)["origin"]
print "Without session: {}".format(ip1)
print "With session: {}".format(ip2)
print
except Exception as e:
pass
运行 这个脚本,我得到以下输出:
$ python2 tortest.py
Without session: 137.74.169.241
With session: 137.74.169.241
Without session: 145.249.104.203
With session: 137.74.169.241
Without session: 5.189.170.221
With session: 137.74.169.241
Without session: 192.36.27.6
With session: 137.74.169.241
Without session: 199.249.224.43
With session: 137.74.169.241
正如我们所见,在无会话的情况下,IP 地址每次都在变化。但是,在我们使用会话对象的情况下,IP 地址仅在第一个请求发生变化,所有后续请求都显示相同的 IP 地址,表明电路没有变化。
为什么会发生这种情况,如何解决?如何使用 request
会话而不失去切换电路的能力?
这是因为...
Excellent news — thanks to urllib3, keep-alive is 100% automatic
within a session! Any requests that you make within a session will
automatically reuse the appropriate connection!
Note that connections are only released back to the pool for reuse
once all body data has been read; be sure to either set stream to
False or read the content property of the Response object.
当您使用 session 时,它会发送一个 Connection: keep-alive
header 并将挂起连接,这会导致它使用之前在 Tor 中建立的电路。
如文档中所述,您可以设置 stream=False
来绕过它:
http.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies, stream=False)
查看以下脚本tortest.py
,它反复切换 TOR 电路并检查 IP 地址是否已更改:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import json
import time
# pip install requests[socks]
import requests
# pip install stem
from stem import Signal
from stem.control import Controller
http = requests.session()
proxies = {'http': 'socks5://127.0.0.1:9050', 'https': 'socks5://127.0.0.1:9050'}
def get_new_ip():
with Controller.from_port(port=9051) as controller:
controller.authenticate(password="xxx")
controller.signal(Signal.NEWNYM)
time.sleep(controller.get_newnym_wait())
for _ in xrange(5):
get_new_ip()
try:
ip1 = json.loads(requests.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies).content)["origin"]
ip2 = json.loads(http.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies).content)["origin"]
print "Without session: {}".format(ip1)
print "With session: {}".format(ip2)
print
except Exception as e:
pass
运行 这个脚本,我得到以下输出:
$ python2 tortest.py
Without session: 137.74.169.241
With session: 137.74.169.241
Without session: 145.249.104.203
With session: 137.74.169.241
Without session: 5.189.170.221
With session: 137.74.169.241
Without session: 192.36.27.6
With session: 137.74.169.241
Without session: 199.249.224.43
With session: 137.74.169.241
正如我们所见,在无会话的情况下,IP 地址每次都在变化。但是,在我们使用会话对象的情况下,IP 地址仅在第一个请求发生变化,所有后续请求都显示相同的 IP 地址,表明电路没有变化。
为什么会发生这种情况,如何解决?如何使用 request
会话而不失去切换电路的能力?
这是因为...
Excellent news — thanks to urllib3, keep-alive is 100% automatic within a session! Any requests that you make within a session will automatically reuse the appropriate connection!
Note that connections are only released back to the pool for reuse once all body data has been read; be sure to either set stream to False or read the content property of the Response object.
当您使用 session 时,它会发送一个 Connection: keep-alive
header 并将挂起连接,这会导致它使用之前在 Tor 中建立的电路。
如文档中所述,您可以设置 stream=False
来绕过它:
http.get("https://httpbin.org/ip", timeout=(5, 5), proxies=proxies, stream=False)