记得301/302是自动跳转的,结果我这直接异常,参考 这里,简单重新实现了下重定向,结果仍然杯具。只能追源码:
def redirect_request(self, req, fp, code, msg, headers, newurl): m = req.get_method() if (code in (301, 302, 303, 307) and m in ("GET", "HEAD") or code in (301, 302, 303) and m == "POST"): # Strictly (according to RFC 2616), 301 or 302 in response # to a POST MUST NOT cause a redirection without confirmation # from the user (of urllib2, in this case). In practice, # essentially all clients do redirect in this case, so we # do the same. # be conciliant with URIs containing a space newurl = newurl.replace(' ', '%20') newheaders = dict((k,v) for k,v in req.headers.items() if k.lower() not in ("content-length", "content-type") ) return Request(newurl, headers=newheaders, origin_req_host=req.get_origin_req_host(), unverifiable=True) else: raise HTTPError(req.get_full_url(), code, msg, headers, fp)
感情是只支持post/get/head 方法,咱这用了put,额...(怎么用put, 参看这里)
只能把redirect_request一起覆盖了,什么put/delete统统加上,齐活~
另,才发现python2.6+的版本,urlopen竟然已支持timeout传参,幸而一直用的requests,才没有傻兮兮的一路setdefault