Skip to content
Albert Wang edited this page Apr 28, 2017 · 6 revisions

Welcome to the hyper2web wiki!

Documentation

from hyper2web import app

if __name__ == '__main__':

	# A basic callback style API is provided
	async def post_echo(http, stream):
		# this route essentially echo the data received back to client
		print('data received:')
		print(str(stream.data, encoding='utf8'))
		await http.send_and_end(stream, stream.data)

	app = app.App(static_file_handle='auto', root_route='index.html')
	app.post('name', post_echo)
	app.up()

class App

App(port=5000, root='./public)

The constructor of App class.

get(self, route: str, handler)

route: the restful api handler: an async function which accepts 2 arguments, a HTTP instance and a HTTP/2 stream

async def get_name(http: http.HTTP, stream: http.Stream):
	print('GET name hit')
	await handler.send_and_end('GET name hit')

app.get('name', get_name)

Module http

class Stream

properties

stream_id: int:
the id of a H2 stream

headers: dict:
the headers of a HTTP request. Just a conventional headers

data:
data of this request. Could be None

class HTTP

You should never construct a HTTP instance yourself. You only get it from the parameter of your router handler function.

methods

await send_and_end(stream: http.Stream, data: bytes):
Send data to client and end this stream.

await send_file(stream: http.Stream, file_path: str):
Send this file to client and end this stream. You should always use this method to send files.
If you use send_and_end, you have to manage flow control yourself.

await send_error(stream. http.Stream, error: int):
Send a HTTP error code to client.