class MyServerFtp: def __init__(self): self.error = False self.port = 2121 def __exit__(self, *err ): self.close() def serverStart(self,port,location): try: self.error = False self.authorizer = DummyAuthorizer() self.authorizer.add_user('user', 'sk', location , perm='elradfmwM') self.handler = FTPHandler self.handler.authorizer = self.authorizer self.handler.banner = "ftp Server ready" self.address = ('', port) self.server = FTPServer(self.address, self.handler) self.server.max_cons = 256 self.server.max_cons_per_ip = 5 self.thread = threading.Thread(target = self.server.serve_forever) self.thread.deamon = True self.thread.start() except socket.error: self.error = True pass def errorOccured(self): return self.error def serverStop(self): self.server.close_all()
def run(ns): """starts the server.""" auth = DummyAuthorizer() if ns.user is not None: auth.add_user(ns.user, ns.pswd, ns.path, perm=ns.perm) else: auth.add_anonymous(ns.path, perm=ns.perm) handler = FTPHandler handler.authorizer = auth handler.banner = "StaSh v{v} FTP-Server".format(v=_stash.__version__) address = ("0.0.0.0", ns.port) server = FTPServer(address, handler) server.max_cons = 128 server.max_cons_per_ip = 128 # setup logging logger = logging.getLogger("pyftpdlib") logger.setLevel(logging.CRITICAL) logger.propagate = False # server needs to run in a thread to be killable thr = threading.Thread(name="FTP-Server Thread", target=server.serve_forever) thr.daemon = True thr.start() print("FTP-Server started on {h}:{p}".format(h=address[0], p=str(address[1]))) try: while True: time.sleep(0.2) except KeyboardInterrupt: print("Stopping Server...") server.close_all()
class Servidor_ftp: def __init__(self, config_ftp, diretorio): ''' config_ftp = classe Config_ftp diretorio = diretorio a ser compartilhado ''' self._config = config_ftp self._dir = diretorio def _config_server(self): authorizer = DummyAuthorizer() authorizer.add_user(self._config.usuario, self._config.senha, self._dir, perm=self._config.permissao) handler = FTPHandler handler.authorizer = authorizer #self._server = MultiprocessFTPServer((self._config.host, self._config.porta), handler) self._server = FTPServer((self._config.host, self._config.porta), handler) def iniciar_servidor(self): authorizer = DummyAuthorizer() authorizer.add_user(self._config.usuario, self._config.senha, self._dir, perm=self._config.permissao) handler = FTPHandler handler.authorizer = authorizer try: self._server = FTPServer((self._config.host, self._config.porta), handler) self._server.serve_forever(timeout=5) except OSError: Log.error('Erro ao iniciar FTP') def desligar_servidor(self): self._server.close_all()
def run(ns): """starts the server.""" auth = DummyAuthorizer() if ns.user is not None: auth.add_user(ns.user, ns.pswd, ns.path, perm=ns.perm) else: auth.add_anonymous(ns.path, perm=ns.perm) handler = FTPHandler handler.authorizer = auth handler.banner = "StaSh v{v} FTP-Server".format(v=_stash.__version__) address = ("0.0.0.0", ns.port) server = FTPServer(address, handler) server.max_cons = 128 server.max_cons_per_ip = 128 # setup logging logger = logging.getLogger("pyftpdlib") logger.setLevel(logging.CRITICAL) logger.propagate = False # server needs to run in a thread to be killable thr = threading.Thread( name="FTP-Server Thread", target=server.serve_forever ) thr.daemon = True thr.start() print("FTP-Server started on {h}:{p}".format(h=address[0], p=str(address[1]))) try: while True: time.sleep(0.2) except KeyboardInterrupt: print("Stopping Server...") server.close_all()
def run(port=2121, passive_ports=range(60000, 65535), masquerade_address=None): authorizer = CosAuthorizer() for login_user, login_password, home_dir, permission in CosFtpConfig( ).login_users: perm = "" if "R" in permission: perm = perm + authorizer.read_perms if "W" in permission: perm = perm + authorizer.write_perms authorizer.add_user(login_user, login_password, home_dir, perm=perm) handler = CosFtpHandler handler.authorizer = authorizer handler.abstracted_fs = CosFileSystem handler.banner = "Welcome to COS FTP Service" handler.permit_foreign_addresses = True if masquerade_address is not None: handler.masquerade_address = masquerade_address handler.passive_ports = passive_ports server = FTPServer(("0.0.0.0", port), handler) server.max_cons = CosFtpConfig().max_connection_num print "starting ftp server..." try: server.serve_forever() finally: server.close_all()
class SimpleFtpServer(multiprocessing.Process): def __init__(self): super().__init__() self.authorizer = DummyAuthorizer() handler = FTPHandler handler.authorizer = self.authorizer self.server = FTPServer(("127.0.0.1", 0), handler) def run(self): self.server.serve_forever() def stop(self): self.server.close_all() self.server.close() self.terminate() self.join() def allow_anonymous(self, cwd): self.authorizer.add_anonymous(cwd) def add_user(self, user, password, cwd): self.authorizer.add_user(user, password, cwd, perm="elradfmwMT") def base_url(self): return "ftp://127.0.0.1:{}".format(self.server.address[1])
class FileServer: def __init__(self, port): self.port = port self.sharedDir = "" self.is_running = False self.ftp_handler = FTPHandler self.connected = 0 self.bytesTransferred = 0 self.filesTransferred = 0 def setSharedDirectory(self, path): print "setting started" if not path_exists(path): raise FileNotFoundError print "hey" self.authorizer = DummyAuthorizer() self.authorizer.add_anonymous(path, perm='elradfmwM') self.ftp_handler.authorizer = self.authorizer def startServer(self): self.server = FTPServer(('', self.port), self.ftp_handler) print "process running:" self.server_proc = Process(target=self.server.serve_forever) self.server_proc.start() self.is_running = True def stopServer(self): if self.is_running: self.server.close_all() self.server_proc.terminate() self.server_proc.join() print("FTP server stopped") del self.server_proc self.server_proc = None self.is_running = False
class FTPd(threading.Thread): def __init__(self, root_dir, address=None, timeout=0.001, dtp_handler=None): threading.Thread.__init__(self) self.__flag = threading.Event() self.__timeout = timeout authorizer = DummyAuthorizer() authorizer.add_anonymous(root_dir) handler = FTPHandler handler.authorizer = authorizer if dtp_handler is not None: handler.dtp_handler = dtp_handler self.server = FTPServer(address, handler) def start(self): self.__flag.clear() threading.Thread.start(self) self.__flag.wait() def run(self): self.__flag.set() while self.__flag.is_set(): self.server.serve_forever(timeout=self.__timeout, blocking=False) self.server.close_all() self.server.close() def stop(self): self.__flag.clear() self.join()
def ftpstop(): authorizer = DummyAuthorizer() handler = FTPHandler handler.authorizer = authorizer address = ('', 2121) server = FTPServer(address, handler) #server.serve_forever() server.close_all()
def main(): """Start a stand alone anonymous FTP server.""" usage = "python -m pyftpdlib.ftpserver [options]" parser = optparse.OptionParser(usage=usage, description=main.__doc__, formatter=CustomizedOptionFormatter()) parser.add_option('-i', '--interface', default=None, metavar="ADDRESS", help="specify the interface to run on (default all " "interfaces)") parser.add_option('-p', '--port', type="int", default=2121, metavar="PORT", help="specify port number to run on (default 21)") parser.add_option('-w', '--write', action="store_true", default=False, help="grants write access for the anonymous user " "(default read-only)") parser.add_option('-d', '--directory', default=getcwdu(), metavar="FOLDER", help="specify the directory to share (default current " "directory)") parser.add_option('-n', '--nat-address', default=None, metavar="ADDRESS", help="the NAT address to use for passive connections") parser.add_option('-r', '--range', default=None, metavar="FROM-TO", help="the range of TCP ports to use for passive " "connections (e.g. -r 8000-9000)") parser.add_option('-v', '--version', action='store_true', help="print pyftpdlib version and exit") options, args = parser.parse_args() if options.version: sys.exit("pyftpdlib %s" % __ver__) passive_ports = None if options.range: try: start, stop = options.range.split('-') start = int(start) stop = int(stop) except ValueError: parser.error('invalid argument passed to -r option') else: passive_ports = list(range(start, stop + 1)) # On recent Windows versions, if address is not specified and IPv6 # is installed the socket will listen on IPv6 by default; in this # case we force IPv4 instead. if os.name in ('nt', 'ce') and not options.interface: options.interface = '0.0.0.0' authorizer = DummyAuthorizer() perm = options.write and "elradfmwM" or "elr" authorizer.add_anonymous(options.directory, perm=perm) handler = FTPHandler handler.authorizer = authorizer handler.masquerade_address = options.nat_address handler.passive_ports = passive_ports ftpd = FTPServer((options.interface, options.port), FTPHandler) try: ftpd.serve_forever() finally: ftpd.close_all()
class Server: def __init__(self, settings): self.settings = settings self.username = self.settings.get_setting("ftp_username") self.password = self.settings.get_setting("ftp_password") self.port = self.settings.get_setting("ftp_port") self.enabled = self.settings.get_setting("enable_ftp_server") self.__server = None def run(self): authorizer = DummyAuthorizer() authorizer.add_user(self.username, self.password, os.environ['HOME'], perm="elradfmw") handler = FTPHandler handler.authorizer = authorizer self.__server = FTPServer(('0.0.0.0', self.port), handler) if self.enabled: ftp_thread = threading.Thread(target=self.__server.serve_forever) ftp_thread.daemon = True ftp_thread.start() def stop(self): if self.__server: self.__server.close_all() def reload(self): new_enabled = self.settings.get_setting("enable_ftp_server") new_username = self.settings.get_setting("ftp_username") new_password = self.settings.get_setting("ftp_password") new_port = self.settings.get_setting("ftp_port") # Check if any of the values were changed settings_were_changed = False if new_enabled != self.enabled: self.enabled = new_enabled settings_were_changed = True if new_username != self.username: self.username = new_username settings_were_changed = True if new_password != self.password: self.password = new_password settings_were_changed = True if new_port != self.port: self.port = new_port settings_were_changed = True # Reload the server if any changes were found if settings_were_changed: self.stop() self.run()
class FTP(object): """Configures and establish an FTP server with default details. Args: user (str): User for FTP server auth password (str): Password for FTP server auth working_dir (str): The local working dir to init the ftp server on. """ def __init__(self, user=FTP_SERVER_USER, password=FTP_SERVER_PASSWORD, working_dir=FTP_WORKING_DIR): """Look at class level docstring.""" self.user = user self.password = password self.working_dir = working_dir def run_server(self, user=FTP_SERVER_USER, password=FTP_SERVER_PASSWORD, working_dir=FTP_WORKING_DIR): """ Configures and runs the ftp server to listen forever until stopped. Args: user (str): User for FTP server auth password (str): Password for FTP server auth working_dir (str): The local working dir to init the ftp server on. """ # Defining an authorizer and configuring the ftp user authorizer = DummyAuthorizer() authorizer.add_user(user, password, working_dir, perm='elradfmw') # Normal ftp handler handler = FTPHandler handler.authorizer = authorizer address = (FTP_SERVER_ADDRESS, FTP_SERVER_PORT) # Configuring the server using the address and handler. Global usage in stop_server thats why using self keyword self.server = FTPServer(address, handler) # Starting ftp server, this server has no auto stop or stop clause, and also, its blocking on use, thats why I # multiproccess is being used here. self.server.serve_forever() def stop_server(self): # Stops the FTP server and closing all connections. self.server.close_all()
class FtpThread(threading.Thread): def __init__(self, source_path, ip='0.0.0.0', port=2121, username='******', password='******'): self.ip = ip self.port = port self.username = username self.password = password if not os.path.exists(source_path): raise ValueError('Path doesnt exist - ({})'.format(source_path)) self.source_path = source_path threading.Thread.__init__(self) def run(self, *args, **kwargs): authorizer = DummyAuthorizer() authorizer.add_user(self.username, self.password, self.source_path, perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer # logging.basicConfig(filename='/var/log/verteiler_ftp.log', level=logging.INFO) handler.banner = "pyftpdlib based ftpd ready." address = (self.ip, self.port) self.server = FTPServer(address, handler) self.server.max_cons = 256 self.server.max_cons_per_ip = 5 self.server.serve_forever() def stop(self): self.server.close_all() @staticmethod def startup_server_thread_with_fixed_lifetime(source_path, seconds=FTP_SERVER_LIFETIME): ''' Ftp server launch SYNCH for infinity (seconds=None) or for fixed no of seconds ''' server = FtpThread(source_path=source_path) server.start() if seconds: time.sleep(seconds) server.stop() @staticmethod def launch_ftp_server_with_fixed_lifetime(source_path, seconds=FTP_SERVER_LIFETIME): ''' Ftp server launch ASYNCH for infinity (seconds=None) or for fixed no of seconds ''' def worker(): FtpThread.startup_server_thread_with_fixed_lifetime(seconds=seconds, source_path=source_path) thread = threading.Thread(target=worker) thread.start()
class Form(QDialog): def __init__(self): super(Form, self).__init__() self.setWindowTitle("FTP Server") # Top Message self.message = QLabel() hostname = socket.gethostname() self.IPAddr = socket.gethostbyname(hostname) msg = "Welcome! \n" + "Host IP: " + self.IPAddr self.message.setText(msg) self.start = QPushButton("Start") self.stop = QPushButton("Stop") # Top Message self.log = QLabel() self.log.setOpenExternalLinks(True) self.log.setText("Server Stopped!") # Create layout and add widgets layout = QVBoxLayout() layout.addWidget(self.message) layout.addWidget(self.start) layout.addWidget(self.stop) layout.addWidget(self.log) self.setLayout(layout) # Add button signal to greetings slot self.start.clicked.connect(self.stratServer) self.stop.clicked.connect(self.stopServer) def stratServer(self): authorizer = DummyAuthorizer() cwd = os.getcwd() authorizer.add_anonymous(cwd) handler = FTPHandler handler.authorizer = authorizer self.server = FTPServer((self.IPAddr, 21), handler) srv = threading.Thread(target=self.server.serve_forever, daemon=True) srv.start() link = "ftp://" + self.IPAddr self.log.setText("FTP server started @ " + '''<a href=%s>%s</a>''' % (link, link)) def stopServer(self): self.server.close_all() self.log.setText("Server Stopped!")
class FtpServer(QtCore.QRunnable): """The FTP server thread""" def __init__(self, ip, port, max_conn, max_conn_per_ip, folder, banner, users_can_upload_files): super(FtpServer, self).__init__() self.signals = FtpServerSignals() self.ip = ip self.port = port self.max_connections = max_conn self.max_connections_per_ip = max_conn_per_ip self.folder = folder self.banner = banner self.permisions = 'elr' if users_can_upload_files: self.permisions += 'w' @QtCore.pyqtSlot() def run(self): """This method defines what actions the FTP server does when running""" authorizer = DummyAuthorizer() authorizer.add_user('hotline', 'hotpassword', homedir=self.folder, perm=self.permisions) handler = MyHandler handler.banner = self.banner handler.authorizer = authorizer self.handler = handler self.handler.signals = self.signals try: self.server = FTPServer((self.ip, self.port), handler) except Exception as e: self.signals.on_error.emit(e) return self.server.max_cons = self.max_connections self.server.max_cons_per_ip = self.max_connections_per_ip # server = FTPServer((self.ip, self.port), handler) try: self.signals.on_start.emit() self.server.serve_forever() except Exception as e: self.signals.on_error.emit(e) def my_jorge_shutdown(self): """This method shutdowns the server""" self.signals.on_shutdown.emit() # self.server.close() self.server.close_all()
def main(): authorizer = DummyAuthorizer() authorizer.add_anonymous(os.path.expanduser('~/Documents'), perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer server = FTPServer(('0.0.0.0', 2121), handler) t = threading.Thread(target=server.serve_forever) t.start() print 'Server started.' print '\nConnect as guest/anonymous user to ftp://localhost:2121 (from this device) or "ftp://(YOUR_IP_ADDRESS):2121" (from other devices in your network -- you can find the IP address of your device in the WiFi settings)' try: while True: pass except KeyboardInterrupt: server.close_all() print 'Server stopped'
def main(): authorizer = DummyAuthorizer() authorizer.add_anonymous(os.path.expanduser('~/Documents'), perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer server = FTPServer(('0.0.0.0', 21), handler) t = threading.Thread(target=server.serve_forever) t.start() try: while True: pass except KeyboardInterrupt: server.close_all() print('Server stopped')
def main(): authorizer = DummyAuthorizer() authorizer.add_anonymous(os.path.expanduser('~/Documents'), perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer server = FTPServer(('0.0.0.0', 2121), handler) t = threading.Thread(target=server.serve_forever) t.start() print('Server started.') print('\nConnect as guest/anonymous user to ftp://localhost:2121 (from this device) or "ftp://(YOUR_IP_ADDRESS):2121" (from other devices in your network -- you can find the IP address of your device in the WiFi settings)') try: while True: pass except KeyboardInterrupt: server.close_all() print('Server stopped')
def main(): authorizer = DummyAuthorizer() authorizer.add_anonymous(os.path.expanduser('~/Documents'), perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer server = FTPServer(('0.0.0.0', 2121), handler) t = threading.Thread(target=server.serve_forever) t.start() print 'Server started.' print '\nConnect as guest/anonymous user to ftp://localhost:2121 (from this device) or "ftp://%s.local:2121" (from other devices in your network)' % (gethostname(),) try: while True: pass except KeyboardInterrupt: server.close_all() print 'Server stopped'
class FTPServerBase(unittest.TestCase): def _run_ftp_server(self): self.server.serve_forever() def _setup_home(self): """ Initialize the temporary homedir of the test user """ self.home = tempfile.mkdtemp() self.storage = tempfile.mkdtemp() tempfile.NamedTemporaryFile(dir=self.home) def _teardown_home(self): pass def setUp(self): self.ftp = None self.host = 'localhost' self.port = 9001 self._setup_home() authorizer = DummyAuthorizer() authorizer.add_user('vagrant', 'vagrant', self.home, perm='elrdfmw') authorizer.add_anonymous(self.home) handler = FTPHandler handler.authorizer = authorizer self.server = FTPServer((self.host, self.port), handler) # run ftp server in a separate thread so as to not block tests from running self.thread = threading.Thread(target=self._run_ftp_server) self.thread.daemon = True self.thread.start() self.ftp = lftp.LFTP(self.host, self.port, 'vagrant', 'vagrant') def tearDown(self): if self.ftp: self.ftp.disconnect() self.server.close_all() self._teardown_home() def test_empty_homedir(self): ftp = self.ftp # listing of an empty directory ls = ftp.list() self.assertEqual(ls, "") def test_dir(self): ftp = self.ftp tempdir = tempfile.mkdtemp(dir=self.home) ls = ftp.list() ls.should.contain(os.path.basename(tempdir))
class FTPServerBase(unittest.TestCase): def _run_ftp_server(self): self.server.serve_forever() def _setup_home(self): """ Initialize the temporary homedir of the test user """ self.home = tempfile.mkdtemp() self.storage = tempfile.mkdtemp() tempfile.NamedTemporaryFile(dir=self.home) def _teardown_home(self): pass def setUp(self): self.ftp = None self.host = 'localhost' self.port = 9001 self._setup_home() authorizer = DummyAuthorizer() authorizer.add_user('vagrant', 'vagrant', self.home, perm='elrdfmw') authorizer.add_anonymous(self.home) handler = FTPHandler handler.authorizer = authorizer self.server = FTPServer((self.host, self.port), handler) # run ftp server in a separate thread so as to not block tests from running self.thread = threading.Thread(target=self._run_ftp_server) self.thread.daemon = True self.thread.start() self.ftp = lftp.LFTP(self.host, self.port, 'vagrant', 'vagrant') def tearDown(self): if self.ftp: self.ftp.disconnect() self.server.close_all() self._teardown_home() def test_empty_homedir(self): ftp = self.ftp # listing of an empty directory ls = ftp.list() self.assertEqual(ls, "") def test_dir(self): ftp = self.ftp tempdir = tempfile.mkdtemp(dir=self.home) ls = ftp.list() ls.should.contain(os.path.basename(tempdir))
class TestUpdateTimestampFromFtpFile(TestCase): def __init__(self, methodName='runTest'): super().__init__(methodName) s = str(tempfile.gettempdir()) + "/" + str(uuid4()) os.mkdir(s) self.file_location = s self.cleanup_folders = [self.file_location] def setUp(self): copy_file_with_fixed_creation_date(self.file_location) authorizer = DummyAuthorizer() authorizer.add_anonymous(self.file_location) self.downloaded_files = [] def record_file_sent(a, b): self.downloaded_files.append(b) handler = FTPHandler handler.on_file_sent = record_file_sent handler.authorizer = authorizer self.server = FTPServer(("localhost", 1026), handler) t1 = FuncThread(serve_fn, self.server) t1.start() pass def test_downloads_new_file(self): self.downloaded_files = [] target_folder = str(tempfile.gettempdir()) + "/" + str(uuid4()) os.mkdir(target_folder) self.cleanup_folders.append(target_folder) update_ftp_file(ftp_uri="ftp://localhost:1026/some.db", soruce_name="some.db", target_folder=target_folder) assert os.path.exists(target_folder + "/some.db.last_mod.txt") assert file_content_to_string( target_folder + "/some.db.last_mod.txt") == "2020-01-01-00-00-00" def tearDown(self) -> None: for folder in self.cleanup_folders: shutil.rmtree(folder) self.server.close_all()
def main(): authorizer = DummyAuthorizer() authorizer.add_anonymous(os.path.expanduser('~/Documents'), perm='elradfmwM') handler = FTPHandler handler.authorizer = authorizer server = FTPServer(('0.0.0.0', 2121), handler) t = threading.Thread(target=server.serve_forever) t.start() print 'Server started.' print '\nConnect as guest/anonymous user to ftp://localhost:2121 (from this device) or "ftp://%s.local:2121" (from other devices in your network)' % ( gethostname(), ) try: while True: pass except KeyboardInterrupt: server.close_all() print 'Server stopped'
def run_ftp_server(username, password, address, port, use_ssl, top_dir, loop): authorizer = DummyAuthorizer() authorizer.add_user(username, password, top_dir, perm='elradfmwMT') if use_ssl: handler = TLS_FTPHandler handler.certfile = 'tests/dummycert.pem' handler.tls_control_required = True handler.tls_data_required = True else: handler = FTPHandler handler.authorizer = authorizer server = FTPServer((address, port), handler, ioloop=loop) try: server.serve_forever(timeout=2) finally: server.close_all()
class TestFTPServer(object): class FTPThread(threading.Thread): def __init__(self, server, *args, **kwargs): super().__init__(*args, **kwargs) self.server = server def run(self): try: self.server.serve_forever() except OSError: pass def __init__(self): self.temp_dir = tempfile.mkdtemp() os.mkdir(os.path.join(self.temp_dir, "dir1")) with open(os.path.join(self.temp_dir, "file1.txt"), 'w') as f: f.write("Test") with open(os.path.join(self.temp_dir, "file2.txt"), 'w') as f: f.write("Test") authorizer = DummyAuthorizer() authorizer.add_user("username1", "password1", self.temp_dir, perm='elradfmwMT') handler = FTPHandler handler.authorizer = authorizer self.server = FTPServer(("127.0.0.1", 2121), handler) self.server_thread = TestFTPServer.FTPThread(server = self.server) def clean_temp_dir(self): os.remove(os.path.join(self.temp_dir, "file1.txt")) os.remove(os.path.join(self.temp_dir, "file2.txt")) if os.path.exists(os.path.join(self.temp_dir, "file3.txt")): os.remove(os.path.join(self.temp_dir, "file3.txt")) os.rmdir(os.path.join(self.temp_dir, "dir1")) os.rmdir(self.temp_dir) def start(self): self.server_thread.start() def stop(self): self.server.close_all() self.server_thread.join() self.clean_temp_dir()
def start_ftp_server(ip, port): authorizer = DummyAuthorizer() user = "******" passw = "pass" print("Servidor FTP iniciado con usuario/clave: master/pass") authorizer.add_user(user, passw, os.getcwd(), perm='elradmwM') authorizer.add_anonymous(os.getcwd()) handler = FTPHandler handler.authorizer = authorizer direccion = (ip, port) global servidor servidor = FTPServer(direccion, handler) servidor.max_cons = 256 servidor.max_cons_per_ip = 5 try: servidor.serve_forever(timeout=1) except KeyboardInterrupt: servidor.close_all() print("Se ha cancelado el servidor FTP") ins = input("Presiona <enter> para continuar")
class serverthread(threading.Thread): def __init__(self, ip, port, directory): threading.Thread.__init__(self) self.ip = ip self.port = port self.directory = directory self.active = False def start(self, flag=None): if (self.active == False): self.flag = flag threading.Thread.start(self) def run(self): if (self.active == False): authorizer = DummyAuthorizer() authorizer.add_user('user', 'pass', self.directory, perm='elradfmwM') handler = MyHandler #ftpfs = FTPFS('/home/aaditya/ESDLproject/serverdata',handler) handler.authorizer = authorizer handler.banner = "pyftpdlib based ftpd ready." address = (self.ip, self.port) self.server = FTPServer(address, handler) self.server.max_cons = 256 self.server.max_cons_per_ip = 5 if self.flag: self.flag.set() self.active = True while self.active: self.server.serve_forever(timeout=1, blocking=False) self.server.close_all() def stop(self): #self.server.close_all() if (self.active == True): #print "stopping" self.active = False
class FTP_Server: """ A FTP Server Class to be launched whithin a non blocking thread """ #Declare version number of the server-class cVersion = "1.0-5" #Class constructor def __init__( self ): #Create an authorizer... self.cAuthorizer = DummyAuthorizer() #Create a FTPHandler ( required for FTPServer ) self.cHandler = FTPHandler #Create a FTPAuthorizer ( required for FTPServer ) self.cHandler.authorizer = self.cAuthorizer #Establish a default banner ( optional ) self.cHandler.banner = "ssftps server v" + self.cVersion + " ready." def run( self ): #Is the user list empty? if not myConfig.USERS: #YES - Activate the anonymous login self.cAuthorizer.add_anonymous( os.getcwd() ) else: #NO - Iterate over userlist for tmpUser in myConfig.USERS: self.cAuthorizer.add_user( tmpUser['user'], tmpUser['pass'], tmpUser['path'], "elradfmwM" ) #Instantiate FTPServer self.cServer = FTPServer( ( myConfig.IPV4, myConfig.PORT ) , self.cHandler ) #Run the server self.cServer.serve_forever() def stop( self ): #Stop the server self.cServer.close_all() def add_user( self, tmpUser, tmpPasswd, tmpPath, tmpPrivileges="elradfmwM" ): #Add a user with all read write privileges, unless otherwise specified self.authorizer.add_user( str( tmpUser ), str( tmpPasswd ), str( tmpPath ), perm=str( tmpPrivileges ) )
def main(username, password, nat_address, perm): # Auth options: authorizer = DummyAuthorizer() if username is not None and password is not None: authorizer.add_user(username, password, HOME_DIR, perm=perm) else: authorizer.add_anonymous(HOME_DIR, perm=perm) # Run server: handler = FTPHandler handler.authorizer = authorizer handler.masquerade_address = nat_address handler.passive_ports = PASSIVE_PORTS server = FTPServer((BIND_ADDRESS, LISTEN_PORT), handler) try: server.serve_forever() finally: server.close_all()
class MyServerFtp: def __init__(self): self.error = False self.port = 2121 def __exit__(self, *err): self.close() def serverStart(self, port, location): try: self.error = False self.authorizer = DummyAuthorizer() self.authorizer.add_user('user', 'sk', location, perm='elradfmwM') self.handler = FTPHandler self.handler.authorizer = self.authorizer self.handler.banner = "ftp Server ready" self.address = ('', port) self.server = FTPServer(self.address, self.handler) self.server.max_cons = 256 self.server.max_cons_per_ip = 5 self.thread = threading.Thread(target=self.server.serve_forever) self.thread.deamon = True self.thread.start() except socket.error: self.error = True pass def errorOccured(self): return self.error def serverStop(self): self.server.close_all()
class TestDownloadProcessor: httpd = None p = None _ftpd = None @classmethod def run_server(cls): cls.httpd = ThreadingHTTPServer(("", 8043), MockHTTPHandler) cls.httpd.allow_reuse_address = True cls.httpd.serve_forever() @classmethod def setUpClass(cls): """ Run a web server in background to mock some specific HTTP behaviours """ from threading import Thread cls.p = Thread(target=cls.run_server) cls.p.start() @classmethod def tearDownClass(cls): """ Stop the http server in background """ cls.httpd.shutdown() cls.p.join() @isolate def test_standard_download(self): """Should download a simple url""" if not online: raise SkipTest("Offline") project = " file://google.html <- http://www.google.com/ ! download" pp = ProjectParser() pp.set_project(project) workflow = pp.parse_extend_and_check_project() workflow.static_check_processes() workflow.discover_resources() TuttleDirectories.straighten_out_process_and_logs(workflow) wr = WorkflowRunner(3) wr.run_parallel_workflow(workflow) assert isfile("google.html") content = open("google.html").read() assert content.find("<title>Google</title>") >= 0 logs = open(join(".tuttle", "processes", "logs", "__1_stdout.txt"), "r").read() assert re.search("\n\.+\n", logs) is not None, logs assert isfile(join(".tuttle", "processes", "logs", "__1_err.txt")) @isolate def test_long_download(self): """ Progress dots should appear in the logs in a long download""" project = " file://huge_resource.js <- http://localhost:8043/huge_resource.js ! download" pp = ProjectParser() pp.set_project(project) workflow = pp.parse_extend_and_check_project() workflow.static_check_processes() workflow.discover_resources() TuttleDirectories.straighten_out_process_and_logs(workflow) wr = WorkflowRunner(3) wr.run_parallel_workflow(workflow) assert isfile("huge_resource.js"), "huge_resource.js is missing" logs = open(join(".tuttle", "processes", "logs", "__1_stdout.txt"), "r").read() assert logs.find("...") >= 0 @isolate def test_pre_check_outputs(self): """Should fail if don't know what to download """ project = " file://foo <- ! download" pp = ProjectParser() pp.set_project(project) try: workflow = pp.parse_extend_and_check_project() assert False, "An exception should be raised" except TuttleError as e: assert e.message.find("don't know how to handle these inputs") >= 0, e @isolate def test_pre_check_inputs(self): """Should fail if don't nowk where to download """ project = " <- http://www.google.com/ ! download" pp = ProjectParser() pp.set_project(project) try: workflow = pp.parse_extend_and_check_project() assert False, "An exception should be raised" except TuttleError as e: assert e.message.find("don't know how to handle these outputs") >= 0, e.message @isolate def test_simple_dl(self): """ Should download a simple url to a file """ project = """file://huge_resource.js <- http://localhost:8043/huge_resource.js ! download""" rcode, output = run_tuttle_file(project) assert rcode == 0, output assert isfile('huge_resource.js') @isolate def test_can_download_in_sub_dir(self): """ Should download as long as there is one file output and exactly one downloadable resource """ sleep(0.5) # Travis needs some time before running the project or http mock server won't be available project = """file://a_directory/a_resource <- http://localhost:8043/a_resource file://a_directory ! download file://a_directory <- mkdir a_directory """ rcode, output = run_tuttle_file(project) assert rcode == 0, output assert isdir('a_directory') assert isfile('a_directory/a_resource') # @isolate # def test_download_fails(self): # """Should raise an exception if download fails""" # project = " file://tuttle.html <- http://www.example.com/tuttle ! download" # pp = ProjectParser() # pp.set_project(project) # # Don't check project or execution of the workflow will not be allowed because input resource is missing # workflow = pp.parse_project() # print workflow._processes # print [res.url for res in workflow._processes[0].inputs] # workflow.prepare_execution() # workflow.run() # assert isfile("tuttle.html") @isolate def test_pre_check_before_running(self): """ Pre check should happen for each process before run the whole workflow """ project = """file://A <- obvious failure file://google.html <- file://A ! download """ rcode, output = run_tuttle_file(project) assert rcode == 2 assert output.find("Download processor") >= 0, output @isolate def test_no_error_with_download_process(self): """ Download process does not create code in reserved_path for the process... Thus it cant be moved when """ """ retreiving logs and reserved path from previous execution(from bug) """ project = """file://g <- http://localhost:8043/a_resource ! download file://h <- file://g ERROR """ rcode, output = run_tuttle_file(project) assert rcode == 2, output rcode, output = tuttle_invalidate() assert rcode == 0, output rcode, output = run_tuttle_file() assert rcode == 2, output @isolate def test_pre_check_before_invalidation(self): """Pre check should happen before invalidation""" project1 = """file://A <- echo A > A """ rcode, output = run_tuttle_file(project1) assert isfile('A') project2 = """file://A <- echo different > A file://google.html <- file://A ! download """ rcode, output = run_tuttle_file(project2) assert rcode == 2 assert output.find("* file://B") == -1 assert output.find("Download processor") >= 0, output @isolate def test_download_https(self): """ https download should work """ if not online: raise SkipTest("Offline") project = "file://google.html <- https://www.google.com/ ! download" rcode, output = run_tuttle_file(project) if output.find("SSL certificate problem: unable to get local issuer certificate") >= 0: raise SkipTest("Skip test because of a certificate bug from appveyor") assert rcode == 0, output assert isfile("google.html") content = open("google.html").read() assert content.find("<title>Google</title>") >= 0 logs = open(join(".tuttle", "processes", "logs", "tuttlefile_1_stdout.txt"), "r").read() assert re.search("\n\.+\n", logs) is not None, logs assert isfile(join(".tuttle", "processes", "logs", "tuttlefile_1_err.txt")) def run_ftp_server(self): authorizer = DummyAuthorizer() ftp_dir = join(dirname(__file__), 'ftp') authorizer.add_user("user", "password", ftp_dir, perm="elrd") handler = FTPHandler handler.authorizer = authorizer self._ftpd = FTPServer(("0.0.0.0", 8021), handler) self._ftpd.serve_forever(timeout=0.2, handle_exit=True) @isolate def test_download_ftp_resource(self): """Download processor should be able to download an ftp resource with authentification """ from threading import Thread p = Thread(target=self.run_ftp_server) p.start() try: sleep(0.1) # The server needs time to start project = """file://downloaded_resource <- ftp://localhost:8021/ftp_resource ! download """ passfile = join(dirname(__file__), '.tuttlepass') with EnvVar('TUTTLEPASSFILE', passfile): rcode, output = run_tuttle_file(project) assert rcode == 0, output assert isfile('downloaded_resource') finally: self._ftpd.close_all() self._ftpd.ioloop.close() p.join()
class FTPtest(threading.Thread): # Start the FTP server in a thread with a dummy authorizer, where we can later on feed users to def __init__(self): threading.Thread.__init__(self) # Instantiate a dummy authorizer for managing 'virtual' users self.authorizer = DummyAuthorizer() # Instantiate FTP handler class self.handler = FTPHandler self.handler.authorizer = self.authorizer # Specify a masquerade address and the range of ports to use for # passive connections. Decomment in case you're behind a NAT. #handler.masquerade_address = '151.25.42.11' #handler.passive_ports = range(60000, 65535) # Instantiate FTP server class and listen on every IP address of the machine on port 2121 # If you use this port, please use the iptables command provided in the installation documentation # If you configured your system so users can listen on ports below 1024, you can set this to 21 and fotget the command self.address = ('', 2121) self.server = FTPServer(self.address, self.handler) # set a limit for connections self.server.max_cons = 256 self.server.max_cons_per_ip = 5 # Define a new user having full r/w permissions and a read-only def addUser(self, username, password): directory = os.getcwd() + "/testdata" print "adding user " + username + " and pw " + password self.authorizer.add_user(username, password, directory, perm="elradfmw") # Removes an user from the server def delUser(self, username): self.authorizer.remove_user(username) print "deletd user", username # Get some random directories to create within the server def getRandomDirs(self): htdocDirs = ["htdocs", "www", "www-data", "htdoc", "web"] htdocDir = random.choice(htdocDirs) dirs = [ "isos", "wareZ", "pron", "files", "logs", "conficential", "misc", "funny", "photos", "gf", "share", "saves" ] dirs.append(htdocDir) numOfPossibleDirs = len(dirs) numOfDirs = random.randrange(0, numOfPossibleDirs) return random.sample(set(dirs), numOfDirs) # Get some random timestamps between the set start timestamp and now, so the timestamps on the server aren't weird def getRandomTimestamp(self): endTimestamp = time.time() startTimestamp = 1353073006.547738 return startTimestamp + random.random() * (endTimestamp - startTimestamp) # Copy a random subset of data from the possibledata directory to the FTP server directory - then create some random directorys def populateFiles(self): # The path to the bait and safe data source = os.getcwd() + "/possibledata/" # The path to the FTP server directory destination = os.getcwd() + "/testdata/" # The uid and gid of the current user currentUser = os.getuid() currentGroup = os.getegid() # Whipe the whole directory the server had before and create a new one shutil.rmtree(destination, ignore_errors=True) os.mkdir(destination) filesToCleanUp = os.listdir(destination) for fileToRemove in filesToCleanUp: os.remove(destination + fileToRemove) # Get the random subset of files from the bait and safe data and copy them into the FTP directory allPossibleFiles = os.listdir(source) print allPossibleFiles numOfPossibleFiles = len(allPossibleFiles) numOfFilesToCopy = random.randrange(1, numOfPossibleFiles) filesToCopy = random.sample(set(allPossibleFiles), numOfFilesToCopy) for fileToCopy in filesToCopy: dstFile = destination + fileToCopy shutil.copy2(source + fileToCopy, dstFile) os.chown(dstFile, currentUser, currentGroup) fileTimestamp = self.getRandomTimestamp() os.utime(dstFile, (fileTimestamp, fileTimestamp)) # Create the random directories dirsToCreate = self.getRandomDirs() for dirToCreate in dirsToCreate: dstPath = destination + dirToCreate os.mkdir(dstPath) dirTimestamp = self.getRandomTimestamp() os.utime(dstPath, (dirTimestamp, dirTimestamp)) # start the FTP server def run(self): self.server.serve_forever() # stop the FTP server def stop(self): self.server.close_all()
for tn in ['01','02','03','04','05','06','07','08','09','10']: if (tfalse(addst(tn+"-enable","false"))==True): tt={}; tt['path']=xbmc.validatePath(xbmc.translatePath(addst(tn+"-path","special://logpath"))) #.replace(pFindWhat,"|tag|").replace("|tag|",pReplaceWith) tt['user']=addst(tn+"-user",""); tt['pass']=addst(tn+"-pass","xbmchub"); tt['perm']=addst(tn+"-perm","elradfmwM") if (len(tt['user']) > 0) and (len(tt['path']) > 0): print "user : "******" : path :"+str(tt['path'])+" :" try: authorizer.add_user(tt['user'],tt['pass'],tt['path'],perm=tt['perm']) except: print"Error adding user: "******"pyftpdlib based ftpd ready." try: LiP=addst("address","") except: LiP="" try: Lport=int(addst("port","2121")) except: Lport=2121 address=(LiP,Lport); server=FTPServer(address,handler); server.max_cons=int(addst("max-connections","5")); server.max_cons_per_ip=int(addst("max-connections-per-ip","5")); print "Starting Server... Port: "+str(Lport); notification("FTP Server","Starting Server... Port: "+str(Lport)) #server.serve_forever() try: server.serve_forever(timeout=int(addst("timeout","10")),blocking=False) except: pass elif (STARTUP==False) and (ENABLED==True): try: server.serve_forever(timeout=int(addst("timeout","10")),blocking=False) except: pass print "Service While Loop has been exited."; print "isEnabled="+str(ENABLED); print "Attempting to Close Server...."; notification("FTP Server","Attempting to Close Server....") addstv("is-serivce-running","false") try: server.ip_map=[] except: print "failed: server.ip_map=[]"; pass try: server.socket.close() except: print "failed: server.socket.close()"; pass try: server.close_all() except: pass sys.exit()
class TFtpServer(): def __init__(self, gui): self.gui = gui self.trayIcon = gui.trayIcon self.started = False def generalConfig(self): self.prefs = configparser.ConfigParser() self.prefs.read(getConfGeneralFilePath()) def loadUsers(self): self.configUsers = configparser.ConfigParser() self.configUsers.read(getConfUsersFilePath()) self.usernames = self.configUsers.sections() for u in self.usernames: username = u password = self.configUsers[u]['password'] root_folder = self.configUsers[u]['root_folder'] if (self.configUsers[u]['readonly'] == 'True'): perm = 'elr' else: perm = 'elradfmwMT' self.authorizer.add_user(username, password, root_folder, perm) def start(self): try: self.authorizer = DummyAuthorizer() #Anonymous user self.generalConfig() if (self.prefs['anonymous']['enable'] == 'True'): if (self.prefs['anonymous']['readonly'] == 'False'): self.authorizer.add_anonymous( self.prefs['anonymous']['root_folder'], perm='elradfmwMT') else: self.authorizer.add_anonymous( self.prefs['anonymous']['root_folder'], perm='elr') #Users confUsersFile = getConfUsersFilePath() if (os.path.isfile(confUsersFile)): self.loadUsers() if (self.prefs['general']['enable_ftps'] == 'False'): self.handler = NotifyHandler self.handler.bindGui(self.handler, self.gui) self.handler.authorizer = self.authorizer self.handler.passive_ports = range(60000, 65535) self.handler.permit_foreign_addresses = True elif (self.prefs['general']['enable_ftps'] == 'True'): self.handler = NotifyHandlerFTPS self.handler.bindGui(self.handler, self.gui) self.handler.certfile = self.prefs['general']['ssl_cert'] self.handler.authorizer = self.authorizer self.handler.tls_control_required = True self.handler.tls_data_required = True self.handler.passive_ports = range(60000, 65535) self.handler.permit_foreign_addresses = True self.server = FTPServer( ("0.0.0.0", self.prefs['general']['ftp_port']), self.handler) self.started = True wx.CallAfter(self.gui.serverStarted) self.server.serve_forever() except OSError as e: print("Os Error: %s" % e) self.started = False wx.CallAfter(self.gui.serverFailed, e) except ValueError as e: print("Value Error : %s" % e) self.started = False wx.CallAfter(self.gui.serverFailed, e) except Exception as e: print("Error : %s" % e) self.started = False wx.CallAfter(Warn, self.gui.mainFrame, str(e)) def stop(self): self.server.close_all() self.started = False wx.CallAfter(self.gui.serverNormal) del self
class dlgFTPD(TdlgFTPD): server = None handler = None def __init__(self, parent): import locale, gettext from pobhelp import getScriptDir try: t = gettext.translation('pobhelp', getScriptDir() + os.sep + 'locale') _ = t.gettext except: _ = gettext.gettext TdlgFTPD.__init__(self, parent) self.lblUser.SetLabel(_("User:"******"Port:")) self.lblRootFolder.SetLabel(_("Root folder:")) self.btStop.SetLabel(_("Stop")) self.btStart.SetLabel(_("Start")) def onText(self, evt): p = self.txtOutput.GetCaretPosition() self.txtOutput.ScrollIntoView(p, wx.WXK_END) def thdFTPD(self): authorizer = DummyAuthorizer() authorizer.add_user(self.entryUser.GetValue(), self.entryPassword.GetValue(), self.dirPkrFTPRoot.GetPath(), perm='elradfmwMT') self.handler = NotifyHandler self.handler.txtOutput = self.txtOutput self.handler.authorizer = authorizer self.handler.banner = "[Pobhelp] Pyftpdlib based ftpd ready." address = ('0.0.0.0', self.entryPort.GetValue()) self.server = FTPServer(address, self.handler) self.server.max_cons = 256 self.server.max_cons_per_ip = 5 self.server.serve_forever() def startServer(self, evt): self.btStart.Enable(False) self.btStop.Enable(True) self.txtOutput.WriteText(' *** (' + getDatetime() + ') Server Started ***') self.txtOutput.Newline() thFtpd = threading.Thread(target=self.thdFTPD) thFtpd.daemon = True thFtpd.start() def stopServer(self, evt): self.server.close_all() self.btStart.Enable(True) self.btStop.Enable(False) self.txtOutput.WriteText(' *** (' + getDatetime() + ') Server Stopped by user ***') self.txtOutput.Newline()
class FTPtest(threading.Thread): # Start the FTP server in a thread with a dummy authorizer, where we can later on feed users to def __init__(self): threading.Thread.__init__(self) # Instantiate a dummy authorizer for managing 'virtual' users self.authorizer = DummyAuthorizer() # Instantiate FTP handler class self.handler = FTPHandler self.handler.authorizer = self.authorizer # Specify a masquerade address and the range of ports to use for # passive connections. Decomment in case you're behind a NAT. # handler.masquerade_address = '151.25.42.11' # handler.passive_ports = range(60000, 65535) # Instantiate FTP server class and listen on every IP address of the machine on port 2121 # If you use this port, please use the iptables command provided in the installation documentation # If you configured your system so users can listen on ports below 1024, you can set this to 21 and fotget the command self.address = ("", 2121) self.server = FTPServer(self.address, self.handler) # set a limit for connections self.server.max_cons = 256 self.server.max_cons_per_ip = 5 # Define a new user having full r/w permissions and a read-only def addUser(self, username, password): directory = os.getcwd() + "/testdata" print "adding user " + username + " and pw " + password self.authorizer.add_user(username, password, directory, perm="elradfmw") # Removes an user from the server def delUser(self, username): self.authorizer.remove_user(username) print "deletd user", username # Get some random directories to create within the server def getRandomDirs(self): htdocDirs = ["htdocs", "www", "www-data", "htdoc", "web"] htdocDir = random.choice(htdocDirs) dirs = [ "isos", "wareZ", "pron", "files", "logs", "conficential", "misc", "funny", "photos", "gf", "share", "saves", ] dirs.append(htdocDir) numOfPossibleDirs = len(dirs) numOfDirs = random.randrange(0, numOfPossibleDirs) return random.sample(set(dirs), numOfDirs) # Get some random timestamps between the set start timestamp and now, so the timestamps on the server aren't weird def getRandomTimestamp(self): endTimestamp = time.time() startTimestamp = 1353073006.547738 return startTimestamp + random.random() * (endTimestamp - startTimestamp) # Copy a random subset of data from the possibledata directory to the FTP server directory - then create some random directorys def populateFiles(self): # The path to the bait and safe data source = os.getcwd() + "/possibledata/" # The path to the FTP server directory destination = os.getcwd() + "/testdata/" # The uid and gid of the current user currentUser = os.getuid() currentGroup = os.getegid() # Whipe the whole directory the server had before and create a new one shutil.rmtree(destination, ignore_errors=True) os.mkdir(destination) filesToCleanUp = os.listdir(destination) for fileToRemove in filesToCleanUp: os.remove(destination + fileToRemove) # Get the random subset of files from the bait and safe data and copy them into the FTP directory allPossibleFiles = os.listdir(source) print allPossibleFiles numOfPossibleFiles = len(allPossibleFiles) numOfFilesToCopy = random.randrange(1, numOfPossibleFiles) filesToCopy = random.sample(set(allPossibleFiles), numOfFilesToCopy) for fileToCopy in filesToCopy: dstFile = destination + fileToCopy shutil.copy2(source + fileToCopy, dstFile) os.chown(dstFile, currentUser, currentGroup) fileTimestamp = self.getRandomTimestamp() os.utime(dstFile, (fileTimestamp, fileTimestamp)) # Create the random directories dirsToCreate = self.getRandomDirs() for dirToCreate in dirsToCreate: dstPath = destination + dirToCreate os.mkdir(dstPath) dirTimestamp = self.getRandomTimestamp() os.utime(dstPath, (dirTimestamp, dirTimestamp)) # start the FTP server def run(self): self.server.serve_forever() # stop the FTP server def stop(self): self.server.close_all()
class tkFTP(): def __init__(self): #Configuring window window = tk.Tk() window.title("FTP Server") window.geometry("320x180") window.resizable(width=False, height=False) window.grid_columnconfigure((0, 1), weight=1, uniform="fred") window.iconbitmap("./smiley.ico") #FTP variables hostname = gethostname() cwd = getcwd() authorizer = DummyAuthorizer() authorizer.add_anonymous(cwd) self.handler = FTPHandler self.handler.authorizer = authorizer self.IPAddr = gethostbyname(hostname) self.p = 21 self.server = None self.widgets() self.placeGrid() window.mainloop() def widgets(self): self.greeting = tk.Label(text="Welcome to !ncredible Tech FTP Server", font=("", 10, 'bold')) self.host_msg = tk.Label(text="Host IP:", font=("", 10, 'italic'), anchor='e') self.port_msg = tk.Label(text="Port:", font=("", 10, 'italic'), anchor="e") self.host = tk.Label(text=self.IPAddr, font=("", 10, 'bold')) self.port = tk.Entry(width=10, font=("", 10, 'bold')) self.port.insert(0, "21") self.start = tk.Button(text="Start server", command=self.startServer, font=("", 11, 'bold')) self.stop = tk.Button(text="Stop server", command=self.stopServer, font=("", 11, 'bold')) self.link = tk.Label(text="Server stopped!", fg="blue", cursor="hand2", font=("", 11, 'bold')) def placeGrid(self): self.greeting.grid(row=0, column=0, columnspan=2, padx=15, pady=5) self.host_msg.grid(row=1, column=0, pady=0) self.host.grid(row=1, column=1, pady=0) self.port_msg.grid(row=2, column=0, pady=0) self.port.grid(row=2, column=1, pady=0) self.start.grid(row=3, column=0, padx=15, pady=10) self.stop.grid(row=3, column=1, padx=15, pady=10) self.link.grid(row=4, column=0, columnspan=2, padx=15, pady=5) def startServer(self): try: self.p = int(self.port.get()) except: pass self.server = FTPServer((self.IPAddr, self.p), self.handler) srv = Thread(target=self.server.serve_forever, daemon=True) srv.start() self.link['text'] = "Server started @ ftp://" + str( self.IPAddr) + ":" + str(self.p) def stopServer(self): self.link['text'] = "Server stopped!" self.server.close_all()
class SimpleFtpWindow(QtGui.QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setFixedSize(415, 185) self.setWindowTitle('Simple FTP Server') self.Directory = QtGui.QLineEdit(self) self.Directory.setGeometry(10, 50, 270, 25) self.SetDirectory = QtGui.QPushButton(self) self.SetDirectory.setGeometry(285, 50, 120, 25) self.groupBox = QtGui.QGroupBox(self) self.groupBox.setGeometry(10, 90, 395, 50) self.OnButton = QtGui.QRadioButton(self.groupBox) self.OnButton.setGeometry(90, 20, 90, 15) self.OffButton = QtGui.QRadioButton(self.groupBox) self.OffButton.setGeometry(220, 20, 90, 15) self.Subject = QtGui.QLabel(self) self.Subject.setGeometry(10, 10, 395, 30) self.Result = QtGui.QLabel(self) self.Result.setGeometry(10, 150, 395, 30) self.SetDirectory.setText("Choose Directoy") self.groupBox.setTitle("Power") self.Result.setText("") self.OnButton.setText("On") self.OffButton.setText("Off") self.OffButton.setChecked(1) self.Subject.setText("Simple FTP Server") self.action() self.show() def action(self): self.SetDirectory.clicked.connect(self.openDirectoryDialog) self.OnButton.clicked.connect(self.on_clicked) self.OffButton.clicked.connect(self.off_clicked) def openDirectoryDialog(self): #path, _ = QtGui.QFileDialog.getOpenFileName(self, "Open File", os.getcwd()) self.flags = QtGui.QFileDialog.DontResolveSymlinks | QtGui.QFileDialog.ShowDirsOnly self.path = directory = QtGui.QFileDialog.getExistingDirectory(self, "Open Directory", os.getcwd(), self.flags) self.Directory.setText(self.path) def on_clicked(self): #global server authorizer = DummyAuthorizer() try: authorizer.add_anonymous(self.Directory.text()) except: authorizer.add_anonymous(os.getcwd()) logging.basicConfig(filename='ftpd.log', level=logging.INFO) handler = FTPHandler handler.authorizer = authorizer self.server = FTPServer(('0.0.0.0', 21), handler) self.th = threading.Thread(target=self.server.serve_forever) self.th.start() self.Result.setText('on, 127.0.0.1:21, anonymous') def off_clicked(self): self.server.close_all() self.th.join() self.Result.setText('off') def closeEvent(self, event): reply = QtGui.QMessageBox.question(self, 'Message', "Are you sure to quit?", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: event.accept() else: event.ignore()
class QuickFtpServer: """ This class provides a tfp server configured through a config file. """ def __init__(self, _configfile): """ Constructor :param _configfile: path to the configuration file. """ if not _configfile: raise Exception('Missing parameter _configfile') self.configfile = os.path.abspath(_configfile) # parse config file parameters = Helpers.get_param_from_config_file( self.configfile, ['ip', 'port', 'ftp_root_dir', 'clients'], ['client_timeout', 'pem_certificate']) self.ip = parameters['ip'] self.port = int(parameters['port']) self.root_dir = os.path.abspath(parameters['ftp_root_dir']) # use certificate ? if parameters['pem_certificate']: pem = os.path.abspath(parameters['pem_certificate']) logging.info('TLS mode use %s', pem) if not os.path.exists(pem): raise IOError('File: ' + pem + ' not found') self.handler = TLS_FTPHandler self.handler.certfile = pem else: logging.info('Unsecure mode') self.handler = FTPHandler # Use timeout ? if parameters['client_timeout']: self.handler.timeout = int(parameters['client_timeout']) if not os.path.exists(self.root_dir): logging.debug('Create %s', self.root_dir) os.makedirs(self.root_dir) self.authorizer = QuickFtpAuthorizer() for client in parameters['clients']: user_dir = os.path.join(self.root_dir, client['directory']) if not os.path.exists(user_dir): logging.debug('Create %s', user_dir) os.makedirs(user_dir) logging.info('Add user %s with directory %s', client['name'], user_dir) self.authorizer.add_user(client['name'], client['password'], user_dir, perm='elr') self.handler.authorizer = self.authorizer def serve(self): """ run the server forever :return: """ self.server_inst = FTPServer((self.ip, self.port), self.handler) logging.info('Listen on %s:%d', self.ip, self.port) self.server_inst.serve_forever() def terminate(self): """ Terminate the server :return: """ self.server_inst.close_all() logging.info('closed_all')
def main(): """Start a stand alone anonymous FTP server.""" usage = "python -m pyftpdlib.ftpserver [options]" parser = optparse.OptionParser(usage=usage, description=main.__doc__, formatter=CustomizedOptionFormatter()) parser.add_option('-i', '--interface', default=None, metavar="ADDRESS", help="specify the interface to run on (default all " "interfaces)") parser.add_option('-p', '--port', type="int", default=2121, metavar="PORT", help="specify port number to run on (default 21)") parser.add_option('-w', '--write', action="store_true", default=False, help="grants write access for the anonymous user " "(default read-only)") parser.add_option('-d', '--directory', default=getcwdu(), metavar="FOLDER", help="specify the directory to share (default current " "directory)") parser.add_option('-n', '--nat-address', default=None, metavar="ADDRESS", help="the NAT address to use for passive connections") parser.add_option('-r', '--range', default=None, metavar="FROM-TO", help="the range of TCP ports to use for passive " "connections (e.g. -r 8000-9000)") parser.add_option('-v', '--version', action='store_true', help="print pyftpdlib version and exit") parser.add_option('-V', '--verbose', action='store_true', help="activate a more verbose logging") options, args = parser.parse_args() if options.version: sys.exit("pyftpdlib %s" % __ver__) if options.verbose: import logging import pyftpdlib.log pyftpdlib.log.LEVEL = logging.DEBUG passive_ports = None if options.range: try: start, stop = options.range.split('-') start = int(start) stop = int(stop) except ValueError: parser.error('invalid argument passed to -r option') else: passive_ports = list(range(start, stop + 1)) # On recent Windows versions, if address is not specified and IPv6 # is installed the socket will listen on IPv6 by default; in this # case we force IPv4 instead. if os.name in ('nt', 'ce') and not options.interface: options.interface = '0.0.0.0' authorizer = DummyAuthorizer() perm = options.write and "elradfmwM" or "elr" authorizer.add_anonymous(options.directory, perm=perm) handler = FTPHandler handler.authorizer = authorizer handler.masquerade_address = options.nat_address handler.passive_ports = passive_ports ftpd = FTPServer((options.interface, options.port), FTPHandler) try: ftpd.serve_forever() finally: ftpd.close_all()
class FTP_server(object): def __init__(self): super(FTP_server, self).__init__() self.ip = "127.0.0.1" self.port = '21' self.max_download = 100 * 1024 self.max_upload = 100 * 1024 self.max_cons = 100 self.max_pre_ip = 10 self.passive_port = (8300, 8500) self.enable_anonymous = True self.anonymous_dir = "" self.log_file = r"..\pyftp.log" self.welcome_msg = "welcome!" self.server_status = False self.setup() def setup(self): # 添加匿名用户 只需要路径 self.dtp_handler = ThrottledDTPHandler self.authorizer = DummyAuthorizer() self.handler = FTPHandler self.loadconfig() if self.enable_anonymous == True: self.authorizer.add_anonymous(self.anonymous_dir) # 下载上传速度设置 self.dtp_handler.read_limit = self.max_download self.dtp_handler.write_limit = self.max_upload # 添加被动端口范围 self.handler.passive_ports = range(self.passive_port[0], self.passive_port[1]) # 欢迎信息 self.handler.banner = self.welcome_msg self.server = FTPServer((self.ip, self.port), self.handler) self.server.max_cons = self.max_cons self.server.max_cons_per_ip = self.max_pre_ip def loadconfig(self): config = configparser.ConfigParser() # config.read("conf.ini") # FTP = config.sections()[0] # self.ip = config[FTP]["IP"] # self.port = config[FTP]["PORT"] # self.max_download = config[FTP]["MAX_DOWNLOAD"] # self.max_upload = config[FTP]["MAX_UPLOAD"] # self.max_cons = config[FTP]["MAX_CONS"] # self.max_pre_ip = config[FTP]["MAX_PER_IP"] # self.passive_port = list(config[FTP]["PASSIVE_PORT"]) # self.enable_anonymous = config[FTP]["ENABLE_ANONYMOUS"] # self.anonymous_dir = config[FTP]["ANONYMOUS_DIR"] # self.welcome_msg = config[FTP]["WELCOME_MSG"] self.ip = IP self.port = PORT self.max_download = MAX_DOWNLOAD self.max_upload = MAX_UPLOAD self.max_cons = MAX_CONS self.max_pre_ip = MAX_PER_IP self.passive_port = list(PASSIVE_PORT) self.enable_anonymous = ENABLE_ANONYMOUS self.anonymous_dir = ANONYMOUS_DIR self.welcome_msg = WELCOME_MSG users = configparser.ConfigParser() users.read('user.ini') user_list = users.sections() for user in user_list: passwd = users[user]["password"] perm = users[user]["perm"] home_dir = users[user]["home"] self.authorizer.add_user(user, passwd, homedir=home_dir, perm=perm) self.handler.authorizer = self.authorizer def startServer(self): self.server_status = True self.server.serve_forever() def close(self): self.server_status = False self.server.close_all()