karl added the comment:

ok let's see

→ ./python.exe -m unittest -v 
Lib.test.test_urllib2net.OtherNetworkTests.test_custom_headers
test_custom_headers (Lib.test.test_urllib2net.OtherNetworkTests) ... FAIL

======================================================================
FAIL: test_custom_headers (Lib.test.test_urllib2net.OtherNetworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/karl/code/cpython/Lib/test/test_urllib2net.py", line 186, in 
test_custom_headers
    self.assertEqual(request.get_header('User-agent'), 'Test-Agent')
AssertionError: 'Python-urllib/3.4' != 'Test-Agent'
- Python-urllib/3.4
+ Test-Agent


----------------------------------------------------------------------
Ran 1 test in 0.551s

FAILED (failures=1)


→ ./python.exe
Python 3.4.2rc1+ (3.4:8eb4eec8626c+, Sep 23 2014, 21:53:11) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> url = 'http://127.0.0.1/'
>>> opener = urllib.request.build_opener()
>>> request = urllib.request.Request(url)
>>> request.header_items()
[]
>>> request.headers
{}
>>> request.add_header('User-Agent', 'Test-Agent')
>>> request.headers
{'User-agent': 'Test-Agent'}
>>> request.header_items()
[('User-agent', 'Test-Agent')]
>>> opener.open(request)
<http.client.HTTPResponse object at 0x10c0aedc0>
>>> request.get_header('User-agent'), 'Test-Agent'
('Test-Agent', 'Test-Agent')
>>> request.header_items()
[('User-agent', 'Test-Agent'), ('Host', '127.0.0.1')]
>>> request.headers
{'User-agent': 'Test-Agent'}


OK so far so good.
And my server recorded 
127.0.0.1 - - [24/Sep/2014:17:07:41 +0900] "GET / HTTP/1.1" 200 9897 "-" 
"Test-Agent"


Let's do it the way, the test has been designed.

→ ./python.exe
Python 3.4.2rc1+ (3.4:8eb4eec8626c+, Sep 23 2014, 21:53:11) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib.request
>>> url = 'http://127.0.0.1/'
>>> opener = urllib.request.build_opener()
>>> request = urllib.request.Request(url)
>>> request.header_items()
[]
>>> opener.open(request)
<http.client.HTTPResponse object at 0x10e05aa80>
>>> request.header_items()
[('User-agent', 'Python-urllib/3.4'), ('Host', '127.0.0.1')]
>>> request.has_header('User-agent')
True
>>> request.add_header('User-Agent', 'Test-Agent')
>>> opener.open(request)
<http.client.HTTPResponse object at 0x10e05ab50>
>>> request.get_header('User-agent'), 'Test-Agent'
('Python-urllib/3.4', 'Test-Agent')
>>> request.add_header('Foo', 'bar')
>>> request.header_items()
[('User-agent', 'Test-Agent'), ('Host', '127.0.0.1'), ('Foo', 'bar')]
>>> opener.open(request)
<http.client.HTTPResponse object at 0x10e05ad58>
>>> request.header_items()
[('User-agent', 'Test-Agent'), ('Host', '127.0.0.1'), ('Foo', 'bar')]
>>> request.get_header('User-agent'), 'Test-Agent'
('Python-urllib/3.4', 'Test-Agent')
>>> request.headers
{'User-agent': 'Test-Agent', 'Foo': 'bar'}


And the server recorded.

127.0.0.1 - - [24/Sep/2014:17:12:52 +0900] "GET / HTTP/1.1" 200 9897 "-" 
"Python-urllib/3.4"
127.0.0.1 - - [24/Sep/2014:17:12:52 +0900] "GET / HTTP/1.1" 200 9897 "-" 
"Python-urllib/3.4"
127.0.0.1 - - [24/Sep/2014:17:14:15 +0900] "GET / HTTP/1.1" 200 9897 "-" 
"Python-urllib/3.4"

So it seems that User-Agent is immutable once it has been set the first time. 
Not in  the same dictionary.


>>> request.unredirected_hdrs
{'User-agent': 'Python-urllib/3.4', 'Host': '127.0.0.1'}

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue22478>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to