Giter Site home page Giter Site logo

creatcodebuild / hyper2web Goto Github PK

View Code? Open in Web Editor NEW
27.0 5.0 5.0 396 KB

Super Fast Backend Framework (Educational Purpose)

Python 57.99% JavaScript 30.94% CSS 3.00% HTML 3.55% Makefile 4.38% Shell 0.14%
python backend web framework http2 corountine

hyper2web's Introduction

Hyper2Web

Super Fast HTTP2 Framework for Progressive Web Application

Installation

To install Hyper2Web, run this command in your terminal:

$ # due to a known issue, please install Curio manually
$ pip install git+https://github.com/dabeaz/curio.git
$ pip install hyper2web

This is the preferred method to install Hyper2Web, as it will always install the most recent stable release.

If you don't have pip installed, this Python installation guide can guide you through the process.

Dependency

Python3.6

h2

curio

Quick Start

Assuming you have a directory structure like:

your project/
--public/
  --index.html
  --index.js
  ...
--app.py

Your app.py looks like

from hyper2web import app

if __name__ == '__main__':

    # A basic callback style API is provided
    # Function name is up to you
    async def post_echo(request, response):
        # Send the data received back to the client
        await response.send(request.stream.data)

    app = app.App(port=5000)
    app.post('name', post_echo)
    app.up()

Then run this script

$ python app.py

That's it!

If you just want to serve static files, it's just 2 lines!

from hyper2web import app
app.App(port=5000).up()

Docs

Documentation is hosted on hyper2web.readthedocs.io.

Example

See the example folders for examples.

Test

$ python -m unittest discover test

Run all tests under test/ dir.

Misc

Why did I create this framework?

April 23rd, 2017, Sunday, I woke up and felt bored and decided to create my own HTTP2 web framework.

Since I had little or some prior web knowledge, this would be a super learning and fun project for me.

hyper2web's People

Contributors

creatcodebuild avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar

hyper2web's Issues

Add Unit Tests

Writing a framework is a big game. Test, test and test.

object has no attribute 'bind'

Traceback (most recent call last):
File "/home/wku/anaconda3/lib/python3.6/site-packages/curio/kernel.py", line 826, in _run_coro
trap = current._send(current.next_value)
File "/home/wku/anaconda3/lib/python3.6/site-packages/curio/task.py", line 96, in _task_runner
return await coro
File "/home/wku/Рабочий стол/[project]/[project_work]/mini/speed_test/server_hyper2web/hyper2web-dev/hyper2web/server.py", line 24, in h2_server
sock = create_listening_ssl_socket(address, certfile, keyfile)
File "/home/wku/Рабочий стол/[project]/[project_work]/mini/speed_test/server_hyper2web/hyper2web-dev/hyper2web/sslsocket.py", line 23, in create_listening_ssl_socket
sock.bind(address)
AttributeError: 'coroutine' object has no attribute 'bind'

Https

localhost.crt.pem and/or localhost.key don't exist. HTTP/2 needs certificate files

Add Application Configuration

Add configuration functionality by either using config files, command line parameters or system environment variables.

Config could be a module of its own.

Separate Server and App

Some functionalities are currently in server, since I used the example curio code in h2 as a starting point.

Now I see the needs to separate these code out and put them into the app module.

server is the module which actually talks with http.
app only handles restful end point.

Users built on top of app module. They should never touch server module unless they want to implement their own app module.

Should raise an error if no response method is called

async def post_record(http, stream, para):
    record = json.load(stream.data)
    # http.send_error(stream, 200)

Currently an end point function which doesn't call any response methods won't cause exceptions.

This function should cause an exception at the initialization time. That is, if the framework sees this, the app should not even start.

http.Response.headers implementation issue

self.headers = {
	':status': '200',
	'content-length': '0',  # 不知用户是否应该自己计算这个
	'server': 'hyper2web'
}

The current implementation doesn't support Python3.5 because dict is unordered in all Pythons prior to 3.6.

But HTTP requires certain order of headers.

A list implementation has order but can not do fast update of headers.

An ordered dict is probably the best choice. However, I can either achieve this by support Python3.6 only or by use the OrderedDict

The concern is what will happen when users do headers.update(some_new_headers).

If the old headers and new headers have common fields and these common fields have different orders, which order will the result dict retain?

If the behavior is obviously better in Python3.6, then I will get rid of Python3.5 support.

If not, I will keep support both versions of Python.

HTTP should not do IO

The current implementation of HTTP does IO. I should consider the design of h2 which does not do IO.

My HTTP class should be a wrapper of h2. The IO part should be done by a specialized IO class

serve index.html

If url points to a directory, serve the index.html under this directory instead of throwing errors.

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.