Trouble with seek(0) command

John J. Lee jjl at pobox.com
Sat Nov 8 07:49:26 EST 2003


"Randy Gamage" <randy at gamages.com> writes:
> I can't figure out why this script gets an error.  This is script that gets
> a web page, then parses the title out of the web page.  When it's done
> parsing, I would like to reset the pointer to the beginnning of the response
> file object, but the seek(0) command does not work.  Anybody know why?
[...]

urllib2's response objects just don't have a seek.  They *can't* have
a seek without caching data, since the data in question is gettin read
from a socket: it just ain't there any more after you've .read() it!

You can just make sure you always keep data read from a response
object, so you can reuse it later -- but that is an annoyance.

If you want a response object that *does* cache, and allow seeking,
you could pinch seek_wrapper from ClientCookie
(http://wwwsearch.sf.net/):

response = seek_wrapper(urllib2.urlopen(url))
page = response.read()
...
response.seek(0)
...


I think Andrew Dalke has also posted a similar thing to seek_wrapper,
that only allows .seek(0).  Called ReseekFile, or something similar.


Or just use ClientCookie itself:

from ClientCookie import build_opener, SeekableProcessor

o = build_opener(SeekableProcessor)

response = o.open(url)
page = response.read()
...
response.seek(0)
...

(or, if you prefer, you can ClientCookie.install_opener(o) so you can
do ClientCookie.urlopen(url) instead of o.open(url))


John




More information about the Python-list mailing list