hyper2web package

Submodules

hyper2web.abstract module

This module defines all Abstract Types in the framework. Mainly for Better Organization of Code and For Better IDE Type Inferences

class hyper2web.abstract.AbstractApp[source]

Bases: object

This class is the base class of any App classes. The framework provides an App class which implements all methods of this class.

A user can also implement this class if the user find that the App class is insufficient. But, the creator recommends user to extend App class instead of AbstractApp class.

get(route: str, handler)[source]
handle_route(http, stream)[source]

This function has to be async.

post(route: str, handler)[source]
up()[source]
class hyper2web.abstract.AbstractHTTP[source]

Bases: object

The HTTP class implements this class.

All methods declared here are async.

handle_event(event)[source]
send(stream_id, headers, data)[source]
class hyper2web.abstract.AbstractRequest[source]

Bases: object

class hyper2web.abstract.AbstractResponse[source]

Bases: object

A Response should be constructed by the router. The router passes Stream information such as stream id to the Response object and Response object is passed to the end point function for top level users’ use.

The flow control is handled by the HTTP class. Therefore, a send method always ends the response.

All send methods should call HTTP’s send method

send(data: bytes)[source]

send this response. :param headers: HTTP headers :param data: Body of the response. Has to be bytes That means, if you want to send some string, you have to convert the string to bytes. The framework does not do type conversion for you. It just fails if incorrect type is passed.

send_file(file_path)[source]
send_status_code(status_code)[source]
class hyper2web.abstract.AbstractRouter[source]

Bases: object

handle_route(http, stream)[source]
register(method: str, route: str, handler)[source]

hyper2web.app module

class hyper2web.app.App(address='0.0.0.0', port=5000, root='./public', auto_serve_static_file=True, default_file='index.html', router=<class 'hyper2web.router.Router'>)[source]

Bases: hyper2web.abstract.AbstractApp

This class is the main class which users should be interact with.

This is the only class which users should construct.

get(route: str, handler)[source]

Register a GET handler.

Parameters:
  • route – A string which represent a RESTful route with optional parameters
  • handler – A handler function. Has to be async.
handle_route(http: hyper2web.http.HTTP, stream: hyper2web.http.Stream)[source]

When the framework gets a incoming request, handle this request to corresponding routing handler.

Only used by the framework. Users should never call it.

post(route: str, handler)[source]

The same as self.get except that it’s for POST

up()[source]

Start the server. This is the last function users should call.

Users only call this function after set up all routing handlers.

hyper2web.app.default_get(app)[source]

This function is the default handler for GET request whose :path is registered in the router.

To be more clear, a user does not have to register GET /index.html or GET /any_static_file.xxx. Any :path which is not found in the router will initiate this method.

This method treats all requests as a GET /static_file. If :path is not a existing file path, it returns status code 404.

Users should not use this function.

hyper2web.app.get_index(app)[source]

The default handler for GET /.

The default behavior for GET / is GET /index.html.

If a user specifies a default_file in the constructor of App, the behavior becomes GET /default_file

Users should not use this function.

hyper2web.cli module

hyper2web.exceptions module

Exceptions in hyper2web

exception hyper2web.exceptions.DifferentStreamIdException[source]

Bases: Exception

exception hyper2web.exceptions.RouteNotRegisteredException[source]

Bases: Exception

hyper2web.http module

This module implements HTTP methods for end user

I currently think that they should be synchronized since they should not do IO Where as endpoint module is designed for IO

class hyper2web.http.HTTP(app: hyper2web.abstract.AbstractApp, sock, connection: h2.connection.H2Connection)[source]

Bases: hyper2web.abstract.AbstractHTTP

This class further implements complete HTTP2 on top of h2

data_received(event: h2.events.DataReceived)[source]

Handle received data for a certain stream. Currently used for POST

handle_event(event: h2.events.Event)[source]
request_received(event: h2.events.RequestReceived)[source]

Handle a request

send(stream_id: int, headers, data: bytes = None)[source]

send the response to the client :param stream_id: the stream id associated with this request/response :param headers: HTTP headers. a sequence(tuple/list) of tuples

((‘:status’, ‘200’),
(‘content-length’, ‘0’), (‘server’, ‘hyper2web’))
Parameters:data – HTTP response body. Has to be bytes(binary data).

It’s users’ responsibility to encode any kinds of data to binary.

wait_for_flow_control(stream_id)[source]

Blocks until the flow control window for a given stream is opened.

window_updated(event)[source]

Unblock streams waiting on flow control, if needed.

class hyper2web.http.Request(stream, para)[source]

Bases: hyper2web.abstract.AbstractRequest

class hyper2web.http.Response(stream_id: int, http: hyper2web.http.HTTP)[source]

Bases: hyper2web.abstract.AbstractResponse

send(data: bytes)[source]
send_file(file_path)[source]
send_status_code(status_code)[source]
set_header(field, value)[source]
set_headers(headers)[source]
update_headers(headers)[source]
class hyper2web.http.Stream(stream_id: int, headers: dict)[source]

Bases: object

As the code is right now, many stream implementation is done in endpoint.EndPointHandler Am moving those functionality to this class

The current design is that application will only return complete stream to top level api But, since a user might also want to program on a live stream. For example, the client may send a giant file 1GB, the user will want to write this stream to disk in real time Also, buffering 1GB in memory is kind of stupid.

But nonethelss, the current focus is on better organization of code instead of more API or performace.

finalize()[source]

concat all data chunks in this handler to one bytes object

update(event: h2.events.DataReceived)[source]

assume only POST stream will call this one

hyper2web.router module

class hyper2web.router.Router(default_get)[source]

Bases: hyper2web.abstract.AbstractRouter

User should never construct Router

find_match(path: str)[source]

‘user/{userId}’ should match ‘user/abc’ userId = abc return a tuple (matched, parameters) matched is the route which matches the incoming path parameters is a dict of parameters and their values

handle_route(http: hyper2web.http.HTTP, stream: hyper2web.http.Stream)[source]
register(method: str, route: str, handler)[source]

hyper2web.server module

A fully-functional HTTP/2 server written for curio.

Requires Python 3.5+.

class hyper2web.server.H2Server(sock, app: hyper2web.abstract.AbstractApp)[source]

Bases: object

This class just connects to socket and that’s about it. Most heavy lifting is done by http.HTTP

run()[source]

Loop over the connection, managing it appropriately.

hyper2web.server.h2_server(address, certfile, keyfile, app: hyper2web.abstract.AbstractApp)[source]

Create an HTTP/2 server at the given address.

hyper2web.sslsocket module

hyper2web.sslsocket.create_listening_ssl_socket(address, certfile, keyfile)[source]

Create and return a listening TLS socket on a given address.

Module contents