Session#
Superclasses: Object
Soup session state object.
Session is the object that controls client-side HTTP. A
Session encapsulates all of the state that libsoup is keeping
on behalf of your program; cached HTTP connections, authentication
information, etc. It also keeps track of various global options
and features that you are using.
Most applications will only need a single Session; the primary
reason you might need multiple sessions is if you need to have
multiple independent authentication contexts. (Eg, you are
connecting to a server and authenticating as two different users at
different times; the easiest way to ensure that each Message
is sent with the authentication information you intended is to use
one session for the first user, and a second session for the other
user.)
Additional Session functionality is provided by
SessionFeature objects, which can be added to a session with
add_feature or add_feature_by_type
For example, Logger provides support for
logging HTTP traffic, ContentDecoder provides support for
compressed response handling, and ContentSniffer provides
support for HTML5-style response body content sniffing.
Additionally, subtypes of Auth can be added
as features, to add support for additional authentication types.
All SoupSessions are created with a AuthManager, and support
for %SOUP_TYPE_AUTH_BASIC and %SOUP_TYPE_AUTH_DIGEST. Additionally,
sessions using the plain Session class (rather than one of its deprecated
subtypes) have a ContentDecoder by default.
Note that all async methods will invoke their callbacks on the thread-default context at the time of the function call.
Constructors#
Methods#
- class Session
 - 
- add_feature(feature: SessionFeature) None#
 Adds
feature's functionality tosession. You cannot add multiple features of the same [alias``GObject``.Type] to a session.See the main
Sessiondocumentation for information on what features are present in sessions by default.- Parameters:
 feature – an object that implements
SessionFeature
- add_feature_by_type(feature_type: GType) None#
 If
feature_typeis the type of a class that implementsSessionFeature, this creates a new feature of that type and adds it tosessionas withadd_feature. You can use this when you don’t need to customize the new feature in any way. Adding multiple features of the samefeature_typeis not allowed.If
feature_typeis not aSessionFeaturetype, this gives each existing feature onsessionthe chance to acceptfeature_typeas a “subfeature”. This can be used to add newAuthtypes, for instance.See the main
Sessiondocumentation for information on what features are present in sessions by default.- Parameters:
 feature_type – a
Type
- get_accept_language() str | None#
 Get the value used by
sessionfor the “Accept-Language” header on new requests.
- get_accept_language_auto() bool#
 Gets whether
sessionautomatically sets the “Accept-Language” header on new requests.
- get_async_result_message(result: AsyncResult) Message | None#
 Gets the
Messageof theresultasynchronous operation This is useful to get theMessageof an asynchronous operation started bysessionfrom its [callback``Gio``.AsyncReadyCallback].- Parameters:
 result – the
AsyncResultpassed to your callback
- get_feature(feature_type: GType) SessionFeature | None#
 Gets the feature in
sessionof typefeature_type.- Parameters:
 feature_type – the
Typeof the feature to get
- get_feature_for_message(feature_type: GType, msg: Message) SessionFeature | None#
 Gets the feature in
sessionof typefeature_type, provided that it is not disabled formsg.- Parameters:
 feature_type – the
Typeof the feature to getmsg – a
Message
- get_idle_timeout() int#
 Get the timeout in seconds for idle connection lifetime currently used by
session.
- get_local_address() InetSocketAddress | None#
 Get the
InetSocketAddressto use for the client side of connections insession.
- get_max_conns_per_host() int#
 Get the maximum number of connections that
sessioncan open at once to a given host.
- get_proxy_resolver() ProxyResolver | None#
 Get the
ProxyResolvercurrently used bysession.
- get_remote_connectable() SocketConnectable | None#
 Gets the remote connectable if one set.
- get_tls_database() TlsDatabase | None#
 Get the
TlsDatabasecurrently used bysession.
- get_tls_interaction() TlsInteraction | None#
 Get the
TlsInteractioncurrently used bysession.
- get_user_agent() str | None#
 Get the value used by
sessionfor the “User-Agent” header on new requests.
- has_feature(feature_type: GType) bool#
 Tests if
sessionhas at a feature of typefeature_type(which can be the type of either aSessionFeature, or else a subtype of some class managed by another feature, such asAuth).- Parameters:
 feature_type – the
Typeof the class of features to check for
- async preconnect_async(self, msg: Message, io_priority: int) bool#
 This is the awaitable version of
preconnect_async().- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
- preconnect_async(msg: Message, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Start a preconnection to
msg.Once the connection is done, it will remain in idle state so that it can be reused by future requests. If there’s already an idle connection for the given
msghost, the operation finishes successfully without creating a new connection. If a new request for the givenmsghost is made while the preconnect is still ongoing, the request will take the ownership of the connection and the preconnect operation will finish successfully (if there’s a connection error it will be handled by the request).The operation finishes when the connection is done or an error occurred.
- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
cancellable – a
Cancellablecallback – the callback to invoke when the operation finishes
user_data – data for
progress_callbackandcallback
- preconnect_finish(result: AsyncResult) bool#
 Complete a preconnect async operation started with
preconnect_async.- Parameters:
 result – the
AsyncResultpassed to your callback
- remove_feature(feature: SessionFeature) None#
 Removes
feature's functionality fromsession.- Parameters:
 feature – a feature that has previously been added to
session
- remove_feature_by_type(feature_type: GType) None#
 Removes all features of type
feature_type(or any subclass offeature_type) fromsession.- Parameters:
 feature_type – a
Type
- send(msg: Message, cancellable: Cancellable | None = None) InputStream#
 Synchronously sends
msgand waits for the beginning of a response.On success, a
InputStreamwill be returned which you can use to read the response body. (“Success” here means only that an HTTP response was received and understood; it does not necessarily mean that a 2xx class status code was received.)If non-
None,cancellablecan be used to cancel the request;sendwill return a %G_IO_ERROR_CANCELLED error. Note that with requests that have side effects (eg,POST,PUT,DELETE) it is possible that you might cancel the request after the server acts on it, but before it returns a response, leaving the remote resource in an unknown state.If
msgis requeued due to a redirect or authentication, the initial (3xx/401/407) response body will be suppressed, andsendwill only return once a final response has been received.Possible error domains include
SessionError,IOErrorEnum, andTlsErrorwhich you may want to specifically handle.- Parameters:
 msg – a
Messagecancellable – a
Cancellable
- send_and_read(msg: Message, cancellable: Cancellable | None = None) Bytes#
 Synchronously sends
msgand reads the response body.On success, a
Byteswill be returned with the response body. This function should only be used when the resource to be retrieved is not too long and can be stored in memory.See
sendfor more details on the general semantics.- Parameters:
 msg – a
Messagecancellable – a
Cancellable
- async send_and_read_async(self, msg: Message, io_priority: int) Bytes#
 This is the awaitable version of
send_and_read_async().- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
- send_and_read_async(msg: Message, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Asynchronously sends
msgand reads the response body.When
callbackis called, then eithermsghas been sent, and its response body read, or else an error has occurred. This function should only be used when the resource to be retrieved is not too long and can be stored in memory. Callsend_and_read_finishto get aByteswith the response body.See
sendfor more details on the general semantics.- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
cancellable – a
Cancellablecallback – the callback to invoke
user_data – data for
callback
- send_and_read_finish(result: AsyncResult) Bytes#
 Gets the response to a
send_and_read_async.If successful, returns a
Byteswith the response body.- Parameters:
 result – the
AsyncResultpassed to your callback
- send_and_splice(msg: Message, out_stream: OutputStream, flags: OutputStreamSpliceFlags, cancellable: Cancellable | None = None) int#
 Synchronously sends
msgand splices the response body stream intoout_stream.See
sendfor more details on the general semantics.Added in version 3.4.
- Parameters:
 msg – a
Messageout_stream – a
OutputStreamflags – a set of
OutputStreamSpliceFlagscancellable – a
Cancellable
- async send_and_splice_async(self, msg: Message, out_stream: OutputStream, flags: OutputStreamSpliceFlags, io_priority: int) int#
 This is the awaitable version of
send_and_splice_async().Added in version 3.4.
- Parameters:
 msg – a
Messageout_stream – a
OutputStreamflags – a set of
OutputStreamSpliceFlagsio_priority – the I/O priority of the request
- send_and_splice_async(msg: Message, out_stream: OutputStream, flags: OutputStreamSpliceFlags, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Asynchronously sends
msgand splices the response body stream intoout_stream. Whencallbackis called, then eithermsghas been sent and its response body spliced, or else an error has occurred.See
sendfor more details on the general semantics.Added in version 3.4.
- Parameters:
 msg – a
Messageout_stream – a
OutputStreamflags – a set of
OutputStreamSpliceFlagsio_priority – the I/O priority of the request
cancellable – a
Cancellablecallback – the callback to invoke
user_data – data for
callback
- send_and_splice_finish(result: AsyncResult) int#
 Gets the response to a
send_and_splice_async.Added in version 3.4.
- Parameters:
 result – the
AsyncResultpassed to your callback
- async send_async(self, msg: Message, io_priority: int) InputStream#
 This is the awaitable version of
send_async().- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
- send_async(msg: Message, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Asynchronously sends
msgand waits for the beginning of a response.When
callbackis called, then eithermsghas been sent, and its response headers received, or else an error has occurred. Callsend_finishto get aInputStreamfor reading the response body.See
sendfor more details on the general semantics.- Parameters:
 msg – a
Messageio_priority – the I/O priority of the request
cancellable – a
Cancellablecallback – the callback to invoke
user_data – data for
callback
- send_finish(result: AsyncResult) InputStream#
 Gets the response to a
send_asynccall.If successful returns a
InputStreamthat can be used to read the response body.- Parameters:
 result – the
AsyncResultpassed to your callback
- set_accept_language(accept_language: str) None#
 Set the value to use for the “Accept-Language” header on
Messages sent fromsession.If
accept_languageisNonethen no “Accept-Language” will be included in requests. Seeaccept_languagefor more information.- Parameters:
 accept_language – the languages string
- set_accept_language_auto(accept_language_auto: bool) None#
 Set whether
sessionwill automatically set the “Accept-Language” header on requests using a value generated from system languages based onget_language_names.See
accept_language_autofor more information.- Parameters:
 accept_language_auto – the value to set
- set_idle_timeout(timeout: int) None#
 Set a timeout in seconds for idle connection lifetime to be used by
sessionon new connections.See
idle_timeoutfor more information.- Parameters:
 timeout – a timeout in seconds
- set_proxy_resolver(proxy_resolver: ProxyResolver | None = None) None#
 Set a
ProxyResolverto be used bysessionon new connections.If
proxy_resolverisNonethen no proxies will be used. Seeproxy_resolverfor more information.- Parameters:
 proxy_resolver – a
ProxyResolverorNone
- set_timeout(timeout: int) None#
 Set a timeout in seconds for socket I/O operations to be used by
sessionon new connections.See
timeoutfor more information.- Parameters:
 timeout – a timeout in seconds
- set_tls_database(tls_database: TlsDatabase | None = None) None#
 Set a
TlsDatabaseto be used bysessionon new connections.If
tls_databaseisNonethen certificate validation will always fail. Seetls_databasefor more information.- Parameters:
 tls_database – a
TlsDatabase
- set_tls_interaction(tls_interaction: TlsInteraction | None = None) None#
 Set a
TlsInteractionto be used bysessionon new connections.If
tls_interactionisNonethen client certificate validation will always fail.See
tls_interactionfor more information.- Parameters:
 tls_interaction – a
TlsInteraction
- set_user_agent(user_agent: str) None#
 Set the value to use for the “User-Agent” header on
Messages sent fromsession.If
user_agenthas trailing whitespace,sessionwill append its own product token (eg,libsoup/3.0.0) to the end of the header for you. Ifuser_agentisNonethen no “User-Agent” will be included in requests. Seeuser_agentfor more information.- Parameters:
 user_agent – the user agent string
- async websocket_connect_async(self, msg: Message, origin: str | None, protocols: list[str] | None, io_priority: int) WebsocketConnection#
 This is the awaitable version of
websocket_connect_async().- Parameters:
 msg –
Messageindicating the WebSocket server to connect toorigin – origin of the connection
protocols – a
None-terminated array of protocols supportedio_priority – the I/O priority of the request
- websocket_connect_async(msg: Message, origin: str | None, protocols: list[str] | None, io_priority: int, cancellable: Cancellable | None = None, callback: Callable[[Object | None, AsyncResult, Any], None] | None = None, user_data: Any = None) None#
 Asynchronously creates a
WebsocketConnectionto communicate with a remote server.All necessary WebSocket-related headers will be added to
msg, and it will then be sent and asynchronously processed normally (including handling of redirection and HTTP authentication).If the server returns “101 Switching Protocols”, then
msg's status code and response headers will be updated, and then the WebSocket handshake will be completed. On success,websocket_connect_finishwill return a newWebsocketConnection. On failure it will return aError.If the server returns a status other than “101 Switching Protocols”, then
msgwill contain the complete response headers and body from the server’s response, andwebsocket_connect_finishwill returnNOT_WEBSOCKET.- Parameters:
 msg –
Messageindicating the WebSocket server to connect toorigin – origin of the connection
protocols – a
None-terminated array of protocols supportedio_priority – the I/O priority of the request
cancellable – a
Cancellablecallback – the callback to invoke
user_data – data for
callback
- websocket_connect_finish(result: AsyncResult) WebsocketConnection#
 Gets the
WebsocketConnectionresponse to awebsocket_connect_asynccall.If successful, returns a
WebsocketConnectionthat can be used to communicate with the server.- Parameters:
 result – the
AsyncResultpassed to your callback
 
Properties#
- class Session
 - 
- props.local_address: InetSocketAddress#
 The type of the None singleton.
- props.proxy_resolver: ProxyResolver#
 The type of the None singleton.
- props.remote_connectable: SocketConnectable#
 The type of the None singleton.
- props.tls_database: TlsDatabase#
 The type of the None singleton.
- props.tls_interaction: TlsInteraction#
 The type of the None singleton.
 
Signals#
Virtual Methods#
Fields#
- class Session
 - parent_instance#