使用Python请求重定向错误太多

时间:2011-10-25 22:58:08

标签: python redirect dns python-requests http-request

HTTP请求在我的localhost上工作正常,但是在我的服务器上使用python请求库运行相同的HTTP请求会返回“Too Many Redirects”错误

当我进入

localhost/terminal/jfk

在浏览器中,我按预期获得了一个json字典。

但是,当我使用服务器上的python请求库在python中运行以下内容时

requests.get('http://splitmyri.de/terminal/jfk')

我从请求模块收到“Too Many Redirects”错误。

有关导致错误的原因的任何想法?或建议缩小潜在原因?

2 个答案:

答案 0 :(得分:6)

阿沙,

在你回答这确实是你的网站后,我查看了为什么我得到一个空的GoDaddy.com页面。问题是你的splitmyri.de的DNS条目正在返回两个不同的A记录...

使用107.10.141.119分析结果
我的107.10.141.119 splitmyri.de文件中的硬编码/etc/hosts允许我向107.10.141.119发出查询,返回"这是我的网站"在index.html中(然后我检查了http://splitmyri.de/terminal/并获得了一个空的json哈希,其中mime类型为= [application / json])。检查此地址的反向条目将返回Amazon AWS主机(我期望用于真实网页的条目类型)。现在您的代码按预期工作......

[mpenning@Bucksnort ~]$ python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get('http://splitmyri.de/terminal/')
<Response [200]>
>>> requests.get('http://splitmyri.de/terminal/').content
u'{}'
>>> # requests.get('http://splitmyri.de/terminal/jfk').content returns a huge json hash 

使用68.178.232.100分析结果
对68.178.232.100的查询执行相同的钻取得到一个空的GoDaddy.com页面。反向映射此地址会显示停放域的典型条目。正如您在尝试中看到的那样,在此处发送请求会返回TooManyRedirects

[mpenning@Bucksnort ~]$ python
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> foo = requests.get('http://splitmyri.de/terminal/jfk')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-i686/egg/requests/api.py", line 50, in get
  File "build/bdist.linux-i686/egg/requests/api.py", line 37, in request
  File "build/bdist.linux-i686/egg/requests/sessions.py", line 170, in request
  File "build/bdist.linux-i686/egg/requests/models.py", line 383, in send
  File "build/bdist.linux-i686/egg/requests/models.py", line 210, in _build_response
requests.exceptions.TooManyRedirects
>>> 

解决方案
修复你的DNS for splitmyri.de(删除A记录为68.178.232.100),一切都会好。

包括下面的DNS信息作为FYI ......

[mpenning@Bucksnort ~]$ dig splitmyri.de

; <<>> DiG 9.6-ESV-R4 <<>> splitmyri.de
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54051
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 2, ADDITIONAL: 4

;; QUESTION SECTION:
;splitmyri.de.                  IN      A

;; ANSWER SECTION:
splitmyri.de.           3193    IN      A       68.178.232.100
splitmyri.de.           3193    IN      A       107.20.141.119

;; AUTHORITY SECTION:
splitmyri.de.           3193    IN      NS      ns49.domaincontrol.com.
splitmyri.de.           3193    IN      NS      ns50.domaincontrol.com.

;; ADDITIONAL SECTION:
ns49.domaincontrol.com. 3193    IN      A       216.69.185.25
ns49.domaincontrol.com. 3193    IN      AAAA    2607:f208:206::19
ns50.domaincontrol.com. 3193    IN      A       208.109.255.25
ns50.domaincontrol.com. 3193    IN      AAAA    2607:f208:302::19

;; Query time: 1 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Oct 26 05:14:51 2011
;; MSG SIZE  rcvd: 205

[mpenning@Bucksnort ~]$ 
[mpenning@Bucksnort ~]$ dig -x 107.20.141.119

; <<>> DiG 9.6-ESV-R4 <<>> -x 107.20.141.119
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41049
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 6, ADDITIONAL: 6

;; QUESTION SECTION:
;119.141.20.107.in-addr.arpa.   IN      PTR

;; ANSWER SECTION:
119.141.20.107.in-addr.arpa. 300 IN     PTR     ec2-107-20-141-119.compute-1.amazonaws.com.

;; AUTHORITY SECTION:
141.20.107.in-addr.arpa. 900    IN      NS      pdns1.ultradns.net.
141.20.107.in-addr.arpa. 900    IN      NS      pdns2.ultradns.net.
141.20.107.in-addr.arpa. 900    IN      NS      pdns3.ultradns.org.
141.20.107.in-addr.arpa. 900    IN      NS      pdns5.ultradns.info.
141.20.107.in-addr.arpa. 900    IN      NS      pdns4.ultradns.org.
141.20.107.in-addr.arpa. 900    IN      NS      pdns6.ultradns.co.uk.

;; ADDITIONAL SECTION:
pdns1.ultradns.net.     86400   IN      A       204.74.108.1
pdns1.ultradns.net.     86400   IN      AAAA    2001:502:f3ff::1
pdns2.ultradns.net.     86400   IN      A       204.74.109.1
pdns3.ultradns.org.     86400   IN      A       199.7.68.1
pdns4.ultradns.org.     86400   IN      A       199.7.69.1
pdns4.ultradns.org.     86400   IN      AAAA    2001:502:4612::1

;; Query time: 306 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Oct 26 05:09:47 2011
;; MSG SIZE  rcvd: 392

[mpenning@Bucksnort ~]$ dig -x 68.178.232.100

; <<>> DiG 9.6-ESV-R4 <<>> -x 68.178.232.100
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38578
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 2, ADDITIONAL: 2

;; QUESTION SECTION:
;100.232.178.68.in-addr.arpa.   IN      PTR

;; ANSWER SECTION:
100.232.178.68.in-addr.arpa. 3600 IN    PTR     parkwebwin-v01.prod.mesa1.secureserver.net.

;; AUTHORITY SECTION:
232.178.68.in-addr.arpa. 3600   IN      NS      cns1.secureserver.net.
232.178.68.in-addr.arpa. 3600   IN      NS      cns2.secureserver.net.

;; ADDITIONAL SECTION:
cns1.secureserver.net.  3600    IN      A       208.109.255.100
cns2.secureserver.net.  3600    IN      A       216.69.185.100

;; Query time: 173 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Wed Oct 26 05:12:06 2011
;; MSG SIZE  rcvd: 171

[mpenning@Bucksnort ~]$ 

答案 1 :(得分:1)

如果您需要缩小问题范围,最好使用wireshark并分析不同的连接及其内容。像这样你会看到通过电线传输的是什么。适合调试TCP相关问题。此外,您可以使用pdb Python模块并调试您的程序。在调用之前发出pdb.set_trace(),然后单步进入函数并查看它的作用。有关pdb的更多信息,请参阅Python文档或按?