Redirect error when trying to request a url with requests/urllib only in python
<p>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 </p>

<pre><code>http://apimy.in/page/test
</code></pre>

<p>the website is written in python3.4/django1.9</p>

<p>i can send request with <code>curl</code> in <code>php</code> without any problem </p>

<p>but any request with python will result on some kind of redirect error </p>

<p>at first i've tried <code>requests</code> lib </p>

<p>i got this error </p>

<pre><code>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.
</code></pre>

<p>i thought maybe something wrong with <code>requests</code> so i tried <code>urllib</code> </p>

<pre><code>request_data = urllib.parse.urlencode({"DATA": 'aaa'}).encode()
response = urllib.request.urlopen("http://apimy.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
</code></pre>

<p>im using mod_wsgi and apache to serve the website </p>

<p>Since you are using the <a href="http://docs.python-requests.org/en/master/" rel="nofollow"><code>requests</code></a> module did you try to tell the requests module to ignore redirects?</p>

<pre><code>import requests
response = requests.get('your_url_here', allow_redirects=False)
</code></pre>

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

<pre><code>import requests
headers = {'user-agent': 'some_user_agent'}
response = requests.get(url, headers=headers)
</code></pre>

python django redirect curl urllib