@@ -86,7 +86,7 @@ any that have been added to the map during asynchronous service) is closed.
8686 | ``handle_close() `` | Implied by a read event with no data |
8787 | | available |
8888 +----------------------+----------------------------------------+
89- | ``handle_accept () `` | Implied by a read event on a listening |
89+ | ``handle_accepted ()``| Implied by a read event on a listening |
9090 | | socket |
9191 +----------------------+----------------------------------------+
9292
@@ -144,8 +144,20 @@ any that have been added to the map during asynchronous service) is closed.
144144
145145 Called on listening channels (passive openers) when a connection can be
146146 established with a new remote endpoint that has issued a :meth: `connect `
147- call for the local endpoint.
147+ call for the local endpoint. Deprecated in version 3.2; use
148+ :meth: `handle_accepted ` instead.
148149
150+ .. deprecated :: 3.2
151+
152+ .. method :: handle_accepted(sock, addr)
153+
154+ Called on listening channels (passive openers) when a connection has been
155+ established with a new remote endpoint that has issued a :meth: `connect `
156+ call for the local endpoint. *conn * is a *new * socket object usable to
157+ send and receive data on the connection, and *address * is the address
158+ bound to the socket on the other end of the connection.
159+
160+ .. versionadded :: 3.2
149161
150162 .. method :: readable()
151163
@@ -210,10 +222,13 @@ any that have been added to the map during asynchronous service) is closed.
210222 .. method :: accept()
211223
212224 Accept a connection. The socket must be bound to an address and listening
213- for connections. The return value is a pair ``(conn, address) `` where
214- *conn * is a *new * socket object usable to send and receive data on the
215- connection, and *address * is the address bound to the socket on the other
216- end of the connection.
225+ for connections. The return value can be either ``None `` or a pair
226+ ``(conn, address) `` where *conn * is a *new * socket object usable to send
227+ and receive data on the connection, and *address * is the address bound to
228+ the socket on the other end of the connection.
229+ When ``None `` is returned it means the connection didn't take place, in
230+ which case the server should just ignore this event and keep listening
231+ for further incoming connections.
217232
218233
219234 .. method :: close()
@@ -223,6 +238,13 @@ any that have been added to the map during asynchronous service) is closed.
223238 flushed). Sockets are automatically closed when they are
224239 garbage-collected.
225240
241+
242+ .. class :: dispatcher_with_send()
243+
244+ A :class: `dispatcher ` subclass which adds simple buffered output capability,
245+ useful for simple clients. For more sophisticated usage use
246+ :class: `asynchat.async_chat `.
247+
226248.. class :: file_dispatcher()
227249
228250 A file_dispatcher takes a file descriptor or :term: `file object ` along
@@ -239,7 +261,7 @@ any that have been added to the map during asynchronous service) is closed.
239261 socket for use by the :class: `file_dispatcher ` class. Availability: UNIX.
240262
241263
242- .. _asyncore-example :
264+ .. _asyncore-example-1 :
243265
244266asyncore Example basic HTTP client
245267----------------------------------
@@ -249,7 +271,7 @@ implement its socket handling::
249271
250272 import asyncore, socket
251273
252- class http_client (asyncore.dispatcher):
274+ class HTTPClient (asyncore.dispatcher):
253275
254276 def __init__(self, host, path):
255277 asyncore.dispatcher.__init__(self)
@@ -273,6 +295,40 @@ implement its socket handling::
273295 sent = self.send(self.buffer)
274296 self.buffer = self.buffer[sent:]
275297
276- c = http_client('www.python.org', '/')
277298
278- asyncore.loop()
299+ client = HTTPClient('www.python.org', '/')
300+ asyncore.loop()
301+
302+ .. _asyncore-example-2 :
303+
304+ asyncore Example basic echo server
305+ ----------------------------------
306+
307+ Here is abasic echo server that uses the :class: `dispatcher ` class to accept
308+ connections and dispatches the incoming connections to a handler::
309+
310+ import asyncore
311+ import socket
312+
313+ class EchoHandler(asyncore.dispatcher_with_send):
314+
315+ def handle_read(self):
316+ data = self.recv(8192)
317+ self.send(data)
318+
319+ class EchoServer(asyncore.dispatcher):
320+
321+ def __init__(self, host, port):
322+ asyncore.dispatcher.__init__(self)
323+ self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
324+ self.set_reuse_addr()
325+ self.bind((host, port))
326+ self.listen(5)
327+
328+ def handle_accepted(self, sock, addr):
329+ print('Incoming connection from %s' % repr(addr))
330+ handler = EchoHandler(sock)
331+
332+ server = EchoServer('localhost', 8080)
333+ asyncore.loop()
334+
0 commit comments