def _send_config_via_wormhole(options, config): out = options.stdout err = options.stderr relay_url = options.parent['wormhole-server'] print("Connecting to '{}'...".format(relay_url), file=out) wh = options.parent.wormhole.create( appid=options.parent['wormhole-invite-appid'], relay_url=relay_url, reactor=reactor, ) yield wh.get_welcome() print("Connected to wormhole server", file=out) # must call allocate_code before get_code will ever succeed wh.allocate_code() code = yield wh.get_code() print("Invite Code for client: {}".format(code), file=out) wh.send_message(json.dumps_bytes({u"abilities": { u"server-v1": {}, }})) client_intro = yield wh.get_message() print(" received client introduction", file=out) client_intro = json.loads(client_intro) if not u'abilities' in client_intro: print("No 'abilities' from client", file=err) defer.returnValue(1) if not u'client-v1' in client_intro[u'abilities']: print("No 'client-v1' in abilities from client", file=err) defer.returnValue(1) print(" transmitting configuration", file=out) wh.send_message(json.dumps_bytes(config)) yield wh.close()
def test_any_bytes_unsupported_by_default(self): """By default non-UTF-8 bytes raise error.""" bytestring = b"abc\xff\x00" with self.assertRaises(UnicodeDecodeError): jsonbytes.dumps(bytestring) with self.assertRaises(UnicodeDecodeError): jsonbytes.dumps_bytes(bytestring) with self.assertRaises(UnicodeDecodeError): json.dumps(bytestring, cls=jsonbytes.UTF8BytesJSONEncoder)
def _get_config_via_wormhole(config): out = config.stdout print("Opening wormhole with code '{}'".format(config['join']), file=out) relay_url = config.parent['wormhole-server'] print("Connecting to '{}'".format(relay_url), file=out) wh = config.parent.wormhole.create( appid=config.parent['wormhole-invite-appid'], relay_url=relay_url, reactor=reactor, ) code = str(config['join']) wh.set_code(code) yield wh.get_welcome() print("Connected to wormhole server", file=out) intro = { u"abilities": { "client-v1": {}, } } wh.send_message(json.dumps_bytes(intro)) server_intro = yield wh.get_message() server_intro = json.loads(server_intro) print(" received server introduction", file=out) if u'abilities' not in server_intro: raise RuntimeError(" Expected 'abilities' in server introduction") if u'server-v1' not in server_intro['abilities']: raise RuntimeError(" Expected 'server-v1' in server abilities") remote_data = yield wh.get_message() print(" received configuration", file=out) defer.returnValue(json.loads(remote_data))
def _received_eliot_log(self, message): """ While this WebSocket connection is open, this function is registered as an eliot destination """ # probably want a try/except around here? what do we do if # transmission fails or anything else bad happens? encoded = json.dumps_bytes(message, any_bytes=True) self.sendMessage(encoded)
def set_children(self): if not self.new_children: return url = (self.nodeurl + "uri/" + url_quote(self.writecap) + "?t=set_children") set_data = {} for (name, filecap) in list(self.new_children.items()): # it just so happens that ?t=set_children will accept both file # read-caps and write-caps as ['rw_uri'], and will handle either # correctly. So don't bother trying to figure out whether the one # we have is read-only or read-write. # TODO: think about how this affects forward-compatibility for # unknown caps set_data[name] = ["filenode", {"rw_uri": filecap}] body = json.dumps_bytes(set_data) POST(url, body)
def test_dumps_bytes(self): """jsonbytes.dumps_bytes always returns bytes.""" x = {u"def\N{SNOWMAN}\uFF00": 123} encoded = jsonbytes.dumps_bytes(x) self.assertIsInstance(encoded, bytes) self.assertEqual(json.loads(encoded), x)