Python Forum
Python Request's Proxies not working. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Python Request's Proxies not working. (/thread-26421.html)



Python Request's Proxies not working. - Fudster - May-01-2020

I'm trying to use requests with proxies, but it only shows my local IP

Here's my test code.

#!/usr/bin/python3

import requests
import random
impo


proxies = [
    "167.172.248.53:3128",
    "194.226.34.132:5555",
    "203.202.245.62:80",
    "141.0.70.211:8080",
    "118.69.50.155:80",
    "201.55.164.177:3128",
    "51.15.166.107:3128",
    "91.205.218.64:80",
    "128.199.237.57:8080",
]


def get_session(proxies):
    session = requests.Session()
    proxy = random.choice(proxies)
    session.proxies = {"http://": proxy, "https://": proxy}
    return session, proxy


for i in range(5):
    s, proxy = get_session(proxies)
    try:
        print(
            f"Proxying through {proxy}:",
            s.get("http://icanhazip.com", timeout=1.5).text.strip(),
        )
    except Exception as e:
        continue
I

Proxying through 141.0.70.211:8080: 51.25x.xxx.193
Proxying through 201.55.164.177:3128: 51.25x.xxx.193
Proxying through 91.205.218.64:80: 51.25x.xxx.193
Proxying through 141.0.70.211:8080: 51.25x.xxx.193
Proxying through 51.15.166.107:3128: 51.25x.xxx.193
Any idea's on why this is not working?


RE: Python Request's Proxies not working. - buran - May-01-2020

try adding scheme to url, as per the docs:
Quote:Note that proxy URLs must include the scheme.

session.proxies = {scheme:f'{scheme}://{proxy}' for scheme in ('http', 'https')}