博客
关于我
强烈建议你试试无所不能的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/

你可能感兴趣的文章
Merge Two Sorted Lists 合并两个有序链表
查看>>
pow(x,n) 为什么错这么多次
查看>>
Jump Game 动态规划
查看>>
Binary Tree Maximum Path Sum 自底向上求解(重重重重)
查看>>
Subsets 深搜
查看>>
Subsets II
查看>>
Edit Distance 字符串距离(重重)
查看>>
Gray Code 格雷码
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
谷歌阅读器将于2013年7月1日停止服务,博客订阅转移到邮箱
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>