hretic hretic - 2 months ago 25
Python Question

Redirect error when trying to request a url with requests/urllib only in python

im trying to post data to a url in my server ... but im stuck in sending any request to that url (any url on that server ) here is one for example

http://apineginpay.in/page/test


the website is written in python3.4/django1.9

i can send request with
curl
in
php
without any problem

but any request with python will result on some kind of redirect error

at first i've tried
requests
lib

i got this error

TooManyRedirects at /api/sender
Exceeded 30 redirects.
Request Method: GET
Request URL: http://localhost:8000/api/sender
Django Version: 1.9.6
Exception Type: TooManyRedirects
Exception Value:

Exceeded 30 redirects.


i thought maybe something wrong with
requests
so i tried
urllib


request_data = urllib.parse.urlencode({"DATA": 'aaa'}).encode()
response = urllib.request.urlopen("http://apineginpay.in/page/test" , data=request_data)



HTTPError at /api/sender
HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
Request Method: GET
Request URL: http://localhost:8000/api/sender
Django Version: 1.9.6
Exception Type: HTTPError
Exception Value:
HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
Exception Location: c:\Python344\lib\urllib\request.py in http_error_302, line 675


im using mod_wsgi and apache to serve the website

Answer

Since you are using the requests module did you try to tell the requests module to ignore redirects?

import requests
response = requests.get('your_url_here', allow_redirects=False)

Maybe this work. If this doesn't work, you can also try to change your user-agent in case your server is configured to drop script-requests for security reasons.

import requests
headers = {'user-agent': 'some_user_agent'}
response = requests.get(url, headers=headers)
Comments