How do I make urllib2 preserve case in HTTP header names?

tepples at spamcop.net tepples at spamcop.net
Wed Oct 10 11:52:17 EDT 2007


The HTTP specification, RFC 2616, states that field names in HTTP
headers are case insensitive.  But I'm trying to use Python 2.5.1's
urllib2 to interact with a web service offered by Amazon.com, which
deviates from the HTTP spec in that it requires use of case-sensitive
header names ReportName, ReportID, and NumberOfReports.  I try to send
an HTTP header named "NumberOfReports", but it comes out mangled as
"Numberofreports'.  What is the best way to use Python 2.5.1 on
Windows Server 2003 to create HTTP or HTTPS requests that do not
mangle the case of header field names?

Test case follows:
==========
"""
To see how Python breaks the CaseSensitiveName, go to
command prompt and start netcat listening on the HTTP port:

nc -l -p 80 localhost

Then run this program on the same machine.  You will see that
CaseSensitiveName has become Casesensitivename.
"""

import urllib2

req = urllib2.Request("http://localhost/", "")
req.add_header('CaseSensitiveName', 'CaseSensitiveValue')
infp = urllib2.urlopen(req)
data = infp.read()
infp.close()
print data




More information about the Python-list mailing list