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

Georg Brandl g.brandl-nospam at gmx.net
Wed Sep 27 15:39:57 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?

response = urlopen(url)
content = response.read()

forms = ParseResponse(content)

i.e., adapt ParseResponse to accept a string, or wrap the string in a
StringIO:

import cStringIO
forms = ParseResponse(cStringIO.StringIO(content))

Georg



More information about the Python-list mailing list