Soup.Socket¶
| Subclasses: | None |
|---|
Methods¶
| Inherited: | GObject.Object (37), Gio.Initable (2) |
|---|---|
| Structs: | GObject.ObjectClass (5) |
connect_async (cancellable, callback, *user_data) |
|
connect_sync (cancellable) |
|
disconnect () |
|
get_fd () |
|
get_local_address () |
|
get_remote_address () |
|
is_connected () |
|
is_ssl () |
|
listen () |
|
read (buffer, cancellable) |
|
read_until (buffer, boundary, boundary_len, got_boundary, cancellable) |
|
start_proxy_ssl (ssl_host, cancellable) |
|
start_ssl (cancellable) |
|
write (buffer, cancellable) |
Virtual Methods¶
| Inherited: | GObject.Object (7), Gio.Initable (1) |
|---|
do_disconnected () |
|
do_new_connection (new_sock) |
|
do_readable () |
|
do_writable () |
Properties¶
| Name | Type | Flags | Short Description |
|---|---|---|---|
async-context |
int |
r/w/co | The GLib.MainContext to dispatch this socket’s async I/O in |
fd |
int |
r/w/co | The socket’s file descriptor |
gsocket |
Gio.Socket |
w/co | The socket’s underlying Gio.Socket |
iostream |
Gio.IOStream |
w/co | The socket’s underlying Gio.IOStream |
ipv6-only |
bool |
r/w | IPv6 only |
is-server |
bool |
r | Whether or not the socket is a server socket |
local-address |
Soup.Address |
r/w/co | Address of local end of socket |
non-blocking |
bool |
r/w | Whether or not the socket uses non-blocking I/O |
remote-address |
Soup.Address |
r/w/co | Address of remote end of socket |
ssl-creds |
int |
r/w | SSL credential information, passed from the session to the SSL implementation |
ssl-fallback |
bool |
r/w/co | Use SSLv3 instead of TLS (client-side only) |
ssl-strict |
bool |
r/w/co | Whether certificate errors should be considered a connection error |
timeout |
int |
r/w | Value in seconds to timeout a blocking I/O |
tls-certificate |
Gio.TlsCertificate |
r | The peer’s TLS certificate |
tls-errors |
Gio.TlsCertificateFlags |
r | Errors with the peer’s TLS certificate |
trusted-certificate |
bool |
r | Whether the server certificate is trusted, if this is an SSL socket |
use-thread-context |
bool |
r/w/co | Use GLib.MainContext.get_thread_default |
Signals¶
| Inherited: | GObject.Object (1) |
|---|
| Name | Short Description |
|---|---|
disconnected |
Emitted when the socket is disconnected, for whatever reason. |
event |
Emitted when a network-related event occurs. |
new-connection |
Emitted when a listening socket (set up with Soup.Socket.listen()) receives a new connection. |
readable |
Emitted when an async socket is readable. |
writable |
Emitted when an async socket is writable. |
Class Details¶
-
class
Soup.Socket(**kwargs)¶ Bases: GObject.Object,Gio.InitableAbstract: No Structure: Soup.SocketClass-
connect_async(cancellable, callback, *user_data)¶ Parameters: - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone - callback (
Soup.SocketCallback) – callback to call after connecting - user_data (
objectorNone) – data to pass to callback
Begins asynchronously connecting to self’s remote address. The socket will call callback when it succeeds or fails (but not before returning from this function).
If cancellable is non-
None, it can be used to cancel the connection. callback will still be invoked in this case, with a status ofSoup.Status.CANCELLED.- cancellable (
-
connect_sync(cancellable)¶ Parameters: cancellable ( Gio.CancellableorNone) – aGio.Cancellable, orNoneReturns: a success or failure code. Return type: intAttempt to synchronously connect self to its remote address.
If cancellable is non-
None, it can be used to cancel the connection, in which caseSoup.Socket.connect_sync() will returnSoup.Status.CANCELLED.
-
disconnect()¶ Disconnects self. Any further read or write attempts on it will fail.
-
get_fd()¶ Returns: self’s file descriptor. Return type: intGets self’s underlying file descriptor.
Note that fiddling with the file descriptor may break the
Soup.Socket.
-
get_local_address()¶ Returns: the Soup.AddressReturn type: Soup.AddressReturns the
Soup.Addresscorresponding to the local end of self.Calling this method on an unconnected socket is considered to be an error, and produces undefined results.
-
get_remote_address()¶ Returns: the Soup.AddressReturn type: Soup.AddressReturns the
Soup.Addresscorresponding to the remote end of self.Calling this method on an unconnected socket is considered to be an error, and produces undefined results.
-
is_ssl()¶ Returns: Trueif self has SSL credentials setReturn type: boolTests if self is doing (or has attempted to do) SSL.
-
listen()¶ Returns: whether or not self is now listening. Return type: boolMakes self start listening on its local address. When connections come in, self will emit #SoupSocket::new_connection.
-
read(buffer, cancellable)¶ Parameters: - buffer (
bytes) – buffer to read into - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone
Raises: Returns: a
Soup.SocketIOStatus, as described above (orSoup.SocketIOStatus.EOFif the socket is no longer connected, orSoup.SocketIOStatus.ERRORon any other error, in which case error will also be set).nread: on return, the number of bytes read into buffer Return type: (
Soup.SocketIOStatus, nread:int)Attempts to read up to len bytes from self into buffer. If some data is successfully read,
Soup.Socket.read() will returnSoup.SocketIOStatus.OK, and nread will contain the number of bytes actually read (which may be less than len).If self is non-blocking, and no data is available, the return value will be
Soup.SocketIOStatus.WOULD_BLOCK. In this case, the caller can connect to theSoup.Socket::readablesignal to know when there is more data to read. (NB: You MUST read all available data off the socket first.Soup.Socket::readableis only emitted afterSoup.Socket.read() returnsSoup.SocketIOStatus.WOULD_BLOCK, and it is only emitted once. See the documentation forSoup.Socket:non-blocking.)- buffer (
-
read_until(buffer, boundary, boundary_len, got_boundary, cancellable)¶ Parameters: - buffer (
bytes) – buffer to read into - boundary (
objectorNone) – boundary to read until - boundary_len (
int) – length of boundary in bytes - got_boundary (
bool) – on return, whether or not the data in buffer ends with the boundary string - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone
Raises: Returns: as for
Soup.Socket.read()nread: on return, the number of bytes read into buffer Return type: (
Soup.SocketIOStatus, nread:int)Like
Soup.Socket.read(), but reads no further than the first occurrence of boundary. (If the boundary is found, it will be included in the returned data, and got_boundary will be set toTrue.) Any data after the boundary will returned in future reads.Soup.Socket.read_until() will almost always return fewer than len bytes: if the boundary is found, then it will only return the bytes up until the end of the boundary, and if the boundary is not found, then it will leave the last(boundary_len - 1)bytes in its internal buffer, in case they form the start of the boundary string. Thus, len normally needs to be at least 1 byte longer than boundary_len if you want to make any progress at all.- buffer (
-
start_proxy_ssl(ssl_host, cancellable)¶ Parameters: - ssl_host (
str) – hostname of the SSL server - cancellable (
Gio.CancellableorNone) – aGio.Cancellable
Returns: success or failure
Return type: Starts using SSL on socket, expecting to find a host named ssl_host.
- ssl_host (
-
start_ssl(cancellable)¶ Parameters: cancellable ( Gio.CancellableorNone) – aGio.CancellableReturns: success or failure Return type: boolStarts using SSL on socket.
-
write(buffer, cancellable)¶ Parameters: - buffer (
bytes) – data to write - cancellable (
Gio.CancellableorNone) – aGio.Cancellable, orNone
Raises: Returns: a
Soup.SocketIOStatus, as described above (orSoup.SocketIOStatus.EOForSoup.SocketIOStatus.ERROR. error will be set if the return value isSoup.SocketIOStatus.ERROR.)nwrote: on return, number of bytes written Return type: (
Soup.SocketIOStatus, nwrote:int)Attempts to write len bytes from buffer to self. If some data is successfully written, the return status will be
Soup.SocketIOStatus.OK, and nwrote will contain the number of bytes actually written (which may be less than len).If self is non-blocking, and no data could be written right away, the return value will be
Soup.SocketIOStatus.WOULD_BLOCK. In this case, the caller can connect to theSoup.Socket::writablesignal to know when more data can be written. (NB:Soup.Socket::writableis only emitted afterSoup.Socket.write() returnsSoup.SocketIOStatus.WOULD_BLOCK, and it is only emitted once. See the documentation forSoup.Socket:non-blocking.)- buffer (
-
do_disconnected() virtual¶
-
do_new_connection(new_sock) virtual¶ Parameters: new_sock ( Soup.Socket) –
-
do_readable() virtual¶
-
do_writable() virtual¶
-
Signal Details¶
-
Soup.Socket.signals.disconnected(socket)¶ Signal Name: disconnectedFlags: RUN_LASTParameters: socket ( Soup.Socket) – The object which received the signalEmitted when the socket is disconnected, for whatever reason.
-
Soup.Socket.signals.event(socket, event, connection)¶ Signal Name: eventFlags: Parameters: - socket (
Soup.Socket) – The object which received the signal - event (
Gio.SocketClientEvent) – the event that occurred - connection (
Gio.IOStream) – the current connection state
Emitted when a network-related event occurs. See
Gio.SocketClient::eventfor more details.New in version 2.38.
- socket (
-
Soup.Socket.signals.new_connection(socket, new)¶ Signal Name: new-connectionFlags: Parameters: - socket (
Soup.Socket) – The object which received the signal - new (
Soup.Socket) – the new socket
Emitted when a listening socket (set up with
Soup.Socket.listen()) receives a new connection.You must ref the new if you want to keep it; otherwise it will be destroyed after the signal is emitted.
- socket (
-
Soup.Socket.signals.readable(socket)¶ Signal Name: readableFlags: RUN_LASTParameters: socket ( Soup.Socket) – The object which received the signalEmitted when an async socket is readable. See
Soup.Socket.read(),Soup.Socket.read_until() andSoup.Socket:non-blocking.
-
Soup.Socket.signals.writable(socket)¶ Signal Name: writableFlags: RUN_LASTParameters: socket ( Soup.Socket) – The object which received the signalEmitted when an async socket is writable. See
Soup.Socket.write() andSoup.Socket:non-blocking.
Property Details¶
-
Soup.Socket.props.async_context¶ Name: async-contextType: intDefault Value: NoneFlags: READABLE,WRITABLE,CONSTRUCT_ONLYThe
GLib.MainContextto dispatch this socket’s async I/O in
-
Soup.Socket.props.fd¶ Name: fdType: intDefault Value: -1Flags: READABLE,WRITABLE,CONSTRUCT_ONLYThe socket’s file descriptor
-
Soup.Socket.props.gsocket¶ Name: gsocketType: Gio.SocketDefault Value: NoneFlags: WRITABLE,CONSTRUCT_ONLYThe socket’s underlying
Gio.Socket
-
Soup.Socket.props.iostream¶ Name: iostreamType: Gio.IOStreamDefault Value: NoneFlags: WRITABLE,CONSTRUCT_ONLYThe socket’s underlying
Gio.IOStream
-
Soup.Socket.props.ipv6_only¶ Name: ipv6-onlyType: boolDefault Value: FalseFlags: READABLE,WRITABLEIPv6 only
-
Soup.Socket.props.is_server¶ Name: is-serverType: boolDefault Value: FalseFlags: READABLEWhether or not the socket is a server socket.
Note that for “ordinary”
Soup.Socketsthis will be set for both listening sockets and the sockets emitted bySoup.Socket::new-connection, but for sockets created by settingSoup.Socket:fd, it will only be set for listening sockets.
-
Soup.Socket.props.local_address¶ Name: local-addressType: Soup.AddressDefault Value: NoneFlags: READABLE,WRITABLE,CONSTRUCT_ONLYAddress of local end of socket
-
Soup.Socket.props.non_blocking¶ Name: non-blockingType: boolDefault Value: TrueFlags: READABLE,WRITABLEWhether or not the socket uses non-blocking I/O.
Soup.Socket’s I/O methods are designed around the idea of using a single codepath for both synchronous and asynchronous I/O. If you want to read off aSoup.Socket, the “correct” way to do it is to callSoup.Socket.read() orSoup.Socket.read_until() repeatedly until you have read everything you want. If it returnsSoup.SocketIOStatus.WOULD_BLOCKat any point, stop reading and wait for it to emit theSoup.Socket::readablesignal. Then go back to the reading-as-much-as-you-can loop. Likewise, for writing to aSoup.Socket, you should callSoup.Socket.write() either until you have written everything, or it returnsSoup.SocketIOStatus.WOULD_BLOCK(in which case you wait forSoup.Socket::writableand then go back into the loop).Code written this way will work correctly with both blocking and non-blocking sockets; blocking sockets will simply never return
Soup.SocketIOStatus.WOULD_BLOCK, and so the code that handles that case just won’t get used for them.
-
Soup.Socket.props.remote_address¶ Name: remote-addressType: Soup.AddressDefault Value: NoneFlags: READABLE,WRITABLE,CONSTRUCT_ONLYAddress of remote end of socket
-
Soup.Socket.props.ssl_creds¶ Name: ssl-credsType: intDefault Value: NoneFlags: READABLE,WRITABLESSL credential information, passed from the session to the SSL implementation
-
Soup.Socket.props.ssl_fallback¶ Name: ssl-fallbackType: boolDefault Value: FalseFlags: READABLE,WRITABLE,CONSTRUCT_ONLYUse SSLv3 instead of TLS (client-side only)
-
Soup.Socket.props.ssl_strict¶ Name: ssl-strictType: boolDefault Value: TrueFlags: READABLE,WRITABLE,CONSTRUCT_ONLYWhether certificate errors should be considered a connection error
-
Soup.Socket.props.timeout¶ Name: timeoutType: intDefault Value: 0Flags: READABLE,WRITABLEValue in seconds to timeout a blocking I/O
-
Soup.Socket.props.tls_certificate¶ Name: tls-certificateType: Gio.TlsCertificateDefault Value: NoneFlags: READABLEThe peer’s TLS certificate
-
Soup.Socket.props.tls_errors¶ Name: tls-errorsType: Gio.TlsCertificateFlagsDefault Value: 0Flags: READABLEErrors with the peer’s TLS certificate
-
Soup.Socket.props.trusted_certificate¶ Name: trusted-certificateType: boolDefault Value: FalseFlags: READABLEWhether the server certificate is trusted, if this is an SSL socket
-
Soup.Socket.props.use_thread_context¶ Name: use-thread-contextType: boolDefault Value: FalseFlags: READABLE,WRITABLE,CONSTRUCT_ONLYUse
GLib.MainContext.get_thread_default().New in version 2.38.