仅在 python 中尝试使用 requests/urllib 请求 url 时出现重定向错误

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

我正在尝试将 post 数据发送到我服务器中的 url ... 但我一直在向 url 发送任何请求(任何 url服务器 ) 这里有一个

http://apimy.in/page/test

网站写在python3.4/django1.9

我可以在 php 中使用 curl 发送请求,没有任何问题

但是任何带有 python 的请求都会导致某种重定向错误

起初我试过requests lib

我遇到了这个错误

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.

我认为 requests 可能有问题所以我尝试了 urllib

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

我正在使用 mod_wsgi 和 apache 来为网站提供服务

既然您正在使用 requests 模块,您是否尝试告诉请求模块忽略重定向?

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

也许这项工作。 如果这不起作用,您也可以尝试更改 user-agent,以防您的服务器出于安全原因配置为丢弃 script-requests

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