[docs] [issue12955] urllib2.build_opener().open() is not friendly to "with ... as:"

Valery Khamenya report at bugs.python.org
Fri Sep 16 20:15:26 CEST 2011


Valery Khamenya <khamenya at gmail.com> added the comment:

Terry, Senthil, thanks, for replying to this ticket. OK, to the question:

1. @Terry, here is the full example as for CPython 2.7 I am talking about and the output:

#################

from urllib2 import Request, build_opener

request = Request('http://example.com')

with build_opener().open(request) as f:
    txt = f.read()
    print '%d characters fetched' % len(txt)
####################

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: addinfourl instance has no attribute '__exit__'


2. @Senthil, regarding the statement being a feature. I assume, that to open a connection, to read from a connection and to close it -- are the fundamental functions for what urllib2 was in particular created for. I was looking in docs for some hints of "canonical" way of doing this using urllib2.opener. After I have failed to find any guidance in docs, I've created this ticket. That is, I assume that no new feature is needed, but just a documented 5-lines example about a typical way of doing the above operations, especially *closing* the connection.

regards,
Valery

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12955>
_______________________________________


More information about the docs mailing list