久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我可以使用 Python 將內存中的對象上傳到 FTP 嗎

Can I upload an object in memory to FTP using Python?(我可以使用 Python 將內存中的對象上傳到 FTP 嗎?)
本文介紹了我可以使用 Python 將內存中的對象上傳到 FTP 嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

這是我現在正在做的事情:

Here's what I'm doing now:

mysock = urllib.urlopen('http://localhost/image.jpg')
fileToSave = mysock.read()
oFile = open(r"C:image.jpg",'wb')
oFile.write(fileToSave)
oFile.close
f=file('image.jpg','rb')
ftp.storbinary('STOR '+os.path.basename('image.jpg'),f)
os.remove('image.jpg')

將文件寫入磁盤然后立即刪除它們似乎是系統上應該避免的額外工作.我可以使用 Python 將內存中的對象上傳到 FTP 嗎?

Writing files to disk and then imediately deleting them seems like extra work on the system that should be avoided. Can I upload an object in memory to FTP using Python?

推薦答案

因為 duck-typing,文件對象(代碼中的f)只需要支持.read(blocksize)調用就可以使用storbinary.當遇到這樣的問題時,我會去源頭,在本例中是 lib/python2.6/ftplib.py:

Because of duck-typing, the file object (f in your code) only needs to support the .read(blocksize) call to work with storbinary. When faced with questions like this, I go to the source, in this case lib/python2.6/ftplib.py:

def storbinary(self, cmd, fp, blocksize=8192, callback=None):
    """Store a file in binary mode.  A new port is created for you.

    Args:
      cmd: A STOR command.
      fp: A file-like object with a read(num_bytes) method.
      blocksize: The maximum data size to read from fp and send over
                 the connection at once.  [default: 8192]
      callback: An optional single parameter callable that is called on
                on each block of data after it is sent.  [default: None]

    Returns:
      The response code.
    """
    self.voidcmd('TYPE I')
    conn = self.transfercmd(cmd)
    while 1:
        buf = fp.read(blocksize)
        if not buf: break
        conn.sendall(buf)
        if callback: callback(buf)
    conn.close()
    return self.voidresp()

正如評論,它只需要一個類文件對象,實際上它甚至不是特別像文件,它只需要 read(n).StringIO 提供了這樣的內存文件"服務.

As commented, it only wants a file-like object, indeed it not even be particularly file-like, it just needs read(n). StringIO provides such "memory file" services.

這篇關于我可以使用 Python 將內存中的對象上傳到 FTP 嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 97精品超碰一区二区三区 | 国产精品久久久99 | 99久久婷婷国产综合精品电影 | 精品久久久久久久久久久 | 91精品国产高清一区二区三区 | 国产精品免费在线 | 伊人网综合在线观看 | 91在线观看免费视频 | 亚洲一区二区三区四区五区中文 | 日日夜夜精品视频 | 亚洲成人免费 | 国产精品久久久久久久久久三级 | 91偷拍精品一区二区三区 | 黄色片大全在线观看 | 精品久久一区 | 欧美福利 | 精品国产精品国产偷麻豆 | 播放一级黄色片 | 成人免费网视频 | 99精品视频在线观看 | 91色视频在线观看 | 国产精品久久久久久久久久久新郎 | 美女激情av | 天天艹天天干天天 | 亚洲久久 | 亚洲一区在线日韩在线深爱 | 久草免费在线视频 | 国产人成精品一区二区三 | 亚洲三区视频 | 在线视频a| 日韩精品在线免费观看视频 | 草久在线 | 国产精品久久一区二区三区 | aa级毛片毛片免费观看久 | av电影手机在线看 | 久草在线| www.天天操 | 精品欧美一区二区三区久久久 | 毛片一级电影 | 亚州av| 久久精品国产亚洲一区二区三区 |