ServerMessage#

class ServerMessage(**properties: Any)#

Superclasses: Object

An HTTP server request and response pair.

A SoupServerMessage represents an HTTP message that is being sent or received on a Server.

Server will create SoupServerMessages automatically for incoming requests, which your application will receive via handlers.

Note that libsoup’s terminology here does not quite match the HTTP specification: in RFC 2616, an “HTTP-message” is either a Request, or a Response. In libsoup, a ServerMessage combines both the request and the response.

Methods#

class ServerMessage
get_http_version() HTTPVersion#

Get the HTTP version of msg.

get_local_address() SocketAddress | None#

Retrieves the SocketAddress associated with the local end of a connection.

get_method() str#

Get the HTTP method of msg.

get_reason_phrase() str | None#

Get the HTTP reason phrase of msg.

get_remote_address() SocketAddress | None#

Retrieves the SocketAddress associated with the remote end of a connection.

get_remote_host() str | None#

Retrieves the IP address associated with the remote end of a connection.

get_request_body() MessageBody#

Get the request body of msg.

get_request_headers() MessageHeaders#

Get the request headers of msg.

get_response_body() MessageBody#

Get the response body of msg.

get_response_headers() MessageHeaders#

Get the response headers of msg.

get_socket() Socket | None#

Retrieves the Socket that msg is associated with.

If you are using this method to observe when multiple requests are made on the same persistent HTTP connection (eg, as the ntlm-test test program does), you will need to pay attention to socket destruction as well (eg, by using weak references), so that you do not get fooled when the allocator reuses the memory address of a previously-destroyed socket to represent a new socket.

get_status() int#

Get the HTTP status code of msg.

get_tls_peer_certificate() TlsCertificate | None#

Gets the peer’s TlsCertificate associated with msg's connection. Note that this is not set yet during the emission of SoupServerMessage::accept-certificate signal.

Added in version 3.2.

get_tls_peer_certificate_errors() TlsCertificateFlags#

Gets the errors associated with validating msg's TLS peer certificate. Note that this is not set yet during the emission of SoupServerMessage::accept-certificate signal.

Added in version 3.2.

get_uri() Uri#

Get msg's URI.

is_options_ping() bool#

Gets if msg represents an OPTIONS message with the path *.

pause() None#

Pauses I/O on msg.

This can be used when you need to return from the server handler without having the full response ready yet. Use unpause to resume I/O.

Added in version 3.2.

set_http_version(version: HTTPVersion) None#

Set the HTTP version of msg.

Parameters:

version – a HTTPVersion

set_redirect(status_code: int, redirect_uri: str) None#

Sets msg's status_code to status_code and adds a Location header pointing to redirect_uri. Use this from a Server when you want to redirect the client to another URI.

redirect_uri can be a relative URI, in which case it is interpreted relative to msg's current URI. In particular, if redirect_uri is just a path, it will replace the path and query of msg's URI.

Parameters:
  • status_code – a 3xx status code

  • redirect_uri – the URI to redirect msg to

set_response(content_type: str | None, resp_use: MemoryUse, resp_body: list[int] | None = None) None#

Convenience function to set the response body of a ServerMessage. If content_type is None, the response body must be empty as well.

Parameters:
  • content_type – MIME Content-Type of the body

  • resp_use – a MemoryUse describing how to handle resp_body

  • resp_body – a data buffer containing the body of the message response.

set_status(status_code: int, reason_phrase: str | None = None) None#

Sets msg's status code to status_code.

If status_code is a known value and reason_phrase is None, the reason_phrase will be set automatically.

Parameters:
  • status_code – an HTTP status code

  • reason_phrase – a reason phrase

unpause() None#

Resumes I/O on msg.

Use this to resume after calling pause, or after adding a new chunk to a chunked response. I/O won’t actually resume until you return to the main loop.

Added in version 3.2.

Properties#

class ServerMessage
props.tls_peer_certificate: TlsCertificate#

The type of the None singleton.

Added in version 3.2.

props.tls_peer_certificate_errors: TlsCertificateFlags#

The type of the None singleton.

Added in version 3.2.

Signals#

class ServerMessage.signals
accept_certificate(tls_peer_certificate: TlsCertificate, tls_peer_errors: TlsCertificateFlags) bool#

The type of the None singleton.

Parameters:
  • tls_peer_certificate – the peer’s TlsCertificate

  • tls_peer_errors – the tls errors of tls_certificate

connected() None#

The type of the None singleton.

disconnected() None#

The type of the None singleton.

finished() None#

The type of the None singleton.

got_body() None#

The type of the None singleton.

got_chunk(chunk: Bytes) None#

The type of the None singleton.

Parameters:

chunk – the just-read chunk

got_headers() None#

The type of the None singleton.

wrote_body() None#

The type of the None singleton.

wrote_body_data(chunk_size: int) None#

The type of the None singleton.

Parameters:

chunk_size – the number of bytes written

wrote_chunk() None#

The type of the None singleton.

wrote_headers() None#

The type of the None singleton.

wrote_informational() None#

The type of the None singleton.