473,396 Members | 1,671 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,396 software developers and data experts.

Trouble with seek(0) command

I can't figure out why this script gets an error. This is script that gets
a web page, then parses the title out of the web page. When it's done
parsing, I would like to reset the pointer to the beginnning of the response
file object, but the seek(0) command does not work. Anybody know why?

The error message says: AttributeError: addinfourl instance has no attribute
'seek'

But in PythonWin IDE, when I type response and then a ".", the popup options
include both read and seek. What's going on?
Here's the code:

#!/usr/bin/python
import urllib2, string

def Title(response):
# Returns the title of a web page
page = response.read()
page = page[string.find(page,'<title>'):string.find(page,'</title>')]
page = page[string.find(page,'>')+1:]
response.seek(0) # This causes an error - WHY?
return page

strurl = 'http://www.gamatronix.com'
resp = urllib2.urlopen(strurl)
print Title(resp)
print resp.read() # Without the seek command, this will return nothing,
because the pointer is at the end

Please copy me on responses.

Thanks,
Randy
Jul 18 '05 #1
2 8999
Randy Gamage wrote:

I can't figure out why this script gets an error. This is script that gets
a web page, then parses the title out of the web page. When it's done
parsing, I would like to reset the pointer to the beginnning of the response
file object, but the seek(0) command does not work. Anybody know why?

The error message says: AttributeError: addinfourl instance has no attribute
'seek'

But in PythonWin IDE, when I type response and then a ".", the popup options
include both read and seek. What's going on?


PythonWin is probably lying to you.
Jul 18 '05 #2
"Randy Gamage" <ra***@gamages.com> writes:
I can't figure out why this script gets an error. This is script that gets
a web page, then parses the title out of the web page. When it's done
parsing, I would like to reset the pointer to the beginnning of the response
file object, but the seek(0) command does not work. Anybody know why?

[...]

urllib2's response objects just don't have a seek. They *can't* have
a seek without caching data, since the data in question is gettin read
from a socket: it just ain't there any more after you've .read() it!

You can just make sure you always keep data read from a response
object, so you can reuse it later -- but that is an annoyance.

If you want a response object that *does* cache, and allow seeking,
you could pinch seek_wrapper from ClientCookie
(http://wwwsearch.sf.net/):

response = seek_wrapper(urllib2.urlopen(url))
page = response.read()
....
response.seek(0)
....
I think Andrew Dalke has also posted a similar thing to seek_wrapper,
that only allows .seek(0). Called ReseekFile, or something similar.
Or just use ClientCookie itself:

from ClientCookie import build_opener, SeekableProcessor

o = build_opener(SeekableProcessor)

response = o.open(url)
page = response.read()
....
response.seek(0)
....

(or, if you prefer, you can ClientCookie.install_opener(o) so you can
do ClientCookie.urlopen(url) instead of o.open(url))
John
Jul 18 '05 #3

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

Similar topics

3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
3
by: Mark Denardo | last post by:
Does anyone have any good VB.NET example code that shows how to use the NOTIFY option using the mciSendString API and then handle the return value. The only examples I can find show the VB way...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
1
by: chrisdev3 | last post by:
I am using DAO360 and Seek command on table with MEMO field (indexed, but not primary key). It seems that Seek command searches only for 255 characters, not more. Am I right ? Is it a bug ?...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.