95
95
should be added to the list of channels :cfunc:`select`\ ed or
96
96
:cfunc:`poll`\ ed for read and write events.
98
Thus, the set of channel events is larger than the basic socket events. The
99
full set of methods that can be overridden in your subclass follows:
102
.. method:: dispatcher.handle_read()
104
Called when the asynchronous loop detects that a :meth:`read` call on the
105
channel's socket will succeed.
108
.. method:: dispatcher.handle_write()
110
Called when the asynchronous loop detects that a writable socket can be
111
written. Often this method will implement the necessary buffering for
112
performance. For example::
114
def handle_write(self):
115
sent = self.send(self.buffer)
116
self.buffer = self.buffer[sent:]
119
.. method:: dispatcher.handle_expt()
121
Called when there is out of band (OOB) data for a socket connection. This
122
will almost never happen, as OOB is tenuously supported and rarely used.
125
.. method:: dispatcher.handle_connect()
127
Called when the active opener's socket actually makes a connection. Might
128
send a "welcome" banner, or initiate a protocol negotiation with the remote
129
endpoint, for example.
132
.. method:: dispatcher.handle_close()
134
Called when the socket is closed.
137
.. method:: dispatcher.handle_error()
139
Called when an exception is raised and not otherwise handled. The default
140
version prints a condensed traceback.
143
.. method:: dispatcher.handle_accept()
145
Called on listening channels (passive openers) when a connection can be
146
established with a new remote endpoint that has issued a :meth:`connect`
147
call for the local endpoint.
150
.. method:: dispatcher.readable()
152
Called each time around the asynchronous loop to determine whether a
153
channel's socket should be added to the list on which read events can
154
occur. The default method simply returns ``True``, indicating that by
155
default, all channels will be interested in read events.
158
.. method:: dispatcher.writable()
160
Called each time around the asynchronous loop to determine whether a
161
channel's socket should be added to the list on which write events can
162
occur. The default method simply returns ``True``, indicating that by
163
default, all channels will be interested in write events.
165
In addition, each channel delegates or extends many of the socket methods.
166
Most of these are nearly identical to their socket partners.
169
.. method:: dispatcher.create_socket(family, type)
171
This is identical to the creation of a normal socket, and will use the same
172
options for creation. Refer to the :mod:`socket` documentation for
173
information on creating sockets.
176
.. method:: dispatcher.connect(address)
178
As with the normal socket object, *address* is a tuple with the first
179
element the host to connect to, and the second the port number.
182
.. method:: dispatcher.send(data)
184
Send *data* to the remote end-point of the socket.
187
.. method:: dispatcher.recv(buffer_size)
189
Read at most *buffer_size* bytes from the socket's remote end-point.
190
An empty string implies that the channel has been closed from the other
194
.. method:: dispatcher.listen(backlog)
196
Listen for connections made to the socket. The *backlog* argument
197
specifies the maximum number of queued connections and should be at least
198
1; the maximum value is system-dependent (usually 5).
201
.. method:: dispatcher.bind(address)
203
Bind the socket to *address*. The socket must not already be bound. (The
204
format of *address* depends on the address family --- see above.) To mark
205
the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
206
the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
209
.. method:: dispatcher.accept()
211
Accept a connection. The socket must be bound to an address and listening
212
for connections. The return value is a pair ``(conn, address)`` where
213
*conn* is a *new* socket object usable to send and receive data on the
214
connection, and *address* is the address bound to the socket on the other
215
end of the connection.
218
.. method:: dispatcher.close()
220
Close the socket. All future operations on the socket object will fail.
221
The remote end-point will receive no more data (after queued data is
222
flushed). Sockets are automatically closed when they are
98
Thus, the set of channel events is larger than the basic socket events. The
99
full set of methods that can be overridden in your subclass follows:
102
.. method:: handle_read()
104
Called when the asynchronous loop detects that a :meth:`read` call on the
105
channel's socket will succeed.
108
.. method:: handle_write()
110
Called when the asynchronous loop detects that a writable socket can be
111
written. Often this method will implement the necessary buffering for
112
performance. For example::
114
def handle_write(self):
115
sent = self.send(self.buffer)
116
self.buffer = self.buffer[sent:]
119
.. method:: handle_expt()
121
Called when there is out of band (OOB) data for a socket connection. This
122
will almost never happen, as OOB is tenuously supported and rarely used.
125
.. method:: handle_connect()
127
Called when the active opener's socket actually makes a connection. Might
128
send a "welcome" banner, or initiate a protocol negotiation with the
129
remote endpoint, for example.
132
.. method:: handle_close()
134
Called when the socket is closed.
137
.. method:: handle_error()
139
Called when an exception is raised and not otherwise handled. The default
140
version prints a condensed traceback.
143
.. method:: handle_accept()
145
Called on listening channels (passive openers) when a connection can be
146
established with a new remote endpoint that has issued a :meth:`connect`
147
call for the local endpoint.
150
.. method:: readable()
152
Called each time around the asynchronous loop to determine whether a
153
channel's socket should be added to the list on which read events can
154
occur. The default method simply returns ``True``, indicating that by
155
default, all channels will be interested in read events.
158
.. method:: writable()
160
Called each time around the asynchronous loop to determine whether a
161
channel's socket should be added to the list on which write events can
162
occur. The default method simply returns ``True``, indicating that by
163
default, all channels will be interested in write events.
166
In addition, each channel delegates or extends many of the socket methods.
167
Most of these are nearly identical to their socket partners.
170
.. method:: create_socket(family, type)
172
This is identical to the creation of a normal socket, and will use the
173
same options for creation. Refer to the :mod:`socket` documentation for
174
information on creating sockets.
177
.. method:: connect(address)
179
As with the normal socket object, *address* is a tuple with the first
180
element the host to connect to, and the second the port number.
183
.. method:: send(data)
185
Send *data* to the remote end-point of the socket.
188
.. method:: recv(buffer_size)
190
Read at most *buffer_size* bytes from the socket's remote end-point. An
191
empty string implies that the channel has been closed from the other end.
194
.. method:: listen(backlog)
196
Listen for connections made to the socket. The *backlog* argument
197
specifies the maximum number of queued connections and should be at least
198
1; the maximum value is system-dependent (usually 5).
201
.. method:: bind(address)
203
Bind the socket to *address*. The socket must not already be bound. (The
204
format of *address* depends on the address family --- see above.) To mark
205
the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
206
the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
211
Accept a connection. The socket must be bound to an address and listening
212
for connections. The return value is a pair ``(conn, address)`` where
213
*conn* is a *new* socket object usable to send and receive data on the
214
connection, and *address* is the address bound to the socket on the other
215
end of the connection.
220
Close the socket. All future operations on the socket object will fail.
221
The remote end-point will receive no more data (after queued data is
222
flushed). Sockets are automatically closed when they are
226
226
.. _asyncore-example: