urllib2 question

L

Laszlo Nagy

Hi All,

I would like to save a URL into a file. I need to manage cookies and use
my own HTTP headers so I'm using urllib2 and a custom openerdirector.
Here is a code fragment:

while True:
data = openerdirector.read(1024)
fd.write(data)
if not data:
break

The main reason for doing this is that the URL can contain big amounts
of data, and I do not want to store it in memory. The other way would be:

fd.write(openerdirector.read())

My question is that am I doing this the right way? I used the
openerdirector as a file here, but I'm not sure if it works like a file.
A file object should block when read() is called, and then return at
most 1024 bytes of data when available. It will return with an empty
string only after EOF reached. But is the same true for the
openerdirector instance? I did not find useful documentation in the
python docs about this. The documentation says:

*class OpenerDirector*( )

The OpenerDirector class opens URLs via BaseHandlers chained
together. It manages the chaining of handlers, and recovery from
errors.

But I'm not sure if it can be used as a file or not.

Thanks,

Laszlo
 

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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top