Django 模型:从 ftp 服务器下载镜像



首先对不起我的英语,这不是我的母语。我正在用python 2.7编写一个XML解析器。有一个图像链接,正在下载并保存到数据库中,它适用于http,但我需要从ftp服务器下载它们。这是从解析器调用的函数:

q=sa_web_images.objects.create(
    web_image_url=iphoto, 
    web_item_id=wi_id,
    non_repeat=non_repeat
)
q.get_remote_image()

其中"iphoto"是链接,例如:ftp://example.com/image.jpg。

这是将其保存到数据库中的函数:

def get_remote_image(self):
    if self.web_image_url and not self.web_image:
        result = urllib.urlretrieve(self.web_image_url)
        self.web_image.save(
            os.path.basename(self.web_image_url),
            File(open(result[0]))
            )
        self.save()

我知道ftp访问的用户名和密码,但我不知道,我应该如何将它们插入代码中,有人可以帮我吗?谢谢。

用户名和密码可以通过

在URL中指定来提供给urllib,例如

    result = urllib.urlretrieve("ftp://user:password@example.com/image.jpg")

最新更新