【问题标题】:is it passible to save image from link to ftp server directly using python [duplicate]是否可以使用python直接将图像从链接保存到ftp服务器[重复]
【发布时间】:2021-11-04 08:27:37
【问题描述】:

我想知道是否可以直接使用 python 将图像从链接保存到 ftp 服务器,而无需将它们下载到笔记本电脑然后将它们上传到 ftp,我尝试使用 ftplib 和请求但没有工作,并抛出此错误,

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 我尝试了 encoding="utf8" 和错误忽略选项,但没有工作

import requests

from ftplib import FTP as tp


url = "https://im0-tub-com.yandex.net/i?id=5fda7c40cfa53d048d169908bbbd7cb6&n=13"

rq =  requests.get(url).content

ftp = tp('host','username','password)
ftp.cwd("www")
    
with open(rq,'rb')  as f:
    ftp.storbinary("STOR "+"name.webp",f)
    ftp.quit()

【问题讨论】:

  • 如果可行,此代码将在上传到 FTP 之前将文件下载(到内存)。这是无法避免的。
  • tnx man 你节省了我的时间@Martin Prikryl

标签: python ftp


【解决方案1】:

感谢 Martin Prikryl,我能够通过在我的代码中编辑一些东西来解决它

import requests

from ftplib import FTP as tp


url = "https://im0-tub-com.yandex.net/i?id=5fda7c40cfa53d048d169908bbbd7cb6&n=13"

rq =  requests.get(url , stream=True).raw

ftp = tp('host','username','password)
ftp.cwd("www")
    
ftp.storbinary("STOR "+"name.webp",rq)
ftp.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-08
    • 2011-09-12
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2019-03-19
    相关资源
    最近更新 更多