博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
web.py 0.3 新手指南 - RESTful doctesting using app.request
阅读量:4107 次
发布时间:2019-05-25

本文共 2640 字,大约阅读时间需要 8 分钟。

!/usr/bin/env python"""RESTful web.py testingusage: python webapp.py 8080 [--test]>>> req = app.request('/mathematicians', method='POST')>>> req.status'400 Bad Request'>>> name = {'first': 'Beno\xc3\xaet', 'last': 'Mandelbrot'}>>> data = urllib.urlencode(name)>>> req = app.request('/mathematicians', method='POST', data=data)>>> req.status'201 Created'>>> created_path = req.headers['Location']>>> created_path'/mathematicians/b-mandelbrot'>>> fn = '

{0} {1}

'.format(name['first'], name['last'])>>> assert fn in app.request(created_path).data"""import doctestimport urllibimport sysimport webpaths = ( '/mathematicians(/)?', 'Mathematicians', '/mathematicians/([a-z])-([a-z]{2,})', 'Mathematician')app = web.application(paths, globals())dbname = {True: 'test', False: 'production'}[sys.argv[-1] == '--test']db = {} # db = web.database(..., db='math_{0}'.format(dbname))class Mathematicians: def GET(self, slash=False): """list all mathematicians and form to create new one""" if slash: raise web.seeother('/mathematicians') mathematicians = db.items() # db.select(...) return web.template.Template("""$def with (mathematicians)
Mathematicians

Mathematicians

$if mathematicians:
""")(mathematicians) def POST(self, _): """create new mathematician""" name = web.input('first', 'last') key = '{0}-{1}'.format(name.first[0].lower(), name.last.lower()) name.first, name.last = name.first.capitalize(), name.last.capitalize() db[key] = name # db.insert(...) path = '/mathematicians/{0}'.format(key) web.ctx.status = '201 Created' web.header('Location', path) return web.template.Template("""$def with (path, name)
Profile Created

Profile created for $name.first $name.last.

""")(path, name)class Mathematician: def GET(self, first_initial, last_name): """display mathematician""" key = '{0}-{1}'.format(first_initial, last_name) try: mathematician = db[key] # db.select(...) except KeyError: raise web.notfound() return web.template.Template("""$def with (name)
$name.first $name.last

Mathematicians

$name.first $name.last

""")(mathematician)if __name__ == "__main__": if sys.argv[-1] == '--test': doctest.testmod() else: app.run()

 

转载地址:http://jzosi.baihongyu.com/

你可能感兴趣的文章
laravel常用
查看>>
使用typescript
查看>>
maven tomcat:run 无法启动
查看>>
win10构建workspace时,报错提示/tmp/sources.sh: not found
查看>>
vant-upload上传
查看>>
docker拉去镜像指定平台
查看>>
create-react-app css不起作用
查看>>
vue-cli脚手架里如何配置屏幕自适应
查看>>
laravel通用apiResource get update
查看>>
laravel 修改api返回默认的异常处理
查看>>
高德坐标转换百度坐标 javascript
查看>>
tp5封装通用的修改某列值
查看>>
laravel控制器与模型名称不统一
查看>>
vue登录拦截
查看>>
npm配置淘宝镜像仓库以及electron镜像
查看>>
linux设置开机自启动脚本的最佳方式
查看>>
VUE SPA 单页面应用 微信oauth网页授权
查看>>
phpstorm 集成 xdebug 进行调试
查看>>
npm和node升级的正确方式
查看>>
laravel事务
查看>>