1

我在使用 python 下载管理器时遇到了困难。我已经尝试过仅使用 wget 下载并且它有效。我还创建了我的 wxpython 界面。但我现在的问题是如何将两者结合起来?如何将 wget 下载代码添加到我的 wxpython 界面并使其工作?是否可以将 wget 与 python 结合起来提供下载管理器,例如 winwget 或 visualwget?


import os
from ftplib import FTP

ftp = FTP("ftpsite","username", "password")
ftp.login()
ftp.retrlines("LIST")

ftp.cwd("folderOne")
ftp.cwd("subFolder")

listing = []
ftp.retrlines("LIST", listing.append)
words = listing[0].split(None, 8)
filename = words[-1].lstrip()

#download the file
local_filename = os.path.join(r"C:\example", file)
lf = open(local_filename, "wb")
ftp.retrbinary("RETR " + filename, lf.write, 8*1024)
lf.close()

我试过这段代码,它来自你的博客。但它说,

Traceback (most recent call last):
  File "directory", line 4, in <module>
    ftp = FTP("ftp://samoa.gsfc.nasa.gov/site/", "user", "password")
  File "C:\Python27\lib\ftplib.py", line 117, in __init__
    self.connect(host)
  File "C:\Python27\lib\ftplib.py", line 132, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
gaierror: [Errno 11004] getaddrinfo failed

代码有什么问题?

4

1 回答 1

1

All you need to do is use event handlers. For example, you could have a text control where you copy and paste the download URL. Then you would have a button to add that download to a ListCtrl or better, an ObjectListview widget. Now you have a way of showing a list of downloads.

You could start the download when you add the item or start all the downloads with a separate button. Or you could use the second button to download stuff in order instead of in parallel. Since downloading a file is a long running process, you'll want to do the downloading part inside a thread. You should check out one of the following links for details on that:

You might also find this simple downloading example useful: http://wiki.wxpython.org/DownloadWidget

This old thread also addresses some of your questions: http://wxpython-users.1045709.n5.nabble.com/wxPython-Python-equivalent-to-wget-lt-url-gt-td2358484.html

And then there's this tutorial on just downloading files with Python: http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/

于 2013-06-19T19:39:33.920 回答