Communicate over TCP or Unix sockets, implementing fully asynchronous reads and writes.
kaa.Socket requires an IPv6-capable stack, and favors IPv6 connectivity when available. This should generally be completely transparent on IPv4-only networks. See connect() for more information.
kaa.Object
└─ kaa.IOChannel
└─ kaa.Socket
| connect() | Connects to the host specified in address. |
|---|---|
| listen() | Set the socket to accept incoming connections. |
| steal() | |
| wrap() | Wraps an existing low-level socket object. |
| address | read-only | This property is deprecated; use peer instead. |
|---|---|---|
| alive | read-only | True if the socket is alive, and False otherwise. |
| buffer_size | read/write | Size of the send and receive socket buffers (SO_SNDBUF and SO_RCVBUF) in bytes. |
| connected | read-only | Boolean representing the connected state of the socket. |
| connecting | read-only | True if the socket is in the process of establishing a connection but is not yet connected. |
| fileno | read-only | |
| listening | read-only | True if this is a listening socket, and False otherwise. |
| local | read-only | Returns either the tuple (host, port, flowinfo, scopeid, scope) representing the local end of a TCP socket, or the string containing the name of a Unix socket. |
| peer | read-only | Returns the tuple (host, port, flowinfo, scopeid, scope, reqhost) representing the remote end of the socket. |
| readable | read-only | True if the socket is readable, and False otherwise. |
| new-client | Emitted when a new client connects to a listening socket. |
|---|
Connects to the host specified in address.
| Parameters: |
|
|---|---|
| Returns: | An InProgress object. |
If addr is given as a 4-tuple, it is in the form (host, service, flowinfo, scope). If given as a 2-tuple, it is in the form (host, service), and in this case the flowinfo and scope are assumed to be 0.
The flowinfo and scope fields are only relevant for IPv6 hosts, where they represent the sin6_flowinfo and sin6_scope_id members in struct sockaddr_in6 in C. scope may be the name of an interface (e.g. eth0) or an interface id, and is needed when connecting to link-local addresses (fe80::/16).
If addr is given as a string, it is treated as a Unix socket path if it does not contain :, otherwise it is specified as host:service[%scope], where [x] indicates that x is optional, and where:
- host is a hostname, an IPv4 dotted quad, or an IPv6 address wrapped in square brackets. e.g. freevo.org, 192.168.0.1, [3000::1]
- service is a service name or port number. e.g. http, 80
- scope is an interface name or number. e.g. eth0, 2
When connecting to a link-local address (fe80::/16), scope must be specified. Relative Unix socket names (those not prefixed with /) are created via kaa.tempfile.
This function is executed in a thread to avoid blocking. It therefore returns an InProgress object. If the socket is connected, the InProgress is finished with no arguments. If the connection cannot be established, an exception is thrown to the InProgress.
Set the socket to accept incoming connections.
| Parameters: |
|
|---|---|
| Raises: | ValueError if addr is invalid, or socket.error if the bind fails. |
If addr is given as a 4-tuple, it is in the form (host, service, flowinfo, scope). If passed as a 2-tuple, it is in the form (host, service), and in this case, it is assumed that flowinfo and scope are both 0. See connect() for more information.
If host is given as a string, it is treated as a Unix socket path if it does not contain :, otherwise it is specified as [host]:[service][%scope], where [x] indicates that x is optional, and where:
- host is a hostname, an IPv4 dotted quad, or an IPv6 address wrapped in square brackets. e.g. localhost, 192.168.0.1, [3000::1]. If host is not specified, the socket will listen on all interfaces.
- service is a service name or port number. e.g. http, 80
- scope is an interface name or number. e.g. eth0, 2
When binding to a link-local address (fe80::/16), scope must be specified. Relative Unix socket names (those not prefixed with /) are created via kaa.tempfile.
Warning
If the bind address supplied is a hostname rather than an IPv4 or IPv6 address, this function will block in order to resolve the hostname if the name is not specified in /etc/hosts. (In other words, localhost is probably safe.)
Once listening, new connections are automatically accepted, and the new-client signal is emitted for each new connection. Callbacks connecting to the signal will receive a new Socket object representing the client connection.
Wraps an existing low-level socket object.
addr specifies the 4-tuple address corresponding to the socket.
True if the socket is alive, and False otherwise.
A socket is considered alive when it is connected or in the process of connecting.
Size of the send and receive socket buffers (SO_SNDBUF and SO_RCVBUF) in bytes.
Setting this to higher values (say 1M) improves performance when sending large amounts of data across the socket. Note that the upper bound may be restricted by the kernel. (Under Linux, this can be tuned by adjusting /proc/sys/net/core/[rw]mem_max)
True if the socket is in the process of establishing a connection but is not yet connected.
Once the socket is connected, the connecting property will be False, but the connected property will be True.
Returns either the tuple (host, port, flowinfo, scopeid, scope) representing the local end of a TCP socket, or the string containing the name of a Unix socket.
scope is the interface name represented by scopeid, and is None if scopeid is 0.
On Python 2.6 and later, the returned value is a namedtuple.
Returns the tuple (host, port, flowinfo, scopeid, scope, reqhost) representing the remote end of the socket.
scope is the interface name represented by scopeid, and is None if scopeid is 0. reqhost is the requested hostname if connect() was called, or None if this is a listening socket.
On Python 2.6 and later, the returned value is a namedtuple.
True if the socket is readable, and False otherwise.
A socket is considered readable when it is listening or alive.