2014-02-11 249 views
1

我在FTP服務器中有一個文本文件。我怎麼能夠直接在服務器上打開這個文件?我有以下內容,我可以登錄,但我知道如何打開文件有問題。我是一個巨大的Python noob,所以我很抱歉如果這是一個非常明顯的問題。如何在Python中讀取FTP服務器上的文本文件

from ftplib import FTP 
ftp = FTP('127.0.0.1') 
ftp.login('USERNAME', 'PASS') 
op = ftp.retrbinary('RETR file.txt', open('file.txt', 'r')) 
txt = op.read() 
print txt 
+0

重複的https://stackoverflow.com/questions/11208957/is-it-possible-to-read-ftp-files-without-writing-them-using-python? – pseudocubic

+0

謝謝!這有幫助! –

回答

-1

你不能用ftp打開一個文件,它只是一個文件傳輸協議,而不是一個shell。我會建議添加一個SSH服務器,並像這樣訪問它。

如果您使用Ubuntu作爲服務器使用:

sudo apt-get install openssh-server 

那麼你就可以訪問它形成可以看到它的任何計算機。

安裝客戶端軟件(Ubuntu的)

sudo apt-get install openssh-client 

然後,

ssh [email protected] 

其中用戶名是帳戶在該服務器上的用戶名和主機或者是URL(如www.site。 COM)或IP地址(如192.168.2.5)

或Windows使用膩子

http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

相關問題