def test_load_connection_file_session_with_kn(): """test load_connection_file() after """ session = Session() app = DummyConsoleApp(session=Session()) app.initialize(argv=[]) session = app.session with TemporaryDirectory() as d: cf = os.path.join(d, 'kernel.json') connect.write_connection_file(cf, **sample_info_kn) app.connection_file = cf app.load_connection_file() assert session.key == sample_info_kn['key'] assert session.signature_scheme == sample_info_kn['signature_scheme']
def test_default_version(): s = Session() msg = s.msg("msg_type") msg["header"].pop("version") original = copy.deepcopy(msg) adapted = adapt(original) assert adapted["header"]["version"] == V4toV5.version
def test_io_api(): """Test that wrapped stdout has the same API as a normal TextIO object""" session = Session() ctx = zmq.Context() pub = ctx.socket(zmq.PUB) thread = IOPubThread(pub) thread.start() stream = OutStream(session, thread, 'stdout') # cleanup unused zmq objects before we start testing thread.stop() thread.close() ctx.term() assert stream.errors is None assert not stream.isatty() with nt.assert_raises(io.UnsupportedOperation): stream.detach() with nt.assert_raises(io.UnsupportedOperation): next(stream) with nt.assert_raises(io.UnsupportedOperation): stream.read() with nt.assert_raises(io.UnsupportedOperation): stream.readline() with nt.assert_raises(io.UnsupportedOperation): stream.seek() with nt.assert_raises(io.UnsupportedOperation): stream.tell()
def test_default_version(): s = Session() msg = s.msg("msg_type") msg['header'].pop('version') original = copy.deepcopy(msg) adapted = adapt(original) nt.assert_equal(adapted['header']['version'], V4toV5.version)
def test_deserialize_binary(): s = Session() msg = s.msg('data_pub', content={'a': 'b'}) msg['buffers'] = [ memoryview(os.urandom(2)) for i in range(3) ] bmsg = serialize_binary_message(msg) msg2 = deserialize_binary_message(bmsg) assert msg2 == msg
def start_watching_activity(self, kernel_id): """Start watching IOPub messages on a kernel for activity. - update last_activity on every message - record execution_state from status messages """ kernel = self._kernels[kernel_id] # add busy/activity markers: kernel.execution_state = 'starting' kernel.last_activity = utcnow() kernel._activity_stream = kernel.connect_iopub() session = Session( config=kernel.session.config, key=kernel.session.key, ) def record_activity(msg_list): """Record an IOPub message arriving from a kernel""" kernel.last_activity = utcnow() idents, fed_msg_list = session.feed_identities(msg_list) msg = session.deserialize(fed_msg_list) msg_type = msg['header']['msg_type'] self.log.debug("activity on %s: %s", kernel_id, msg_type) if msg_type == 'status': kernel.execution_state = msg['content']['execution_state'] kernel._activity_stream.on_recv(record_activity)
def test_deserialize_binary(): s = Session() msg = s.msg("data_pub", content={"a": "b"}) msg["buffers"] = [memoryview(os.urandom(2)) for i in range(3)] bmsg = serialize_binary_message(msg) msg2 = deserialize_binary_message(bmsg) assert msg2 == msg
def __init__(self, *args, **kwargs): super(WidgetHandler, self).__init__(*args, **kwargs) self.context = zmq.Context() self.socket = self.context.socket(zmq.XREQ) self.socket.linger = 1000 self.socket.connect("tcp://127.0.0.1:%s" % port) self.session = Session(key=session_key.encode())
def test_io_isatty(): session = Session() ctx = zmq.Context() pub = ctx.socket(zmq.PUB) thread = IOPubThread(pub) thread.start() stream = OutStream(session, thread, 'stdout', isatty=True) assert stream.isatty()
def main(connection_file): """watch iopub channel, and print messages""" ctx = zmq.Context.instance() with open(connection_file) as f: cfg = json.loads(f.read()) # reg_url = cfg['interface'] reg_url = 'tcp://140.117.168.49' iopub_port = cfg['iopub'] iopub_url = "{}:{}".format(reg_url, iopub_port) print("iopub_url:", iopub_url) session = Session(key=cfg['key'].encode('ascii')) sub = ctx.socket(zmq.SUB) # This will subscribe to all messages: sub.SUBSCRIBE = b'' # replace with b'' with b'engine.1.stdout' to subscribe only to engine 1's stdout # 0MQ subscriptions are simple 'foo*' matches, so 'engine.1.' subscribes # to everything from engine 1, but there is no way to subscribe to # just stdout from everyone. # multiple calls to subscribe will add subscriptions, e.g. to subscribe to # engine 1's stderr and engine 2's stdout: # sub.SUBSCRIBE = b'engine.1.stderr' # sub.SUBSCRIBE = b'engine.2.stdout' sub.connect(iopub_url) while True: try: idents, msg = session.recv(sub, mode=0) except KeyboardInterrupt: return # ident always length 1 here topic = idents[0].decode('utf8', 'replace') if msg['msg_type'] == 'stream': # stdout/stderr # stream names are in msg['content']['name'], if you want to handle # them differently print("{}: {}".format(topic, msg['content']['text'])) elif msg['msg_type'] == 'error': # Python traceback c = msg['content'] print(topic + ':') for line in c['traceback']: # indent lines print(' ' + line) elif msg['msg_type'] == 'error': # Python traceback c = msg['content'] print(topic + ':') for line in c['traceback']: # indent lines print(' ' + line)
def _create_session(self): from jupyter_client.session import Session try: from jupyter_client.session import new_id_bytes except ImportError: def new_id_bytes(): import uuid return uuid.uuid4() self._session = Session(username=u'kernel', key=new_id_bytes())
def __init__(self, kernel_id, signature_key, executing_kernel_client_settings): super(ExecutingKernelClient, self).__init__() self.client_settings = executing_kernel_client_settings self.kernel_id = kernel_id self.context = zmq.Context() self.session = Session(key=signature_key) self.subscriber = {} self._rabbit_sender_client, self._rabbit_listener = self._init_rabbit_clients( ) self._socket_forwarders = self._init_socket_forwarders()
def create_shell(username, session_id, key): """Instantiates a CapturingSocket and SwiftShell and hooks them up. After you call this, the returned CapturingSocket should capture all IPython display messages. """ socket = CapturingSocket() session = Session(username=username, session=session_id, key=key) shell = SwiftShell.instance() shell.display_pub.session = session shell.display_pub.pub_socket = socket return (socket, shell)
def load_connection_file(self): """load config from a JSON connector file, at a *lower* priority than command-line/config files. Same content can be specified in $IPP_CONNECTION_INFO env """ config = self.config if self.connection_info_env: self.log.info("Loading connection info from $IPP_CONNECTION_INFO") d = json.loads(self.connection_info_env) else: self.log.info("Loading connection file %r", self.url_file) with open(self.url_file) as f: d = json.load(f) # allow hand-override of location for disambiguation # and ssh-server if 'IPEngine.location' not in self.cli_config: self.location = d['location'] if 'ssh' in d and not self.sshserver: self.sshserver = d.get("ssh") proto, ip = d['interface'].split('://') ip = disambiguate_ip_address(ip, self.location) d['interface'] = f'{proto}://{ip}' if d.get('curve_serverkey'): # connection file takes precedence over env, if present and defined self.curve_serverkey = d['curve_serverkey'].encode('ascii') if self.curve_serverkey: self.log.info("Using CurveZMQ security") self._ensure_curve_keypair() else: self.log.warning("Not using CurveZMQ security") # DO NOT allow override of basic URLs, serialization, or key # JSON file takes top priority there if d.get('key') or 'key' not in config.Session: config.Session.key = d.get('key', '').encode('utf8') config.Session.signature_scheme = d['signature_scheme'] self.registration_url = f"{d['interface']}:{d['registration']}" config.Session.packer = d['pack'] config.Session.unpacker = d['unpack'] self.session = Session(parent=self) self.log.debug("Config changed:") self.log.debug("%r", config) self.connection_info = d
def _setup_session(self, reply, comp_id): """ Set up the kernel information contained in the untrusted reply message `reply` from computer `comp_id`. """ reply_content = reply["content"] kernel_id = reply_content["kernel_id"] kernel_connection = reply_content["connection"] self._kernels[kernel_id] = { "comp_id": comp_id, "connection": kernel_connection, "executing": 0 } self._comps[comp_id]["kernels"][kernel_id] = None self._sessions[kernel_id] = Session(key=kernel_connection["key"])
def _setup_session(self, reply, comp_id, timeout=None): """ Set up the kernel information contained in the untrusted reply message `reply` from computer `comp_id`. """ reply_content = reply["content"] kernel_id = reply_content["kernel_id"] kernel_connection = reply_content["connection"] if timeout is None : timeout = self.max_kernel_timeout self._kernels[kernel_id] = {"comp_id": comp_id, "connection": kernel_connection, "executing": 0, # number of active execute_requests "deadline": time.time()+timeout, "timeout": timeout} self._comps[comp_id]["kernels"][kernel_id] = None self._sessions[kernel_id] = Session(key=kernel_connection["key"])
def create_on_reply(self, kernel_id): """Creates an anonymous function to handle reply messages from the kernel. Parameters ---------- kernel_id Kernel to listen to Returns ------- function Callback function taking a kernel ID and 0mq message list """ kernel = self.kernel_clients[kernel_id] session = Session( config=kernel.session.config, key=kernel.session.key, ) return lambda msg_list: self._on_reply(kernel_id, session, msg_list)
def start_watching_activity(self, kernel_id): """Start watching IOPub messages on a kernel for activity. - update last_activity on every message - record execution_state from status messages """ kernel = self._kernels[kernel_id] # add busy/activity markers: kernel.execution_state = "starting" kernel.reason = "" kernel.last_activity = utcnow() kernel._activity_stream = kernel.connect_iopub() session = Session( config=kernel.session.config, key=kernel.session.key, ) def record_activity(msg_list): """Record an IOPub message arriving from a kernel""" self.last_kernel_activity = kernel.last_activity = utcnow() idents, fed_msg_list = session.feed_identities(msg_list) msg = session.deserialize(fed_msg_list, content=False) msg_type = msg["header"]["msg_type"] if msg_type == "status": msg = session.deserialize(fed_msg_list) kernel.execution_state = msg["content"]["execution_state"] self.log.debug( "activity on %s: %s (%s)", kernel_id, msg_type, kernel.execution_state, ) else: self.log.debug("activity on %s: %s", kernel_id, msg_type) kernel._activity_stream.on_recv(record_activity)
def __init__( self, *, pid: int, engine_id: int, control_url: str, registration_url: str, identity: bytes, curve_serverkey: bytes, curve_publickey: bytes, curve_secretkey: bytes, config: Config, pipe, log_level: int = logging.INFO, ): self.pid = pid self.engine_id = engine_id self.parent_process = psutil.Process(self.pid) self.control_url = control_url self.registration_url = registration_url self.identity = identity self.curve_serverkey = curve_serverkey self.curve_publickey = curve_publickey self.curve_secretkey = curve_secretkey self.config = config self.pipe = pipe self.session = Session(config=self.config) self.log = local_logger(f"{self.__class__.__name__}.{engine_id}", log_level) self.log.propagate = False self.control_handlers = { "signal_request": self.signal_request, } self._finish_called = False
def initialize(self): self.log.debug("Initializing websocket connection %s", self.request.path) self.session = Session(config=self.config) # TODO: make kernel client class configurable self.gateway = KernelGatewayWSClient()
def _session_default(self): from jupyter_client.session import Session return Session(parent=self)
def _default_session(self): from jupyter_client.session import Session return Session(parent=self, key=INPROCESS_KEY)
def setUp(self): self.session = Session() self.db = self.create_db() self.load_records(16)
def _session_default(self): # don't sign in-process messages return Session(key=b'', parent=self)
def _default_session(self): # don't sign in-process messages return Session(key=INPROCESS_KEY, parent=self)
def setUp(self): self.session = Session()
def _default_session(self): return Session(parent=self)
def initialize(self): self.log.debug("Initializing websocket connection %s", self.request.path) self.session = Session(config=self.config)
def initialize(self): self.log.debug("Initializing websocket connection %s", self.request.path) self.session = Session(config=self.config) self.gateway = GatewayWebSocketClient( gateway_url=GatewayClient.instance().url)
def test_serialize_binary(): s = Session() msg = s.msg("data_pub", content={"a": "b"}) msg["buffers"] = [memoryview(os.urandom(3)) for i in range(3)] bmsg = serialize_binary_message(msg) assert isinstance(bmsg, bytes)