# -*- coding: utf-8 -*- """code.py""" from application import my_app urls = ( ("/", "index"), ("/hello/(.*)", "hello"), ) wsgiapp = my_app(urls, globals()) class index: def GET(self): my_app.header('Content-type', 'text/plain') return "Welcome!\n" class hello: def GET(self, name): my_app.header('Content-type', 'text/plain') return "Hello %s!\n" % name if __name__ == '__main__': from wsgiref.simple_server import make_server httpd = make_server('', 8086, wsgiapp) sa = httpd.socket.getsockname() print 'http://{0}:{1}/'.format(*sa) # Respond to requests until process is killed httpd.serve_forever() 当然,您还可以在code.py中设备更多的URL映射,并实现响应的类来对请求作出响应。
本文重要参考了 How to write a web framework in Python(作者 anandology 是web.py代码的两位保护者之一,另一位则是大年夜名鼎鼎却竽暌耿年早逝的 Aaron Swartz),在此基本上作了一些调剂和修改,并掺杂了本身的一些设法主意。
推荐阅读
FreeFileSync:在Ubuntu中对比及同步文件
FreeFileSync 是一个自由、开源以及跨平台的文件夹比较及同步软件,它可以赞助你同步 Linux、Windows 和 Mac OS 中的文件和文件夹。它是便携的,也可以被安装在本地体系中,它的功能丰富>>>详细阅读
本文标题:用Python写一个简单的Web框架
地址:http://www.17bianji.com/lsqh/35657.html
1/2 1