Python Glossary Русский

all Python 3http.clientexception

http.client.BadStatusLine

exceptions

The first line of the response does not look like an HTTP status line, for example the server sent something other than HTTP, or garbage. A subclass of HTTPException.

Syntax

except http.client.BadStatusLine as e: e.line

Common causes: talking plain http:// to a port that expects TLS, or to a non-HTTP service (a database, SSH). The exception text is the line itself in repr, showing what arrived. Before 3.5 an empty response gave BadStatusLine(''); now it is RemoteDisconnected, a subclass, so an old except BadStatusLine still catches that case.

Examples

import http.client
import socket
server, client = socket.socketpair()
server.sendall(b'SSH-2.0-OpenSSH_9.6\r\n')
server.close()
resp = http.client.HTTPResponse(client)
try:
    resp.begin()
except http.client.BadStatusLine as e:
    print(repr(e.line))  # → 'SSH-2.0-OpenSSH_9.6\r\n'
client.close()

See also

Open in the full glossarydocs.python.orgReport an inaccuracy