|
25 | 25 | import urllib2 |
26 | 26 |
|
27 | 27 | class SmartRedirectHandler(urllib2.HTTPRedirectHandler): |
28 | | - def http_error_301(self, req, fp, code, msg, headers): |
29 | | - result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers) |
30 | | - |
| 28 | + def common_http_redirect(self, result, headers, code): |
31 | 29 | if "location" in headers: |
32 | 30 | result.redurl = headers.getheaders("location")[0].split("?")[0] |
33 | 31 | elif "uri" in headers: |
34 | 32 | result.redurl = headers.getheaders("uri")[0].split("?")[0] |
35 | 33 |
|
| 34 | + result.redcode = code |
| 35 | + |
36 | 36 | return result |
37 | 37 |
|
| 38 | + def http_error_301(self, req, fp, code, msg, headers): |
| 39 | + result = urllib2.HTTPRedirectHandler.http_error_301(self, req, fp, code, msg, headers) |
| 40 | + return self.common_http_redirect(result, headers, code) |
| 41 | + |
38 | 42 | def http_error_302(self, req, fp, code, msg, headers): |
39 | 43 | result = urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers) |
40 | | - |
41 | | - if "location" in headers: |
42 | | - result.redurl = headers.getheaders("location")[0].split("?")[0] |
43 | | - elif "uri" in headers: |
44 | | - result.redurl = headers.getheaders("uri")[0].split("?")[0] |
45 | | - |
46 | | - return result |
| 44 | + return self.common_http_redirect(result, headers, code) |
0 commit comments