blob: 7cacca1941efe5d4d6b14575c86ba40fe3d450ce [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`asyncore` --- Asynchronous socket handler
2===============================================
3
4.. module:: asyncore
Georg Brandl9afde1c2007-11-01 20:32:30 +00005 :synopsis: A base class for developing asynchronous socket handling
6 services.
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Sam Rushing <rushing@nightmare.com>
8.. sectionauthor:: Christopher Petrilli <petrilli@amber.org>
9.. sectionauthor:: Steve Holden <sholden@holdenweb.com>
Christian Heimes5b5e81c2007-12-31 16:14:33 +000010.. heavily adapted from original documentation by Sam Rushing
Georg Brandl116aa622007-08-15 14:28:22 +000011
Raymond Hettingeread49752011-01-24 16:28:06 +000012**Source code:** :source:`Lib/asyncore.py`
13
14--------------
Georg Brandl116aa622007-08-15 14:28:22 +000015
16This module provides the basic infrastructure for writing asynchronous socket
17service clients and servers.
18
Georg Brandl116aa622007-08-15 14:28:22 +000019There are only two ways to have a program on a single processor do "more than
20one thing at a time." Multi-threaded programming is the simplest and most
Georg Brandl9afde1c2007-11-01 20:32:30 +000021popular way to do it, but there is another very different technique, that lets
Georg Brandl116aa622007-08-15 14:28:22 +000022you have nearly all the advantages of multi-threading, without actually using
23multiple threads. It's really only practical if your program is largely I/O
Georg Brandl9afde1c2007-11-01 20:32:30 +000024bound. If your program is processor bound, then pre-emptive scheduled threads
25are probably what you really need. Network servers are rarely processor
26bound, however.
Georg Brandl116aa622007-08-15 14:28:22 +000027
Georg Brandl60203b42010-10-06 10:11:56 +000028If your operating system supports the :c:func:`select` system call in its I/O
Georg Brandl116aa622007-08-15 14:28:22 +000029library (and nearly all do), then you can use it to juggle multiple
Georg Brandl9afde1c2007-11-01 20:32:30 +000030communication channels at once; doing other work while your I/O is taking
31place in the "background." Although this strategy can seem strange and
32complex, especially at first, it is in many ways easier to understand and
33control than multi-threaded programming. The :mod:`asyncore` module solves
34many of the difficult problems for you, making the task of building
35sophisticated high-performance network servers and clients a snap. For
36"conversational" applications and protocols the companion :mod:`asynchat`
37module is invaluable.
Georg Brandl116aa622007-08-15 14:28:22 +000038
Georg Brandl9afde1c2007-11-01 20:32:30 +000039The basic idea behind both modules is to create one or more network
40*channels*, instances of class :class:`asyncore.dispatcher` and
41:class:`asynchat.async_chat`. Creating the channels adds them to a global
42map, used by the :func:`loop` function if you do not provide it with your own
43*map*.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45Once the initial channel(s) is(are) created, calling the :func:`loop` function
Georg Brandl9afde1c2007-11-01 20:32:30 +000046activates channel service, which continues until the last channel (including
47any that have been added to the map during asynchronous service) is closed.
Georg Brandl116aa622007-08-15 14:28:22 +000048
49
50.. function:: loop([timeout[, use_poll[, map[,count]]]])
51
Georg Brandl9afde1c2007-11-01 20:32:30 +000052 Enter a polling loop that terminates after count passes or all open
53 channels have been closed. All arguments are optional. The *count*
54 parameter defaults to None, resulting in the loop terminating only when all
55 channels have been closed. The *timeout* argument sets the timeout
56 parameter for the appropriate :func:`select` or :func:`poll` call, measured
57 in seconds; the default is 30 seconds. The *use_poll* parameter, if true,
58 indicates that :func:`poll` should be used in preference to :func:`select`
59 (the default is ``False``).
Georg Brandl116aa622007-08-15 14:28:22 +000060
Georg Brandl9afde1c2007-11-01 20:32:30 +000061 The *map* parameter is a dictionary whose items are the channels to watch.
62 As channels are closed they are deleted from their map. If *map* is
63 omitted, a global map is used. Channels (instances of
64 :class:`asyncore.dispatcher`, :class:`asynchat.async_chat` and subclasses
65 thereof) can freely be mixed in the map.
Georg Brandl116aa622007-08-15 14:28:22 +000066
67
68.. class:: dispatcher()
69
70 The :class:`dispatcher` class is a thin wrapper around a low-level socket
Georg Brandl9afde1c2007-11-01 20:32:30 +000071 object. To make it more useful, it has a few methods for event-handling
72 which are called from the asynchronous loop. Otherwise, it can be treated
73 as a normal non-blocking socket object.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Georg Brandl9afde1c2007-11-01 20:32:30 +000075 The firing of low-level events at certain times or in certain connection
76 states tells the asynchronous loop that certain higher-level events have
77 taken place. For example, if we have asked for a socket to connect to
78 another host, we know that the connection has been made when the socket
79 becomes writable for the first time (at this point you know that you may
80 write to it with the expectation of success). The implied higher-level
81 events are:
Georg Brandl116aa622007-08-15 14:28:22 +000082
83 +----------------------+----------------------------------------+
84 | Event | Description |
85 +======================+========================================+
R. David Murray78532ba2009-04-12 15:35:44 +000086 | ``handle_connect()`` | Implied by the first read or write |
87 | | event |
Georg Brandl116aa622007-08-15 14:28:22 +000088 +----------------------+----------------------------------------+
89 | ``handle_close()`` | Implied by a read event with no data |
90 | | available |
91 +----------------------+----------------------------------------+
Giampaolo Rodolà977c7072010-10-04 21:08:36 +000092 | ``handle_accepted()``| Implied by a read event on a listening |
Georg Brandl116aa622007-08-15 14:28:22 +000093 | | socket |
94 +----------------------+----------------------------------------+
95
96 During asynchronous processing, each mapped channel's :meth:`readable` and
97 :meth:`writable` methods are used to determine whether the channel's socket
Georg Brandl60203b42010-10-06 10:11:56 +000098 should be added to the list of channels :c:func:`select`\ ed or
99 :c:func:`poll`\ ed for read and write events.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 Thus, the set of channel events is larger than the basic socket events. The
102 full set of methods that can be overridden in your subclass follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 .. method:: handle_read()
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 Called when the asynchronous loop detects that a :meth:`read` call on the
108 channel's socket will succeed.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 .. method:: handle_write()
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 Called when the asynchronous loop detects that a writable socket can be
114 written. Often this method will implement the necessary buffering for
115 performance. For example::
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Benjamin Petersone41251e2008-04-25 01:59:09 +0000117 def handle_write(self):
118 sent = self.send(self.buffer)
119 self.buffer = self.buffer[sent:]
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 .. method:: handle_expt()
Georg Brandl116aa622007-08-15 14:28:22 +0000123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 Called when there is out of band (OOB) data for a socket connection. This
125 will almost never happen, as OOB is tenuously supported and rarely used.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 .. method:: handle_connect()
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 Called when the active opener's socket actually makes a connection. Might
131 send a "welcome" banner, or initiate a protocol negotiation with the
132 remote endpoint, for example.
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 .. method:: handle_close()
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Benjamin Petersone41251e2008-04-25 01:59:09 +0000137 Called when the socket is closed.
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 .. method:: handle_error()
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 Called when an exception is raised and not otherwise handled. The default
143 version prints a condensed traceback.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 .. method:: handle_accept()
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 Called on listening channels (passive openers) when a connection can be
149 established with a new remote endpoint that has issued a :meth:`connect`
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000150 call for the local endpoint. Deprecated in version 3.2; use
151 :meth:`handle_accepted` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Georg Brandleeed9552010-10-05 07:16:01 +0000153 .. deprecated:: 3.2
154
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000155
156 .. method:: handle_accepted(sock, addr)
157
158 Called on listening channels (passive openers) when a connection has been
159 established with a new remote endpoint that has issued a :meth:`connect`
Senthil Kumaran656df5e2011-06-19 18:22:33 -0700160 call for the local endpoint. *sock* is a *new* socket object usable to
161 send and receive data on the connection, and *addr* is the address
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000162 bound to the socket on the other end of the connection.
163
Georg Brandleeed9552010-10-05 07:16:01 +0000164 .. versionadded:: 3.2
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 .. method:: readable()
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Benjamin Petersone41251e2008-04-25 01:59:09 +0000169 Called each time around the asynchronous loop to determine whether a
170 channel's socket should be added to the list on which read events can
171 occur. The default method simply returns ``True``, indicating that by
172 default, all channels will be interested in read events.
Georg Brandl116aa622007-08-15 14:28:22 +0000173
174
Benjamin Petersone41251e2008-04-25 01:59:09 +0000175 .. method:: writable()
Georg Brandl116aa622007-08-15 14:28:22 +0000176
Benjamin Petersone41251e2008-04-25 01:59:09 +0000177 Called each time around the asynchronous loop to determine whether a
178 channel's socket should be added to the list on which write events can
179 occur. The default method simply returns ``True``, indicating that by
180 default, all channels will be interested in write events.
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182
Benjamin Petersone41251e2008-04-25 01:59:09 +0000183 In addition, each channel delegates or extends many of the socket methods.
184 Most of these are nearly identical to their socket partners.
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186
Giampaolo Rodolà103a6d62011-02-25 22:21:22 +0000187 .. method:: create_socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 This is identical to the creation of a normal socket, and will use the
190 same options for creation. Refer to the :mod:`socket` documentation for
191 information on creating sockets.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Georg Brandlf5a1d762012-03-09 12:22:12 +0100193 .. versionchanged:: 3.3
194 *family* and *type* arguments can be omitted.
Giampaolo Rodolà103a6d62011-02-25 22:21:22 +0000195
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 .. method:: connect(address)
Georg Brandl116aa622007-08-15 14:28:22 +0000198
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 As with the normal socket object, *address* is a tuple with the first
200 element the host to connect to, and the second the port number.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
202
Benjamin Petersone41251e2008-04-25 01:59:09 +0000203 .. method:: send(data)
Georg Brandl116aa622007-08-15 14:28:22 +0000204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 Send *data* to the remote end-point of the socket.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 .. method:: recv(buffer_size)
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Petersone41251e2008-04-25 01:59:09 +0000210 Read at most *buffer_size* bytes from the socket's remote end-point. An
211 empty string implies that the channel has been closed from the other end.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 .. method:: listen(backlog)
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Benjamin Petersone41251e2008-04-25 01:59:09 +0000216 Listen for connections made to the socket. The *backlog* argument
217 specifies the maximum number of queued connections and should be at least
218 1; the maximum value is system-dependent (usually 5).
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220
Benjamin Petersone41251e2008-04-25 01:59:09 +0000221 .. method:: bind(address)
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Benjamin Petersone41251e2008-04-25 01:59:09 +0000223 Bind the socket to *address*. The socket must not already be bound. (The
Benjamin Peterson21896a32010-03-21 22:03:03 +0000224 format of *address* depends on the address family --- refer to the
225 :mod:`socket` documentation for more information.) To mark
Benjamin Petersone41251e2008-04-25 01:59:09 +0000226 the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
227 the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
Benjamin Petersone41251e2008-04-25 01:59:09 +0000230 .. method:: accept()
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Benjamin Petersone41251e2008-04-25 01:59:09 +0000232 Accept a connection. The socket must be bound to an address and listening
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000233 for connections. The return value can be either ``None`` or a pair
234 ``(conn, address)`` where *conn* is a *new* socket object usable to send
235 and receive data on the connection, and *address* is the address bound to
236 the socket on the other end of the connection.
237 When ``None`` is returned it means the connection didn't take place, in
238 which case the server should just ignore this event and keep listening
239 for further incoming connections.
Benjamin Petersone41251e2008-04-25 01:59:09 +0000240
241
242 .. method:: close()
243
244 Close the socket. All future operations on the socket object will fail.
245 The remote end-point will receive no more data (after queued data is
246 flushed). Sockets are automatically closed when they are
247 garbage-collected.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000249
250.. class:: dispatcher_with_send()
251
252 A :class:`dispatcher` subclass which adds simple buffered output capability,
253 useful for simple clients. For more sophisticated usage use
254 :class:`asynchat.async_chat`.
255
Josiah Carlsond74900e2008-07-07 04:15:08 +0000256.. class:: file_dispatcher()
257
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000258 A file_dispatcher takes a file descriptor or :term:`file object` along
Georg Brandl60203b42010-10-06 10:11:56 +0000259 with an optional map argument and wraps it for use with the :c:func:`poll`
260 or :c:func:`loop` functions. If provided a file object or anything with a
261 :c:func:`fileno` method, that method will be called and passed to the
Josiah Carlsond74900e2008-07-07 04:15:08 +0000262 :class:`file_wrapper` constructor. Availability: UNIX.
263
264.. class:: file_wrapper()
265
266 A file_wrapper takes an integer file descriptor and calls :func:`os.dup` to
267 duplicate the handle so that the original handle may be closed independently
268 of the file_wrapper. This class implements sufficient methods to emulate a
269 socket for use by the :class:`file_dispatcher` class. Availability: UNIX.
270
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000272.. _asyncore-example-1:
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274asyncore Example basic HTTP client
275----------------------------------
276
277Here is a very basic HTTP client that uses the :class:`dispatcher` class to
278implement its socket handling::
279
Giampaolo Rodola'0fb41b52012-05-15 15:46:00 +0200280 import asyncore
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000282 class HTTPClient(asyncore.dispatcher):
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284 def __init__(self, host, path):
285 asyncore.dispatcher.__init__(self)
Giampaolo Rodolà103a6d62011-02-25 22:21:22 +0000286 self.create_socket()
Georg Brandl116aa622007-08-15 14:28:22 +0000287 self.connect( (host, 80) )
Georg Brandl45ec3332011-03-06 11:05:03 +0100288 self.buffer = bytes('GET %s HTTP/1.0\r\nHost: %s\r\n\r\n' %
289 (path, host), 'ascii')
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291 def handle_connect(self):
292 pass
293
294 def handle_close(self):
295 self.close()
296
297 def handle_read(self):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000298 print(self.recv(8192))
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300 def writable(self):
301 return (len(self.buffer) > 0)
302
303 def handle_write(self):
304 sent = self.send(self.buffer)
305 self.buffer = self.buffer[sent:]
306
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000308 client = HTTPClient('www.python.org', '/')
309 asyncore.loop()
310
311.. _asyncore-example-2:
312
313asyncore Example basic echo server
314----------------------------------
315
Giampaolo Rodolà61a0bf52011-02-25 14:50:57 +0000316Here is a basic echo server that uses the :class:`dispatcher` class to accept
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000317connections and dispatches the incoming connections to a handler::
318
319 import asyncore
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000320
321 class EchoHandler(asyncore.dispatcher_with_send):
322
323 def handle_read(self):
324 data = self.recv(8192)
Giampaolo Rodolà61a0bf52011-02-25 14:50:57 +0000325 if data:
326 self.send(data)
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000327
328 class EchoServer(asyncore.dispatcher):
329
330 def __init__(self, host, port):
331 asyncore.dispatcher.__init__(self)
Giampaolo Rodolà103a6d62011-02-25 22:21:22 +0000332 self.create_socket()
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000333 self.set_reuse_addr()
334 self.bind((host, port))
335 self.listen(5)
336
337 def handle_accepted(self, sock, addr):
338 print('Incoming connection from %s' % repr(addr))
339 handler = EchoHandler(sock)
340
341 server = EchoServer('localhost', 8080)
342 asyncore.loop()