def _loop(self, handler): """Serve handler's IO loop in a separate thread or process.""" ioloop = IOLoop() try: handler.ioloop = ioloop try: handler.add_channel() except EnvironmentError: err = sys.exc_info()[1] if err.errno == errno.EBADF: # we might get here in case the other end quickly # disconnected (see test_quick_connect()) return else: raise # Here we localize variable access to minimize overhead. poll = ioloop.poll socket_map = ioloop.socket_map tasks = ioloop.sched._tasks sched_poll = ioloop.sched.poll poll_timeout = getattr(self, 'poll_timeout', None) soonest_timeout = poll_timeout while socket_map and not self._exit.is_set(): try: poll(timeout=soonest_timeout) if tasks: soonest_timeout = sched_poll() else: soonest_timeout = None except (KeyboardInterrupt, SystemExit): # note: these two exceptions are raised in all sub # processes self._exit.set() except select.error: # on Windows we can get WSAENOTSOCK if the client # rapidly connect and disconnects err = sys.exc_info()[1] if os.name == 'nt' and err.args[0] == 10038: for fd in socket_map.keys(): try: select.select([fd], [], [], 0) except select.error: logger.info("discarding broken socket %r", socket_map[fd]) del socket_map[fd] else: raise else: if poll_timeout: if soonest_timeout is None \ or soonest_timeout > poll_timeout: soonest_timeout = poll_timeout finally: try: self._active_tasks.remove(self._current_task()) except ValueError: pass ioloop.close()
def __init__(self, username, password, address, port, use_ssl, top_dir): self.ioloop = IOLoop() self.thread = threading.Thread(target=run_ftp_server, args=[ username, password, address, port, use_ssl, top_dir, self.ioloop ])
class TestFTPServer(TestServer): def __init__(self, username, password, address, port, use_ssl, top_dir): self.ioloop = IOLoop() self.thread = threading.Thread(target=run_ftp_server, args=[ username, password, address, port, use_ssl, top_dir, self.ioloop ]) def stop(self): self.ioloop.close() self.thread.join()
def get_server_handler(): """Return the first FTPHandler instance running in the IOLoop.""" ioloop = IOLoop.instance() for fd in ioloop.socket_map: instance = ioloop.socket_map[fd] if isinstance(instance, FTPHandler): return instance raise RuntimeError("can't find any FTPHandler instance")
def cleanup(): """Cleanup function executed on interpreter exit.""" map = IOLoop.instance().socket_map for x in list(map.values()): try: sys.stderr.write("garbage: %s\n" % repr(x)) x.close() except Exception: pass map.clear()
def run(self): self._serving = True self._flag_started.set() started = time.time() try: while self._serving: with self._lock: self.server.serve_forever(timeout=self._timeout, blocking=False) if self.shutdown_after and time.time() >= started + self.shutdown_after: now = time.time() if now <= now + self.shutdown_after: print("shutting down test FTPd due to timeout") self.server.close_all() raise Exception("test FTPd shutdown due to timeout") print(IOLoop.instance()) self.server.close_all() print(IOLoop.instance()) finally: self._flag_stopped.set()
def __init__(self, hub): self.hub = hub self.args = hub.args hs = [] if self.args.ftp: hs.append([FtpHandler, self.args.ftp]) if self.args.ftps: try: h = SftpHandler except: m = "\nftps requires pyopenssl;\nplease run the following:\n\n {} -m pip install --user pyopenssl\n" print(m.format(sys.executable)) sys.exit(1) h.certfile = os.path.join(E.cfg, "cert.pem") h.tls_control_required = True h.tls_data_required = True hs.append([h, self.args.ftps]) for h in hs: h, lp = h h.hub = hub h.args = hub.args h.authorizer = FtpAuth() h.authorizer.hub = hub if self.args.ftp_pr: p1, p2 = [int(x) for x in self.args.ftp_pr.split("-")] if self.args.ftp and self.args.ftps: # divide port range in half d = int((p2 - p1) / 2) if lp == self.args.ftp: p2 = p1 + d else: p1 += d + 1 h.passive_ports = list(range(p1, p2 + 1)) if self.args.ftp_nat: h.masquerade_address = self.args.ftp_nat if self.args.ftp_dbg: config_logging(level=logging.DEBUG) ioloop = IOLoop() for ip in self.args.i: for h, lp in hs: FTPServer((ip, int(lp)), h, ioloop) t = threading.Thread(target=ioloop.loop) t.daemon = True t.start()
def cleanup(): """Cleanup function executed on interpreter exit.""" remove_test_files() map = IOLoop.instance().socket_map for x in list(map.values()): try: sys.stderr.write("garbage: %s\n" % repr(x)) x.close() except Exception: pass map.clear()
def handle(self): """Hands control off to pyftpd to process the client connection. """ # server attributes/methods expected by pyftp handler self.server.backlog = 50 self.server.ip_map = [] self.server._accept_new_cons = lambda: True self.server._af = socket.AF_INET tmpdir = None try: # set up a temp dir as the ftp root for the user tmpdir = tempfile.mkdtemp(prefix='tmpftp') ftproot = os.path.join(tmpdir, self.dirseed).decode('utf-8') shutil.copytree(get_data_file(self.dirseed), ftproot) # hand off control to their handler with its own async ioloop handler = PyFTPHandler(self.request, self.server, ioloop=IOLoop()) handler.authorizer = PermissiveAuthorizer(ftproot) handler.handle() handler.ioloop.loop(1) finally: if handler.ioloop: handler.ioloop.close() if tmpdir: shutil.rmtree(tmpdir)
def setUp(self): self.ioloop = IOLoop.instance() for task in self.ioloop.sched._tasks: if not task.cancelled: task.cancel() del self.ioloop.sched._tasks[:]
def _loop(self, handler): """Serve handler's IO loop in a separate thread or process.""" ioloop = IOLoop() try: handler.ioloop = ioloop try: handler.add_channel() except EnvironmentError: err = sys.exc_info()[1] if err.errno == errno.EBADF: # we might get here in case the other end quickly # disconnected (see test_quick_connect()) return else: raise # Here we localize variable access to minimize overhead. poll = ioloop.poll sched_poll = ioloop.sched.poll poll_timeout = getattr(self, 'poll_timeout', None) soonest_timeout = poll_timeout while (ioloop.socket_map or ioloop.sched._tasks) and \ not self._exit.is_set(): try: if ioloop.socket_map: poll(timeout=soonest_timeout) if ioloop.sched._tasks: soonest_timeout = sched_poll() # Handle the case where socket_map is emty but some # cancelled scheduled calls are still around causing # this while loop to hog CPU resources. # In theory this should never happen as all the sched # functions are supposed to be cancel()ed on close() # but by using threads we can incur into # synchronization issues such as this one. # https://github.com/giampaolo/pyftpdlib/issues/245 if not ioloop.socket_map: # get rid of cancel()led calls ioloop.sched.reheapify() soonest_timeout = sched_poll() if soonest_timeout: time.sleep(min(soonest_timeout, 1)) else: soonest_timeout = None except (KeyboardInterrupt, SystemExit): # note: these two exceptions are raised in all sub # processes self._exit.set() except select.error: # on Windows we can get WSAENOTSOCK if the client # rapidly connect and disconnects err = sys.exc_info()[1] if os.name == 'nt' and err.args[0] == 10038: for fd in list(ioloop.socket_map.keys()): try: select.select([fd], [], [], 0) except select.error: try: logger.info("discarding broken socket %r", ioloop.socket_map[fd]) del ioloop.socket_map[fd] except KeyError: # dict changed during iteration pass else: raise else: if poll_timeout: if (soonest_timeout is None or soonest_timeout > poll_timeout): soonest_timeout = poll_timeout finally: ioloop.close()
def __init__(self, *args, **kwargs): _depwarn("CallEvery is deprecated; use " "pyftpdlib.ioloop.IOLoop.instance().call_every() instead") from pyftpdlib.ioloop import IOLoop IOLoop.instance().call_every(*args, **kwargs)
def _loop(self, handler): """Serve handler's IO loop in a separate thread or process.""" ioloop = IOLoop() try: handler.ioloop = ioloop try: handler.add_channel() except EnvironmentError: err = sys.exc_info()[1] if err.errno == errno.EBADF: # we might get here in case the other end quickly # disconnected (see test_quick_connect()) return else: raise # Here we localize variable access to minimize overhead. poll = ioloop.poll socket_map = ioloop.socket_map tasks = ioloop.sched._tasks sched_poll = ioloop.sched.poll poll_timeout = getattr(self, 'poll_timeout', None) soonest_timeout = poll_timeout while (socket_map or tasks) and not self._exit.is_set(): try: if socket_map: poll(timeout=soonest_timeout) if tasks: soonest_timeout = sched_poll() else: soonest_timeout = None except (KeyboardInterrupt, SystemExit): # note: these two exceptions are raised in all sub # processes self._exit.set() except select.error: # on Windows we can get WSAENOTSOCK if the client # rapidly connect and disconnects err = sys.exc_info()[1] if os.name == 'nt' and err.args[0] == 10038: for fd in list(socket_map.keys()): try: select.select([fd], [], [], 0) except select.error: try: logger.info("discarding broken socket %r", socket_map[fd]) del socket_map[fd] except KeyError: # dict changed during iteration pass else: raise else: if poll_timeout: if soonest_timeout is None \ or soonest_timeout > poll_timeout: soonest_timeout = poll_timeout finally: try: self._active_tasks.remove(self._current_task()) except ValueError: pass ioloop.close()