前两天用tcl实验了一个简单的ftp服务器.转念一想,应该python也是可以的.但是官方好像没有提供相关的库.在网上查了一下,在google code上面有提供相关的库.而且这个库的功能算是比较强大了,从感觉上讲,要比tcl实现的要漂亮.简单写一下步骤:
1).从下载最新的库.我的实验环境为server为fedora10,client为vista;
2).安装该库:sudo python setup.py install,如果安装的时候有问题,好像说是makefile找不到,可以安装python-devel,然后就可以安装成功;
3).安装成功后,可以在解压的包里找到basic_ftpd.py,用力运行一下吧.
4).在本机上做测试,好像有点问题,一是说目录未建好,另一个是端口权限问题.我将里面的os.getcwd()改成一个自己用的目录,将端口改成非防火墙禁止的窗口.
5).放一下修改后的FTP服务器代码,明天再深入研究一下其它的高级内容吧.
#!/usr/bin/env python
# basic_ftpd.py
"""A basic FTP server which uses a DummyAuthorizer for managing 'virtual
users', setting a limit for incoming connections.
"""
import os
from pyftpdlib import ftpserver
if __name__ == "__main__":
# Instantiate a dummy authorizer for managing 'virtual' users
authorizer = ftpserver.DummyAuthorizer()
# Define a new user having full r/w permissions and a read-only
# anonymous user
#authorizer.add_user('user', '12345', os.getcwd(), perm='elradfmw')
authorizer.add_user('user', '12345', '/home/riverbird/temp', perm='elradfmw')
#authorizer.add_anonymous(os.getcwd())
authorizer.add_anonymous('/home/riverbird/temp')
# Instantiate FTP handler class
ftp_handler = ftpserver.FTPHandler
ftp_handler.authorizer = authorizer
# Define a customized banner (string returned when client connects)
ftp_handler.banner = "pyftpdlib %s based ftpd ready." %ftpserver.__ver__
# Specify a masquerade address and the range of ports to use for
# passive connections. Decomment in case you're behind a NAT.
#ftp_handler.masquerade_address = '151.25.42.11'
#ftp_handler.passive_ports = range(60000, 65535)
# Instantiate FTP server class and listen to 0.0.0.0:21
#address = ('', 21)
address = ('192.168.1.103',8000)
ftpd = ftpserver.FTPServer(address, ftp_handler)
# set a limit for connections ftpd.max_cons = 256 ftpd.max_cons_per_ip = 5 # start ftp server ftpd.serve_forever()
|
阅读(3460) | 评论(2) | 转发(0) |