pythonpython-requestsproxyurllib3proxies

what is the proper way to use proxies with requests in python


Requests is not honoring the proxies flag.

There is something I am missing about making a request over a proxy with python requests library.

If I enable the OS system proxy, then it works, but if I make the request with just requests module proxies setting, the remote machine will not see the proxy set in requests, but will see my real ip, it is as if not proxy was set.

The bellow example will show this effect, at the time of this post the bellow proxy is alive but any working proxy should replicate the effect.

import requests

proxy ={
  'http:': 'https://143.208.200.26:7878',
  'https:': 'http://143.208.200.26:7878'
}
data = requests.get(url='http://ip-api.com/json', proxies=proxy).json()
print('Ip: %s\nCity: %s\nCountry: %s' % (data['query'], data['city'], data['country']))

I also tried changing the proxy_dict format:

proxy ={
          'http:': '143.208.200.26:7878',
          'https:': '143.208.200.26:7878'
       }

But still it has not effect.

I am using: -Windows 10 -python 3.9.6 -urllib 1.25.8

Many thanks in advance for any response to help sort this out.


Solution

  • Ok is working yea !!! . The credits for solving this goes to (Olvin Rogh) Thanks Olvin for your help and pointing out my problem. I was adding colon ":" inside the keys This code is working now.

    PROXY = {'https': 'https://143.208.200.26:7878',
             'http': 'http://143.208.200.26:7878'}
    
    
    with requests.Session() as session:
        session.proxies = PROXY
        r = session.get('http://ip-api.com/json')
        print(json.dumps(r.json(), indent=2))