Python Glossary Русский

all Python 3http.clientexception

http.client.LineTooLong

exceptions

A status, header or chunk-size line in the response is longer than 65536 bytes. A guard against a server sending endless lines. A subclass of HTTPException.

Syntax

except http.client.LineTooLong as e: ...

The line length limit is hard-coded in the module and cannot be configured. The other guard, at most 100 headers, works differently: exceeding it raises a plain HTTPException "got more than 100 headers", not LineTooLong, so catch the base class (3.15 makes the limit a max_response_headers parameter). The exception text says which line was too long.

Examples

import http.client
import io
class FakeSock:  # сокет, читающий из готовых байтов
    def __init__(self, data):
        self.data = data
    def makefile(self, mode):
        return io.BytesIO(self.data)
long_header = b'HTTP/1.1 200 OK\r\nX: ' + b'a' * 70000 + b'\r\n\r\n'
try:
    http.client.HTTPResponse(FakeSock(long_header)).begin()
except http.client.LineTooLong as e:
    print(e)  # → got more than 65536 bytes when reading header line
many = b'HTTP/1.1 200 OK\r\n' + b'X: 1\r\n' * 101 + b'\r\n'
try:
    http.client.HTTPResponse(FakeSock(many)).begin()
except http.client.HTTPException as e:
    print(type(e).__name__, e)  # → HTTPException got more than 100 headers

See also

Open in the full glossarydocs.python.orgReport an inaccuracy