How to read() twice from file-like objects (and get some data)?

Fredrik Lundh fredrik at pythonware.com
Wed Sep 27 15:47:33 EDT 2006


askar.bektassov at gmail.com wrote:

> Hello all, please correct me, if I do...
> 
> from ClientForm import ParseResponse
> from urllib2 import urlopen
> ...
> response=urlopen(url)
> forms=ParseResponse(response)
> 
> I won't be able to
> 
> print response.read()
> 
> in order to see HTML source anymore. In other words I will see only an
> empty string. Suggestions?

the server is sending you a stream of bytes; there's no way to "rewind" 
that without issuing another request.

if you want to use the data multiple times, you'll have to save it in
a buffer, and parse it from there (use StringIO to wrap a string in a 
file-like object).

</F>




More information about the Python-list mailing list