I'm trying to write a block of code to return the meaning (description) of an HTTP status code using the http.HTTPStatus Python class. I read the docs, but it seems I can only do the reverse. Is there way I can do this?
try:
if resp.status != 200:
message =
This isn't in the HTTPStatus docs because the behavior you are looking for is inherited from Enum. From the documentation, in python, enum:
uses call syntax to return members by value
In your example, this can be used like:
import http
print( http.HTTPStatus(404).name )
to get:
'NOT_FOUND'
EDIT 2025:
You can also use .phrase
or .description
print( http.HTTPStatus(404).phrase )
to get it as text without _
:
'Not Found'
and
print( http.HTTPStatus(404).description )
to get longer description:
'Nothing matches the given URI'
For your example you can use
message = http.HTTPStatus(resp.status).description