由于作业需要,要求搭建一个SSL加密的安全的FTP服务器,在网上随便找了一下,因为比较偏好Python,便决定用pyftpdlib。
首先还是使用pip进行安装:
sudo pip install pyftpdlib
安装很顺利,没有遇到什么问题。
接下来是使用了,首先架设一个没有使用SSL的FTP服务器:
#!/usr/bin/env python #encoding:utf-8 from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyftpdlib.servers import FTPServer def main(): # Instantiate a dummy authorizer for managing 'virtual' users authorizer = DummyAuthorizer() # Define a new user having full r/w permissions authorizer.add_user('Jayvic', '******', './ftp_home', perm='elradfmwM') # Define a read-only anonymous user authorizer.add_anonymous('./ftp_home') # Instantiate FTP handler class handler = FTPHandler handler.authorizer = authorizer # Define a customized banner (string returned when client connects) handler.banner = "Welcome to Jayvic's FTP." # Instantiate FTP server class and listen on 127.0.0.1:21 address = ('127.0.0.1', 21) server = FTPServer(address, handler) # set a limit for connections server.max_cons = 256 server.max_cons_per_ip = 5 # start ftp server server.serve_forever() if __name__ == '__main__': main()
由于此处我们使用的是21号端口,所以需要使用sudo来运行,否则会报如下错误:
Traceback (most recent call last): File "./ftp_server.py", line 37, in <module> main() File "./ftp_server.py", line 26, in main server = FTPServer(address, handler) File "/Library/Python/2.7/site-packages/pyftpdlib/servers.py", line 145, in __init__ self._af = self.bind_af_unspecified(address_or_socket) File "/Library/Python/2.7/site-packages/pyftpdlib/ioloop.py", line 774, in bind_af_unspecified raise socket.error(err) socket.error: [Errno 13] Permission denied
如果不想使用sudo权限,可以选择换一个用户级别的端口。
确认FTP可以正常使用后,接下来,我们将代码换成支持SSL的版本:
#!/usr/bin/env python #encoding:utf-8 from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import TLS_FTPHandler from pyftpdlib.servers import FTPServer def main(): # Instantiate a dummy authorizer for managing 'virtual' users authorizer = DummyAuthorizer() # Define a new user having full r/w permissions authorizer.add_user('Jayvic', '******', './ftp_home', perm='elradfmwM') # Define a read-only anonymous user authorizer.add_anonymous('./ftp_home') # Instantiate TLS FTP handler class handler = TLS_FTPHandler handler.authorizer = authorizer handler.certfile = './server.crt' handler.keyfile = './server.key' # Define a customized banner (string returned when client connects) handler.banner = "Welcome to Jayvic's FTPS." # Instantiate FTP server class and listen on 127.0.0.1:21 address = ('127.0.0.1', 21) server = FTPServer(address, handler) # set a limit for connections server.max_cons = 256 server.max_cons_per_ip = 5 # start ftp server server.serve_forever() if __name__ == '__main__': main()
证书的制作可见此文,如果使用最后的方法生成的证书,可以不指定keyfile。
做这题的时候,脑残了一下,还把FTPS和SFTP给弄混了,竟然在架设好了FTPS的服务器后,使用SFTP命令来连接,真是脑残的不轻,想之前只有在windows下SSH的时候想传文件还是用的FTP软件开SFTP传的,这几天不用竟然就忘了。