首先,对不起我的英语,这不是我的母语。我正在用python2.7编写XML解析器。有一个图片的链接,正在下载和保存到DB,它运行良好的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。
下面是将其保存到DB中的函数:
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访问的用户名和密码,但我不知道如何将它们插入代码中,有人能帮我吗?谢谢。
发布于 2014-12-21 15:20:57
通过在url中指定用户名和密码,可以将用户名和密码提供给urllib,例如
result = urllib.urlretrieve("ftp://user:password@example.com/image.jpg")https://stackoverflow.com/questions/27590524
复制相似问题