Python Requests verify proxy is being utilized

Benjamin James

I am trying to use the proxy feature of python requests library, but the data being returned from the requests being made via proxies is incorrect (page text is still english when it should be the localized language). Is there a way to verify that the proxy is being utilized correctly?

agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/XX.X.XXXX.XX Safari/537.36"

proxy_list = {
'South Korea' : 'http://1.241.102.9:3128',
'Sweden' : 'http://79.136.65.150:80',
'Russia' : 'http://77.236.87.175:80',
'Japan' : 'http://153.149.158.149:3128',
'Germany' : 'http://213.136.89.121:80',
}

# Check app availability via each proxy
for proxy_country, proxy_val in proxy_list.items():
    proxyDict = {"http" : proxy_val}
    try:
        req = requests.get(url, headers={'user-agent':agent}, proxies=proxyDict,timeout=5)
    except:
        print "COULD NOT DETERMINE AVAILABILITY FOR: %s" % (proxy_country)
    else:
        print "%s : %s" % (proxy_country,req.status_code)
larsks

The easiest way to verify whether or not requests is using a proxy is simply to enable debug logging. The requests module logs a variety of interesting at DEBUG priority, so just do:

import logging
logging.basicConfig(level='DEBUG')

Here's my simple test script:

#!/usr/bin/env python

import sys
import logging
import requests

logging.basicConfig(level='DEBUG')
res = requests.get(sys.argv[1])
res.raise_for_status()

If I run this:

$ python reqtest.py http://lwn.net/

I see:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): lwn.net
DEBUG:requests.packages.urllib3.connectionpool:"GET / HTTP/1.1" 200 9098

But if I enable a proxy:

$ http_proxy=http://squid.corp.example.com:3128 pytyhon reqtest.py http://lwn.net/

I clearly see that requests is connecting to the proxy, rather than directly to the remote system:

INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): squid.corp.example.com
DEBUG:requests.packages.urllib3.connectionpool:"GET http://lwn.net/ HTTP/1.1" 200 9098

I see the same behavior if I modify the code like this:

#!/usr/bin/env python

import sys
import logging
import requests

logging.basicConfig(level='DEBUG')
res = requests.get(sys.argv[1],
                   proxies=dict(http='http://squid.corp.example.com:3128'))
res.raise_for_status()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python requests SSL error - certificate verify failed

check Python requests with charles proxy for HTTPS

Why does Python requests ignore the verify parameter?

python Requests SSL ERROR (certificate verify failed)

How to verify if the IP is replaced with proxy server(selenium, python)

Set up a proxy with python requests

Catch proxy errors on requests python

How to programatically remove any view in a Fragment being utilized by a FragmentStatePagerAdapter that is not a LinearLayout?

httr equivalent to verify in requests

Static files not being utilized by Django on docker with nginx

How can a Python Microservice verify requests using a JWT in an Authorization header?

Certificate Verify Failed when using Requests in Python with a self signed certificate

verify auth in python mock_requests lib

curl --cacert vs python requests verify

How to determine number of GPU cores being utilized for a process?

Python requests "certificate verify failed"

Why is my GPU not being fully utilized?

Is my server being used as a proxy or malicious requests?

python 2.7.5 requests and certificate verify failed

How to check the 'verify' setting in Python's requests using RequestBin or similar

Python Requests doesnt work for https proxy

Does Python requests really connect to http proxy?

Python Proxy Requests and User Agent

python requests 'certificate verify failed' for a specific SNI certificate

Unable to verify the first certificate; NodeJs vs Python Requests

Python requests with https - certificate verify failed

Python requests with proxy failing for WinError 10060

Prevent requests to 127.0.0.1 from being forwarded to http_proxy

Python requests using public ip instead of proxy