Giter Site home page Giter Site logo

kbengine-httpserver's Introduction

kbengine-httpserver

asynchronous httpserver for kbengine using fd operation api of kbengine

为kbengine写的http server 框架。站在巨人肩膀上面,借鉴收纳了tornadosanic的代码特性

features

  • 异步httpserver
  • 内嵌于kbengine引擎
  • 使用 kbengine 提供的 registerReadFileDescriptor deregisterReadFileDescriptor 等和读写文件描述符的API
  • 类tornado 和 sanic 的用法,结合两个框架的内容

Quick Start

类似于python tornado的使用方式,看下面的例子

from httpserver.app import Application, RequestHandler


class IndexHandler(RequestHandler):

    def get(self, *args, **kwargs):
        print("index, get")
        self.write_text("index ok")


class HomeHandler(RequestHandler):

    def get(self, *args, **kwargs):
        print("home, get")
        self.write_text("home ok")

    def post(self, *args, **kwargs):
        # DEBUG_MSG("post body, %s" % str(self.request.json))
        self.write_json(self.request.json)

url_map = [
    ("/index", IndexHandler),
    ("/home", HomeHandler)
]
app = Application(url_map)
app.run("0.0.0.0", 9191)

主要使用步骤:

  • 继承RequestHandler类,需要实现哪个HTTP method就赋写哪个函数(这些和tornado的使用方式类似)
    • get, post, put, delete 等等
  • 此类有self.request成员属性是Request对象,可以获取请求的各种数据。更多具体请看Request对象源码。此对象借鉴了sanic框架的思路
    • request.json
    • request.args:
      • args.get(key)
      • args.get_list(key)
    • request.query_string
    • request.path
  • 回给客户端请求使用下面几个函数。可以在去做一个异步的操作,然后在回调函数中去给client 回http包
    • write_json
    • write_text
    • write_raw
  • 定义 url 和 handler的对应关系,示例中的 url_map
  • 生成app实例,app.run() 就运行起此httpserver

说明:

  • 现在没有出pip包,如果要放到工程里面的话,直接把httpserver目录拷贝到自己工程中assets/scripts/server_common目录下面

requirements

  • httptools
  • multidict

安装上面两个库的最新版本即可,另外在使用pip安装的时候一定要安装和引擎内置Python版本一致的Python,再用pip安装,这样才能正确在引擎的Python版本下正常运行。因为httptools库使用了c扩展,所以要保证Python版本一致

完善与改进

  • 现在的router还不完善,url中现在只支持具体的url,不支持url中带不定参数给handler函数,也不支持url正则匹配
  • 如果要做 html template渲染,直接使用jinja2的包即可,自己封装一下渲染后的结果然后write_text

kbengine-httpserver's People

Contributors

asynchronoust avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kbengine-httpserver's Issues

安装httptools失败

感谢大佬的分享,期间遇到了个问题,过来请教一下
启动kbe, 提示 ModuleNotFoundError: No module named 'httptools.parser.parser'
1)kbe版本 1.3.12
2)python 3.7.3
3) httptools = 0.1.1 , 0.0.13 都试了不行

大佬,这个是什么问题,该怎么解决好 ? 期待回复,十分感谢

QQ截图20201109165402

QQ截图20201109165320

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.