Pyjo.Headers - Headers

import Pyjo.Headers

# Parse
headers = Pyjo.Headers.new()
headers.parse("Content-Length: 42\x0d\x0a")
headers.parse("Content-Type: text/html\x0d\x0a\x0d\x0a")
print(headers.content_length)
print(headers.content_type)

# Build
headers = Pyjo.Headers.new()
headers.content_length = 42
headers.content_type = 'text/plain'
print(headers.to_str())

Pyjo.Headers is a container for HTTP headers based on RFC 7230 and RFC 7231.

Classes

class Pyjo.Headers.Pyjo_Headers(chunk=None, **kwargs)

Pyjo.Headers inherits all attributes and methods from Pyjo.Base and Pyjo.String.Mixin and implements the following new ones.

__init__(chunk=None, **kwargs)
headers = Pyjo.Headers.new()
headers = Pyjo.Headers.new("Content-Type: text/html\x0d\x0a\x0d\x0a")

Construct a new :mod`Pyjo.Headers` object.

__repr__()
reprstring = headers()

String representation of an object shown in console.

accept
accept = headers.accept
headers.accept = 'application/json'

Shortcut for the Accept header.

accept_charset
charset = headers.accept_charset
headers.accept_charset = 'UTF-8'

Shortcut for the Accept-Charset header.

accept_encoding
encoding = headers.accept_encoding
headers.accept_encoding = 'gzip'

Shortcut for the Accept-Encoding header.

accept_language
language = headers.accept_language
headers.accept_language = 'de, en'

Shortcut for the Accept-Language header.

accept_ranges
ranges = headers.accept_ranges
headers.accept_ranges = 'bytes'

Shortcut for the Accept-Ranges header.

access_control_allow_origin
origin = headers.access_control_allow_origin
headers.accept_ranges = 'bytes'

Shortcut for the Access-Control-Allow-Origin header from Cross-Origin Resource Sharing.

add(name, *args)
headers = headers.add('Foo', 'one value')
headers = headers.add('Foo', 'first value', 'second value')

Add one or more header values with one or more lines.

# "Vary: Accept"
# "Vary: Accept-Encoding"
headers.set(vary='Accept').add('Vary', 'Accept-Encoding').to_str()
allow
allow = headers.allow
headers.allow = 'GET, POST'

Shortcut for the Allow header.

append(name, value)
headers = headers.append('Vary', 'Accept-Encoding')

Append value to header and flatten it if necessary.

# "Vary: Accept"
headers.append('Vary', 'Accept').to_str()

# "Vary: Accept, Accept-Encoding"
headers.set(vary='Accept').append('Vary', 'Accept-Encoding').to_str()
authorization
authorization = headers.authorization
headers.authorization = 'Basic Zm9vOmJhcg=='

Shortcut for the Authorization header.

cache_control
control = headers.cache_control
headers.cache_control = 'max-age=1, no-cache'

Shortcut for the Cache-Control header.

charset = None
charset = headers.charset
headers.charset = 'ascii'

Charset used for encoding and decoding, defaults to ascii.

clone()
clone = headers.clone

Clone headers.

connection
connection = headers.connection
headers.connection = 'close'

Shortcut for the Connection header.

content_disposition
disposition = headers.content_disposition
headers.content_disposition = 'foo'

Shortcut for the Content-Disposition header.

content_encoding
encoding = headers.content_encoding
headers.content_encoding = 'gzip'

Shortcut for the Content-Encoding header.

content_language
language = headers.content_language
headers.content_language = 'en'

Shortcut for the Content-Language header.

content_length
length = int(headers.content_length)
headers.content_length = 4000

Shortcut for the Content-Length header.

content_location
location = headers.content_location
headers.content_location = 'http://127.0.0.1/foo'

Shortcut for the Content-Location header.

content_range
content_range = headers.content_range
headers.content_range = 'bytes 2-8/100'

Shortcut for the Content-Range header.

content_security_policy
policy = headers.content_security_policy
headers.content_security_policy = 'default-src https:'

Shortcut for the Content-Security-Policy header from Content Security Policy 1.0.

content_type
content_type = headers.content_type
headers.content_type = 'text/plain'

Shortcut for the Content-Type header.

cookie
cookie = headers.cookie
headers.cookie = 'f=b'

Shortcut for the Cookie header from RFC 6265.

date
date = headers.date
headers.date = 'Sun, 17 Aug 2008 16:27:35 GMT'

Shortcut for the Date header.

dnt
dnt = headers.dnt
headers.dnt = 1

Shortcut for the DNT (Do Not Track) header, which has no specification yet, but is very commonly used.

etag
etag = headers.etag
headers.etag = '"abc321"'

Shortcut for the ETag header.

expect
expect = headers.expect
headers.expect = '100-continue'

Shortcut for the Expect header.

expires
expires = headers.expires
headers.expires = 'Thu, 01 Dec 1994 16:00:00 GMT'

Shortcut for the Expires header.

from_dict(d)
headers = headers.from_dict({'Cookie': 'a=b'})
headers = headers.from_dict({'Cookie': ['a=b', 'c=d']})
headers = headers.from_dict({})

Parse headers from a hash reference, an empty hash removes all headers.

header(name, *args)
value = headers.header('Foo')
headers = headers.header('Foo', 'one value')
headers = headers.header('Foo', 'first value', 'second value')

Get or replace the current header values.

host
host = headers.host
headers.host = '127.0.0.1'

Shortcut for the Host header.

if_modified_since
date = headers.if_modified_since
headers.if_modified_since = 'Sun, 17 Aug 2008 16:27:35 GMT'

Shortcut for the If-Modified-Since header.

if_none_match
etag = headers.if_none_match
headers.if_none_match = '"abc321"'

Shortcut for the If-None-Match header.

is_finished
boolean = headers.is_finished

Check if header parser is finished.

is_limit_exceeded
boolean = headers.is_limit_exceeded

Check if headers have exceeded max_line_size or max_lines.

last_modified
date = headers.last_modified
headers.last_modified = 'Sun, 17 Aug 2008 16:27:35 GMT'

Shortcut for the Last-Modified header.

leftovers
chunk = headers.leftovers

Get and remove leftover data from header parser.

link = headers.link
headers.link = '<http://127.0.0.1/foo/3>; rel="next"'

Shortcut for the Link header from RFC 5988.

location
location = headers.location
headers.location = 'http://127.0.0.1/foo'

Shortcut for the Location header.

max_line_size = None
size = headers.max_line_size
headers.max_line_size = 1024

Maximum header line size in bytes, defaults to the value of the PYJO_MAX_LINE_SIZE environment variable or 8192 (8KB).

max_lines = None
num = headers.max_lines
headers.max_lines = 200

Maximum number of header lines, defaults to the value of the PYJO_MAX_LINES environment variable or 100.

names
names = headers.names

Return a list of all currently defined headers.

# Names of all headers
for n in headers.name:
    print(n)
origin
origin = headers.origin
headers.origin = 'http://example.com'

Shortcut for the Origin header from RFC 6454.

parse(string)
headers = headers.parse(b"Content-Type: text/plain\x0d\x0a\x0d\x0a")

Parse formatted headers.

proxy_authenticate
authenticate = headers.proxy_authenticate
headers.proxy_authenticate = 'Basic "realm"'

Shortcut for the Proxy-Authenticate header.

proxy_authorization
authorization = headers.proxy_authorization
headers.proxy_authorization = 'Basic Zm9vOmJhcg=='

Shortcut for the Proxy-Authorization header.

range
range = headers.range
headers.range = 'bytes=2-8'

Shortcut for the Range header.

referrer
referrer = headers.referrer
headers.referrer = 'http://example.com'

Shortcut for the Referer header, there was a typo in RFC 2068 which resulted in Referer becoming an official header.

remove(name)
headers = headers.remove('Foo')

Remove a header.

sec_websocket_accept
accept = headers.sec_websocket_accept
headers.sec_websocket_accept = 's3pPLMBiTxaQ9kYGzzhZRbK+xOo='

Shortcut for the Sec-WebSocket-Accept header from RFC 6455.

sec_websocket_extensions
extensions = headers.sec_websocket_extensions
headers.sec_websocket_extensions = 'foo'

Shortcut for the Sec-WebSocket-Extensions header from RFC 6455.

sec_websocket_key
key = headers.sec_websocket_key
headers.sec_websocket_key = 'dGhlIHNhbXBsZSBub25jZQ=='

Shortcut for the Sec-WebSocket-Key header from RFC 6455.

sec_websocket_protocol
protocol = headers.sec_websocket_protocol
headers.sec_websocket_protocol = 'sample'

Shortcut for the Sec-WebSocket-Protocol header from RFC 6455.

sec_websocket_version
version = int(headers.sec_websocket_version)
headers.sec_websocket_version = 13

Shortcut for the Sec-WebSocket-Version header from RFC 6455.

server
server = headers.server
headers.server = 'Pyjo'

Shortcut for the Server header.

cookie = headers.set_cookie
headers.set_cookie = 'f=b; path=/'

Shortcut for the Set-Cookie header from RFC 6265.

status
status = headers.status
headers.status = '200 OK'

Shortcut for the Status header from RFC 3875.

strict_transport_security
policy = headers.strict_transport_security
headers.strict_transport_security = 'max-age=31536000'

Shortcut for the Strict-Transport-Security header from RFC 6797.

te
te = headers.te
headers.te = 'chunked'

Shortcut for the TE header.

to_bytes()
bstring = headers.to_bytes()

Turn headers into a bytes string, suitable for HTTP messages.

to_dict()
single = headers.to_dict()

Turn headers into dict.

to_dict_list()
multi = headers.to_dict_list()

Turn headers into dict with list as its values.

to_str()
string = headers.to_str()

Turn headers into a string, suitable for HTTP messages.

trailer
trailer = headers.trailer
headers.trailer = 'X-Foo'

Shortcut for the Trailer header.

transfer_encoding
encoding = headers.transfer_encoding
headers.transfer_encoding = 'chunked'

Shortcut for the Transfer-Encoding header.

upgrade
upgrade = headers.upgrade
headers.upgrade = 'websocket'

Shortcut for the Upgrade header.

user_agent
agent = headers.user_agent
headers.user_agent = 'Mojo/1.0'

Shortcut for the User-Agent header.

vary
vary = headers.vary
headers.vary = '*'

Shortcut for the Vary header.

www_authenticate
authenticate = headers.www_authenticate
headers.www_authenticate = 'Basic realm="realm"'

Shortcut for the WWW-Authenticate header.

Pyjo.Headers.object

alias of Pyjo_Headers