Python搭建FTP服務(wù)器的方法示例
Python版本 3.6.2
使用的ftp包:pyftpdlib pip install pyftpdlib就可以下載安裝了
FTP協(xié)議下載上傳文件在文件過(guò)大的情況下會(huì)比HTTP更具有優(yōu)勢(shì),更為方便的實(shí)現(xiàn)斷點(diǎn)上傳和進(jìn)度監(jiān)控,下面是官方文檔中的
基本方法
import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers import FTPServer def main(): # 實(shí)例化用戶(hù)授權(quán)管理 authorizer = DummyAuthorizer() authorizer.add_user('user', '12345', 'path', perm='elradfmwMT')#添加用戶(hù) 參數(shù):username,password,允許的路徑,權(quán)限 authorizer.add_anonymous(os.getcwd())#這里是允許匿名用戶(hù),如果不允許刪掉此行即可 # 實(shí)例化FTPHandler handler = FTPHandler handler.authorizer = authorizer # 設(shè)定一個(gè)客戶(hù)端鏈接時(shí)的標(biāo)語(yǔ) 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ù)器實(shí)例 # set a limit for connections server.max_cons = 256 server.max_cons_per_ip = 5 # 開(kāi)啟服務(wù)器 server.serve_forever() if __name__ == '__main__': main()
開(kāi)啟ftp服務(wù)器后要確定防火墻開(kāi)啟了21,20端口,并且在客戶(hù)端的瀏覽器中設(shè)置internet選項(xiàng)高級(jí)選項(xiàng)卡中的被動(dòng)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é)一個(gè)塊 ftp.set_debuglevel(0) ftp.close()
FTP服務(wù)器事件回調(diào)函數(shù):
class MyHandler(FTPHandler): def on_connect(self):#鏈接時(shí)調(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):#登錄時(shí)調(diào)用 # do something when user login pass def on_logout(self, username):#登出時(shí)調(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):#下載文件時(shí)調(diào)用 # do something when a file is partially sent pass def on_incomplete_file_received(self, file):#上傳文件時(shí)調(diào)用 # remove partially uploaded files import os os.remove(file)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Ubuntu下Python+Flask分分鐘搭建自己的服務(wù)器教程
- 在阿里云服務(wù)器上配置CentOS+Nginx+Python+Flask環(huán)境
- Python中使用Flask、MongoDB搭建簡(jiǎn)易圖片服務(wù)器
- Python使用socketServer包搭建簡(jiǎn)易服務(wù)器過(guò)程詳解
- Python3搭建http服務(wù)器的實(shí)現(xiàn)代碼
- Python Web程序搭建簡(jiǎn)單的Web服務(wù)器
- Python3 jupyter notebook 服務(wù)器搭建過(guò)程
- 使用python實(shí)現(xiàn)快速搭建簡(jiǎn)易的FTP服務(wù)器
- Python3之簡(jiǎn)單搭建自帶服務(wù)器的實(shí)例講解
- 用Python一鍵搭建Http服務(wù)器的方法
- Python搭建HTTP服務(wù)器和FTP服務(wù)器
- Python 搭建Web站點(diǎn)之Web服務(wù)器與Web框架
- Python 利用flask搭建一個(gè)共享服務(wù)器的步驟
相關(guān)文章
教你如何使用Python快速爬取需要的數(shù)據(jù)
學(xué)點(diǎn)數(shù)據(jù)爬蟲(chóng)基礎(chǔ)能讓繁瑣的數(shù)據(jù)CV工作(Ctrl+C,Ctrl+V)成為自動(dòng)化就足夠了.作為一名數(shù)據(jù)分析師而并非開(kāi)發(fā)工程師,需要掌握的爬蟲(chóng)必備的知識(shí)內(nèi)容,能獲取需要的數(shù)據(jù)即可 ,需要的朋友可以參考下2021-06-06總結(jié)Python函數(shù)參數(shù)的六種類(lèi)型
這篇文章主要總結(jié)了Python函數(shù)參數(shù)的六種類(lèi)型,傳遞參數(shù)實(shí)現(xiàn)不同場(chǎng)景的靈活使用,下面總結(jié)的六種函數(shù)參數(shù)類(lèi)型,需要的小伙伴可以參考一下2022-03-03PyTorch上實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN的方法
本篇文章主要介紹了PyTorch上實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)CNN的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04