from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler,ThrottledDTPHandler from pyftpdlib.servers import FTPServer from pyftpdlib.log import LogFormatter import logging import configparser import pyftpdlib logger = logging.getLogger() logger.setLevel(logging.INFO) ch = logging.StreamHandler() fh = logging.FileHandler(filename='myftpserver.log',encoding='GBK') ch.setFormatter(LogFormatter()) fh.setFormatter(LogFormatter()) logger.addHandler(ch) logger.addHandler(fh) authorizer = DummyAuthorizer() authorizer.add_user("user", "12345", "d:/", perm="elradfmw") ENABLE_ANONYMOUS = 'on' if ENABLE_ANONYMOUS == 'on': authorizer.add_anonymous("d:/") handler = FTPHandler handler.authorizer = authorizer handler.passive_ports = range(8300, 8500) dtp_handler = ThrottledDTPHandler upload = 100 * 1024 #100kb/s download = 100 * 1024 #100kb/s dtp_handler = ThrottledDTPHandler dtp_handler.read_limit = download dtp_handler.write_limit = upload ip = '0.0.0.0' port = '21' server = FTPServer((ip, port), handler) Max_con = 100 server.max_per_ip = 10 server.max_cons = Max_con server.max_cons_per_ip =server.max_per_ip server.serve_forever() #创建文件 import ftplib from io import StringIO import io from ftplib import FTP ftp = FTP(host='localhost',user='user',passwd='12345') ftp.cwd('test') ftp.storlines('STOR poem.txt',io.StringIO('') ) ftp.quit() #写入数据 from io import StringIO import io from ftplib import FTP ftp = FTP(host='localhost',user='user',passwd='12345') ftp.cwd('test') binary_data = b'Hello, world!' text = binary_data.decode('utf-8') data_as_bytes = text.encode('utf-8') ftp.storlines('STOR test.txt',io.BytesIO(data_as_bytes)) #上传下载文件 from ftplib import FTP ftp = FTP(host='localhost',user='user',passwd='12345') ftp.encoding = 'gbk' ftp.cwd('test') ftp.retrlines('LIST') ftp.retrbinary('RETR poem.txt', open('poem.txt', 'wb').write) ftp.storbinary('STOR ftpserver.py', open('ftpserver.py', 'rb')) for f in ftp.mlsd(path='/test'): print(f)这段代码的数据处理分析
时间: 2023-06-25 19:05:00 浏览: 179
Python库 | pyftpdlib-0.2.0.tar.gz
这段代码实现了一个基于pyftpdlib的FTP服务器,并提供了创建文件、写入数据和上传下载文件的功能。具体分析如下:
1. 导入模块
```
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler,ThrottledDTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.log import LogFormatter
import logging
import configparser
import pyftpdlib
```
导入pyftpdlib的相关模块以及其他需要用到的模块。
2. 配置日志
```
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
fh = logging.FileHandler(filename='myftpserver.log',encoding='GBK')
ch.setFormatter(LogFormatter())
fh.setFormatter(LogFormatter())
logger.addHandler(ch)
logger.addHandler(fh)
```
创建一个logger对象并设置日志级别为INFO,同时将日志输出到控制台和myftpserver.log文件中。
3. 创建用户
```
authorizer = DummyAuthorizer()
authorizer.add_user("user", "12345", "d:/", perm="elradfmw")
ENABLE_ANONYMOUS = 'on'
if ENABLE_ANONYMOUS == 'on':
authorizer.add_anonymous("d:/")
```
使用DummyAuthorizer创建一个虚拟用户,用户名为"user",密码为"12345",根目录为"d:/",并设置其权限为elradfmw。如果允许匿名访问,则使用add_anonymous方法添加一个匿名用户。
4. 配置FTP服务器
```
handler = FTPHandler
handler.authorizer = authorizer
handler.passive_ports = range(8300, 8500)
dtp_handler = ThrottledDTPHandler
upload = 100 * 1024 #100kb/s
download = 100 * 1024 #100kb/s
dtp_handler = ThrottledDTPHandler
dtp_handler.read_limit = download
dtp_handler.write_limit = upload
ip = '0.0.0.0'
port = '21'
server = FTPServer((ip, port), handler)
Max_con = 100
server.max_per_ip = 10
server.max_cons = Max_con
server.max_cons_per_ip =server.max_per_ip
server.serve_forever()
```
创建一个FTPHandler对象,并将其authorizer属性设置为上面创建的虚拟用户。同时设置被动模式端口范围和上传下载速度限制。指定FTP服务器的IP地址和端口号,并设置最大连接数和每个IP地址的最大连接数。最后,调用serve_forever方法启动FTP服务器。
5. 创建文件
```
import ftplib
from io import StringIO
import io
from ftplib import FTP
ftp = FTP(host='localhost',user='user',passwd='12345')
ftp.cwd('test')
ftp.storlines('STOR poem.txt',io.StringIO('') )
ftp.quit()
```
使用ftplib创建一个FTP对象,然后登录FTP服务器。调用cwd方法进入test目录,使用storlines方法创建一个空的poem.txt文件。
6. 写入数据
```
from io import StringIO
import io
from ftplib import FTP
ftp = FTP(host='localhost',user='user',passwd='12345')
ftp.cwd('test')
binary_data = b'Hello, world!'
text = binary_data.decode('utf-8')
data_as_bytes = text.encode('utf-8')
ftp.storlines('STOR test.txt',io.BytesIO(data_as_bytes))
```
调用storlines方法将数据写入test.txt文件中。
7. 上传下载文件
```
from ftplib import FTP
ftp = FTP(host='localhost',user='user',passwd='12345')
ftp.encoding = 'gbk'
ftp.cwd('test')
ftp.retrlines('LIST')
ftp.retrbinary('RETR poem.txt', open('poem.txt', 'wb').write)
ftp.storbinary('STOR ftpserver.py', open('ftpserver.py', 'rb'))
for f in ftp.mlsd(path='/test'):
print(f)
```
使用retrlines方法获取test目录下的文件列表,并使用retrbinary方法下载poem.txt文件,使用storbinary方法上传ftpserver.py文件。最后,使用mlsd方法获取test目录下的文件列表,并打印出来。
阅读全文