urllib question

Russell E. Owen rowen at cesmail.net
Tue Aug 26 17:12:33 EDT 2003


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




More information about the Python-list mailing list