def do_setup(self): try: properties = self.config['properties'] # setup the passive profiles if self.http_profiles: root = Resource() for p in self.http_profiles: vdir = self.profiles_virtualbase[p] root.putChild(p, RequestHandler(self, vdir, callback=self._file_added_http, profile_name=p)) local_dir = vdir.localize(self._local) safe_mkdirs(local_dir, "monitored") factory = Site(root) self.port = properties.get("port", 7680) self._listener = IReactorTCP(reactor).listenTCP(self.port, factory) if not self.port: # setting port to 0 means random port, read and store. self.port = self._listener.getHost().port # setup the active profiles self._scanPeriod = properties.get("scan-period", 10) for p in self.active_profiles: vdir = self.profiles_virtualbase[p] local_dir = vdir.localize(self._local) safe_mkdirs(local_dir, "monitored", self._pathAttr) watcher = DirectoryWatcher(self, local_dir, p, timeout=self._scanPeriod) watcher.connect('file-added', self._file_added, vdir, p) watcher.connect('file-completed', self._file_completed, vdir, p) watcher.connect('file-removed', self._file_removed, vdir, p) watcher.start() self.watchers.append(watcher) setup_callback = properties.get('setup-callback') if setup_callback: data = {'hostname': gethostname(), 'port': self.port} url = setup_callback % data getPage(url, method='POST') # htpp://sdfsdkfjlsdjfklsdf/?host=%(host)s&myport=%(port)s # htpp://sdfsdkfjlsdjfklsdf/?myip=123.123.123.123&myport=1234 # TODO: make the http call to register. except: return fail(self._unexpected_error(task="component setup"))
def listen_tcp( bind_addresses: Collection[str], port: int, factory: ServerFactory, reactor: IReactorTCP = reactor, backlog: int = 50, ) -> List[Port]: """ Create a TCP socket for a port and several addresses Returns: list of twisted.internet.tcp.Port listening for TCP connections """ r = [] for address in bind_addresses: try: r.append(reactor.listenTCP(port, factory, backlog, address)) except error.CannotListenError as e: check_bind_error(e, address, bind_addresses) # IReactorTCP returns an object implementing IListeningPort from listenTCP, # but we know it will be a Port instance. return r # type: ignore[return-value]
from twisted.internet.interfaces import IReactorTCP from twisted.internet.protocol import Protocol, Factory class SimpleLogger(Protocol): def connectionMade(self): print('got connection from %s' % self.transport.client) def connectionLost(self, reason): print('%s disconnected' % self.transport.client) def dataReceived(self, data): print(data) factory = Factory() factory.protocol = SimpleLogger IReactorTCP.listenTCP(1234, factory) IReactorTCP.Run()
class HttpMonitor(MonitorBase): """ Launch a transcoding task based on an HTTP post request. """ logCategory = compconsts.HTTP_MONITOR_LOG_CATEGORY def init(self): self.watchers = [] self._scanPeriod = None self._directories = [] self.port = None self._listener = None def do_setup(self): try: properties = self.config['properties'] # setup the passive profiles if self.http_profiles: root = Resource() for p in self.http_profiles: vdir = self.profiles_virtualbase[p] root.putChild(p, RequestHandler(self, vdir, callback=self._file_added_http, profile_name=p)) local_dir = vdir.localize(self._local) safe_mkdirs(local_dir, "monitored") factory = Site(root) self.port = properties.get("port", 7680) self._listener = IReactorTCP(reactor).listenTCP(self.port, factory) if not self.port: # setting port to 0 means random port, read and store. self.port = self._listener.getHost().port # setup the active profiles self._scanPeriod = properties.get("scan-period", 10) for p in self.active_profiles: vdir = self.profiles_virtualbase[p] local_dir = vdir.localize(self._local) safe_mkdirs(local_dir, "monitored", self._pathAttr) watcher = DirectoryWatcher(self, local_dir, p, timeout=self._scanPeriod) watcher.connect('file-added', self._file_added, vdir, p) watcher.connect('file-completed', self._file_completed, vdir, p) watcher.connect('file-removed', self._file_removed, vdir, p) watcher.start() self.watchers.append(watcher) setup_callback = properties.get('setup-callback') if setup_callback: data = {'hostname': gethostname(), 'port': self.port} url = setup_callback % data getPage(url, method='POST') # htpp://sdfsdkfjlsdjfklsdf/?host=%(host)s&myport=%(port)s # htpp://sdfsdkfjlsdjfklsdf/?myip=123.123.123.123&myport=1234 # TODO: make the http call to register. except: return fail(self._unexpected_error(task="component setup")) def do_stop(self): if self._listener: self._listener.stopListening() for w in self.watchers: w.stop() self.watchers[:] = [] ## Signal Handler Methods ## def _file_added(self, watcher, file, fileinfo, detection_time, virt_base, profile_name): localFile = virt_base.append(file).localize(self._local) self.debug("File added : '%s'", localFile) # put here the parameters self._set_ui_item('pending-files', (profile_name, file), (MonitorFileStateEnum.downloading, fileinfo, detection_time, None, None, None)) def _file_completed(self, watcher, file, fileinfo, virt_base, profile_name): self.get_file_info(file, fileinfo, watcher.path, virt_base, profile_name=profile_name, params={}) def _file_removed(self, watcher, file, virtBase, profile_name): localFile = virtBase.append(file).localize(self._local) self.debug("File removed '%s'", localFile) self._del_ui_item('pending-files', (profile_name, file)) # a new request was received def _file_added_http(self, _, file_path, file, fileinfo, detection_time, virt_base, profile_name, params): local_file = virt_base.append(file).localize(self._local) self.debug("File added : '%s'", local_file) incoming_folder = os.path.dirname(file_path) + '/' # Set what we know in the UI. We'll set the rest after computing # the checksum self._set_ui_item('pending-files', (profile_name, file), (MonitorFileStateEnum.downloading, fileinfo, detection_time, None, None, params)) self.get_file_info(file, fileinfo, incoming_folder, virt_base, profile_name=profile_name, params=params)
async def _sendmail( reactor: IReactorTCP, smtphost: str, smtpport: int, from_addr: str, to_addr: str, msg_bytes: bytes, username: Optional[bytes] = None, password: Optional[bytes] = None, require_auth: bool = False, require_tls: bool = False, enable_tls: bool = True, ) -> None: """A simple wrapper around ESMTPSenderFactory, to allow substitution in tests Params: reactor: reactor to use to make the outbound connection smtphost: hostname to connect to smtpport: port to connect to from_addr: "From" address for email to_addr: "To" address for email msg_bytes: Message content username: username to authenticate with, if auth is enabled password: password to give when authenticating require_auth: if auth is not offered, fail the request require_tls: if TLS is not offered, fail the reqest enable_tls: True to enable TLS. If this is False and require_tls is True, the request will fail. """ msg = BytesIO(msg_bytes) d: "Deferred[object]" = Deferred() def build_sender_factory(**kwargs: Any) -> ESMTPSenderFactory: return ESMTPSenderFactory( username, password, from_addr, to_addr, msg, d, heloFallback=True, requireAuthentication=require_auth, requireTransportSecurity=require_tls, **kwargs, ) if _is_old_twisted: # before twisted 21.2, we have to override the ESMTPSender protocol to disable # TLS factory = build_sender_factory() if not enable_tls: factory.protocol = _NoTLSESMTPSender else: # for twisted 21.2 and later, there is a 'hostname' parameter which we should # set to enable TLS. factory = build_sender_factory( hostname=smtphost if enable_tls else None) reactor.connectTCP( smtphost, smtpport, factory, timeout=30, bindAddress=None, ) await make_deferred_yieldable(d)