473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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?

Sep 27 '06 #1
4 4540
as*************@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
Sep 27 '06 #2
as*************@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>

Sep 27 '06 #3
Georg Brandl <g.*************@gmx.netwrites:
as*************@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
Sep 27 '06 #4
Exactly what I needed!

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

Thank you all!

Sep 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BillH | last post by:
I have an ASP page to download a CSV file using the following code Response.ContentType="text/plain Response.AddHeader "Content-disposition", "attachment; filename=Report.csv Response.Write...
7
by: Patrick Ruhnow | last post by:
Hello, i have an ASP Page that generates a PDF document. After the generation i save the generated document as a file. all works fine. the only strange thing is, that my document is stored...
0
by: Peter Stojkovic | last post by:
I make a search for a spical word : ( here IRefreshActivData ) My Development-Enviroment finds everything TWICE in file root111 See example: twotimes line 35 twotime line 248 The file...
7
by: Douglas | last post by:
I have a frontend and backend database , program.mde and data.mdb on a client PC. How do i prevent them launching the program.mde more than once on a single PC? Somtimes they minizmise the program...
8
by: noleander | last post by:
I've got two threads reading from one disk file (I'm using the Pthread library). I need each thread to have its own file seek pointer, so they can read from independent parts of the file. I...
2
by: Sharon | last post by:
Hi all. I have an aspx page that starts a download: Response.WriteFile(file). The problem is that the Open / Save dialog appears twice when i choose Open. Any idea why? 10x. Sharon.
4
by: Vikram | last post by:
i am using following code in button click... of asp.net code behind page Response.Clear() 'clears the current output content from the buffer Response.AppendHeader("Content-Disposition", _ ...
2
by: Kiuhnm | last post by:
I am having trouble with the function Add4Ptrs_v2. I thought "twice< add_pointer<_>, _ >" was a perfectly valid metafunction, but the following code does not work: >>>> #include <iostream> ...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
9
by: Prakash Singh Bhakuni | last post by:
am replacing the default "Browse..." button for input type=file. This works fine except that the form will only submit after the SUBMIT button is clicked twice. Any ideas on why this is happening...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.