本文整理汇总了Python中message.Messager.output_json方法的典型用法代码示例。如果您正苦于以下问题:Python Messager.output_json方法的具体用法?Python Messager.output_json怎么用?Python Messager.output_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类message.Messager
的用法示例。
在下文中一共展示了Messager.output_json方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _safe_serve
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def _safe_serve(params, client_ip, client_hostname, cookie_data):
# Note: Only logging imports here
from config import WORK_DIR
from logging import basicConfig as log_basic_config
# Enable logging
try:
from config import LOG_LEVEL
log_level = _convert_log_level(LOG_LEVEL)
except ImportError:
from logging import WARNING as LOG_LEVEL_WARNING
log_level = LOG_LEVEL_WARNING
log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
level=log_level)
# Do the necessary imports after enabling the logging, order critical
try:
from common import ProtocolError, ProtocolArgumentError, NoPrintJSONError
from dispatch import dispatch
from jsonwrap import dumps
from message import Messager
from session import get_session, init_session, close_session, NoSessionError
except ImportError:
# Note: Heisenbug trap for #612, remove after resolved
from logging import critical as log_critical
from sys import path as sys_path
log_critical('Heisenbug trap reports: ' + str(sys_path))
raise
init_session(client_ip, cookie_data=cookie_data)
try:
# Unpack the arguments into something less obscure than the
# Python FieldStorage object (part dictonary, part list, part FUBAR)
http_args = DefaultNoneDict()
for k in params:
# Also take the opportunity to convert Strings into Unicode,
# according to HTTP they should be UTF-8
try:
http_args[k] = unicode(params.getvalue(k), encoding='utf-8')
except TypeError:
Messager.error('protocol argument error: expected string argument %s, got %s' % (k, type(params.getvalue(k))))
raise ProtocolArgumentError
# Dispatch the request
json_dic = dispatch(http_args, client_ip, client_hostname)
response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
except ProtocolError, e:
# Internal error, only reported to client not to log
json_dic = {}
e.json(json_dic)
# Add a human-readable version of the error
err_str = str(e)
if err_str != '':
Messager.error(err_str)
response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
示例2: serve
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def serve(params, client_ip, client_hostname, cookie_data):
# The session relies on the config, wait-for-it
cookie_hdrs = None
# Do we have a Python version compatibly with our libs?
if (version_info < REQUIRED_PY_VERSION):
# Bail with hand-written JSON, this is very fragile to protocol changes
return cookie_hdrs, ((JSON_HDR, ),
('''
{
"messages": [
[
"Incompatible Python version (%s), %s or above is supported",
"error",
-1
]
]
}
''' % (PY_VER_STR, REQUIRED_PY_VERSION_STR)).strip())
# We can now safely use json and Messager
from jsonwrap import dumps
from message import Messager
try:
# We need to lock here since flup uses threads for each request and
# can thus manipulate each other's global variables
try:
CONFIG_CHECK_LOCK.acquire()
_config_check()
finally:
CONFIG_CHECK_LOCK.release()
except ConfigurationError as e:
json_dic = {}
e.json(json_dic)
return cookie_hdrs, ((JSON_HDR, ), dumps(
Messager.output_json(json_dic)))
# We can now safely read the config
from config import DEBUG
try:
_permission_check()
except PermissionError as e:
json_dic = {}
e.json(json_dic)
return cookie_hdrs, ((JSON_HDR, ), dumps(
Messager.output_json(json_dic)))
try:
# Safe region, can throw any exception, has verified installation
return _safe_serve(params, client_ip, client_hostname, cookie_data)
except BaseException as e:
# Handle the server crash
return _server_crash(cookie_hdrs, e)
示例3: _server_crash
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def _server_crash(cookie_hdrs, e):
from config import ADMIN_CONTACT_EMAIL, DEBUG
from jsonwrap import dumps
from message import Messager
stack_trace = _get_stack_trace()
if DEBUG:
# Send back the stack-trace as json
error_msg = '\n'.join(('Server Python crash, stack-trace is:\n',
stack_trace))
Messager.error(error_msg, duration=-1)
else:
# Give the user an error message
# Use the current time since epoch as an id for later log look-up
error_msg = ('The server encountered a serious error, '
'please contact the administrators at %s '
'and give the id #%d'
) % (ADMIN_CONTACT_EMAIL, int(time()))
Messager.error(error_msg, duration=-1)
# Print to stderr so that the exception is logged by the webserver
print(stack_trace, file=stderr)
json_dic = {
'exception': 'serverCrash',
}
return (cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json(json_dic))))
示例4: serve
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def serve(params, client_ip, client_hostname, cookie_data):
# The session relies on the config, wait-for-it
cookie_hdrs = None
# Do we have a Python version compatibly with our libs?
if (version_info[0] != REQUIRED_PY_VERSION[0] or
version_info < REQUIRED_PY_VERSION):
# Bail with hand-writen JSON, this is very fragile to protocol changes
return cookie_hdrs, ((JSON_HDR, ),
('''
{
"messages": [
[
"Incompatible Python version (%s), %s or above is supported",
"error",
-1
]
]
}
''' % (PY_VER_STR, REQUIRED_PY_VERSION_STR)).strip())
# We can now safely use json and Messager
from jsonwrap import dumps
from message import Messager
try:
# We need to lock here since flup uses threads for each request and
# can thus manipulate each other's global variables
with CONFIG_CHECK_LOCK:
_config_check()
except ConfigurationError, e:
json_dic = {}
e.json(json_dic)
return cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
示例5: serve
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def serve(params, client_ip, client_hostname):
# At this stage we can not get any cookie data, wait-for-it
cookie_hdrs = None
# Do we have a Python version compatibly with our libs?
if (version_info[0] != REQUIRED_PY_VERSION_MAJOR or
version_info[1] < REQUIRED_PY_VERSION_MINOR):
# Bail with hand-writen JSON, this is very fragile to protocol changes
return cookie_hdrs, ((JSON_HDR, ),
('''
{
"messages": [
[
"Incompatible Python version (%s), %d.%d or above is supported",
"error",
-1
]
]
}
''' % (PY_VER_STR, REQUIRED_PY_VERSION_MAJOR,
REQUIRED_PY_VERSION_MINOR)).strip())
# We can now safely use json and Messager
from jsonwrap import dumps
from message import Messager
try:
_config_check()
except ConfigurationError:
return cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json({})))
# We can now safely read the config
from config import DEBUG
try:
_permission_check()
except PermissionError:
return cookie_hdrs, ((JSON_HDR, ), dumps(Messager.output_json({})))
try:
# Safe region, can throw any exception, has verified installation
return _safe_serve(params, client_ip, client_hostname)
except BaseException, e:
# Handle the server crash
return _server_crash(cookie_hdrs, e)
示例6: _safe_serve
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
def _safe_serve(params, client_ip, client_hostname):
from common import ProtocolError, NoPrintJSONError
from config import WORK_DIR
from dispatch import dispatch
from jsonwrap import dumps
from logging import basicConfig as log_basic_config
from message import Messager
from session import get_session
# Enable logging
try:
from config import LOG_LEVEL
log_level = _convert_log_level(LOG_LEVEL)
except ImportError:
from logging import WARNING as LOG_LEVEL_WARNING
log_level = LOG_LEVEL_WARNING
log_basic_config(filename=path_join(WORK_DIR, 'server.log'),
level=log_level)
# Session information is now available
cookie_hdrs = get_session().get_cookie_hdrs()
try:
# Dispatch the request
json_dic = dispatch(params, client_ip, client_hostname)
response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
except ProtocolError, e:
# Internal error, only reported to client not to log
json_dic = {}
e.json(json_dic)
# Add a human-readable version of the error
err_str = str(e)
if err_str != '':
Messager.error(err_str)
response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
示例7: session
# 需要导入模块: from message import Messager [as 别名]
# 或者: from message.Messager import output_json [as 别名]
except NoPrintJSONError, e:
# Terrible hack to serve other things than JSON
response_data = (e.hdrs, e.data)
response_is_JSON = False
# Get the potential cookie headers and close the session (if any)
try:
cookie_hdrs = get_session().cookie.hdrs()
close_session()
except SessionStoreError:
Messager.error("Failed to store cookie (missing write permission to brat work directory)?", -1)
except NoSessionError:
cookie_hdrs = None
if response_is_JSON:
response_data = ((JSON_HDR, ), dumps(Messager.output_json(json_dic)))
return (cookie_hdrs, response_data)
# Programmatically access the stack-trace
def _get_stack_trace():
from traceback import print_exc
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
# Getting the stack-trace requires a small trick
buf = StringIO()
print_exc(file=buf)