java centipede,centipede-蜈蚣是一个WSGI微框架,带有一个简单的基于装饰器的路由器。-Asbjorn Enge Query string data Form data 0.2...

Centipede是一个轻量级的WSGI框架,它使用装饰器来定义路由,强调简单性和清晰的编程模式。它支持HTTP响应状态码、内容类型和头信息的设置。对于GET和POST请求,它可以方便地处理查询字符串和JSON数据。推荐在生产环境中使用静态文件服务器托管静态文件,而在开发阶段,可以通过Centipede提供。部署时,建议将其置于如Gunicorn或uWSGI等WSGI服务器后面。
摘要由CSDN通过智能技术生成

作者:Asbjorn Enge

### 作者邮箱:asbjorn@asbjornenge.com

### 首页:http://centipede.asbjornenge.com

### 文档:None

### 下载链接 UNKNOWN

===============

Centipede 0.2.5

Centipede is a WSGI microframework with a simple decorator based router. It's strength is that it models the technology in use and tries not to confuse developers with complex patterns and tricks. It inherits strongly from urlrelay_.

Installation

::

$ pip install centipede

Defining handlers

With Centipede you expose functions to urls. Functions either return a string or a tuple. A string is treated as the document body, http status is set to 200 OK and returned to the browser. Should you return a tuple, status code, body and headers are expected. The expose decorator also supports a few arguments.

::

from centipede import expose, app

@expose('^/$')

def index(request):

""" Simple Hello

"""

return 'Hello IgglePigglePartyPants!'

@expose('^/google$')

def index(request):

""" A redirect

"""

return (307, '', {'Location':'http://google.com'})

import json

@expose('^/twitter','POST',content_type='application/json')

def tweet_post(request):

""" Tweet

"""

data = request['data']

text = data['text']

user = data['user']

tweet = magic.tweet(text, user)

return json.dumps(tweet)

@expose('^/twitter/(?P\w+)$','GET',content_type='application/json')

def twitter(request):

""" Get a tweet

"""

id = req['wsgiorg.routing_args'][1]['tweet']

meta = request['params']['metadata']

tweet = magic.get_tweet(id, meta=meta)

return json.dumps(tweet)

application = app()

Expose arguments

The expose decorator looks like this::

expose(url_pattern, method='GET', content_type='text/html', charset='UTF-8')

Request

The parameter passed to the functions exposed (request in the examples above) is the WSGI environ_ dictionary. For convenience the query string parameters and form data parameters are packed into environ's params and data keys.

Query string data

For convenience, query string parameters are available as a dictionary in environ's params key. Both key and value are unquoted using urllib.unquote. Unquoted parameters are passed to the params_raw key.

Form data

For convenience, form data are available as a dictionary in environ's data key. Both key and value are unquoted using urllib.unquote_plus. Unquoted parameters are passed to the data_raw key.

Templates

I would recommend keeping your html templates static on the client side and use a javascript template library. But if you really need some server side templating, have a look at mako.

Static files

For production you should always host your static files directly from the webserver or a varnish cache or something. But for development purposes you can have centipede serve your static files by passing app a parameter::

app('path/to/static')

Deployment

For deployment it is a good idea to run your centipede application behind a good WSGI server. There is a bunch. Gunicorn is good. I usually end up running uwsgi_ behind nginx.

Changelog

0.2.5

Separated query string params and form data

Form data in data key

Query string params in params key

Improved error handling for unpacking params (needs more work)

0.2.4

Added urllib.unquote_plus for POST parameters.

Added params_raw key to environ in case urllib.unquote mess up your parameters.

0.2.3

Added params key to environ for easy parameter access.

Added urllib.unquote for params

enjoy.

.. _urlrelay: http://pypi.python.org/pypi/urlrelay/ .. _environ: http://www.python.org/dev/peps/pep-0333/#environ-variables .. _Gunicorn: http://gunicorn.org/ .. _uwsgi: http://projects.unbit.it/uwsgi/ .. _bunch: http://www.wsgi.org/en/latest/servers.html

Copy from pypi.org

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值