简体   繁体   English

请求保留我的IP地址,而我正在使用Tor

[英]Requests keeping my IP address whereas I'm using Tor

when I do python requests I notice that this is my actual IP address that get sent to the requests whereas I've set a new one using Tor. 当我执行python请求时,我注意到这是发送给请求的实际IP地址,而我使用Tor设置了一个新IP地址。 Here is my code: 这是我的代码:

from torrequest import TorRequest

tr = TorRequest(proxy_port=9050, ctrl_port=9051, password=r"mypassword")
response = tr.get('http://ipecho.net/plain')
proxies = {'http': "socks5://"+response.text+":9050"}

page_response = requests.get('https://www.google.com/search?&q=Apple', timeout=60, verify=False, headers={'User-Agent': random.choice(user_agents)}, proxies=proxies)
soup = BeautifulSoup(page_response.content, 'html.parser')

However, google realises that it is still my IP address and not the Tor generated one. 但是,谷歌意识到这仍然是我的IP地址,而不是Tor生成的IP地址。 How come? 怎么会?

How do you know that Google can get your actual IP while you are using a proxy? 您如何知道Google在使用代理时可以获取您的实际IP? Chances are the proxy you are using could be blocked by Google or proxy get timeout during first connect. 您使用的代理可能会被Google阻止,或者代理在首次连接时超时。

To understand these possible cause you can code like this -> 要了解这些可能的原因,您可以这样编写代码->

from torrequest import TorRequest

tr = TorRequest(proxy_port=9050, ctrl_port=9051, password=r"mypassword")
response = tr.get('http://ipecho.net/plain')
proxies = {'http': "socks5://"+response.text+":9050"}

# Using this check, you will know weather your proxies are working or not.
# if proxy for request and current ip are same than proxy is working
try:
    print "The proxy for request is {0}".format(response.text)
    proxy_check = requests.get('http://icanhazip.com', timeout=60, proxies=proxies)
    print "Proxy is {0}".format(proxy_check)
except requests.exceptions.RequestException as e:
    print e

# we should catch request exception to check any exception raise from requests like 
# timeout
try:
    page_response = requests.get('https://www.google.com/search?&q=Apple', timeout=60, verify=False, headers={'User-Agent': random.choice(user_agents)}, proxies=proxies)
except requests.exceptions.RequestException as e:
    print e
soup = BeautifulSoup(page_response.content, 'html.parser')

Now you can know which IP you are using to access Google. 现在,您可以知道要使用哪个IP来访问Google。 If proxy is good then chances are Google has already blocked that proxy. 如果代理是好的,那么谷歌很可能已经阻止了该代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM