@@ -86,7 +86,7 @@ any that have been added to the map during asynchronous service) is closed.
86
86
| ``handle_close()`` | Implied by a read event with no data |
87
87
| | available |
88
88
+----------------------+----------------------------------------+
89
-
| ``handle_accept()`` | Implied by a read event on a listening |
89
+
| ``handle_accepted()``| Implied by a read event on a listening |
90
90
| | socket |
91
91
+----------------------+----------------------------------------+
92
92
@@ -144,8 +144,20 @@ any that have been added to the map during asynchronous service) is closed.
144
144
145
145
Called on listening channels (passive openers) when a connection can be
146
146
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.
148
149
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
149
161
150
162
.. method:: readable()
151
163
@@ -210,10 +222,13 @@ any that have been added to the map during asynchronous service) is closed.
210
222
.. method:: accept()
211
223
212
224
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.
217
232
218
233
219
234
.. method:: close()
@@ -223,6 +238,13 @@ any that have been added to the map during asynchronous service) is closed.
223
238
flushed). Sockets are automatically closed when they are
224
239
garbage-collected.
225
240
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
+
226
248
.. class:: file_dispatcher()
227
249
228
250
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.
239
261
socket for use by the :class:`file_dispatcher` class. Availability: UNIX.
240
262
241
263
242
-
.. _asyncore-example:
264
+
.. _asyncore-example-1:
243
265
244
266
asyncore Example basic HTTP client
245
267
----------------------------------
@@ -249,7 +271,7 @@ implement its socket handling::
249
271
250
272
import asyncore, socket
251
273
252
-
class http_client(asyncore.dispatcher):
274
+
class HTTPClient(asyncore.dispatcher):
253
275
254
276
def __init__(self, host, path):
255
277
asyncore.dispatcher.__init__(self)
@@ -273,6 +295,40 @@ implement its socket handling::
273
295
sent = self.send(self.buffer)
274
296
self.buffer = self.buffer[sent:]
275
297
276
-
c = http_client('www.python.org', '/')
277
298
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
+
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4