简体   繁体   English

Python请求模块-代理不起作用

[英]Python Requests module - proxy not working

I'm trying to connect to website with python requests, but not with my real IP. 我正在尝试使用python请求连接到网站,但没有使用我的真实IP。 So, I found some proxy on the internet and wrote this code: 因此,我在互联网上找到了一些代理,并编写了以下代码:

import requests

proksi = {
    'http': 'http://5.45.64.97:3128'
}

x = requests.get('http://www.whatsmybrowser.org/', proxies = proksi)
print(x.text)

When I get output, proxy simple don't work. 当我得到输出时,简单的代理不起作用。 Site returns my real IP Address. 网站返回了我的真实IP地址。 What I did wrong? 我做错了什么? Thanks. 谢谢。

The answer is quite simple. 答案很简单。 Although it is a proxy service, it doesn't guarantee 100% anonymity. 尽管它是代理服务,但不能保证100%的匿名性。 When you send the HTTP GET request via the proxy server, the request sent by your program to the proxy server is: 通过代理服务器发送HTTP GET请求时,程序发送到代理服务器的请求为:

GET http://www.whatsmybrowser.org/ HTTP/1.1
Host: www.whatsmybrowser.org
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.10.0

Now, when the proxy server sends this request to the actual destination, it sends: 现在,当代理服务器将此请求发送到实际目的地时,它将发送:

GET http://www.whatsmybrowser.org/ HTTP/1.1
Host: www.whatsmybrowser.org
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.10.0
Via: 1.1 naxserver (squid/3.1.8)
X-Forwarded-For: 122.126.64.43
Cache-Control: max-age=18000
Connection: keep-alive

As you can see, it throws your IP (in my case, 122.126.64.43 ) in the HTTP header: X-Forwarded-For and hence the website knows that the request was sent on behalf of 122.126.64.43 如您所见,它将IP(在我的情况下为122.126.64.43122.126.64.43在HTTP标头中: X-Forwarded-For ,因此网站知道请求是代表122.126.64.43发送的

Read more about this header at: https://tools.ietf.org/html/rfc7239 在以下网址了解有关此标头的更多信息: https : //tools.ietf.org/html/rfc7239

If you want to host your own squid proxy server and want to disable setting X-Forwarded-For header, read: http://www.squid-cache.org/Doc/config/forwarded_for/ 如果要托管自己的鱿鱼代理服务器,并且想禁用X-Forwarded-For标头设置,请阅读: http : //www.squid-cache.org/Doc/config/forwarded_for/

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

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