karlooper.web
karlooper.web 提供了一个Http Server的相关基本功能。
karlooper.web.request
这个模块提供了Request类.
class Request(http_data_dict, http_message, settings)
HTTP处理器的基类。
子类中必须包含至少以下一种HTTP请求方法。
Entry points
def get()
HTTP请求get方法
def post()
HTTP请求post方法
def put()
HTTP请求put方法
def head()
HTTP请求head方法
def options()
HTTP请求options方法
def delete()
HTTP请求delete方法
def trace()
HTTP请求trace方法
def connect()
HTTP请求connect方法
def before_request()
在每次响应HTTP请求之前执行
def teardown_request()
在每次响应HTTP请求之后执行
Cookies
def get_cookie(key, default=None)
根据key获取对应的cookie,如果key不存在,则返回默认值。
def set_cookie(key, value, expires_days=1, path="/", domain=None)
设置cookie的值, 其中:
- key为cookie的键,
- value为cookie的值,
- expires_days为cookie过期时间,
- path为cookie有效路径,
- domain为cookie有效作用域。
def get_security_cookie(key, default=None)
根据key获取已解密的cookie值, 该cookie值在浏览器端进行了加密。
def set_security_cookie(key, value, expires_days=1, path="/", domain=None)
设置cookie的值。
该cookie的值在浏览器端保存的是经过加密之后的。
def clear_cookie(key, path="/", domain=None)
按照cookie的相关规范, 清除在作用域domain, 路径path下的对应的cookie。
def clear_all_cookie(path="/", domain=None)
清除所有的cookie。
输入
def get_parameter(key, default=None)
获取HTTP请求的参数值。
如果key不存在,则返回默认值。
无论是通过HTTP报文体还是url传递的参数,都可以通过这个函数获取到。
def decode_parameter(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
无论是通过HTTP报文体还是url传递的参数,都可以通过这个函数获取到。
注意: 这个方法获取的值必须是经过urlencode之后的值。
def decode_parameter_plus(key, default=None)
这个方法与decode_parameter类似, 但可以将“+”号转换为空格.
def get_path_param(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
这个值是从url中的path获取的参数值。
def get_path_param_int(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
这个值是从url中的path获取的参数值,并转换为int类型。
def get_path_param_str(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
这个值是从url中的path获取的参数值,并转换为str类型。
def get_path_param_boolean(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
这个值是从url中的path获取的参数值,并转换为boolean类型。
def get_path_param_float(key, default=None)
根据key值获取参数值。
若key值不存在则返回默认值。
这个值是从url中的path获取的参数值,并转换为float类型。
def get_http_request_message()
获取HTTP请求报文。
输出
def set_header(self, header_dict)
设置HTTP报文头。
代码示例:
self.set_header({
"Content-Type": "application/json",
"Content-Length": "65"
})
_header_dict_中的key就是HTTP报文头的名称。
_header_dict_中的value就是HTTP报文头的值。
def clear_header(self, header_dict)
清除HTTP报文头, 相当于回撤set_header函数的操作。
def http_response(data)
将数据以http报文的形式返回。
def response_as_json(data)
将python的list或是dict转换成json数据。
def render(template_path, **kwargs)
返回模板引擎渲染后的数据, 详细用法请参照jinja2文档。
def redirect(url, status=302)
将页面重定向到指定url, 默认重定向的HTTP状态码为302。
karlooper.web.application
此模块提供了Application类。
class Application(handlers, settings=None, **kwargs)
根据handlers实例化一个HTTP服务。
示例:
handlers = {
"/hello": HelloHandler
}
application = Application(handlers, port=8080)
application.run()
如果在程序的任何地方都没有指定端口,那么程序会默认绑定80端口。
构造函数中的参数
handlers: urls和handlers之间的映射关系。
kwargs: 其他参数, 示例: port。
settings: 相关配置参数。
template: 配置模板路径。cookie: 配置cookie加密的基串。log_enable: 配置log日志是否开启port: 配置端口
方法
def listen(port)
监听端口。
def run()
运行web服务。
karlooper.template
_karlooper.template_为我们提供了一个简单地模板引擎。
这个模板系统是基于jinja2实现的。
Methods:
def render(template_path, **kwargs)
根据模板文件相对路径获取模板, 并生成HTML标签。
def render_string(template_string, **kwargs)
根据模板字符串生成HTML标签。
karlooper.escape
karlooper.escape 为HTML、XML、url提供了一些转义和解析的方法。
Escaping functions
def xhtml_escape(value)
将HTML或XML进行转义。
def xhtml_unescape(value)
将转义后的HTML或XML进行解析。
def url_escape(value, plus=True)
对url进行转义。
def url_unescape(value, plus=True)
将转义后的url进行解析。
Byte/unicode conversions
def to_unicode(value)
将utf-8字串转换为Unicode字串。
def to_basestring(value)
将bytes或Unicode转为str型。
def utf8(value)
将字符串转换为utf-8格式的。
karlooper.utils
karlooper.utils 提供了 security 和 parse_command_line 这两个模块。
security
class DES()
加密类
Methods:
def input(key, base=10)
设置加密使用的密钥。
def encode(s)
加密字符串。
def decode(s)
解密字符串。
parse_command_line
class CommandLineParser()
这个类用来解析命令行输入的参数。
Method:
def default(**kwargs)
设置相关参数的默认值, 如果参数未在命令行中设置, 则程序运行时使用此函数设置的默认值。
def parse_command_line()
解析命令行。
例如:
python index.py port=8080 log_enable=true cookie=9cedxz1r2q
其中, port=8080为指定端口号为8080,
log_enable=true为制定log日志为开启,
cookie=xxxxxx为制定cookie加密用的密令串。
karlooper.logger
karlooper.logger 提供了一个模块将log写入文件。
Methods:
def init_logger(config_path=None)
初始化logger, 如果配置文件路径未指定, 则读取默认配置文件。
初始化logger文件后, 我们可以在代码的任一地方使用如下代码:
logger = logging.getLogger()
logger.info("it is test")
将log写入文件。
karlooper.config
karlooper.config provides a model to operate the configuration file.
Methods:
def set_global_conf(key, data)
设置全局配置,如redis等的配置信息,更多实例可参考demos/chat-group项目。
def get_global_conf(key)
获取全局配置。