Most Shrapnel programs make heavy use of sockets. The coro package implements its own socket class, which is nearly identical to the socket class in Python. Indeed, if you use coro.install_thread_emulation() then the socket class will be monkey-patched into Python’s socket module.
Though you are free to directly instantiate the coro.sock object, there are a variety of functions to assist in creating socket objects with a little more clarity.
Create a streaming IPv6 socket.
Returns: | A socket object. |
---|---|
Raises OSError: | OS-level error. |
Create a streaming IPv4 socket.
Returns: | A socket object. |
---|---|
Raises OSError: | OS-level error. |
Create a datagram IPv6 socket.
Returns: | A socket object. |
---|---|
Raises OSError: | OS-level error. |
Create a datagram IPv4 socket.
Returns: | A socket object. |
---|---|
Raises OSError: | OS-level error. |
Create a streaming unix-domain socket.
Returns: | A socket object. |
---|---|
Raises OSError: | OS-level error. |
Create an unnamed pair of connected sockets.
Parameters: |
|
---|---|
Returns: | A tuple of 2 connected sockets. |
Raises OSError: | OS-level error. |
Whether or not this system can create an IPv6 socket.
Returns: | True if this system can create an IPv6 socket, False otherwise |
---|
Bases: object
sock(int domain=AF_INET, int stype=SOCK_STREAM, int protocol=0, int fd=-1)
Coro socket object.
This is typically used for network sockets, but can also be used for coro-safe IO on any file descriptor that supports kqueue non-blocking operations.
The constructor takes the following parameters:
Parameters: |
|
---|---|
Variables: |
|
Accept a connection.
Returns: | A tuple (socket, address) where socket is a socket object and address is an (IP, port) tuple for IP addresses or a string for UNIX-domain sockets. IP addresses are returned as strings. |
---|---|
Raises OSError: | OS-level error. |
Accept multiple connections.
This will accept up to max connections for any connections available on the listen queue. This will block if there are no connections waiting.
Parameters: | max – The maximum number of connections to accept. If not specified, defaults to infinity (accept all pending connections). |
---|---|
Returns: | A list of (socket, address) tuples (see accept() method for information on return format). |
Raises OSError: | OS-level error. |
Bind the socket.
Parameters: | address – The address to bind to. For IP, it should be a (IP, port) tuple where the IP is a string. Use the empty string to indicate INADDR_ANY. The port should always be a host-byte-order integer. For Unix-domain sockets, the address should be a string. |
---|---|
Raises OSError: | OS-level error. |
Close the socket.
It is safe to call this if the socket is already closed.
Raises OSError: | OS-level error. |
---|
Connect the socket.
Parameters: | address – The address to connect to. For IP, it should be a (IP, port) tuple where the IP is a string. The port should always be a host-byte-order integer. For Unix-domain sockets, the address should be a string. |
---|---|
Raises OSError: | OS-level error. |
domain: ‘int’
Duplicate the socket object using the OS dup() call.
Returns: | A new sock instance that holds the new file descriptor. |
---|
fd: ‘int’
Get the current file descriptor.
Returns: | The current file descriptor number. Returns -1 if the socket is closed. |
---|
Get the current file descriptor.
Returns: | The current file descriptor number. Returns -1 if the socket is closed. |
---|
Get the remote-side address.
Returns: | A (IP, port) tuple for IP addresses where IP is a string. Returns a string for UNIX-domain sockets. |
---|---|
Raises OSError: | OS-level error. |
Get the local address of the socket.
Returns: | A (IP, port) tuple for IP addresses where IP is a string or an empty string for INADDR_ANY. Returns a string for UNIX-domain sockets (empty string if not bound). |
---|
Get a socket option.
Parameters: |
|
---|---|
Returns: | An integer if buflen is zero, otherwise returns a string. |
Raises OSError: | OS-level error. |
Set the socket to listen for connections.
Parameters: | backlog – The maximum size of the queue for pending connections. |
---|---|
Raises OSError: | OS-level error. |
Return a regular file object corresponding to the socket.
The mode and bufsize arguments are as for the built-in open() function.
The underlying socket is duplicated via sock.dup to emulate Python’s reference counting behavior.
Parameters: |
|
---|---|
Returns: | A file-like object that wraps the socket. |
orig_fd: ‘int’
Read data.
This is an alias for the recv() method.
Read a vector array of data.
This will repeatedly call readv until all data is received. If the end of the stream is reached before all data is received, then the result tuple will only contain the elements competely or partially received.
Parameters: | size_list – A sequence of integers that indicates the buffer sizes to read. |
---|---|
Returns: | A tuple of strings corresponding to the sizes requested in size_list. |
Raises OSError: | OS-level error. |
Receive data.
This may return less data than you request if the socket buffer is not large enough. Use recv_exact() to ensure you receive exactly the amount requested.
Parameters: | buffer_size – The number of bytes to receive. |
---|---|
Returns: | A string of data. Returns the empty string when the end of the stream is reached. |
Raises OSError: | OS-level error. |
Receive exactly the number of bytes requested.
This will repeatedly call read until all data is received.
Parameters: | bytes – The number of bytes to receive. |
---|---|
Returns: | The data as a string. |
Raises: |
|
Receive data into a Python buffer.
This is for the Python buffer interface. If you don’t know what that is, move along. This method is for Python socket compatibility.
Parameters: |
|
---|---|
Returns: | The number of bytes read. |
Raises OSError: | OS-level error. |
Receive data.
This may return less data than you request if the socket buffer is not large enough.
Parameters: |
|
---|---|
Returns: | A tuple (data, address) where data is a string and address is the address of the remote side (string for unix-domain, tuple of (IP, port) for IP where IP is a string and port is an integer). Data is the empty string when the end of the stream is reached. |
Raises OSError: | OS-level error. |
Receive data into a Python buffer.
This is for the Python buffer interface. If you don’t know what that is, move along. This method is for Python socket compatibility.
Parameters: |
|
---|---|
Returns: | A tuple (nbytes, address) where bytes is the number of bytes read and address then it is the address of the remote side. |
Raises OSError: | OS-level error. |
Send data on the socket.
This will repeatedly call write to ensure all data has been sent. This will raise OSError if it is unable to send all data.
Parameters: | data – The data to send. |
---|---|
Returns: | The number of bytes sent, which should always be the length of data. |
Raises OSError: | OS-level error. |
Send all data.
This is an alias for the send() method.
Send data to a specific address.
Parameters: |
|
---|---|
Returns: | The number of bytes sent which may be less than the send requested. |
Raises OSError: | OS-level error. |
Set the SO_REUSEADDR socket option.
Set a socket option.
Parameters: |
|
---|---|
Raises OSError: | OS-level error. |
Shutdown the socket.
Parameters: | how – How to shut down the socket (see the shutdown(2) manpage). |
---|---|
Raises OSError: | OS-level error. |
stype: ‘int’
Block until the socket is readable.
This will block until there is data available to be read.
Returns: | The amount “readable”. For different sockets, this may be different values, see the EVFILT_READ section of the kevent manpage for details. |
---|---|
Raises OSError: | OS-level error. |
Block until the socket is writeable.
This will block until it is possible to write to the socket.
Returns: | The number of bytes writeable on the socket. |
---|---|
Raises OSError: | OS-level error. |
Write data.
This is an alias for the send() method.
Write a vector array of data.
This will repeatedly call writev until all data is sent. If it is unable to send all data, it will raise an OSError exception.
Parameters: | data – A sequence of strings to write. |
---|---|
Returns: | The number of bytes sent which should always be the sum of the lengths of all the strings in the data sequence. |
Raises OSError: | OS-level error. |
Bases: coro._coro.sock
file_sock(fileobj) A file-object wrapper using the socket object.
The constructor takes one argument:
param fileobj: A Python-like file object. Currently only needs to implement the fileno method. When the object is deallocated, the file descriptor is closed.
Bases: coro._coro.sock
fd_sock(fd) A file-descriptor wrapper using the socket object.
The constructor takes one argument:
param fd: A file descriptor. When the object is deallocated, the file descriptor is closed.