My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from cogen.core.sockets import Send, Recv, SendAll, Accept, Connect, \
getdefaulttimeout, setdefaulttimeout, Socket as CogenSocket

from cogen.magic.corolets import yield_


from socket import _fileobject

class Socket(CogenSocket):
"""
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 corolet like:

.. sourcecode:: python

sock = Socket(family, type, proto) # just like the builtin socket module
sock.read(1024)


Constructor details:

.. sourcecode:: python

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.
"""
__slots__ = ()

def recv(self, 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_. """
return yield_(Recv(self, bufsize, timeout=self._timeout, **kws))


def makefile(self, 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.
"""
return _fileobject(Socket(
_sock=self._fd._sock,
_timeout=self._timeout,
_proactor_added=self._proactor_added
), mode, bufsize)

def send(self, 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."""
return yield_(Send(self, data, timeout=self._timeout, **kws))

def sendall(self, data, **kws):
"""Send data to the socket. The socket must be connected to a remote
socket. All the data is guaranteed to be sent."""
return yield_(SendAll(self, data, timeout=self._timeout, **kws))

def accept(self, **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()
}}}
"""
return yield_(Accept(self, timeout=self._timeout, **kws))

def connect(self, address, **kws):
"""Connect to a remote socket at _address_. """
return yield_(Connect(self, address, timeout=self._timeout, **kws))

def sendfile(self, file_handle, offset=None, length=None, blocksize=4096, **kws):
return yield_(SendFile(file_handle, self, offset, length, blocksize, **kws))

Show details Hide details

Change log

r585 by ionel.mc on Apr 26, 2009   Diff
changed socketlets.Socket to have makefile
working in the same way as the one in
stdlib (that is dupe the socket on
makefile)
Go to: 
Project members, sign in to write a code review

Older revisions

r569 by ionel.mc on Jan 18, 2009   Diff
added a Coroutine spin-off that uses
greenlets behind the scenes instead of
generator schematics
All revisions of this file

File info

Size: 3482 bytes, 87 lines
Hosted by Google Code