Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests + proxy + 302 redirect, why incorrect request?

I'm using Python requests and I send requests through a proxy. The site I'm sending requests to has 302 redirection, and the request doesn't work properly. It seems that the request sends without proxy and the site finds out my real IP.

Python code:

try:
    session = Session()
    request = Request('GET', url, headers=headers)
    prepped = session.prepare_request(request)
    resp = session.send(prepped, proxies=proxy, timeout=8)
    session.cookies.clear()
    print(resp.status_code)
    print(resp.history)
except requests.exceptions.Timeout:
    print("Timeout error ... :( " + "\n")
except requests.exceptions.ConnectionError:
    print("Connection error ... :( " + "\n")
except requests.exceptions.HTTPError:
    print("HTTPError ... :( " + "\n")

Response history

<Response [302]>

Basically I need to send request from another IP and every time as a new user with new cookies and so on. But with this code I can't manage to do it. Can someone help me with this and say what's the problem?

like image 437
Vlad Avatar asked May 23 '26 15:05

Vlad


1 Answers

You can using requests.head to get redirected url first.

r = requests.head(url, allow_redirects=True) print(r.url)

like image 125
Ertuğrul Altınboğa Avatar answered May 25 '26 06:05

Ertuğrul Altınboğa