kopia lustrzana https://github.com/cirospaciari/socketify.py
added exception when listen fails, moved some files to ./tests/examples
rodzic
aab4427efc
commit
9d537a5919
|
@ -240,6 +240,8 @@ def uws_generic_listen_handler(listen_socket, config, user_data):
|
|||
app = ffi.from_handle(user_data)
|
||||
if hasattr(app, "_listen_handler") and hasattr(app._listen_handler, '__call__'):
|
||||
app.socket = listen_socket
|
||||
if listen_socket == ffi.NULL:
|
||||
raise RuntimeError("Failed to listen on port %d" % int(config.port))
|
||||
app._listen_handler(None if config == ffi.NULL else AppListenOptions(port=int(config.port),host=None if config.host == ffi.NULL else ffi.string(config.host).decode("utf-8"), options=int(config.options)))
|
||||
|
||||
@ffi.callback("void(uws_res_t *, void*)")
|
||||
|
|
|
@ -8,7 +8,7 @@ async def delayed_hello(delay, res):
|
|||
res.end("Hello with delay!")
|
||||
|
||||
def home(res, req):
|
||||
#request objecy only lives during the live time of this call
|
||||
#request object only lives during the life time of this call
|
||||
#get parameters, query, headers anything you need here
|
||||
delay = req.get_query("delay")
|
||||
delay = 0 if delay == None else float(delay)
|
||||
|
@ -16,15 +16,19 @@ def home(res, req):
|
|||
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
||||
res.run_async(delayed_hello(delay, res))
|
||||
|
||||
async def json_message(res, req):
|
||||
async def json(res, _):
|
||||
#req maybe will not be available in direct attached async functions
|
||||
#but if you dont care about req info you can do it
|
||||
await asyncio.sleep(2) #do something async
|
||||
res.write_header("Content-Type", "application/json")
|
||||
res.end({ "message": "I'm delayed!"})
|
||||
|
||||
def not_found(res, req):
|
||||
res.write_status(404).end("Not Found")
|
||||
|
||||
app.get("/", home)
|
||||
app.get("/json_message", json_message)
|
||||
app.get("/json", json)
|
||||
app.any("/*", not_found)
|
||||
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port)))
|
||||
app.run()
|
|
@ -0,0 +1,24 @@
|
|||
from socketify import App, AppOptions, AppListenOptions
|
||||
|
||||
app = App()
|
||||
|
||||
def home(res, req):
|
||||
res.end("Hello, World!")
|
||||
|
||||
def user(res, req):
|
||||
try:
|
||||
if int(req.get_parameter(0)) == 1:
|
||||
return res.end("Hello user 1!")
|
||||
finally:
|
||||
#invalid user tells to go, to the next route valid route (not found)
|
||||
req.set_yield(1)
|
||||
|
||||
def not_found(res, req):
|
||||
res.write_status(404).end("Not Found")
|
||||
|
||||
app.get("/", home)
|
||||
app.get("/user/:user_id", user)
|
||||
app.any("/*", not_found)
|
||||
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port)))
|
||||
app.run()
|
Ładowanie…
Reference in New Issue