Skip to main content
 首页 » 编程设计

Python 词干+请求 : Not switching circut/changing IP address when using a session

2024年09月07日9mate10pro

查看以下脚本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 

正如我们所见,在无 session 的情况下,IP 地址每次都会发生变化。然而,在我们使用 session 对象的情况下,IP 地址仅在第一个请求发生变化,所有后续请求都显示相同的 IP 地址,表明电路没有变化。

为什么会发生这种情况,如何解决?如何在不失去切换电路的能力的情况下使用request session ?

请您参考如下方法:

这是因为...

Keep-Alive

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)