DecaK DecaK - 3 months ago 75
Python Question

Python Requests module - proxy not working

I'm trying to connect to website with python requests, but not with my real 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. What I did wrong? Thanks.

Answer

The answer is quite simple. Although it is a proxy service, it doesn't guarantee 100% anonymity. When you send the HTTP GET request via the proxy server, the request sent by your program to the proxy server is:

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

Read more about this header at: 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/