基于twisted的web server框架简单原型
来源:互联网 发布:遥知不是雪,为有暗香来 编辑:程序博客网 时间:2024/06/11 19:55
# -*- coding=utf-8 -*-import sys, osfrom twisted.web import server, resourcefrom twisted.internet import reactorfrom twisted.web import static, serverfrom twisted.web.client import Requestimport jsonwebroot = ''class PathHandler(object): # 处理 http://domain/user/xxx 格式的请求 path_root = '/user/' def __init__(self): pass @classmethod def handle(self, request, path, args): index = path.find(self.path_root) if index < 0: return False path = path[index+len(self.path_root):] index = path.find('?') if index > 0: path = path[:index] func = 'handle_' + str(path) ret = False args_ = {} for key in args: args_[key] = args[key][0] if hasattr(PathHandler, func): print('func--', func) ret = {'errcode': 0, 'info': 'success'} exec "ret=self." + func + '(args_, request, ret)' else: print('func %s not found'%(func)) return ret @classmethod def handle_get_user_info(self, args, ret): """ /user/get_user_info handle_xxx:类似java web中spring的处理方式,函数自动对应到URI 这里的xxx对应get_user_info,请求路径为 /user/get_user_info """ pass @classmethod def handle_test404(self, args, request, ret): """ 测试返回404错误 如果这里直接finish,那么需要返回1,见server.py L228: if body == NOT_DONE_YET: return """ request.setResponseCode(404) ret = {'errcode': 1, 'info': 'not found'} request.write(json.dumps(ret)) request.finish() return 1class WebSite(resource.Resource): def __init__(self): resource.Resource.__init__(self) # self.putChild("*", self) def getChild(self, path, request): uri = request.uri # 处理静态文件 if uri.endswith('.js') or uri.endswith('.html')\ or uri.endswith('.css') or uri.endswith('.png') \ or uri.endswith('.jpg') or uri.endswith('.jpeg') \ or uri.endswith('.ico') or uri.endswith('.gif') \ or uri.endswith('.txt') : print webroot + uri return static.File(webroot + uri) return self # 处理get请求 def render_GET(self, request): uri = request.uri print 'uri:', uri try: # 将请求交给PathHandler处理,进一步扩展:创建并注册任意的handler以处理各类请求 ret = PathHandler.handle(request, request.uri, request.args) except Exception, e: print e.message ret = False print 'ret:' + str(ret) if ret == 1: print('ret is None') return ret if not ret: ret = {'errcode': 1, 'info': 'not implemented!'} return json.dumps(ret) # 返回json格式 # 处理post请求 def render_POST(self, request): return self.render_GET(request)def run(): dirpath = os.getcwd() global webroot webroot = dirpath + '/../../webroot' reactor.listenTCP(8081, server.Site(WebSite())) reactor.run()if __name__ == '__main__': run()
0 0
- 基于twisted的web server框架简单原型
- 一个简单的Twisted框架应用
- twisted编写简单的web代理服务器
- twisted 实现简单的web服务器
- 使用python的twisted框架编写一个简单的聊天室
- 使用python的twisted框架编写一个简单的聊天室
- 简单的web服务器原型
- Twisted的WEB开发
- twisted 框架学习 ----1.一个简单的客户端
- 一个简单的基于epoll的web server
- 一个简单的基于多线程的web server
- 一个简单的基于多线程的web server
- 一个简单的基于epoll的web server
- 基于OSGi的Virgo Server最简单Spring web实例
- 基于OSGi的Virgo Server最简单Spring web实例
- 基于OSGi的Virgo Server最简单Spring web实例
- python twisted 框架 client 和server 模型
- Configuring and Using the Twisted Web Server
- android studio 报编码 GBK 的不可映射字符问题
- jQuery 取值、赋值的基本方法
- 基于大数据报表系统
- 动态规划
- 设计模式深入学习IOS版------架构设计
- 基于twisted的web server框架简单原型
- JAVA_WEB项目之Lucene检索框架中的IndexWriter、IndexSearch优化
- C#.NET为List添加扩展方法:获取唯一值
- c++复习
- 53KF在线客服这些功能小而美
- Android 给应用定制皮肤
- 自己制作deb 包 (二)
- 非常有用的位运算技巧
- 监控系统的多协议直播(RTSP RTMP HTTP Live Streaming)