2022-05-31 17:56:37 +00:00
|
|
|
from socketify import App, AppOptions, AppListenOptions
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
app = App()
|
|
|
|
|
|
|
|
async def delayed_hello(delay, res):
|
|
|
|
await asyncio.sleep(delay) #do something async
|
|
|
|
res.end("Hello with delay!")
|
|
|
|
|
|
|
|
def home(res, req):
|
2022-05-31 19:27:56 +00:00
|
|
|
#request object only lives during the life time of this call
|
2022-05-31 17:56:37 +00:00
|
|
|
#get parameters, query, headers anything you need here
|
|
|
|
delay = req.get_query("delay")
|
|
|
|
delay = 0 if delay == None else float(delay)
|
|
|
|
#tell response to run this in the event loop
|
|
|
|
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
|
|
|
res.run_async(delayed_hello(delay, res))
|
|
|
|
|
2022-05-31 19:27:56 +00:00
|
|
|
async def json(res, _):
|
2022-05-31 17:56:37 +00:00
|
|
|
#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.end({ "message": "I'm delayed!"})
|
|
|
|
|
2022-05-31 19:27:56 +00:00
|
|
|
def not_found(res, req):
|
|
|
|
res.write_status(404).end("Not Found")
|
|
|
|
|
2022-05-31 17:56:37 +00:00
|
|
|
app.get("/", home)
|
2022-05-31 19:27:56 +00:00
|
|
|
app.get("/json", json)
|
|
|
|
app.any("/*", not_found)
|
2022-05-31 17:56:37 +00:00
|
|
|
|
|
|
|
app.listen(3000, lambda config: print("Listening on port http://localhost:%s now\n" % str(config.port)))
|
2022-06-02 19:00:42 +00:00
|
|
|
|
2022-05-31 17:56:37 +00:00
|
|
|
app.run()
|