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

John J. Lee jjl at pobox.com
Wed Sep 27 17:22:46 EDT 2006


Georg Brandl <g.brandl-nospam at gmx.net> writes:

> 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))

That won't work, because a StringIO does not support the 'response'
interface required of ParseResponse's argument.

You may use ClientForm.ParseFile on the StringIO instead.  Use
response.geturl() to get the URL that ParseFile wants.


John



More information about the Python-list mailing list