homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author karlcow
Recipients karlcow
Date 2014年09月24日.08:22:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1411546927.8.0.816080375663.issue22478@psf.upfronthosting.co.za>
In-reply-to
Content
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'}
History
Date User Action Args
2014年09月24日 08:22:07karlcowsetrecipients: + karlcow
2014年09月24日 08:22:07karlcowsetmessageid: <1411546927.8.0.816080375663.issue22478@psf.upfronthosting.co.za>
2014年09月24日 08:22:07karlcowlinkissue22478 messages
2014年09月24日 08:22:07karlcowcreate

AltStyle によって変換されたページ (->オリジナル) /