digitalocean使用Python簡易ftp服務器

digitalocean使用Python簡易ftp服務器

 

1、安裝Python,pip是Python包管理器安裝pip會自動安裝Python後續添加其他Python管理包會很簡單,下邊安裝ftp包就會用到。若已經安裝請看2步

Debian / Ubuntu:

apt-get install python-pip

CentOS:

yum install python-setuptools && easy_install pip

若如果安裝了Python沒有安裝pip,可以單獨安裝,下列命令是必須是在安裝了Python情況下執行:

官網安裝方法:https://pip.pypa.io/en/stable/installing/

2、以上命令執行完成就會安裝了,通過以下命令查看Python版本

python –V

3、安裝pyftpdlib,也就是Python的ftp工具,安裝命令如下:

pip install pyftpdlib

4、安裝完畢後,通過如下命令可以啓動匿名ftp服務器,後邊的21即指定端口號

python -m pyftpdlib -p 21

打開瀏覽器,輸入下邊連接即可打開,ip部分換成ftp所在電腦的IP地址,若是本機也可以使用ftp://localhost:21或ftp://127.0.0.1:21,

使用匿名用戶登錄:anonymous 密碼不用輸

ftp://ip:21

5、以上方式通過匿名方式登錄,只能讀取不能上傳文件,以下是一個Python腳本,保存到雲服務器的一個位置後,使用python pythonFTP.py 運行即可

from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
 
def main():
    # Instantiate a dummy authorizer for managing 'virtual' users
    authorizer = DummyAuthorizer()
 
    # Define a new user having full r/w permissions and a read-only
    # anonymous user
    authorizer.add_user('user', '12345', '.', perm='elradfmwM')
 
    # Instantiate FTP handler class
    handler = FTPHandler
    handler.authorizer = authorizer
 
    # Define a customized banner (string returned when client connects)
    handler.banner = "pyftpdlib based ftpd ready."
 
    # Specify a masquerade address and the range of ports to use for
    # passive connections.  Decomment in case you're behind a NAT.
    #handler.masquerade_address = '151.25.42.11'
    #handler.passive_ports = range(60000, 65535)
 
    # Instantiate FTP server class and listen on 0.0.0.0:2121
    address = ('', 2121)
    server = FTPServer(address, handler)
 
    # set a limit for connections
    server.max_cons = 256
    server.max_cons_per_ip = 5
 
    # start ftp server
    server.serve_forever()
 
if __name__ == '__main__':
    main()

以上add_user是添加用戶第一個參數是登錄名,第二個是密碼,第三個是ftp連接初始目錄,第四個是ftp對目錄的操作權限,詳細可以看下邊官方文檔。Address後邊兩個參數第一個是地址,可以輸入也可以不輸入,若輸入就輸入公網地址,2121是指定的端口,還有最大連接數max_cons,每個ip最大連接限制,更多的接口建議直接看docstrings:https://code.google.com/p/pyftpdlib/wiki/Tutorial

 

參考地址:http://www.cnblogs.com/yili16438/p/d3209323913c6d53e6060fcd8d27e4c0.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章