Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33200

Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'none,': 0.05; 'python': 0.09; 'patches': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'release,': 0.09; 'supported.': 0.09; 'terry': 0.09; 'wrong,': 0.09; 'subject:not': 0.11; "skip:' 30": 0.15; 'slightly': 0.15; '3.1.3': 0.16; 'bug,': 0.16; 'capitalizes': 0.16; 'expects': 0.16; 'fixes.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subject:case': 0.16; 'url:example': 0.16; 'wrote:': 0.17; 'headers': 0.17; 'skip:u 30': 0.17; 'jan': 0.18; 'module': 0.19; 'skip:" 30': 0.20; 'tracker': 0.20; 'import': 0.21; 'supposed': 0.21; 'http': 0.22; 'thus': 0.24; 'header': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'skip:[ 10': 0.26; 'skip:" 20': 0.26; 'am,': 0.27; 'bugs': 0.27; 'header:X-Complaints-To:1': 0.28; 'noticed': 0.28; '3.1': 0.29; '>>>>': 0.29; 'once.': 0.29; 'questions.': 0.29; 'case,': 0.29; 'no,': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'gets': 0.32; "skip:' 20": 0.32; 'to:addr:python-list': 0.33; 'form.': 0.33; 'times.': 0.33; 'agree': 0.34; 'updated': 0.34; 'false': 0.35; 'open': 0.35; 'expected': 0.35; 'received:org': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'itself': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'latest': 0.61; 'time,': 0.62; 'request.': 0.64; 'behavior': 0.64; 'account': 0.67; '(yes,': 0.84; 'opener': 0.84; 'received:fios.verizon.net': 0.84; 'subject:updated': 0.84; 'suspected': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive
Date Mon, 12 Nov 2012 16:35:32 -0500
References <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <50a12949$0$6566$9b4e6d93@newsspool3.arcor-online.net>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3594.1352756164.27098.python-list@python.org> (permalink)
Lines 65
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352756164 news.xs4all.nl 6895 [2001:888:2000:d::a6]:35754
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33200

Show key headers only | View raw


On 11/12/2012 10:52 AM, Johannes Kleese wrote:
> Hi!
>
> (Yes, I did take a look at the issue tracker but couldn't find any
> corresponding bug, and no, I don't want to open a new account just for
> this one.)

You only have to open a tracker account just once. I am reluctant to 
report this myself as I do not use the module and cannot answer questions.

> I'm reusing a single urllib.request.Request object to HTTP-POST data to
> the same URL a number of times. While the data itself is sent as
> expected every time, the Content-Length header is not updated after the
> first request. Tested with Python 3.1.3 and Python 3.1.4.

3.1 only gets security fixes. Consider upgrading. In any case, suspected 
bugs need to be tested with the latest release, as patches get applied 
daily. As it happens,

import urllib.request
opener = urllib.request.build_opener()
request = urllib.request.Request("http://example.com/", headers =
         {"Content-Type": "application/x-www-form-urlencoded"})

opener.open(request, "1".encode("us-ascii"))
print(request.data, '\n', request.header_items())

opener.open(request, "123456789".encode("us-ascii"))
print(request.data, '\n', request.header_items())

exhibits the same behavior in 3.3.0 of printing ('Content-length', '1') 
in the last output. I agree that that looks wrong, but I do not know if 
such re-use is supposed to be supported.


> While at it, I noticed that urllib.request.Request.has_header() and
> .get_header() are case-sensitive,

Python is case sensitive.

> while HTTP headers are not (RFC 2616, 4.2).
 > Thus the following, slightly unfortunate behaviour:
>
>>>> request.header_items()
> [('Content-length', '1'), ('Content-type',
> 'application/x-www-form-urlencoded'), ('Host', 'example.com'),
> ('User-agent', 'Python-urllib/3.1')]
>
>>>> request.has_header("Content-Type")
> False
>>>> request.has_header("Content-type")
> True
>>>> request.get_header("Content-Type")
# this return None, which is not printed
>>>> request.get_header("Content-type")
> 'application/x-www-form-urlencoded'

Judging from 'Content-type', 'User-agent', 'Content-length', 'Host', 
urllib.request consistently capitalizes the first word of all header 
tags and expects them in that form. If that is not standard, it should 
be documented.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Johannes Kleese <j.kleese@arcor.de> - 2012-11-12 16:52 +0100
  Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Terry Reedy <tjreedy@udel.edu> - 2012-11-12 16:35 -0500
    Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Johannes Kleese <j.kleese@arcor.de> - 2012-11-13 08:24 +0100
  Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Terry Reedy <tjreedy@udel.edu> - 2012-11-12 20:58 -0500
  Re: Bugs: Content-Length not updated by reused urllib.request.Request / has_header() case-sensitive Terry Reedy <tjreedy@udel.edu> - 2012-11-27 18:24 -0500

csiph-web