400多行Python代码实现了一个FTP服务器

本文介绍了一款基于Python编写的FTP服务器,该服务器支持多用户、虚拟目录,并且允许设置用户权限和空间限制。文章提供了详细的代码实现,包括用户认证、虚拟目录处理、空间检查等功能,并分享了测试地址和使用方法。
摘要由CSDN通过智能技术生成

Python版本
实现了比之前的xxftp更多更完善的功能
1、继续支持多用户
2、继续支持虚拟目录
3、增加支持用户根目录以及映射虚拟目录的权限设置
4、增加支持限制用户根目录或者虚拟目录的空间大小

xxftp的特点
1、开源、跨平台
2、简单、易用
3、不需要数据库
4、可扩展性超强
5、你可以免费使用xxftp假设自己的私人FTP服务器

测试地址
ftp://xiaoxia.org
匿名帐号可以使用!
匿名根目录只读,映射了一个虚拟目录,可以上传文件但不允许更改!

使用方法
跟之前用C语言写的xxftp使用方法一样:

  1. Create a root directory to hold the user directories.
    Configure it in config.xml.
  2. Create user directories under the root directory.
    If you want to specify a password, create a directory named “.xxftp”,
    under which create a text file named “password” containing the MD5
    code of the password.
  3. If you want to specify the welcome and goodbye message, write it in
    xxftp.welcome and xxftp.goodbye under the root directory.
  4. Configure config.xml.

The structure of your FTP server root may be like:

-/root
-xxftp.welcome
-xxftp.goodbye

-user1
-.xxftp
-password
-…
-user2
-.xxftp
-password
-…
-anonymous源代码

复制代码 代码如下:

import socket, threading, os, sys, time
import hashlib, platform, stat

listen_ip = “localhost”
listen_port = 21
conn_list = []
root_dir = “./home”
max_connections = 500
conn_timeout = 120

class FtpConnection(threading.Thread):
def init(self, fd):
threading.Thread.init(self)
self.fd = fd
self.running = True
self.setDaemon(True)
self.alive_time = time.time()
self.option_utf8 = False
self.identified = False
self.option_pasv = True
self.username = “”
def process(self, cmd, arg):
cmd = cmd.upper();
if self.option_utf8:
arg = unicode(arg, “utf8”).encode(sys.getfilesystemencoding())
print “<<”, cmd, arg, self.fd

Ftp Command

if cmd == “BYE” or cmd == “QUIT”:
if os.path.exists(root_dir + “/xxftp.goodbye”):
self.message(221, open(root_dir + “/xxftp.goodbye”).read())
else:
self.message(221, “Bye!”)
self.running = False
return
elif cmd == “USER”:

Set Anonymous User

if arg == “”: arg = “anonymous”
for c in arg:
if not c.isalpha() and not c.isdigit() and c!="_":
self.message(530, “Incorrect username.”)
return
self.username = arg
self.home_dir = root_dir + “/” + self.username
self.curr_dir = “/”
self.curr_dir, self.full_path, permission, self.vdir_list, \
limit_size, is_virtual = self.parse_path("/")
if not os.path.isdir(self.home_dir):
self.message(530, “User " + self.username + " not exists.”)
return
self.pass_path = self.home_dir + “/.xxftp/password”
if os.path.isfile(self.pass_path):
self.message(331, "Password required for " + self.username)
else:
self.message(230, “Identified!”)
self.identified = True
return
elif cmd == “PASS”:
if open(self.pass_path).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值