bottle Python HTTP benutzerdefinierter Response-Code minimales Beispiel
In bottle kann ein benutzerdefinierter HTTP-Response-Code generiert werden, indem zuerst HTTPResponse importiert wird:
bottle_custom_response.py
from bottle import HTTPResponseund dann in der @route-Funktion ein HTTPResponse raisen, zum Beispiel
bottle_raise_httpresponse.py
raise HTTPResponse("This is a HTTP 401 test URL", status=401)Vollständiges Beispiel
bottle_custom_server.py
#!/usr/bin/env python3
from bottle import HTTPResponse, route, run
@route('/test401')
def hello():
raise HTTPResponse("This is a HTTP 401 test URL", status=401)
run(host='localhost', port=8080, debug=True)Dieses Skript ausführen, dann http://localhost/test401 öffnen, was zu einem HTTP-401-Antwortcode (unautorisiert) führt mit
output.txt
This is a HTTP 401 test URLals Antwortkörper.
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow