環境:
- win7 旗艦版
- python @3.6.1
- pyftpdlib @1.5.4
python客戶端上官網下載:python 安裝的時候勾上pip還有add path 環境變量。
pyftpdlib模塊
可用pip
安裝:
pip(3) install pyftpdlib
pyftpdlib
Github項目鏈接:https://github.com/giampaolo/pyftpdlib/
下面給出一個基礎的例子:
開啟一個ftp服務器,用戶user
,密碼12345
,禁止匿名登錄
import os
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='elradfmwMT')
#authorizer.add_anonymous(os.getcwd())
# 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 = ('127.0.0.1', 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()
要開啟請執行:
python server.py
看到如下界面說明開啟成功。
資源管理器打開:
ftp://127.0.0.1:2121
成功訪問。更多內容可參考官方文檔。