作者eight0 (人类)
看板Python
标题[问题] requests 和非法的 headers
时间Mon Apr 17 22:16:57 2017
虽然标题写 requests,不过 python 3.6 的 urllib 也有这现象。
问题出自於 server 回传的 header 不太正常。这是 curl -D 拿到的︰
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.0
Date: Mon, 17 Apr 2017 11:18:21 GMT
Content-Type: text/html
Connection: keep-alive
Status 302:
Location: ...
而用 requests 拿到的 response.headers,缺少 "Location" 这个 key,无法处理转址。
如果用 urllib,拿到的 response.info() 也是缺少 Location,且 as_string()
长这样︰
Server: nginx/1.8.0
Date: Mon, 17 Apr 2017 12:24:27 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Status 302:
Location: ...
Firefox 则是可以正常辨认 Location,Node.js 也是︰
{ server: 'nginx/1.8.0',
date: 'Mon, 17 Apr 2017 13:59:56 GMT',
'content-type': 'text/html',
'transfer-encoding': 'chunked',
connection: 'keep-alive',
'status 302': '',
location: '...' }
感觉是 parser 的问题。不知道这种情况怎麽处理?或是有任何办法在 requests 中存取
raw data 找出 Location?
最後附上网址,是个漫画网站,内可能含有较清凉图片︰
ht tp://smp.yoedge.com/smp-app/1017018/shinmangaplayer/index.html
--
(* ̄▽ ̄)/‧★*"`'*-.,_,.-*'`"*-.,_☆,.-*`
http://i.imgur.com/oAd97.png
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 1.160.246.132
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1492438624.A.BBC.html
1F:推 zerof: r.raw._original_response.msg.to_string() 04/18 00:03
2F:→ zerof: py2 没这问题, 应该是 py3 urllib parser 的 bug 04/18 00:10
3F:→ zerof: 可以 report 回 bug.python 04/18 00:11
4F:→ uranusjr: 其实也不能说是 bug, 这个 header 本身就 malformed 了 04/18 01:00
5F:→ uranusjr: HTTP header key 不应该包含空白, 读到 status 302 那行 04/18 01:00
6F:→ uranusjr: 会爆掉也是合情合理的事情... 04/18 01:01
7F:→ s860134: 看到上面我才惊觉,原来 status 那行算在 heaser 里面? 04/18 01:08
8F:→ uranusjr: HTTP status 不是那样写的啊, 根本不知道为什麽有那行 04/18 15:41
9F:→ eight0: 感谢一楼 04/18 19:27