Created
December 10, 2012 11:58
-
-
Save pyokagan/4250200 to your computer and use it in GitHub Desktop.
For future reference: How to properly modify the URL of a urllib.request.Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import urllib.request | |
| from urllib.parse import urlsplit, urlunsplit, unquote | |
| class ModifyUrlHandler(urllib.request.BaseHandler): | |
| def __init__(self, target): | |
| x = urlsplit(target) | |
| self.full_url = urlunsplit(x._replace(fragment="")) | |
| self.fragment = x.fragment | |
| #Remove port from x.netloc if present for origin_req_host | |
| self.origin_req_host = x.netloc.partition(":")[0] | |
| self.host = unquote(x.netloc) | |
| self.selector = x.path | |
| if x.query: | |
| self.selector += "?" + x.query | |
| if not self.selector: | |
| self.selector = "/" | |
| self.type = x.scheme | |
| def http_request(self, req): | |
| req.full_url = self.full_url | |
| req.fragment = self.fragment | |
| req.origin_req_host = self.origin_req_host | |
| req.host = self.host | |
| req.selector = self.selector | |
| req.type = self.type | |
| req.unredirected_hdrs["Host"] = self.host | |
| return req | |
| if __name__ == "__main__": | |
| opener = urllib.request.build_opener(ModifyUrlHandler("http://httpbin.org/get?x=a")) | |
| r = opener.open("http://www.google.com") | |
| print(r.read().decode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment