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

是否可以在不使用 Python 編寫的情況下讀取 FTP 文

Is it possible to read FTP files without writing them using Python?(是否可以在不使用 Python 編寫的情況下讀取 FTP 文件?)
本文介紹了是否可以在不使用 Python 編寫的情況下讀取 FTP 文件?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試使用 Python 的 ftplib 讀取文件而不寫入它們.大致相當(dāng)于:

I am trying to read files using Python's ftplib without writing them. Something roughly equivalent to:

def get_page(url):
    try:
        return urllib.urlopen(url).read()
    except:
        return ""

但使用 FTP.

我試過了:

def get_page(path):
    try:
        ftp = FTP('ftp.site.com', 'anonymous', 'passwd')
        return ftp.retrbinary('RETR '+path, open('page').read())
    except:
        return ''

但這不起作用.文檔中的唯一示例涉及使用 ftp.retrbinary('RETR README', open('README', 'wb').write) 格式編寫文件.是否可以不先寫入就讀取ftp文件?

but this doesn't work. The only examples in the docs involve writing files using the ftp.retrbinary('RETR README', open('README', 'wb').write) format. Is it possible to read ftp files without writing first?

推薦答案

好吧,答案就在眼前:FTP.retrbinary 方法接受作為第二個參數(shù)的函數(shù)引用每當(dāng)從 FTP 連接檢索文件內(nèi)容時都會調(diào)用它.

Well, you have the answer right in front of you: The FTP.retrbinary method accepts as second parameter a reference to a function that is called whenever file content is retrieved from the FTP connection.

這是一個簡單的例子:

#!/usr/bin/env python
from ftplib import FTP

def writeFunc(s):
  print "Read: " + s

ftp = FTP('ftp.kernel.org') 
ftp.login()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', writeFunc)

你應(yīng)該實現(xiàn) writeFunc 以便它實際上將讀取的數(shù)據(jù)附加到一個內(nèi)部變量,就像這樣,它使用一個可調(diào)用的對象:

You should implement writeFunc so that it actually appends the data read to an internal variable, something like this, which uses a callable object:

#!/usr/bin/env python
from ftplib import FTP

class Reader:
  def __init__(self):
    self.data = ""
  def __call__(self,s):
     self.data += s

ftp = FTP('ftp.kernel.org') 
ftp.login()
r = Reader()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r)

print r.data

更新:我意識到 Python 標(biāo)準(zhǔn)庫中有一個模塊專門用于此類事情,BytesIO:

Update: I realized that there is a module in the Python standard library that is meant for this kind of things, BytesIO:

#!/usr/bin/env python
from ftplib import FTP
from io import BytesIO

ftp = FTP('ftp.kernel.org') 
ftp.login()
r = BytesIO()
ftp.retrbinary('RETR /pub/README_ABOUT_BZ2_FILES', r.write)

print r.getvalue()

這篇關(guān)于是否可以在不使用 Python 編寫的情況下讀取 FTP 文件?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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:“浮動對象不可下標(biāo))
主站蜘蛛池模板: 精品久久视频 | 国产成人99久久亚洲综合精品 | 亚洲二三区| 国产成人精品毛片 | 一区二区三区蜜桃 | 久久久精品一区二区 | 91午夜精品亚洲一区二区三区 | 国产美女啪啪 | 亚洲精品伦理 | 精品欧美日韩 | 91午夜精品| 日韩国产中文字幕 | 日本黄网站 | 九色在线播放 | 日本一级黄色大片 | 黄色激情视频网站 | 操碰在线视频 | 一区二区三区免费 | 亚洲精品1| 一区二区在线看 | 亚洲免费视频一区 | 国产精品乱| 欧美性猛交一区二区三区精品 | 欧美一级淫片免费视频黄 | 国产精品网站在线观看 | 亚洲狠狠 | 亚洲免费在线视频 | 久久精品福利视频 | 青草av在线 | 操操操干干干 | 一本色道久久综合亚洲精品小说 | 欧美区一区二 | 日韩成人一区二区 | 国产成人精品一区二区三区福利 | 天天操天天干天天爽 | 日本在线视频一区 | 国产精品偷乱一区二区三区 | 中文字幕免费在线观看 | av不卡在线观看 | 精品国产一区二区在线观看 | 一区中文字幕 |