Package coro :: Module _coro :: Class sock
[hide private]
[frames] | no frames]

Class sock

object --+
         |
        sock
Known Subclasses:

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:

Instance Methods [hide private]
 
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
a new object with type S, a subtype of T
__new__(T, S, ...)
 
__repr__(x)
repr(x)
 
accept(...)
Accept a connection.
 
accept_many(...)
Accept multiple connections.
 
bind(...)
Bind the socket.
 
close(...)
Close the socket.
 
connect(...)
Connect the socket.
 
dup(...)
Duplicate the socket object using the OS dup() call.
 
fileno(...)
Get the current file descriptor.
 
get_fileno(...)
Get the current file descriptor.
 
getpeername(...)
Get the remote-side address.
 
getsockname(...)
Get the local address of the socket.
 
getsockopt(...)
Get a socket option.
 
listen(...)
Set the socket to listen for connections.
 
makefile(...)
Return a regular file object corresponding to the socket.
 
read(...)
Read data.
 
readv(...)
Read a vector array of data.
 
recv(...)
Receive data.
 
recv_exact(...)
Receive exactly the number of bytes requested.
 
recv_into(...)
Receive data into a Python buffer.
 
recvfrom(...)
Receive data.
 
recvfrom_into(...)
Receive data into a Python buffer.
 
send(...)
Send data on the socket.
 
sendall(...)
Send all data.
 
sendto(...)
Send data to a specific address.
 
set_reuse_addr(...)
Set the SO_REUSEADDR socket option.
 
setsockopt(...)
Set a socket option.
 
shutdown(...)
Shutdown the socket.
 
wait_for_read(...)
Block until the socket is readable.
 
wait_for_write(...)
Block until the socket is writeable.
 
write(...)
Write data.
 
writev(...)
Write a vector array of data.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Instance Variables [hide private]
  domain
: The socket domain (AF_INET, AF_UNIX, AF_UNSPEC).
  fd
: The file descriptor number.
  orig_fd
: The original file descriptor number.
  stype
: The socket type (SOCK_STREAM, SOCK_DGRAM)
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(...)
(Constructor)

 
x.__init__(...) initializes x; see help(type(x)) for signature
Overrides: object.__init__

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

__repr__(x)
(Representation operator)

 
repr(x)
Overrides: object.__repr__

accept(...)

 
Accept a connection.
Returns:
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_many(...)

 

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:
Returns a list of (socket, address) tuples (see accept method for information on return format).
Raises:
  • OSError - : OS-level error.

bind(...)

 
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(...)

 

Close the socket.

It is safe to call this if the socket is already closed.

Raises:
  • OSError - : OS-level error.

connect(...)

 
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.

dup(...)

 
Duplicate the socket object using the OS dup() call.
Returns:
Returns a new sock instance that holds the new file descriptor.

fileno(...)

 
Get the current file descriptor.
Returns:
Returns the current file descriptor number. Returns -1 if the socket is closed.

get_fileno(...)

 
Get the current file descriptor.
Returns:
Returns the current file descriptor number. Returns -1 if the socket is closed.

getpeername(...)

 
Get the remote-side address.
Returns:
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.

getsockname(...)

 
Get the local address of the socket.
Returns:
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).

getsockopt(...)

 
Get a socket option.
Parameters:
  • level - : The socket level to get (see SOL).
  • optname - : The socket option to get (see SO).
  • buflen - : The size of the buffer needed to retrieve the value. If not specified, it assumes the result is an integer and will return an integer. Otherwise, it will create a new string with the result, and you may use the struct module to decode it.
Returns:
Returns an integer if buflen is zero, otherwise returns a string.
Raises:
  • OSError - : OS-level error.

listen(...)

 
Set the socket to listen for connections.
Parameters:
  • backlog - : The maximum size of the queue for pending connections.
Raises:
  • OSError - : OS-level error.

makefile(...)

 

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:
  • mode - : The mode of the file, defaults to 'r'.
  • bufsize - : The buffer size (0 is no buffering, 1 is line buffering, greater than 1 is the explicit buffer size). Defaults to -1 (does not change the default buffering).
Returns:
Returns a file-like object that wraps the socket.

read(...)

 

Read data.

This is an alias for the recv method.

readv(...)

 

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:
Returns a tuple of strings corresponding to the sizes requested in size_list.
Raises:
  • OSError - : OS-level error.

recv(...)

 

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:
Returns a string of data. Returns the empty string when the end of the stream is reached.
Raises:
  • OSError - : OS-level error.

recv_exact(...)

 

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:
Returns the data as a string.
Raises:
  • OSError - : OS-level error.
  • EOFError - : Not all data could be read. The first argument includes any partial data read as a string.

recv_into(...)

 

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:
  • buffer - : A writeable Python buffer object. Must be a contiguous segment.
  • nbytes - : Number of bytes to read. Must be less than or equal to the size of the buffer. Defaults to 0 which means the size of buffer.
  • flags - : Flags for the recv system call (see recv(2) manpage). Defaults to 0.
Returns:
Returns the number of bytes read.
Raises:
  • OSError - : OS-level error.

recvfrom(...)

 

Receive data.

This may return less data than you request if the socket buffer is not large enough.

Parameters:
  • buffer_size - : The number of bytes to receive.
  • flags - : Socket flags to set (defaults to 0) (see recvfrom(2) manpage).
Returns:
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.

recvfrom_into(...)

 

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:
  • buffer - : A writeable Python buffer object. Must be a contiguous segment.
  • nbytes - : Number of bytes to read. Must be less than or equal to the size of the buffer. Defaults to 0 which means the size of buffer.
  • flags - : Flags for the recv system call (see recvfrom(2) manpage). Defaults to 0.
Returns:
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(...)

 

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:
Returns the number of bytes sent, which should always be the length of data.
Raises:
  • OSError - : OS-level error.

sendall(...)

 

Send all data.

This is an alias for the send method.

sendto(...)

 
Send data to a specific address.
Parameters:
  • data - : The data to send.
  • address - : The address to send to. For unix-domain sockets, this is a string. For IP sockets, this is a tuple (IP, port) where IP is a string. Port is always an integer.
  • flags - : sendto flags to use (defaults to 0) (see sendto(2) manpage).
Returns:
Returns the number of bytes sent which may be less than the send requested.
Raises:
  • OSError - : OS-level error.

setsockopt(...)

 
Set a socket option.
Parameters:
  • level - : The socket level to set (see SOL).
  • optname - : The socket option to set (see SO).
  • value - : The value to set. May be an integer, or a struct-packed string.
Raises:
  • OSError - : OS-level error.

shutdown(...)

 
Shutdown the socket.
Parameters:
  • how - : How to shut down the socket (see the shutdown(2) manpage).
Raises:
  • OSError - : OS-level error.

wait_for_read(...)

 

Block until the socket is readable.

This will block until there is data available to be read.

Returns:
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.

wait_for_write(...)

 

Block until the socket is writeable.

This will block until it is possible to write to the socket.

Returns:
Returns the number of bytes writeable on the socket.
Raises:
  • OSError - : OS-level error.

write(...)

 

Write data.

This is an alias for the send method.

writev(...)

 

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:
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.

Instance Variable Details [hide private]

fd

: The file descriptor number. Set to -1 when the socket is closed.

orig_fd

: The original file descriptor number. This is left for debugging purposes to determine which file descriptor was in use before the socket was closed.