Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i use proxy with requests.session

Hi i am trying to do an action with a session like this:
with requests.Session() as ses:
s = ses.get(f"URL")

my proxy format is: http://username:password@url_of_proxyprovider:port

i tried using in this case: ses.proxies.update(proxy) but it didnt work.
also tried this, but didnt work: s.proxies = {'proxy'}
also tried adding proxy like this: s = ses.get(f"url", proxies=proxy)
its always done through my IP and not through the proxy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Converting comment to answer.

The correct way to do it:

import requests

with requests.Session() as session
    session.proxies = {
        'http': 'http://username:password@url_of_proxyprovider:port',
        'https': 'http://username:password@url_of_proxyprovider:port'
    }

    url = 'http://mywebsite.com/example'
    response = session.get(url)

If you want to proxy FTP also, you can add it to the proxies dictionary:

    session.proxies = {
        'http': 'http://username:password@url_of_proxyprovider:port',
        'https': 'http://username:password@url_of_proxyprovider:port',
        'ftp': 'http://username:password@url_of_proxyprovider:port'
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading