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

你可能感兴趣的文章
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Bracket Subsequence
查看>>
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D. Array Restoration
查看>>
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-E. Down or Right
查看>>
Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra
查看>>
D - happy happy happy HDU - 6196(剪枝+中途相遇+搜索)
查看>>
B - K-th Number HDU - 6231
查看>>
M - Geometry Problem HDU - 6242
查看>>
《人人都是产品经理2.0》阅读笔记-01——摘自作者自序
查看>>
Centos 7下mysql 8.0.11的rpm包的安装方式
查看>>
多线程入门
查看>>
Java中线程的几种状态转换以及涉及到的方法
查看>>
Java中interrupt、interrupted和isInterrupted的关系与区别
查看>>
Java线程的join()方法浅析
查看>>
Centos7安装与卸载Docker
查看>>
Linux/Centos7以RPM方式安装mysql-5.7.23
查看>>
快速配置Let's encrypt通配符证书
查看>>
Centos7安装OpenJDK1.8
查看>>
开源博客Solo安装详细教程及注意事项
查看>>
spark简介
查看>>
spark算子详解------spark算子分类
查看>>