linkchecker/tests/checker/ftpserver.py

106 lines
3.1 KiB
Python
Raw Normal View History

2010-03-09 06:42:56 +00:00
# -*- coding: iso-8859-1 -*-
2014-03-04 20:36:24 +00:00
# Copyright (C) 2010-2014 Bastian Kleineidam
2010-03-09 06:42:56 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""
Define http test support classes for LinkChecker tests.
"""
import os
import time
import threading
2014-03-02 19:01:55 +00:00
import pytest
from ftplib import FTP
2010-03-09 06:42:56 +00:00
from . import LinkCheckTest
TIMEOUT = 5
2010-03-09 06:42:56 +00:00
class FtpServerTest (LinkCheckTest):
"""Start/stop an FTP server that can be used for testing."""
def __init__ (self, methodName='runTest'):
"""Init test class and store default ftp server port."""
super(FtpServerTest, self).__init__(methodName=methodName)
self.host = 'localhost'
self.port = None
2010-03-09 06:42:56 +00:00
2012-11-06 20:34:22 +00:00
def setUp (self):
2010-03-09 06:42:56 +00:00
"""Start a new FTP server in a new thread."""
self.port = start_server(self.host, 0)
self.assertFalse(self.port is None)
2010-03-09 06:42:56 +00:00
2012-11-06 20:34:22 +00:00
def tearDown (self):
2012-10-30 16:44:00 +00:00
"""Send stop request to server."""
2010-03-09 06:42:56 +00:00
try:
stop_server(self.host, self.port)
except Exception:
2010-03-09 06:42:56 +00:00
pass
def start_server (host, port):
def line_logger(msg):
if "kill" in msg:
2012-10-01 08:43:20 +00:00
raise KeyboardInterrupt()
2010-03-09 06:42:56 +00:00
2014-03-02 06:45:04 +00:00
try:
from pyftpdlib import ftpserver
except ImportError:
pytest.skip("pyftpdlib is not available")
return
2010-03-09 06:42:56 +00:00
authorizer = ftpserver.DummyAuthorizer()
datadir = os.path.join(os.path.dirname(__file__), 'data')
authorizer.add_anonymous(datadir)
# Instantiate FTP handler class
ftp_handler = ftpserver.FTPHandler
ftp_handler.authorizer = authorizer
ftp_handler.timeout = TIMEOUT
2010-03-09 06:42:56 +00:00
ftpserver.logline = line_logger
# Define a customized banner (string returned when client connects)
ftp_handler.banner = "pyftpdlib %s based ftpd ready." % ftpserver.__ver__
# Instantiate FTP server class and listen to host:port
address = (host, port)
server = ftpserver.FTPServer(address, ftp_handler)
port = server.address[1]
t = threading.Thread(None, server.serve_forever)
t.start()
# wait for server to start up
tries = 0
while tries < 5:
tries += 1
try:
ftp = FTP()
ftp.connect(host, port, TIMEOUT)
ftp.login()
ftp.close()
break
except:
time.sleep(0.5)
return port
def stop_server (host, port):
"""Stop a running FTP server."""
ftp = FTP()
ftp.connect(host, port, TIMEOUT)
ftp.login()
try:
ftp.sendcmd("kill")
except EOFError:
pass
ftp.close()