urllib question

R

Russell E. Owen

I'm using urllib.urlopen() to retrieve data via ftp. I'm using Tkinter
to display progress and so using a file even handler to read the data.

My question is whether there's some easy way to do a nonblocking read?

Here's a brief summary of the code:

class ftpget:
def __init__(self, url):
self.bytesRead = 0
self.fromfp = urllib.urlopen(url)
tk = Tkinter.Frame().tk
tk.createfilehandler(self.fromfp, Tkinter.READABLE, readCallback)

def readCallback(self, *args):
nextData = self.fromfp.fp.fp._sock.recv(10000)
self.bytesRead += len(nextData)
if nextData:
# handle the data
else:
# clean up: close the connection, etc.

# display info in a Tkinter widget

This does the job, but the line that reads the data:
nextData = self.fromfp.fp.fp._sock.recv(10000)
is dreadful because it relies on undocumented internals inside the
object returned by urllib.urlopen.

The simplest way to read data is self.fromfp.read(maxbytes) and that's
how urllib.urlreceive reads data. But it's a blocking read and I'm
afraid a slow net will cause problems for my GUI.

Any suggestions?

-- Russell
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top