def __init__(self): self._cmdNumber = 1 self.io = TerminalIO(self) stdio.StandardIO(self.io) self.first = True self.stop = False
def startService(self): j = MediaServerControlProtocol(False) stdio.StandardIO(j)
from twisted.python import log, reflect from twisted.internet import stdio, protocol from twisted.protocols import basic def failed(err): log.startLogging(sys.stderr) log.err(err) class ConsumerChild(protocol.Protocol): def __init__(self, junkPath): self.junkPath = junkPath def connectionMade(self): d = basic.FileSender().beginFileTransfer(file(self.junkPath), self.transport) d.addErrback(failed) d.addCallback(lambda ign: self.transport.loseConnection()) def connectionLost(self, reason): reactor.stop() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() from twisted.internet import reactor stdio.StandardIO(ConsumerChild(sys.argv[2])) reactor.run()
client_manager.sendDatapoint(metric, datapoint) except: log.err(None, 'Dropping invalid line: %s' % line) def connectionLost(self, reason): log.msg('stdin disconnected') def startShutdown(results): log.msg("startShutdown(%s)" % str(results)) allStopped = client_manager.stopAllClients() allStopped.addCallback(shutdown) firstConnectsAttempted.addCallback(startShutdown) stdio.StandardIO(StdinMetricsReader()) exitCode = 0 def shutdown(results): global exitCode for success, result in results: if not success: exitCode = 1 break if reactor.running: reactor.stop() reactor.run()
def ursula(click_config, action, dev, quiet, dry_run, force, lonely, network, teacher_uri, min_stake, rest_host, rest_port, db_filepath, checksum_address, withdraw_address, federated_only, poa, config_root, config_file, provider_uri, recompile_solidity, no_registry, registry_filepath, value, duration, index, list_, divide) -> None: """ Manage and run an "Ursula" PRE node. \b Actions ------------------------------------------------- \b init Create a new Ursula node configuration. view View the Ursula node's configuration. run Run an "Ursula" node. save-metadata Manually write node metadata to disk without running forget Forget all known nodes. destroy Delete Ursula node configuration. stake Manage stakes for this node. confirm-activity Manually confirm-activity for the current period. collect-reward Withdraw staking reward. """ # # Boring Setup Stuff # if not quiet: log = Logger('ursula.cli') if click_config.debug and quiet: raise click.BadOptionUsage( option_name="quiet", message="--debug and --quiet cannot be used at the same time.") if not click_config.json_ipc and not click_config.quiet: click.secho(URSULA_BANNER.format(checksum_address or '')) # # Pre-Launch Warnings # if not click_config.quiet: if dev: click.secho("WARNING: Running in Development mode", fg='yellow') if force: click.secho("WARNING: Force is enabled", fg='yellow') # # Unauthenticated Configurations & Un-configured Ursula Control # if action == "init": """Create a brand-new persistent Ursula""" if not network: raise click.BadArgumentUsage( '--network is required to initialize a new configuration.') if dev: click_config.emit(message="WARNING: Using temporary storage area", color='yellow') if not config_root: # Flag config_root = click_config.config_file # Envvar if not rest_host: rest_host = click.prompt( "Enter Ursula's public-facing IPv4 address" ) # TODO: Remove this step ursula_config = UrsulaConfiguration.generate( password=click_config.get_password(confirm=True), config_root=config_root, rest_host=rest_host, rest_port=rest_port, db_filepath=db_filepath, domains={network} if network else None, federated_only=federated_only, checksum_public_address=checksum_address, no_registry=federated_only or no_registry, registry_filepath=registry_filepath, provider_uri=provider_uri, poa=poa) painting.paint_new_installation_help(new_configuration=ursula_config, config_root=config_root, config_file=config_file, federated_only=federated_only) return # # Configured Ursulas # # Development Configuration if dev: ursula_config = UrsulaConfiguration( dev_mode=True, domains={TEMPORARY_DOMAIN}, poa=poa, registry_filepath=registry_filepath, provider_uri=provider_uri, checksum_public_address=checksum_address, federated_only=federated_only, rest_host=rest_host, rest_port=rest_port, db_filepath=db_filepath) # Authenticated Configurations else: # Domains -> bytes | or default domains = set(bytes(network, encoding='utf-8')) if network else None # Load Ursula from Configuration File try: ursula_config = UrsulaConfiguration.from_configuration_file( filepath=config_file, domains=domains, registry_filepath=registry_filepath, provider_uri=provider_uri, rest_host=rest_host, rest_port=rest_port, db_filepath=db_filepath, poa=poa, federated_only=federated_only) except FileNotFoundError: return actions.handle_missing_configuration_file( character_config_class=UrsulaConfiguration, config_file=config_file) click_config.unlock_keyring(character_configuration=ursula_config) # Handle destruction *before* network bootstrap and character initialization below if action == "destroy": """Delete all configuration files from the disk""" if dev: message = "'nucypher ursula destroy' cannot be used in --dev mode" raise click.BadOptionUsage(option_name='--dev', message=message) return actions.destroy_configuration(character_config=ursula_config, force=force) # # Connect to Blockchain (Non-Federated) # if not ursula_config.federated_only: click_config.connect_to_blockchain( character_configuration=ursula_config, recompile_contracts=recompile_solidity) click_config.ursula_config = ursula_config # Pass Ursula's config onto staking sub-command # # Launch Warnings # if ursula_config.federated_only: click_config.emit(message="WARNING: Running in Federated mode", color='yellow') # Seed - Step 1 teacher_uris = [teacher_uri] if teacher_uri else list() teacher_nodes = actions.load_seednodes( teacher_uris=teacher_uris, min_stake=min_stake, federated_only=ursula_config.federated_only, network_middleware=click_config.middleware) # Produce - Step 2 URSULA = ursula_config(known_nodes=teacher_nodes, lonely=lonely) # # Action Switch # if action == 'run': """Seed, Produce, Run!""" # GO! try: click_config.emit(message="Starting Ursula on {}".format( URSULA.rest_interface), color='green', bold=True) # Ursula Deploy Warnings click_config.emit(message="Connecting to {}".format(','.join( str(d, encoding='utf-8') for d in ursula_config.domains)), color='green', bold=True) if not URSULA.federated_only and URSULA.stakes: click_config.emit( message= f"Staking {str(URSULA.current_stake)} ~ Keep Ursula Online!", color='blue', bold=True) if not click_config.debug: stdio.StandardIO(UrsulaCommandProtocol(ursula=URSULA)) if dry_run: return # <-- ABORT -X (Last Chance) # Run - Step 3 node_deployer = URSULA.get_deployer() node_deployer.addServices() node_deployer.catalogServers(node_deployer.hendrix) node_deployer.run() # <--- Blocking Call (Reactor) # Handle Crash except Exception as e: ursula_config.log.critical(str(e)) click_config.emit(message="{} {}".format(e.__class__.__name__, str(e)), color='red', bold=True) raise # Crash :-( # Graceful Exit / Crash finally: click_config.emit(message="Stopping Ursula", color='green') ursula_config.cleanup() click_config.emit(message="Ursula Stopped", color='red') return elif action == "save-metadata": """Manually save a node self-metadata file""" metadata_path = ursula.write_node_metadata(node=URSULA) return click_config.emit( message="Successfully saved node metadata to {}.".format( metadata_path), color='green') elif action == "view": """Paint an existing configuration to the console""" response = UrsulaConfiguration._read_configuration_file( filepath=config_file or ursula_config.config_file_location) return click_config.emit(response=response) elif action == "forget": actions.forget(configuration=ursula_config) return elif action == 'stake': # List Only if list_: if not URSULA.stakes: click.echo( f"There are no existing stakes for {URSULA.checksum_public_address}" ) painting.paint_stakes(stakes=URSULA.stakes) return # Divide Only if divide: """Divide an existing stake by specifying the new target value and end period""" # Validate if len(URSULA.stakes) == 0: click.secho("There are no active stakes for {}".format( URSULA.checksum_public_address)) return # Selection if index is None: painting.paint_stakes(stakes=URSULA.stakes) index = click.prompt("Select a stake to divide", type=click.IntRange( min=0, max=len(URSULA.stakes) - 1)) # Lookup the stake current_stake = URSULA.stakes[index] # Value if not value: value = click.prompt( f"Enter target value (must be less than {str(current_stake.value)})", type=STAKE_VALUE) value = NU(value, 'NU') # Duration if not duration: extension = click.prompt("Enter number of periods to extend", type=STAKE_EXTENSION) else: extension = duration if not force: painting.paint_staged_stake_division( ursula=URSULA, original_index=index, original_stake=current_stake, target_value=value, extension=extension) click.confirm("Is this correct?", abort=True) modified_stake, new_stake = URSULA.divide_stake( stake_index=index, target_value=value, additional_periods=extension) if not quiet: click.secho('Successfully divided stake', fg='green') click.secho( f'Transaction Hash ........... {new_stake.receipt}') # Show the resulting stake list painting.paint_stakes(stakes=URSULA.stakes) return # Confirm new stake init if not force: click.confirm("Stage a new stake?", abort=True) # Validate balance balance = URSULA.token_balance if balance == 0: click.secho(f"{ursula.checksum_public_address} has 0 NU.") raise click.Abort if not quiet: click.echo(f"Current balance: {balance}") # Gather stake value if not value: min_locked = NU( URSULA.miner_agent.economics.minimum_allowed_locked, 'NuNit') value = click.prompt(f"Enter stake value", type=STAKE_VALUE, default=min_locked) else: value = NU(int(value), 'NU') # Duration if not quiet: message = f"Minimum duration: {URSULA.economics.minimum_allowed_locked} | " \ f"Maximum Duration: {URSULA.economics.maximum_allowed_locked}" click.echo(message) if not duration: duration = click.prompt( "Enter stake duration in periods (1 Period = 24 Hours)", type=STAKE_DURATION) start_period = URSULA.miner_agent.get_current_period() end_period = start_period + duration # Review if not force: painting.paint_staged_stake(ursula=URSULA, stake_value=value, duration=duration, start_period=start_period, end_period=end_period) if not dev: actions.confirm_staged_stake(ursula=URSULA, value=value, duration=duration) # Last chance to bail if not force: click.confirm("Publish staged stake to the blockchain?", abort=True) stake = URSULA.initialize_stake(amount=int(value), lock_periods=duration) painting.paint_staking_confirmation(ursula=URSULA, transactions=stake.transactions) return elif action == 'confirm-activity': if not URSULA.stakes: click.secho("There are no active stakes for {}".format( URSULA.checksum_public_address)) return URSULA.miner_agent.confirm_activity( node_address=URSULA.checksum_public_address) return elif action == 'collect-reward': """Withdraw staking reward to the specified wallet address""" if not force: click.confirm( f"Send {URSULA.calculate_reward()} to {URSULA.checksum_public_address}?" ) URSULA.collect_policy_reward( collector_address=withdraw_address or checksum_address) URSULA.collect_staking_reward() else: raise click.BadArgumentUsage("No such argument {}".format(action))
def init_client(): from command_protocol import CommandProtocol stdio.StandardIO(CommandProtocol())
def wrap_input(self): input_forwarder = DataWrapper() input_forwarder.output = self.transport stdio_wrapper = stdio.StandardIO(input_forwarder) self.output = stdio_wrapper
def connectionMade(self): testCommandProtocol = TestCommandProtocol() testCommandProtocol.server_transport = self.transport self.stdioWrapper = stdio.StandardIO(testCommandProtocol)
except: print("[PSO2PD] I got an error :(") print("[PSO2PD] Cached ship query.") print("[PSO2PD] Starting reactors...") for x in xrange(0, 10): endpoint = TCP4ServerEndpoint(reactor, 12000 + (100 * x), interface=redis_config['bindip']) endpoint.listen(BlockSenderFactory()) for x in xrange(0, 10): endpoint = TCP4ServerEndpoint(reactor, 12099 + (100 * x), interface=redis_config['bindip']) endpoint.listen(ShipInfoFactory()) stdio.StandardIO(ServerConsole()) print("[PSO2PD] Reactor started.") print("[PSO2PD] Announcing presence...") r.publish("proxy-global", json.dumps({'command': "register"})) setup_web() reactor.run() rthread.stop()
def init_cmd_support(self): stdio.StandardIO(CmdInterface(self.world))
def buildProtocol(self, addr): print('Connected.') web_client = WebClient() stdio.StandardIO(TWCli(web_client)) return web_client
def main(): log_file = logfile.LogFile.fromFullPath('log/serverlog.log') log.addObserver(log.FileLogObserver(log_file).emit) print("===== PSO2Proxy vGIT %s =====" % config.proxy_ver) time_string = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) print("[ServerStart] Trying to start server at %s" % time_string) if myIp == "0.0.0.0": print("==== ERROR 001 ====") print("You have NOT configured the IP address for PSO2Proxy!") print( "Please edit cfg/pso2proxy.config.yml and change myIpAddr to your IP public IP address " "(Not LAN address if you're on a LAN!) ") print("After you fix this, please restart PSO2Proxy.") sys.exit(0) if bindIp == "0.0.0.0": interface_ip = myIp else: interface_ip = bindIp if not os.path.isfile("keys/myKey.pem"): print("==== ERROR 002 ====") print( "You do NOT have your local RSA private key installed to 'keys/myKey.pem'!" ) print( "Please see README.md's section on RSA keys for more information.") print("After you fix this, please restart PSO2Proxy.") sys.exit(0) if not os.path.isfile("keys/SEGAKey.pem"): print("==== ERROR 003 ====") print( "You do NOT have a SEGA RSA public key installed to 'keys/SEGAKey.pem'!" ) print( "Please see README.md's section on RSA keys for more information.") print("After you fix this, please restart PSO2Proxy.") sys.exit(0) for shipNum in range( 0, 10 ): # PSO2 Checks all ships round robin, so sadly for max compatibility we have to open these no matter what ships are enabled... ship_endpoint = endpoints.TCP4ServerEndpoint(reactor, 12099 + (100 * shipNum), interface=interface_ip) ship_endpoint.listen(ShipAdvertiserFactoryPC()) for shipNum in range( 0, 10 ): # PSO2 Checks all ships round robin, so sadly for max compatibility we have to open these no matter what ships are enabled... ship_endpoint = endpoints.TCP4ServerEndpoint(reactor, 12094 + (100 * shipNum), interface=interface_ip) ship_endpoint.listen(ShipAdvertiserFactoryVita()) for shipNum in config.globalConfig.get_key('enabledShips'): query_endpoint = endpoints.TCP4ServerEndpoint(reactor, 12000 + (100 * shipNum), interface=interface_ip) query_endpoint.listen(BlockScraperFactory()) print("[ShipProxy] Bound port %i for ship %i query server!" % ((12000 + (100 * shipNum)), shipNum)) query_endpoint = endpoints.TCP4ServerEndpoint(reactor, 13000, interface=interface_ip) query_endpoint.listen(BlockScraperFactory()) stdio.StandardIO(ServerConsole()) print("[ShipProxy] Loading plugins...") import glob for plug in glob.glob("plugins/*.py"): plug = plug[:-3] plug = plug.replace(os.sep, '.') print("[ShipProxy] Importing %s..." % plug) __import__(plug) for f in plugin_manager.onStart: f() reactor.suggestThreadPoolSize(30) reactor.run() data.clients.dbManager.close_db() for f in plugin_manager.onStop: f()
for arg in args: if arg.startswith("-"): k, v = arg.split("=") gstarArgs[k] = v else: gstarArgs[i] = arg i += 1 # Get the args you need chapAddr = gstarArgs.get("--cA", "127.0.0.1") chapPort = gstarArgs.get("--cP", "9090") gatesFile = gstarArgs.get("--gFile", None) # Do logging things logger = logging.getLogger(__name__) logctx = playgroundlog.LoggingContext("GATESTARTER_MAIN") # Uncomment the next line to turn on "packet tracing" #logctx.doPacketTracing = True playgroundlog.startLogging(logctx) playgroundlog.UseStdErrHandler(True) # Start the Gate Starter starter = GateStarter(logctx, chapAddr, chapPort, gatesFile) deferLater(reactor, .2, starter.reset) stdio.StandardIO(starter) TwistedShutdownErrorHandler.HandleRootFatalErrors() reactor.run()
class Factory(protocol.ClientFactory): def __init__(self, client): self.client = client def buildProtocol(self, addr): return TCPBridge(self.client) class StdioBridgeClient(basic.LineReceiver): delimiter = os.linesep.encode("ascii") buf = [] tcp_bridge = None def connectionMade(self): reactor.connectTCP("localhost", port, Factory(self)) def lineReceived(self, line): # print >>sys.stderr, "HERE/lineReceived()", self.tcp_bridge, line if self.tcp_bridge is None: self.buf.append(line) else: self.tcp_bridge.requestSendLine(line) def requestSendLine(self, line): self.sendLine(line) if __name__ == "__main__": stdio.StandardIO(StdioBridgeClient()) reactor.run()
if -1 != str(e).find('Permission denied'): print( 'If you are attempting to use a low port (below 1024), do not.' ) print( 'Low ports require root privilege and you should not run HoneyPy as root.' ) print( 'Run the service on a high port and use IP Tables to redirect the low port' ) print( 'to a high port. This may help, https://github.com/foospidy/ipt-kit' ) if -1 != str(e).find('Address already in use'): print('A service (' + service + ') is configured to run on a port that is already') print( 'in use by another process. Kill the other process or use a different port.' ) sys.exit() # run HoneyPy Console if daemon mode not specified if False == args.d: stdio.StandardIO(HoneyPyConsole(honeypy_config, services)) # start reactor reactor.run()
def __init__(self, task_path, env): self.process = None self.listener = None self.task_path = task_path self.__done = False self.__waits_for = [] # FIXME: is inheriting the whole environment desirable? if env is not USE_DEFAULT: self.env = dict(env) else: self.env = dict(os.environ) # FIXME: Any other env.variables to set? # FIXME: What values should be used here? # - some values could be received from LC when task is scheduled, but # it would create a dependency! # - let's use fake values, and let the Backend translate it (if # supported) # - e.g. JOBID, RECIPESETID, RECIPEID are not interesting at all # - use task_id for RECIPESETID, and BE (or LC eventually) should # be able to find about the rest... taskid = "J%(JOBID)s-S%(RECIPESETID)s-R%(RECIPEID)s-T%(TASKID)s" % self.env # FIXME! use tempfile and upload log when process ends. log = logging.getLogger('rhts_task') twmisc.twisted_logging(log, level=logging.WARNING) ll = self.env.get('BEAH_TASK_LOG', "warning") log.setLevel(str2log_level(ll)) make_log_handler(log, LOG_PATH, "rhts_task_%s.log" % (taskid, ), syslog=True, console=self.env.get('BEAH_TASK_CONSOLE', False)) # parse task's metadata: try: from rhts import testinfo ti = testinfo.parse_file(os.path.join(self.env['TESTPATH'], 'testinfo.desc'), raise_errors=False) except: log.error("Error in tasks metadata: %s" % format_exc()) ti = None if ti is not None: for k, v in getattr(ti, 'environment', {}).iteritems(): self.env.setdefault(k, v) for o in getattr(ti, 'options', []): opt_lower = o.lower() if opt_lower[0] == '-': opt_lower = opt_lower[1:] value = '' else: value = 'yes' if opt_lower.startswith('compatible'): self.env.setdefault('RHTS_OPTION_COMPATIBLE', value) elif opt_lower.startswith('compatservice'): self.env.setdefault('RHTS_OPTION_COMPAT_SERVICE', value) elif opt_lower.startswith('strongeravc'): self.env.setdefault('RHTS_OPTION_STRONGER_AVC', value) # update log level if necessary: ll2 = self.env.get('BEAH_TASK_LOG', ll) if ll2 != ll: log.setLevel(str2log_level(ll2)) # No point in storing everything in one big file. Use one file per task rt = runtimes.ShelveRuntime(RUNTIME_PATHNAME_TEMPLATE % taskid) self.__files = runtimes.TypeDict(rt, 'files') # FIXME: use configurable range of ports. self.variables = runtimes.TypeDict(rt, 'variables') port = self.variables.setdefault( 'port', int(self.env.get('RHTS_PORT', random.randint(7080, 7099)))) self.variables.setdefault('nohup', False) self.variables.setdefault('has_result', False) self.env.setdefault( 'DIGEST_METHOD', 'no_digest' ) # use no digests by default... Seems waste of time on localhost. self.env.setdefault('TESTORDER', '123') # FIXME: More sensible default # update defaults: for k, v in self.ENV_DEFAULTS.iteritems(): self.env.setdefault(k, v) # provide sensible defaults for selected system env.variables: self.env.setdefault('HOME', '/root') self.env.setdefault('LANG', 'en_US.UTF-8') # FIXME: should any checks go here? # e.g. does Makefile PURPOSE exist? try running `make testinfo.desc`? ... self.controller = ControllerLink(self) stdio.StandardIO(self.controller) self.task = RHTSTask(self) self.server = RHTSServer(self) # If IPv6 has not been disabled, attempt to listen on IPv6 # otherwise fall back to IPv4 def listen_tcp(interface): return reactor.listenTCP(port, self.server, interface=interface) conf = beah.config.get_conf('beah') if not parse_bool(conf.get('DEFAULT', 'IPV6_DISABLED')): try: listen_tcp('::1') self.env['RESULT_SERVER'] = '[::1]:%s' % port except CannotListenError: listen_tcp('127.0.0.1') self.env['RESULT_SERVER'] = '127.0.0.1:%s' % port else: listen_tcp('127.0.0.1') self.env['RESULT_SERVER'] = '127.0.0.1:%s' % port # save env: env_file = ENV_PATHNAME_TEMPLATE % taskid self.env['RHTS_ENV'] = env_file jsonenv.export_env(env_file, self.env) # Execute rhts-test-runner.sh self.server_started()
if command: self.sendLine(getattr(self, 'do_' + command).__doc__) else: commands = [cmd[3:] for cmd in dir(self) if cmd.startswith('do_')] self.sendLine("Valid commands: " + " ".join(commands)) def do_quit(self): """quit: Quit this session""" self.sendLine('Goodbye.') self.transport.loseConnection() def do_check(self, url): """check <url>: Attempt to download the given web page""" client.getPage(url).addCallback(self.__checkSuccess).addErrback( self.__checkFailure) def __checkSuccess(self, pageData): self.sendLine("Success: got %i bytes." % len(pageData)) def __checkFailure(self, failure): self.sendLine("Failure: " + failure.getErrorMessage()) def connectionLost(self, reason): # stop the reactor, only because this is meant to be run in Stdio. reactor.stop() if __name__ == "__main__": stdio.StandardIO(WebCheckerCommandProtocol()) reactor.run()
# Copyright (c) 2006-2007 Twisted Matrix Laboratories. # See LICENSE for details. """ Main program for the child process run by L{twisted.test.test_stdio.StandardInputOutputTestCase.test_hostAndPeer} to test that ITransport.getHost() and ITransport.getPeer() work for process transports. """ import sys from twisted.internet import stdio, protocol from twisted.python import reflect class HostPeerChild(protocol.Protocol): def connectionMade(self): self.transport.write('\n'.join( [str(self.transport.getHost()), str(self.transport.getPeer())])) self.transport.loseConnection() def connectionLost(self, reason): reactor.stop() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() from twisted.internet import reactor stdio.StandardIO(HostPeerChild()) reactor.run()
# Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Main program for the child process run by L{twisted.test.test_stdio.StandardInputOutputTestCase.test_write} to test that ITransport.write() works for process transports. """ import sys, _preamble from twisted.internet import stdio, protocol from twisted.python import reflect class WriteChild(protocol.Protocol): def connectionMade(self): for ch in 'ok!': self.transport.write(ch) self.transport.loseConnection() def connectionLost(self, reason): reactor.stop() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() from twisted.internet import reactor stdio.StandardIO(WriteChild()) reactor.run()
print tell_me_about(s) print s f.close() class TrivialPrompter(basic.LineReceiver): from os import linesep as delimiter promptDeferred = None def prompt(self, msg): assert self.promptDeferred is None self.display(msg) self.promptDeferred = defer.Deferred() return self.promptDeferred def display(self, msg): self.transport.write(msg) def lineReceived(self, line): if self.promptDeferred is None: return d, self.promptDeferred = self.promptDeferred, None d.callback(line) if __name__ == '__main__': tp = TrivialPrompter() stdio.StandardIO(tp) tp.display("HI ashish")
if mode.lower() == "server": # This guy will be the server. Create an instance of the factory echoProtocolServer = EchoServer() # install the echoProtocolServer (factory) on playground port 101 #client.listen(echoProtocolServer, 101, connectionType=connectionType) # tell the playground client to connect to playground server and start running #client.connectToChaperone(chaperoneAddr, chaperonePort) echoServerEndpoint = GateServerEndpoint.CreateFromConfig( reactor, 13370, gateKey, networkStack=RIPProtocol) d = echoServerEndpoint.listen(echoProtocolServer) d.addErrback(logger.error) else: echoServerAddr = mode #try: # echoServerAddr = PlaygroundAddress.FromString(mode) #except: # sys.exit(USAGE) # This guy will be the client. The server's address is hard coded echoClientEndpoint = GateClientEndpoint.CreateFromConfig( reactor, echoServerAddr, 13370, gateKey, networkStack=RIPProtocol) tester = ClientTest(echoServerAddr, echoClientEndpoint) stdio.StandardIO(tester) TwistedShutdownErrorHandler.HandleRootFatalErrors() reactor.run()
def run(): """ Entry point into (native) worker processes. This wires up stuff such that a worker instance is talking WAMP-over-stdio to the node controller. """ import os import sys import platform import signal # Ignore SIGINT so we get consistent behavior on control-C versus # sending SIGINT to the controller process. When the controller is # shutting down, it sends TERM to all its children but ctrl-C # handling will send a SIGINT to all the processes in the group # (so then the controller sends a TERM but the child already or # will very shortly get a SIGINT as well). Twisted installs signal # handlers, but not for SIGINT if there's already a custom one # present. def ignore(sig, frame): log.debug("Ignoring SIGINT in worker.") signal.signal(signal.SIGINT, ignore) # create the top-level parser # import argparse parser = argparse.ArgumentParser() parser.add_argument('--reactor', default=None, choices=['select', 'poll', 'epoll', 'kqueue', 'iocp'], help='Explicit Twisted reactor selection (optional).') parser.add_argument('--loglevel', default="info", choices=['none', 'error', 'warn', 'info', 'debug', 'trace'], help='Initial log level.') parser.add_argument('-c', '--cbdir', type=six.text_type, help="Crossbar.io node directory (required).") parser.add_argument('-r', '--realm', type=six.text_type, help='Crossbar.io node (management) realm (required).') parser.add_argument('-t', '--type', choices=['router', 'container', 'websocket-testee'], help='Worker type (required).') parser.add_argument('-w', '--worker', type=six.text_type, help='Crossbar.io worker ID (required).') parser.add_argument('--title', type=six.text_type, default=None, help='Worker process title to set (optional).') parser.add_argument('--expose_controller', type=bool, default=False, help='Expose node controller session to all components (this feature requires Crossbar.io Fabric extension).') parser.add_argument('--expose_shared', type=bool, default=False, help='Expose a shared object to all components (this feature requires Crossbar.io Fabric extension).') options = parser.parse_args() # make sure logging to something else than stdio is setup _first_ # from crossbar._logging import make_JSON_observer, cb_logging_aware from txaio import make_logger, start_logging from twisted.logger import globalLogPublisher log = make_logger() # Print a magic phrase that tells the capturing logger that it supports # Crossbar's rich logging print(cb_logging_aware, file=sys.__stderr__) sys.__stderr__.flush() flo = make_JSON_observer(sys.__stderr__) globalLogPublisher.addObserver(flo) start_logging(None, options.loglevel) # we use an Autobahn utility to import the "best" available Twisted reactor # from autobahn.twisted.choosereactor import install_reactor reactor = install_reactor(options.reactor) from twisted.python.reflect import qual log.info("Worker process starting ({python}-{reactor}) ..", python=platform.python_implementation(), reactor=qual(reactor.__class__).split('.')[-1]) # set process title if requested to # try: import setproctitle except ImportError: log.debug("Could not set worker process title (setproctitle not installed)") else: if options.title: setproctitle.setproctitle(options.title) else: WORKER_TYPE_TO_TITLE = { 'router': 'crossbar-worker [router]', 'container': 'crossbar-worker [container]', 'websocket-testee': 'crossbar-worker [websocket-testee]' } setproctitle.setproctitle(WORKER_TYPE_TO_TITLE[options.type].strip()) # node directory # options.cbdir = os.path.abspath(options.cbdir) os.chdir(options.cbdir) # log.msg("Starting from node directory {}".format(options.cbdir)) from crossbar.worker.router import RouterWorkerSession from crossbar.worker.container import ContainerWorkerSession from crossbar.worker.testee import WebSocketTesteeWorkerSession WORKER_TYPE_TO_CLASS = { 'router': RouterWorkerSession, 'container': ContainerWorkerSession, 'websocket-testee': WebSocketTesteeWorkerSession } from twisted.internet.error import ConnectionDone from autobahn.twisted.websocket import WampWebSocketServerProtocol class WorkerServerProtocol(WampWebSocketServerProtocol): def connectionLost(self, reason): # the behavior here differs slightly whether we're shutting down orderly # or shutting down because of "issues" if isinstance(reason.value, ConnectionDone): was_clean = True else: was_clean = False try: # this log message is unlikely to reach the controller (unless # only stdin/stdout pipes were lost, but not stderr) if was_clean: log.info("Connection to node controller closed cleanly") else: log.warn("Connection to node controller lost: {reason}", reason=reason) # give the WAMP transport a change to do it's thing WampWebSocketServerProtocol.connectionLost(self, reason) except: # we're in the process of shutting down .. so ignore .. pass finally: # after the connection to the node controller is gone, # the worker is "orphane", and should exit # determine process exit code if was_clean: exit_code = 0 else: exit_code = 1 # exit the whole worker process when the reactor has stopped reactor.addSystemEventTrigger('after', 'shutdown', os._exit, exit_code) # stop the reactor try: reactor.stop() except ReactorNotRunning: pass try: # create a WAMP application session factory # from autobahn.twisted.wamp import ApplicationSessionFactory from autobahn.wamp.types import ComponentConfig session_config = ComponentConfig(realm=options.realm, extra=options) session_factory = ApplicationSessionFactory(session_config) session_factory.session = WORKER_TYPE_TO_CLASS[options.type] # create a WAMP-over-WebSocket transport server factory # from autobahn.twisted.websocket import WampWebSocketServerFactory transport_factory = WampWebSocketServerFactory(session_factory, u'ws://localhost') transport_factory.protocol = WorkerServerProtocol transport_factory.setProtocolOptions(failByDrop=False) # create a protocol instance and wire up to stdio # from twisted.python.runtime import platform as _platform from twisted.internet import stdio proto = transport_factory.buildProtocol(None) if _platform.isWindows(): stdio.StandardIO(proto) else: stdio.StandardIO(proto, stdout=3) # now start reactor loop # if False: log.info("vmprof enabled.") import os import vmprof PROFILE_FILE = 'vmprof_{}.dat'.format(os.getpid()) outfd = os.open(PROFILE_FILE, os.O_RDWR | os.O_CREAT | os.O_TRUNC) vmprof.enable(outfd, period=0.01) log.info("Entering event loop...") reactor.run() vmprof.disable() else: log.debug("Entering event loop...") reactor.run() except Exception as e: log.info("Unhandled exception: {e}", e=e) if reactor.running: reactor.addSystemEventTrigger('after', 'shutdown', os._exit, 1) reactor.stop() else: sys.exit(1)
def errLineReceived(self, line): if 'starting data transfer loop' in line: os.system('chmod 777 /dev/ttyUSB0') self.serialPort = SerialPort(basic.LineReceiver(), '/dev/ttyToUSB', reactor, timeout=3) self.loop = LoopingCall(writeToSerialPort, self.serialPort, self.emulator) self.loop.start(2) parser = argparse.ArgumentParser('Emulator for an oximeter') parser.add_argument('--interactive', dest='interactive', action='store_true', help='Interactive mode (default is randomized)') args = parser.parse_args() if args.interactive: emulator = UserInputEmulator() stdio.StandardIO(emulator) else: emulator = RandomizedEmulator() args = r'socat -d -d pty,raw,echo=0,link=/dev/ttyUSB0 pty,raw,echo=0,link=/dev/ttyToUSB'.split( ) spawnNonDaemonProcess(reactor, SocatProcessProtocol(emulator), 'socat', args) reactor.run()
def start(): parser = argparse.ArgumentParser(description='Bot arguments.') parser.add_argument('--serverhost', default=config.SERVER_HOST, dest='serverhost', help='Minecraft server host') parser.add_argument('--serverport', type=int, default=config.SERVER_PORT, dest='serverport', help='Minecraft server port') parser.add_argument('--botname', default=config.USERNAME, dest='botname', help='username that will be used by the bot') parser.add_argument('--botpass', default=config.PASSWORD, dest='botpass', help='password that will be used by the bot') parser.add_argument('--botemail', default=config.EMAIL, dest='botemail', help='email address that will be used by the bot') parser.add_argument('--onlinemode', action='store_true', help='Authenticate with Mojang') parser.add_argument('--use_encryption', action='store_true', help='use encryption, turned true if onlinemode used') parser.add_argument('--commandername', default=config.COMMANDER, dest='commandername', help='your username that you use in Minecraft') parser.add_argument('--log2file', action='store_true', help='Save log data to file') args = parser.parse_args() if args.log2file: logbot.start_bot_filelog() config.USERNAME = args.botname config.PASSWORD = args.botpass config.EMAIL = args.botemail config.USE_ENCRYPTION = args.use_encryption or args.onlinemode config.ONLINE_LOGIN = args.onlinemode if config.USE_ENCRYPTION: factory.import_encryption() config.COMMANDER = args.commandername.lower() host = args.serverhost port = args.serverport world = World(host=host, port=port, commander_name=args.commandername, bot_name=args.botname) try: from twisted.internet import stdio stdio.StandardIO(ConsoleChat(world)) except ImportError: log.msg("no terminal chat available") mc_factory = factory.MineCraftFactory(world) def customKeyboardInterruptHandler(signum, stackframe): log.msg("CTRL-C from user, exiting....") mc_factory.log_connection_lost = False reactor.callFromThread(reactor.stop) @inlineCallbacks def connect(): if config.ONLINE_LOGIN: yield mc_factory.online_auth() if mc_factory.clean_to_connect: reactor.connectTCP(host, port, mc_factory) connect() signal.signal(signal.SIGINT, customKeyboardInterruptHandler) reactor.addSystemEventTrigger("before", "shutdown", world.on_shutdown) reactor.run()
self.commands += 1 self.parsecommands(line) #pass line for instructions that have more than one argument if len(sys.argv) != 4: print "Usage: python commander.py <bootstrap ip> <bootstrap port> <commander port>" exit(0) boot_ip = str(sys.argv[1]) boot_port = int(sys.argv[2]) myport = int(sys.argv[3]) #Logging is useful for debugging but it interferes with our command interface so usually comment this line out #log.startLogging(sys.stdout) #Server is a high level implementation of the Kademlia protocol. It's powering the DHT kserver = Server() kserver.listen(myport) #UDP port we will be listening on #need a bootstrap address to join the network. This could be any computer on the network. kserver.bootstrap([(boot_ip,boot_port)]) #kserver.bootstrap([("192.168.56.101", 8468)]) key = hashlib.sha1() #this is an arbitray key in DHT where a bot reports its existence. We have hashed it in sha1 so that it appears #just like any other query on a kademlia network key.update('specialstring') keyhash = key.hexdigest() #the commander takes in standard input passed into our custom Slave Driver protocol which has an underlying kademlia DHT #This could easily be changed from std input to remote input by changing what twisted factory calls our protocol. #we used stdin for proof of concept but the remote input would allow the botmaster to spin up a commander from any location at any time. stdio.StandardIO(SlaveDriver(kserver,keyhash)) reactor.run()
from zope.interface import implementer from twisted.internet import interfaces log.startLogging(sys.stderr) from twisted.internet import protocol, reactor, stdio @implementer(interfaces.IHalfCloseableProtocol) class Echo(protocol.Protocol): def connectionMade(self): print("connection made") def dataReceived(self, data): self.transport.write(data) def readConnectionLost(self): print("readConnectionLost") self.transport.loseConnection() def writeConnectionLost(self): print("writeConnectionLost") def connectionLost(self, reason): print("connectionLost", reason) reactor.stop() stdio.StandardIO(Echo()) reactor.run() # type: ignore[attr-defined]
""" self.transport.write(b"x") def readConnectionLost(self): """ This is the desired event. Once it has happened, stop the reactor so the process will exit. """ self.exitCode = 0 reactor.stop() def connectionLost(self, reason): """ This may only be invoked after C{readConnectionLost}. If it happens otherwise, mark it as an error and shut down. """ if self.exitCode is None: self.exitCode = 1 log.err(reason, "Unexpected call to connectionLost") reactor.stop() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() log.startLogging(open(sys.argv[2], 'wb')) from twisted.internet import reactor protocol = HalfCloseProtocol() stdio.StandardIO(protocol) reactor.run() sys.exit(protocol.exitCode)
self.sendLine("###[%s/%s]>>> Got reply:\n%s" % (line, command_name, pprint.pformat(result))) except Exception as e: self.sendLine("###[%s]>>> FAILED %s(%s)" % (line, e.__class__.__name__, str(e))) self.transport.write(self.prompt) ################################################################################# # Startup glue code ################################################################################# if __name__ == "__main__": if "debug" in sys.argv: log_level = logging.DEBUG else: log_level = logging.WARN logging.basicConfig(level=log_level) cn = chessnet.ChessNet(load_model_filename=sys.argv[1], move_temp=0.01) my_console = MyConsoleProtocol() my_client = MyFicsClient(my_console, cn) reactor.connectTCP( FICS_HOST, FICS_PORT, fics_connector.FicsFactory(client=my_client, auth_username=FICS_USER, auth_password=FICS_PASSWORD)) stdio.StandardIO(my_console) reactor.run()
# reactor.stop() def dataReceived(self, bytes): self.buf += bytes if self._paused: log.startLogging(sys.stderr) log.msg("dataReceived while transport paused!") self.transport.loseConnection() else: self.transport.write(bytes) if self.buf.endswith('\n0\n'): self.transport.loseConnection() else: self.pause() def pause(self): self._paused = True self.transport.pauseProducing() reactor.callLater(0.01, self.unpause) def unpause(self): self._paused = False self.transport.resumeProducing() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() from twisted.internet import reactor stdio.StandardIO(ProducerChild()) reactor.run()
# -*- test-case-name: twisted.test.test_stdio.StandardInputOutputTestCase.test_writeSequence -*- # Copyright (c) Twisted Matrix Laboratories. # See LICENSE for details. """ Main program for the child process run by L{twisted.test.test_stdio.StandardInputOutputTestCase.test_writeSequence} to test that ITransport.writeSequence() works for process transports. """ import sys, _preamble from twisted.internet import stdio, protocol from twisted.python import reflect class WriteSequenceChild(protocol.Protocol): def connectionMade(self): self.transport.writeSequence(list('ok!')) self.transport.loseConnection() def connectionLost(self, reason): reactor.stop() if __name__ == '__main__': reflect.namedAny(sys.argv[1]).install() from twisted.internet import reactor stdio.StandardIO(WriteSequenceChild()) reactor.run()