twisted08-1 多级访问-httpserver
2015-08-12 19:59
393 查看
#coding=utf8 from twisted.internet import reactor from twisted.web.server import Site from twisted.web.resource import Resource, NoResource from twisted.web.static import File from calendar import calendar import sys class Calendar(Resource): def __init__(self, year): #必须要调用父类的构造函数,否则由于 #self.children没有初始化,getChildWithDefault会出错,因为self.children是None Resource.__init__(self) #super在这里不能使用,super只能用于python新类(从object派生),不能用于老类 #super(Calendar,self).__init__() assert(isinstance(year, (int, long))) self.year = year def render_GET(self, request): return '<html><h1><pre>%s</pre></h1></html>' % calendar(self.year) def getChild(self, name,request): if name.isdigit() and int(name) > 1990: #递归处理下一个name的日历 print 'next_name->',name return Calendar(int(name)) else: return NoResource() class URL_Dispatcher(Resource): def getChild(self, name, request): #name不是URI,仅是名称,request.uri才是真正的URI if name == '/' or not name: return self #return ico resouce elif name == 'favicon.ico': return File('d:/down.png') #仅仅只能处理 http://localhost:8000/1995 #不能处理http://localhost:8000/1995/1995 or http://localhost:8000/1995/1995/../../.. #如果返回一个资源对象,将继续getChild往下递归访问,以'/'为分隔符 elif name.isdigit() and int(name) > 1990: print 'name->:',name y = int(name) return Calendar(y) else: return NoResource() def render_GET(self, request): return '<html><h1>Calendar</h1></html>' if __name__ == '__main__': reload(sys) sys.setdefaultencoding('utf8') s = Site(URL_Dispatcher()) reactor.listenTCP(8000,s) reactor.run()
相关文章推荐
- HttpURLConnection和HttpClient的用法
- HTTPConnection与HTTPClient
- http://blog.sina.com.cn/s/blog_777a9ccb01013maj.html
- Java判断和检查网络
- TCP/IP网络编程 学习笔记_9 --域名系统(DNS)
- TCP/IP网络编程 学习笔记_9 --域名系统(DNS)
- 黑马程序员——java学习14(毕23-24)——网络编程
- http服务器交互get,put,post,delete等概念
- 网络篇----数据安全
- iOS安全系列之一:HTTPS
- Android网络取数据显示到ListView实例
- 卷积神经网络(CNN)笔记
- TCP/IP、Http、Socket的区别
- Http 异步批量下载图片之优化
- IOS--http状态码快速查看
- iOS安全系列之 HTTPS
- android中Http和HttpClient的Get和Post方法的使用
- http异步大量图片下载,并用GridView显示九宫格图片样式
- 通过http user-agent判断是否为手机浏览器
- 运行中修改协调器PAN ID和Channel,协调器广播至所有的设备重启加入新建后的网络