cogen.core.sockets
Socket-only coroutine operations and Socket wrapper.
Really - the only thing you need to know for most stuff is
the Socket class.
-
cogen.core.sockets.getdefaulttimeout()
-
cogen.core.sockets.setdefaulttimeout(timeout)
- Set the default timeout used by the socket wrapper
(Socket class)
-
class cogen.core.sockets.Socket(*a, **k)
Bases: object
A wrapper for socket objects, sets nonblocking mode and
adds some internal bufers and wrappers. Regular calls to the usual
socket methods return operations for use in a coroutine.
So you use this in a coroutine like:
sock = Socket(family, type, proto) # just like the builtin socket module
yield sock.read(1024)
Constructor details:
Socket([family[, type[, proto]]]) -> socket object
Open a socket of the given type. The family argument specifies the
address family; it defaults to AF_INET. The type argument specifies
whether this is a stream (SOCK_STREAM, this is the default)
or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
specifying the default protocol. Keyword arguments are accepted.
A socket object represents one endpoint of a network connection.
-
accept(**kws)
Accept a connection. The socket must be bound to an address and
listening for connections. The return value is a pair (conn, address)
where conn is a new socket object usable to send and receive data on the
connection, and address is the address bound to the socket on the other
end of the connection.
Example:
{{{
conn, address = yield mysock.accept()
}}}
-
bind(*args)
- Bind the socket to _address_. The socket must not already be bound.
(The format of _address_ depends on the address family)
-
close(*args)
- Close the socket. All future operations on the socket object will
fail. The remote end will receive no more data (after queued data is
flushed). Sockets are automatically closed when they are garbage-collected.
-
connect(address, **kws)
- Connect to a remote socket at _address_.
-
fileno()
- Return the socket’s file descriptor
-
getpeername()
- Return the remote address to which the socket is connected.
-
getsockname(*args)
- Return the socket’s own address.
-
gettimeout(*args)
- Return the associated timeout value.
-
listen(backlog)
- Listen for connections made to the socket. The _backlog_ argument
specifies the maximum number of queued connections and should be at
least 1; the maximum value is system-dependent (usually 5).
-
makefile(mode='r', bufsize=-1)
- Returns a special fileobject that has corutines instead of the usual
read/readline/write methods. Will work in the same manner though.
-
recv(bufsize, **kws)
- Receive data from the socket. The return value is a string
representing the data received. The amount of data may be less than the
ammount specified by _bufsize_.
-
send(data, **kws)
- Send data to the socket. The socket must be connected to a remote
socket. Ammount sent may be less than the data provided.
-
sendall(data, **kws)
- Send data to the socket. The socket must be connected to a remote
socket. All the data is guaranteed to be sent.
-
sendfile(file_handle, offset=None, length=None, blocksize=4096, **kws)
-
setblocking(val)
-
setsockopt(*args)
- Set the value of the given socket option. Same as the usual socket
method.
-
settimeout(to)
- Set a timeout on blocking socket operations. The value argument can
be a nonnegative float expressing seconds, timedelta or None.
-
shutdown(*args)
- Shut down one or both halves of the connection. Same as the usual
socket method.
-
class cogen.core.sockets.SendFile(file_handle, sock, offset=None, length=None, blocksize=4096, **kws)
Bases: cogen.core.sockets.SocketOperation
Uses underling OS sendfile (or equivalent) call or a regular memory copy
operation if there is no sendfile.
You can use this as a WriteAll if you specify the length.
Usage:
yield sockets.SendFile(file_object, socket_object, 0)
# will send till send operations return 0
yield sockets.SendFile(file_object, socket_object, 0, blocksize=0)
# there will be only one send operation (if successfull)
# that meas the whole file will be read in memory if there is
#no sendfile
yield sockets.SendFile(file_object, socket_object, 0, file_size)
# this will hang if we can't read file_size bytes
#from the file
-
blocksize
-
file_handle
-
finalize()
-
length
-
offset
-
position
-
process(sched, coro)
-
sent
-
class cogen.core.sockets.Recv(sock, len=4096, **kws)
Bases: cogen.core.sockets.SocketOperation
Example usage:
yield sockets.Read(socket_object, buffer_length)
buffer_length is max read size, BUT, if if there are buffers from ReadLine
return them first.
-
buff
-
finalize()
-
len
-
process(sched, coro)
-
class cogen.core.sockets.Send(sock, buff, **kws)
Bases: cogen.core.sockets.SocketOperation
Write the buffer to the socket and return the number of bytes written.
-
buff
-
finalize()
-
process(sched, coro)
-
sent
-
class cogen.core.sockets.SendAll(sock, buff, **kws)
Bases: cogen.core.sockets.SocketOperation
Run this operation till all the bytes have been written.
-
buff
-
finalize()
-
process(sched, coro)
-
sent
-
class cogen.core.sockets.Accept(sock, **kws)
Bases: cogen.core.sockets.SocketOperation
Returns a (conn, addr) tuple when the operation completes.
-
addr
-
cbuff
-
conn
-
finalize()
-
process(sched, coro)
-
class cogen.core.sockets.Connect(sock, addr, **kws)
Bases: cogen.core.sockets.SocketOperation
-
addr
-
conn
-
connect_attempted
-
finalize()
-
process(sched, coro)
-
class cogen.core.sockets.SocketOperation(sock, **kws)
Bases: cogen.core.events.TimedOperation
This is a generic class for a operation that involves some socket call.
A socket operation should subclass WriteOperation or ReadOperation, define a
run method and call the __init__ method of the superclass.
-
cleanup(sched, coro)
-
coro
-
fileno()
-
flags
-
last_update
-
sock