Python搭建FTP服務(wù)器的方法示例
Python版本 3.6.2
使用的ftp包:pyftpdlib pip install pyftpdlib就可以下載安裝了
FTP協(xié)議下載上傳文件在文件過大的情況下會比HTTP更具有優(yōu)勢,更為方便的實現(xiàn)斷點上傳和進(jìn)度監(jiān)控,下面是官方文檔中的
基本方法
import os
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
def main():
# 實例化用戶授權(quán)管理
authorizer = DummyAuthorizer()
authorizer.add_user('user', '12345', 'path', perm='elradfmwMT')#添加用戶 參數(shù):username,password,允許的路徑,權(quán)限
authorizer.add_anonymous(os.getcwd())#這里是允許匿名用戶,如果不允許刪掉此行即可
# 實例化FTPHandler
handler = FTPHandler
handler.authorizer = authorizer
# 設(shè)定一個客戶端鏈接時的標(biāo)語
handler.banner = "pyftpdlib based ftpd ready."
#handler.masquerade_address = '151.25.42.11'#指定偽裝ip地址
#handler.passive_ports = range(60000, 65535)#指定允許的端口范圍
address = (ipaddr, 21)#FTP一般使用21,20端口
server = FTPServer(address, handler)#FTP服務(wù)器實例
# set a limit for connections
server.max_cons = 256
server.max_cons_per_ip = 5
# 開啟服務(wù)器
server.serve_forever()
if __name__ == '__main__':
main()
開啟ftp服務(wù)器后要確定防火墻開啟了21,20端口,并且在客戶端的瀏覽器中設(shè)置internet選項高級選項卡中的被動ftp的勾去掉之后才能登陸到ftp服務(wù)器
從Windows登錄到服務(wù)器:

利用Python從ftp服務(wù)器上下載文件
from ftplib import FTP
ftp=FTP()
ftp.connect('localhost',21)#localhost改成服務(wù)器ip地址
ftp.login(user='user',passwd='12345')
file=open('f://ftpdownload/test.txt','wb')
ftp.retrbinary("RETR test.txt",file.write,1024)#從服務(wù)器上下載文件 1024字節(jié)一個塊
ftp.set_debuglevel(0)
ftp.close()
FTP服務(wù)器事件回調(diào)函數(shù):
class MyHandler(FTPHandler):
def on_connect(self):#鏈接時調(diào)用
print "%s:%s connected" % (self.remote_ip, self.remote_port)
def on_disconnect(self):#關(guān)閉連接是調(diào)用
# do something when client disconnects
pass
def on_login(self, username):#登錄時調(diào)用
# do something when user login
pass
def on_logout(self, username):#登出時調(diào)用
# do something when user logs out
pass
def on_file_sent(self, file):#文件下載后調(diào)用
# do something when a file has been sent
pass
def on_file_received(self, file):#文件上傳后調(diào)用
# do something when a file has been received
pass
def on_incomplete_file_sent(self, file):#下載文件時調(diào)用
# do something when a file is partially sent
pass
def on_incomplete_file_received(self, file):#上傳文件時調(diào)用
# remove partially uploaded files
import os
os.remove(file)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Ubuntu下Python+Flask分分鐘搭建自己的服務(wù)器教程
- 在阿里云服務(wù)器上配置CentOS+Nginx+Python+Flask環(huán)境
- Python中使用Flask、MongoDB搭建簡易圖片服務(wù)器
- Python使用socketServer包搭建簡易服務(wù)器過程詳解
- Python3搭建http服務(wù)器的實現(xiàn)代碼
- Python Web程序搭建簡單的Web服務(wù)器
- Python3 jupyter notebook 服務(wù)器搭建過程
- 使用python實現(xiàn)快速搭建簡易的FTP服務(wù)器
- Python3之簡單搭建自帶服務(wù)器的實例講解
- 用Python一鍵搭建Http服務(wù)器的方法
- Python搭建HTTP服務(wù)器和FTP服務(wù)器
- Python 搭建Web站點之Web服務(wù)器與Web框架
- Python 利用flask搭建一個共享服務(wù)器的步驟
相關(guān)文章
教你如何使用Python快速爬取需要的數(shù)據(jù)
學(xué)點數(shù)據(jù)爬蟲基礎(chǔ)能讓繁瑣的數(shù)據(jù)CV工作(Ctrl+C,Ctrl+V)成為自動化就足夠了.作為一名數(shù)據(jù)分析師而并非開發(fā)工程師,需要掌握的爬蟲必備的知識內(nèi)容,能獲取需要的數(shù)據(jù)即可 ,需要的朋友可以參考下2021-06-06
總結(jié)Python函數(shù)參數(shù)的六種類型
這篇文章主要總結(jié)了Python函數(shù)參數(shù)的六種類型,傳遞參數(shù)實現(xiàn)不同場景的靈活使用,下面總結(jié)的六種函數(shù)參數(shù)類型,需要的小伙伴可以參考一下2022-03-03
PyTorch上實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN的方法
本篇文章主要介紹了PyTorch上實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

