本文整理汇总了Python中pulsar.utils.system.json.loads函数的典型用法代码示例。如果您正苦于以下问题:Python loads函数的具体用法?Python loads怎么用?Python loads使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了loads函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rpc
def test_rpc(self):
'''Send a message to the rpc'''
loop = self.http._loop
ws = yield from self.http.get(self.ws, websocket_handler=Message(loop))
self.assertEqual(ws.status_code, 101)
ws.write('Hello there!')
data = yield from ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hello there!')
result = yield from self.rpc.message('Hi!')
self.assertEqual(result, 'OK')
data = yield from ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hi!')
示例2: test_rpc
def test_rpc(self):
'''Send a message to the rpc'''
ws = yield self.http.get(self.ws,
websocket_handler=MessageHandler()
).on_headers
self.assertEqual(ws.handshake.status_code, 101)
ws.write('Hello there!')
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hello there!')
result = yield self.rpc.message('Hi!')
self.assertEqual(result, 'OK')
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hi!')
示例3: _ready
def _ready(self, data):
self.environ['wsgi.input'] = BytesIO(data)
try:
self.result = (json.loads(data.decode(self.charset)), None)
except Exception as exc:
raise BadRequest('Could not decode JSON') from exc
return self.result
示例4: _data_and_files
def _data_and_files(self, data=True, files=True, future=None):
result = {}, None
chunk = None
if future is None:
stream = self.environ.get('wsgi.input')
if self.method not in ENCODE_URL_METHODS and stream:
chunk = stream.read()
if isinstance(chunk, Future):
return chain_future(
chunk, partial(self._data_and_files, data, files))
else:
chunk = future
if chunk is not None:
content_type, options = self.content_type_options
charset = options.get('charset', 'utf-8')
if content_type in JSON_CONTENT_TYPES:
result = json.loads(chunk.decode(charset)), None
elif content_type:
self.environ['wsgi.input'] = BytesIO(chunk)
result = parse_form_data(self.environ, charset)
else:
result = chunk, None
self.environ['wsgi.input'] = BytesIO(chunk)
self.cache.data_and_files = result
return self.data_and_files(data, files)
示例5: process_data
def process_data(self, response, **kw):
'''Callback passed to :class:`HttpClient` for processing
streaming data.
'''
if response.status_code == 200:
messages = []
data = response.recv_body()
while data:
idx = data.find(b'\r\n')
if idx < 0: # incomplete data - add to buffer
self.buffer.append(data)
data = None
else:
self.buffer.append(data[:idx])
data = data[idx+2:]
msg = b''.join(self.buffer)
self.buffer = []
if msg:
body = json.loads(msg.decode('utf-8'))
if 'disconnect' in body:
msg = body['disconnect']
self.logger.warning('Disconnecting (%d): %s',
msg['code'], msg['reason'])
elif 'warning' in body:
message = body['warning']['message']
self.logger.warning(message)
else:
messages.append(body)
if messages:
# a list of messages is available
if self.cfg.callable:
self.cfg.callable(self, messages)
示例6: parse_frame
def parse_frame(self, websocket, frame):
parser = frame_parser(kind=1)
frame = parser.decode(frame)
wsclient = websocket.cache.wsclient
websocket.connection.reset_mock()
msg = _json.loads(frame.body[1:])[0]
return wsclient.protocol.decode(msg)
示例7: design_delete
def design_delete(self, name):
'''Delete an existing design document at ``name``.
'''
response = yield self.request('head', self._database, '_design', name)
if response.status_code == 200:
rev = json.loads(response.headers['etag'])
yield self.request('delete', self._database, '_design', name,
rev=rev)
示例8: on_message
def on_message(self, websocket, msg):
request = websocket.request
client = request.cache.mailclient
if msg and client:
msg = json.loads(msg)
if 'mailbox' in msg:
mailbox = yield client.examine(msg['mailbox'])
self.write(request, json.dumps({'mailbox': mailbox}))
示例9: test_websocket
def test_websocket(self):
ws = yield self.http.get(self.ws, websocket_handler=MessageHandler())
response = ws.handshake
self.assertEqual(response.status_code, 101)
self.assertEqual(response.headers["upgrade"], "websocket")
self.assertEqual(response.connection, ws.connection)
self.assertTrue(ws.connection)
self.assertIsInstance(ws.handler, MessageHandler)
#
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data["message"], "joined")
#
ws.write("Hello there!")
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data["message"], "Hello there!")
示例10: test_websocket
def test_websocket(self):
ws = yield self.http.get(self.ws, websocket_handler=MessageHandler()
).on_headers
self.assertTrue(ws)
self.assertIsInstance(ws.handler, MessageHandler)
ws.write('Hello there!')
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hello there!')
示例11: test_websocket
def test_websocket(self):
c = self.http
ws = yield c.get(self.ws, websocket_handler=MessageHandler(c._loop))
response = ws.handshake
self.assertEqual(response.status_code, 101)
self.assertEqual(response.headers['upgrade'], 'websocket')
self.assertEqual(response.connection, ws.connection)
self.assertTrue(ws.connection)
self.assertIsInstance(ws.handler, MessageHandler)
#
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'joined')
#
ws.write('Hello there!')
data = yield ws.handler.get()
data = json.loads(data)
self.assertEqual(data['message'], 'Hello there!')
示例12: to_python
def to_python(self, value, store=None):
if value is None:
return self.get_default()
if isinstance(value, str):
try:
return json.loads(value)
except TypeError:
return None
else:
return value
示例13: _data_and_files
def _data_and_files(self):
result = {}, None
stream = self.environ.get('wsgi.input')
try:
if self.method not in ENCODE_URL_METHODS and stream:
chunk = yield stream.read()
content_type, options = self.content_type_options
charset = options.get('charset', 'utf-8')
if content_type in JSON_CONTENT_TYPES:
result = json.loads(chunk.decode(charset)), None
else:
self.environ['wsgi.input'] = BytesIO(chunk)
result = parse_form_data(self.environ, charset)
# set the wsgi.input to a readable file-like object for
# third-parties application (django or any other web-framework)
self.environ['wsgi.input'] = BytesIO(chunk)
finally:
self._cached_data_and_files = result
coroutine_return(result)
示例14: media_libraries
def media_libraries():
global _media_libraries
if _media_libraries is None:
if os.path.isfile('libs.json'): # pragma nocover
with open('libs.json') as f:
data = f.read()
_media_libraries = json.loads(data)
else:
from pulsar import new_event_loop
from pulsar.apps.http import HttpClient
http = HttpClient(loop=new_event_loop())
try:
response = http.get(_libs_url)
_media_libraries = response.json()
except Exception: # pragma nocover
http.logger.error('Could not import media library',
exc_info=True)
_media_libraries = {'libs': {}, 'deps': {}}
return _media_libraries
示例15: _data_and_files
def _data_and_files(self, data=True, files=True):
result = {}, None
stream = self.environ.get("wsgi.input")
chunk = None
try:
if self.method not in ENCODE_URL_METHODS and stream:
chunk = stream.read()
if isinstance(chunk, Future):
chunk = yield chunk
content_type, options = self.content_type_options
charset = options.get("charset", "utf-8")
if content_type in JSON_CONTENT_TYPES:
result = json.loads(chunk.decode(charset)), None
else:
self.environ["wsgi.input"] = BytesIO(chunk)
result = parse_form_data(self.environ, charset)
finally:
self.cache.data_and_files = result
if chunk is not None:
self.environ["wsgi.input"] = BytesIO(chunk)
coroutine_return(self.data_and_files(data, files))