Commit 06aa0c62 authored by Fred Drake's avatar Fred Drake

wrap lines to <80 characters before fixing errors

parent 074d045a
...@@ -9,72 +9,77 @@ ...@@ -9,72 +9,77 @@
This module builds on the :mod:`asyncore` infrastructure, simplifying This module builds on the :mod:`asyncore` infrastructure, simplifying
asynchronous clients and servers and making it easier to handle protocols whose asynchronous clients and servers and making it easier to handle protocols
elements are terminated by arbitrary strings, or are of variable length. whose elements are terminated by arbitrary strings, or are of variable length.
:mod:`asynchat` defines the abstract class :class:`async_chat` that you :mod:`asynchat` defines the abstract class :class:`async_chat` that you
subclass, providing implementations of the :meth:`collect_incoming_data` and subclass, providing implementations of the :meth:`collect_incoming_data` and
:meth:`found_terminator` methods. It uses the same asynchronous loop as :meth:`found_terminator` methods. It uses the same asynchronous loop as
:mod:`asyncore`, and the two types of channel, :class:`asyncore.dispatcher` and :mod:`asyncore`, and the two types of channel, :class:`asyncore.dispatcher`
:class:`asynchat.async_chat`, can freely be mixed in the channel map. Typically and :class:`asynchat.async_chat`, can freely be mixed in the channel map.
an :class:`asyncore.dispatcher` server channel generates new Typically an :class:`asyncore.dispatcher` server channel generates new
:class:`asynchat.async_chat` channel objects as it receives incoming connection :class:`asynchat.async_chat` channel objects as it receives incoming
requests. connection requests.
.. class:: async_chat() .. class:: async_chat()
This class is an abstract subclass of :class:`asyncore.dispatcher`. To make This class is an abstract subclass of :class:`asyncore.dispatcher`. To make
practical use of the code you must subclass :class:`async_chat`, providing practical use of the code you must subclass :class:`async_chat`, providing
meaningful :meth:`collect_incoming_data` and :meth:`found_terminator` methods. meaningful :meth:`collect_incoming_data` and :meth:`found_terminator`
methods.
The :class:`asyncore.dispatcher` methods can be used, although not all make The :class:`asyncore.dispatcher` methods can be used, although not all make
sense in a message/response context. sense in a message/response context.
Like :class:`asyncore.dispatcher`, :class:`async_chat` defines a set of events Like :class:`asyncore.dispatcher`, :class:`async_chat` defines a set of
that are generated by an analysis of socket conditions after a :cfunc:`select` events that are generated by an analysis of socket conditions after a
call. Once the polling loop has been started the :class:`async_chat` object's :cfunc:`select` call. Once the polling loop has been started the
methods are called by the event-processing framework with no action on the part :class:`async_chat` object's methods are called by the event-processing
of the programmer. framework with no action on the part of the programmer.
Unlike :class:`asyncore.dispatcher`, :class:`async_chat` allows you to define a Unlike :class:`asyncore.dispatcher`, :class:`async_chat` allows you to
first-in-first-out queue (fifo) of *producers*. A producer need have only one define a first-in-first-out queue (fifo) of *producers*. A producer need
method, :meth:`more`, which should return data to be transmitted on the channel. have only one method, :meth:`more`, which should return data to be
transmitted on the channel.
The producer indicates exhaustion (*i.e.* that it contains no more data) by The producer indicates exhaustion (*i.e.* that it contains no more data) by
having its :meth:`more` method return the empty string. At this point the having its :meth:`more` method return the empty string. At this point the
:class:`async_chat` object removes the producer from the fifo and starts using :class:`async_chat` object removes the producer from the fifo and starts
the next producer, if any. When the producer fifo is empty the using the next producer, if any. When the producer fifo is empty the
:meth:`handle_write` method does nothing. You use the channel object's :meth:`handle_write` method does nothing. You use the channel object's
:meth:`set_terminator` method to describe how to recognize the end of, or an :meth:`set_terminator` method to describe how to recognize the end of, or
important breakpoint in, an incoming transmission from the remote endpoint. an important breakpoint in, an incoming transmission from the remote
endpoint.
To build a functioning :class:`async_chat` subclass your input methods To build a functioning :class:`async_chat` subclass your input methods
:meth:`collect_incoming_data` and :meth:`found_terminator` must handle the data :meth:`collect_incoming_data` and :meth:`found_terminator` must handle the
that the channel receives asynchronously. The methods are described below. data that the channel receives asynchronously. The methods are described
below.
.. method:: async_chat.close_when_done() .. method:: async_chat.close_when_done()
Pushes a ``None`` on to the producer fifo. When this producer is popped off the Pushes a ``None`` on to the producer fifo. When this producer is popped off
fifo it causes the channel to be closed. the fifo it causes the channel to be closed.
.. method:: async_chat.collect_incoming_data(data) .. method:: async_chat.collect_incoming_data(data)
Called with *data* holding an arbitrary amount of received data. The default Called with *data* holding an arbitrary amount of received data. The
method, which must be overridden, raises a :exc:`NotImplementedError` exception. default method, which must be overridden, raises a
:exc:`NotImplementedError` exception.
.. method:: async_chat.discard_buffers() .. method:: async_chat.discard_buffers()
In emergencies this method will discard any data held in the input and/or output In emergencies this method will discard any data held in the input and/or
buffers and the producer fifo. output buffers and the producer fifo.
.. method:: async_chat.found_terminator() .. method:: async_chat.found_terminator()
Called when the incoming data stream matches the termination condition set by Called when the incoming data stream matches the termination condition set
:meth:`set_terminator`. The default method, which must be overridden, raises a by :meth:`set_terminator`. The default method, which must be overridden,
:exc:`NotImplementedError` exception. The buffered input data should be raises a :exc:`NotImplementedError` exception. The buffered input data
available via an instance attribute. should be available via an instance attribute.
.. method:: async_chat.get_terminator() .. method:: async_chat.get_terminator()
...@@ -90,59 +95,59 @@ requests. ...@@ -90,59 +95,59 @@ requests.
.. method:: async_chat.handle_read() .. method:: async_chat.handle_read()
Called when a read event fires on the channel's socket in the asynchronous loop. Called when a read event fires on the channel's socket in the asynchronous
The default method checks for the termination condition established by loop. The default method checks for the termination condition established
:meth:`set_terminator`, which can be either the appearance of a particular by :meth:`set_terminator`, which can be either the appearance of a
string in the input stream or the receipt of a particular number of characters. particular string in the input stream or the receipt of a particular number
When the terminator is found, :meth:`handle_read` calls the of characters. When the terminator is found, :meth:`handle_read` calls the
:meth:`found_terminator` method after calling :meth:`collect_incoming_data` with :meth:`found_terminator` method after calling :meth:`collect_incoming_data`
any data preceding the terminating condition. with any data preceding the terminating condition.
.. method:: async_chat.handle_write() .. method:: async_chat.handle_write()
Called when the application may write data to the channel. The default method Called when the application may write data to the channel. The default
calls the :meth:`initiate_send` method, which in turn will call method calls the :meth:`initiate_send` method, which in turn will call
:meth:`refill_buffer` to collect data from the producer fifo associated with the :meth:`refill_buffer` to collect data from the producer fifo associated
channel. with the channel.
.. method:: async_chat.push(data) .. method:: async_chat.push(data)
Creates a :class:`simple_producer` object (*see below*) containing the data and Creates a :class:`simple_producer` object (*see below*) containing the data
pushes it on to the channel's ``producer_fifo`` to ensure its transmission. This and pushes it on to the channel's ``producer_fifo`` to ensure its
is all you need to do to have the channel write the data out to the network, transmission. This is all you need to do to have the channel write the
although it is possible to use your own producers in more complex schemes to data out to the network, although it is possible to use your own producers
implement encryption and chunking, for example. in more complex schemes to implement encryption and chunking, for example.
.. method:: async_chat.push_with_producer(producer) .. method:: async_chat.push_with_producer(producer)
Takes a producer object and adds it to the producer fifo associated with the Takes a producer object and adds it to the producer fifo associated with
channel. When all currently-pushed producers have been exhausted the channel the channel. When all currently-pushed producers have been exhausted the
will consume this producer's data by calling its :meth:`more` method and send channel will consume this producer's data by calling its :meth:`more`
the data to the remote endpoint. method and send the data to the remote endpoint.
.. method:: async_chat.readable() .. method:: async_chat.readable()
Should return ``True`` for the channel to be included in the set of channels Should return ``True`` for the channel to be included in the set of
tested by the :cfunc:`select` loop for readability. channels tested by the :cfunc:`select` loop for readability.
.. method:: async_chat.refill_buffer() .. method:: async_chat.refill_buffer()
Refills the output buffer by calling the :meth:`more` method of the producer at Refills the output buffer by calling the :meth:`more` method of the
the head of the fifo. If it is exhausted then the producer is popped off the producer at the head of the fifo. If it is exhausted then the producer is
fifo and the next producer is activated. If the current producer is, or becomes, popped off the fifo and the next producer is activated. If the current
``None`` then the channel is closed. producer is, or becomes, ``None`` then the channel is closed.
.. method:: async_chat.set_terminator(term) .. method:: async_chat.set_terminator(term)
Sets the terminating condition to be recognised on the channel. ``term`` may be Sets the terminating condition to be recognized on the channel. ``term``
any of three types of value, corresponding to three different ways to handle may be any of three types of value, corresponding to three different ways
incoming protocol data. to handle incoming protocol data.
+-----------+---------------------------------------------+ +-----------+---------------------------------------------+
| term | Description | | term | Description |
...@@ -158,8 +163,8 @@ requests. ...@@ -158,8 +163,8 @@ requests.
| | forever | | | forever |
+-----------+---------------------------------------------+ +-----------+---------------------------------------------+
Note that any data following the terminator will be available for reading by the Note that any data following the terminator will be available for reading
channel after :meth:`found_terminator` is called. by the channel after :meth:`found_terminator` is called.
.. method:: async_chat.writable() .. method:: async_chat.writable()
...@@ -174,29 +179,29 @@ asynchat - Auxiliary Classes and Functions ...@@ -174,29 +179,29 @@ asynchat - Auxiliary Classes and Functions
.. class:: simple_producer(data[, buffer_size=512]) .. class:: simple_producer(data[, buffer_size=512])
A :class:`simple_producer` takes a chunk of data and an optional buffer size. A :class:`simple_producer` takes a chunk of data and an optional buffer
Repeated calls to its :meth:`more` method yield successive chunks of the data no size. Repeated calls to its :meth:`more` method yield successive chunks of
larger than *buffer_size*. the data no larger than *buffer_size*.
.. method:: simple_producer.more() .. method:: simple_producer.more()
Produces the next chunk of information from the producer, or returns the empty Produces the next chunk of information from the producer, or returns the
string. empty string.
.. class:: fifo([list=None]) .. class:: fifo([list=None])
Each channel maintains a :class:`fifo` holding data which has been pushed by the Each channel maintains a :class:`fifo` holding data which has been pushed
application but not yet popped for writing to the channel. A :class:`fifo` is a by the application but not yet popped for writing to the channel. A
list used to hold data and/or producers until they are required. If the *list* :class:`fifo` is a list used to hold data and/or producers until they are
argument is provided then it should contain producers or data items to be required. If the *list* argument is provided then it should contain
written to the channel. producers or data items to be written to the channel.
.. method:: fifo.is_empty() .. method:: fifo.is_empty()
Returns ``True`` iff the fifo is empty. Returns ``True`` if and only if the fifo is empty.
.. method:: fifo.first() .. method:: fifo.first()
...@@ -206,14 +211,14 @@ asynchat - Auxiliary Classes and Functions ...@@ -206,14 +211,14 @@ asynchat - Auxiliary Classes and Functions
.. method:: fifo.push(data) .. method:: fifo.push(data)
Adds the given data (which may be a string or a producer object) to the producer Adds the given data (which may be a string or a producer object) to the
fifo. producer fifo.
.. method:: fifo.pop() .. method:: fifo.pop()
If the fifo is not empty, returns ``True, first()``, deleting the popped item. If the fifo is not empty, returns ``True, first()``, deleting the popped
Returns ``False, None`` for an empty fifo. item. Returns ``False, None`` for an empty fifo.
The :mod:`asynchat` module also defines one utility function, which may be of The :mod:`asynchat` module also defines one utility function, which may be of
use in network and textual analysis operations. use in network and textual analysis operations.
...@@ -221,8 +226,8 @@ use in network and textual analysis operations. ...@@ -221,8 +226,8 @@ use in network and textual analysis operations.
.. function:: find_prefix_at_end(haystack, needle) .. function:: find_prefix_at_end(haystack, needle)
Returns ``True`` if string *haystack* ends with any non-empty prefix of string Returns ``True`` if string *haystack* ends with any non-empty prefix of
*needle*. string *needle*.
.. _asynchat-example: .. _asynchat-example:
...@@ -231,19 +236,20 @@ asynchat Example ...@@ -231,19 +236,20 @@ asynchat Example
---------------- ----------------
The following partial example shows how HTTP requests can be read with The following partial example shows how HTTP requests can be read with
:class:`async_chat`. A web server might create an :class:`http_request_handler` :class:`async_chat`. A web server might create an
object for each incoming client connection. Notice that initially the channel :class:`http_request_handler` object for each incoming client connection.
terminator is set to match the blank line at the end of the HTTP headers, and a Notice that initially the channel terminator is set to match the blank line at
flag indicates that the headers are being read. the end of the HTTP headers, and a flag indicates that the headers are being
read.
Once the headers have been read, if the request is of type POST (indicating that Once the headers have been read, if the request is of type POST (indicating
further data are present in the input stream) then the ``Content-Length:`` that further data are present in the input stream) then the
header is used to set a numeric terminator to read the right amount of data from ``Content-Length:`` header is used to set a numeric terminator to read the
the channel. right amount of data from the channel.
The :meth:`handle_request` method is called once all relevant input has been The :meth:`handle_request` method is called once all relevant input has been
marshalled, after setting the channel terminator to ``None`` to ensure that any marshalled, after setting the channel terminator to ``None`` to ensure that
extraneous data sent by the web client are ignored. :: any extraneous data sent by the web client are ignored. ::
class http_request_handler(asynchat.async_chat): class http_request_handler(asynchat.async_chat):
...@@ -281,4 +287,3 @@ extraneous data sent by the web client are ignored. :: ...@@ -281,4 +287,3 @@ extraneous data sent by the web client are ignored. ::
self.handling = True self.handling = True
self.ibuffer = [] self.ibuffer = []
self.handle_request() self.handle_request()
...@@ -3,7 +3,8 @@ ...@@ -3,7 +3,8 @@
=============================================== ===============================================
.. module:: asyncore .. module:: asyncore
:synopsis: A base class for developing asynchronous socket handling services. :synopsis: A base class for developing asynchronous socket handling
services.
.. moduleauthor:: Sam Rushing <rushing@nightmare.com> .. moduleauthor:: Sam Rushing <rushing@nightmare.com>
.. sectionauthor:: Christopher Petrilli <petrilli@amber.org> .. sectionauthor:: Christopher Petrilli <petrilli@amber.org>
.. sectionauthor:: Steve Holden <sholden@holdenweb.com> .. sectionauthor:: Steve Holden <sholden@holdenweb.com>
...@@ -20,55 +21,58 @@ popular way to do it, but there is another very different technique, that lets ...@@ -20,55 +21,58 @@ popular way to do it, but there is another very different technique, that lets
you have nearly all the advantages of multi-threading, without actually using you have nearly all the advantages of multi-threading, without actually using
multiple threads. It's really only practical if your program is largely I/O multiple threads. It's really only practical if your program is largely I/O
bound. If your program is processor bound, then pre-emptive scheduled threads bound. If your program is processor bound, then pre-emptive scheduled threads
are probably what you really need. Network servers are rarely processor bound, are probably what you really need. Network servers are rarely processor
however. bound, however.
If your operating system supports the :cfunc:`select` system call in its I/O If your operating system supports the :cfunc:`select` system call in its I/O
library (and nearly all do), then you can use it to juggle multiple library (and nearly all do), then you can use it to juggle multiple
communication channels at once; doing other work while your I/O is taking place communication channels at once; doing other work while your I/O is taking
in the "background." Although this strategy can seem strange and complex, place in the "background." Although this strategy can seem strange and
especially at first, it is in many ways easier to understand and control than complex, especially at first, it is in many ways easier to understand and
multi-threaded programming. The :mod:`asyncore` module solves many of the control than multi-threaded programming. The :mod:`asyncore` module solves
difficult problems for you, making the task of building sophisticated many of the difficult problems for you, making the task of building
high-performance network servers and clients a snap. For "conversational" sophisticated high-performance network servers and clients a snap. For
applications and protocols the companion :mod:`asynchat` module is invaluable. "conversational" applications and protocols the companion :mod:`asynchat`
module is invaluable.
The basic idea behind both modules is to create one or more network *channels*,
instances of class :class:`asyncore.dispatcher` and The basic idea behind both modules is to create one or more network
:class:`asynchat.async_chat`. Creating the channels adds them to a global map, *channels*, instances of class :class:`asyncore.dispatcher` and
used by the :func:`loop` function if you do not provide it with your own *map*. :class:`asynchat.async_chat`. Creating the channels adds them to a global
map, used by the :func:`loop` function if you do not provide it with your own
*map*.
Once the initial channel(s) is(are) created, calling the :func:`loop` function Once the initial channel(s) is(are) created, calling the :func:`loop` function
activates channel service, which continues until the last channel (including any activates channel service, which continues until the last channel (including
that have been added to the map during asynchronous service) is closed. any that have been added to the map during asynchronous service) is closed.
.. function:: loop([timeout[, use_poll[, map[,count]]]]) .. function:: loop([timeout[, use_poll[, map[,count]]]])
Enter a polling loop that terminates after count passes or all open channels Enter a polling loop that terminates after count passes or all open
have been closed. All arguments are optional. The *count* parameter defaults channels have been closed. All arguments are optional. The *count*
to None, resulting in the loop terminating only when all channels have been parameter defaults to None, resulting in the loop terminating only when all
closed. The *timeout* argument sets the timeout parameter for the appropriate channels have been closed. The *timeout* argument sets the timeout
:func:`select` or :func:`poll` call, measured in seconds; the default is 30 parameter for the appropriate :func:`select` or :func:`poll` call, measured
seconds. The *use_poll* parameter, if true, indicates that :func:`poll` should in seconds; the default is 30 seconds. The *use_poll* parameter, if true,
be used in preference to :func:`select` (the default is ``False``). indicates that :func:`poll` should be used in preference to :func:`select`
(the default is ``False``).
The *map* parameter is a dictionary whose items are the channels to watch. As The *map* parameter is a dictionary whose items are the channels to watch.
channels are closed they are deleted from their map. If *map* is omitted, a As channels are closed they are deleted from their map. If *map* is
global map is used. Channels (instances of :class:`asyncore.dispatcher`, omitted, a global map is used. Channels (instances of
:class:`asynchat.async_chat` and subclasses thereof) can freely be mixed in the :class:`asyncore.dispatcher`, :class:`asynchat.async_chat` and subclasses
map. thereof) can freely be mixed in the map.
.. class:: dispatcher() .. class:: dispatcher()
The :class:`dispatcher` class is a thin wrapper around a low-level socket The :class:`dispatcher` class is a thin wrapper around a low-level socket
object. To make it more useful, it has a few methods for event-handling which object. To make it more useful, it has a few methods for event-handling
are called from the asynchronous loop. Otherwise, it can be treated as a which are called from the asynchronous loop. Otherwise, it can be treated
normal non-blocking socket object. as a normal non-blocking socket object.
Two class attributes can be modified, to improve performance, or possibly even Two class attributes can be modified, to improve performance, or possibly
to conserve memory. even to conserve memory.
.. data:: ac_in_buffer_size .. data:: ac_in_buffer_size
...@@ -80,12 +84,13 @@ that have been added to the map during asynchronous service) is closed. ...@@ -80,12 +84,13 @@ that have been added to the map during asynchronous service) is closed.
The asynchronous output buffer size (default ``4096``). The asynchronous output buffer size (default ``4096``).
The firing of low-level events at certain times or in certain connection states The firing of low-level events at certain times or in certain connection
tells the asynchronous loop that certain higher-level events have taken place. states tells the asynchronous loop that certain higher-level events have
For example, if we have asked for a socket to connect to another host, we know taken place. For example, if we have asked for a socket to connect to
that the connection has been made when the socket becomes writable for the first another host, we know that the connection has been made when the socket
time (at this point you know that you may write to it with the expectation of becomes writable for the first time (at this point you know that you may
success). The implied higher-level events are: write to it with the expectation of success). The implied higher-level
events are:
+----------------------+----------------------------------------+ +----------------------+----------------------------------------+
| Event | Description | | Event | Description |
...@@ -101,11 +106,11 @@ that have been added to the map during asynchronous service) is closed. ...@@ -101,11 +106,11 @@ that have been added to the map during asynchronous service) is closed.
During asynchronous processing, each mapped channel's :meth:`readable` and During asynchronous processing, each mapped channel's :meth:`readable` and
:meth:`writable` methods are used to determine whether the channel's socket :meth:`writable` methods are used to determine whether the channel's socket
should be added to the list of channels :cfunc:`select`\ ed or :cfunc:`poll`\ ed should be added to the list of channels :cfunc:`select`\ ed or
for read and write events. :cfunc:`poll`\ ed for read and write events.
Thus, the set of channel events is larger than the basic socket events. The full Thus, the set of channel events is larger than the basic socket events. The
set of methods that can be overridden in your subclass follows: full set of methods that can be overridden in your subclass follows:
.. method:: dispatcher.handle_read() .. method:: dispatcher.handle_read()
...@@ -116,9 +121,9 @@ set of methods that can be overridden in your subclass follows: ...@@ -116,9 +121,9 @@ set of methods that can be overridden in your subclass follows:
.. method:: dispatcher.handle_write() .. method:: dispatcher.handle_write()
Called when the asynchronous loop detects that a writable socket can be written. Called when the asynchronous loop detects that a writable socket can be
Often this method will implement the necessary buffering for performance. For written. Often this method will implement the necessary buffering for
example:: performance. For example::
def handle_write(self): def handle_write(self):
sent = self.send(self.buffer) sent = self.send(self.buffer)
...@@ -127,15 +132,15 @@ set of methods that can be overridden in your subclass follows: ...@@ -127,15 +132,15 @@ set of methods that can be overridden in your subclass follows:
.. method:: dispatcher.handle_expt() .. method:: dispatcher.handle_expt()
Called when there is out of band (OOB) data for a socket connection. This will Called when there is out of band (OOB) data for a socket connection. This
almost never happen, as OOB is tenuously supported and rarely used. will almost never happen, as OOB is tenuously supported and rarely used.
.. method:: dispatcher.handle_connect() .. method:: dispatcher.handle_connect()
Called when the active opener's socket actually makes a connection. Might send a Called when the active opener's socket actually makes a connection. Might
"welcome" banner, or initiate a protocol negotiation with the remote endpoint, send a "welcome" banner, or initiate a protocol negotiation with the remote
for example. endpoint, for example.
.. method:: dispatcher.handle_close() .. method:: dispatcher.handle_close()
...@@ -152,40 +157,40 @@ set of methods that can be overridden in your subclass follows: ...@@ -152,40 +157,40 @@ set of methods that can be overridden in your subclass follows:
.. method:: dispatcher.handle_accept() .. method:: dispatcher.handle_accept()
Called on listening channels (passive openers) when a connection can be Called on listening channels (passive openers) when a connection can be
established with a new remote endpoint that has issued a :meth:`connect` call established with a new remote endpoint that has issued a :meth:`connect`
for the local endpoint. call for the local endpoint.
.. method:: dispatcher.readable() .. method:: dispatcher.readable()
Called each time around the asynchronous loop to determine whether a channel's Called each time around the asynchronous loop to determine whether a
socket should be added to the list on which read events can occur. The default channel's socket should be added to the list on which read events can
method simply returns ``True``, indicating that by default, all channels will occur. The default method simply returns ``True``, indicating that by
be interested in read events. default, all channels will be interested in read events.
.. method:: dispatcher.writable() .. method:: dispatcher.writable()
Called each time around the asynchronous loop to determine whether a channel's Called each time around the asynchronous loop to determine whether a
socket should be added to the list on which write events can occur. The default channel's socket should be added to the list on which write events can
method simply returns ``True``, indicating that by default, all channels will occur. The default method simply returns ``True``, indicating that by
be interested in write events. default, all channels will be interested in write events.
In addition, each channel delegates or extends many of the socket methods. Most In addition, each channel delegates or extends many of the socket methods.
of these are nearly identical to their socket partners. Most of these are nearly identical to their socket partners.
.. method:: dispatcher.create_socket(family, type) .. method:: dispatcher.create_socket(family, type)
This is identical to the creation of a normal socket, and will use the same This is identical to the creation of a normal socket, and will use the same
options for creation. Refer to the :mod:`socket` documentation for information options for creation. Refer to the :mod:`socket` documentation for
on creating sockets. information on creating sockets.
.. method:: dispatcher.connect(address) .. method:: dispatcher.connect(address)
As with the normal socket object, *address* is a tuple with the first element As with the normal socket object, *address* is a tuple with the first
the host to connect to, and the second the port number. element the host to connect to, and the second the port number.
.. method:: dispatcher.send(data) .. method:: dispatcher.send(data)
...@@ -195,38 +200,41 @@ of these are nearly identical to their socket partners. ...@@ -195,38 +200,41 @@ of these are nearly identical to their socket partners.
.. method:: dispatcher.recv(buffer_size) .. method:: dispatcher.recv(buffer_size)
Read at most *buffer_size* bytes from the socket's remote end-point. An empty Read at most *buffer_size* bytes from the socket's remote end-point.
string implies that the channel has been closed from the other end. An empty string implies that the channel has been closed from the other
end.
.. method:: dispatcher.listen(backlog) .. method:: dispatcher.listen(backlog)
Listen for connections made to the socket. The *backlog* argument specifies the Listen for connections made to the socket. The *backlog* argument
maximum number of queued connections and should be at least 1; the maximum value specifies the maximum number of queued connections and should be at least
is system-dependent (usually 5). 1; the maximum value is system-dependent (usually 5).
.. method:: dispatcher.bind(address) .. method:: dispatcher.bind(address)
Bind the socket to *address*. The socket must not already be bound. (The Bind the socket to *address*. The socket must not already be bound. (The
format of *address* depends on the address family --- see above.) To mark the format of *address* depends on the address family --- see above.) To mark
socket as re-usable (setting the :const:`SO_REUSEADDR` option), call the the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
:class:`dispatcher` object's :meth:`set_reuse_addr` method. the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
.. method:: dispatcher.accept() .. method:: dispatcher.accept()
Accept a connection. The socket must be bound to an address and listening for Accept a connection. The socket must be bound to an address and listening
connections. The return value is a pair ``(conn, address)`` where *conn* is a for connections. The return value is a pair ``(conn, address)`` where
*new* socket object usable to send and receive data on the connection, and *conn* is a *new* socket object usable to send and receive data on the
*address* is the address bound to the socket on the other end of the connection. connection, and *address* is the address bound to the socket on the other
end of the connection.
.. method:: dispatcher.close() .. method:: dispatcher.close()
Close the socket. All future operations on the socket object will fail. The Close the socket. All future operations on the socket object will fail.
remote end-point will receive no more data (after queued data is flushed). The remote end-point will receive no more data (after queued data is
Sockets are automatically closed when they are garbage-collected. flushed). Sockets are automatically closed when they are
garbage-collected.
.. _asyncore-example: .. _asyncore-example:
...@@ -266,4 +274,3 @@ implement its socket handling:: ...@@ -266,4 +274,3 @@ implement its socket handling::
c = http_client('www.python.org', '/') c = http_client('www.python.org', '/')
asyncore.loop() asyncore.loop()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment