Index: Lib/urllib2.py =================================================================== --- Lib/urllib2.py (revision 62759) +++ Lib/urllib2.py (working copy) @@ -194,9 +194,9 @@ self.port = None self.data = data self.headers = {} + self.unredirected_hdrs = {} for key, value in headers.items(): self.add_header(key, value) - self.unredirected_hdrs = {} if origin_req_host is None: origin_req_host = request_host(self) self.origin_req_host = origin_req_host @@ -263,11 +263,17 @@ def add_header(self, key, val): # useful for something like authentication - self.headers[key.capitalize()] = val + norm_key = key.capitalize() + if norm_key in self.unredirected_hdrs: + del self.unredirected_hdrs[norm_key] + self.headers[norm_key] = val def add_unredirected_header(self, key, val): # will not be added to a redirected request - self.unredirected_hdrs[key.capitalize()] = val + norm_key = key.capitalize() + if norm_key in self.headers: + del self.headers[norm_key] + self.unredirected_hdrs[norm_key] = val def has_header(self, header_name): return (header_name in self.headers or Index: Doc/library/urllib2.rst =================================================================== --- Doc/library/urllib2.rst (revision 62759) +++ Doc/library/urllib2.rst (working copy) @@ -314,16 +314,19 @@ Add another header to the request. Headers are currently ignored by all handlers except HTTP handlers, where they are added to the list of headers sent to the server. Note that there cannot be more than one header with the same - name, and later calls will overwrite previous calls in case the *key* collides. - Currently, this is no loss of HTTP functionality, since all headers which have - meaning when used more than once have a (header-specific) way of gaining the - same functionality using only one header. + name (between both the headers and unredirected headers), and later calls will + overwrite previous calls in case the *key* collides. Currently, this is no + loss of HTTP functionality, since all headers which have meaning when used + more than once have a (header-specific) way of gaining the same functionality + using only one header. .. method:: Request.add_unredirected_header(key, header) - Add a header that will not be added to a redirected request. - + Add a header that will not be added to a redirected request. Calling this method + will overwrite a previous header with the same name, even if the previous header + was set using add_header(). + .. versionadded:: 2.4