socketify.py/examples/not_found.py

34 wiersze
649 B
Python

from socketify import App, AppOptions, AppListenOptions
app = App()
2022-11-16 19:28:46 +00:00
2022-05-31 20:53:20 +00:00
async def home(res, req):
res.end("Hello, World!")
2022-11-16 19:28:46 +00:00
def user(res, req):
try:
if int(req.get_parameter(0)) == 1:
return res.end("Hello user 1!")
finally:
2022-11-16 19:28:46 +00:00
# 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")
2022-11-16 19:28:46 +00:00
app.get("/", home)
app.get("/user/:user_id", user)
app.any("/*", not_found)
2022-11-16 19:28:46 +00:00
app.listen(
3000,
lambda config: print(
"Listening on port http://localhost:%s now\n" % str(config.port)
),
)
app.run()