urllib question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell E. Owen

    urllib question

    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.createfileha ndler(self.from fp, Tkinter.READABL E, readCallback)

    def readCallback(se lf, *args):
    nextData = self.fromfp.fp. fp._sock.recv(1 0000)
    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(1 0000)
    is dreadful because it relies on undocumented internals inside the
    object returned by urllib.urlopen.

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

    Any suggestions?

    -- Russell
Working...