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

A

askar.bektassov

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?
 
G

Georg Brandl

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
 
F

Fredrik Lundh

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

John J. Lee

Georg Brandl said:
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
 
A

askar.bektassov

Exactly what I needed!

import StringIO from StringIO
....
content=response.read()
forms=ParseFile(StringIO(content), response.geturl())
....

Thank you all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top