Hooking up GUI interface with asynchronous (s)ftp

2019-07-26 09:33发布

问题:

Trying to implement a progress dialog window for file uploads that look like a cross between IE download dialog and Firefox download dialog with a python GUI library on Windows.

  1. What asynchronous (S)FTP libraries are there for python? Ideally I should be able to do file upload resumes and track the progress of each parallel file uploads.
  2. If I'm running each file upload in a separate process each, how would I get the upload status and display it in a progress bar dialog?

回答1:

"ftplib" is the standard ftp library built in to Python. In Python 2.6, it had a callback parameter added to the method used for uploading.

That callback is a function you provide to the library; it is called once for every block that is completed.

Your function can send a message to the GUI (perhaps on a different thread/process, using standard inter-thread or inter-process communications) to tell it to update its progress bar.

Reference



回答2:

If you data transfer runs in a separate thread from the GUI, you can use wx.CallAfter() whenever you have to update you progress bar from the data transfer thread.

First, using CallAfter() is mandatory as wxPython function cannot be called from child threads.

Second, this will decouple the execution of the data transfer from the GUI in the main thread.

Note that CallAfter() only works for threads, not for separate processes. In that case, using the multiprocessing package should help.



回答3:

If you want a complete example of how to use threads and events to update your GUI with long running tasks using WxPython have a look at this page. This tutorial is quite useful and helped me perform a similar program than yours.



回答4:

If you can't use Python 2.6's ftplib, there is a company offering a commercial solution.

Chilkat's CKFTP2 costs several hundreds of dollars, but promises to work with Python 2.5, and offers a function call get_AsyncBytesSent() which returns the information you need. (I didn't see a callback, but it may offer that too.)

I haven't used this product.

Also consider that if FTP proves to be too hard/expensive, you could always switch to HTTP uploads instead. Chilkat have a free HTTP/HTTPS upload library.