@@ -209,10 +209,13 @@ any that have been added to the map during asynchronous service) is closed.
209
209
.. method:: accept()
210
210
211
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.
212
+
for connections. The return value can be either ``None`` or a pair
213
+
``(conn, address)`` where *conn* is a *new* socket object usable to send
214
+
and receive data on the connection, and *address* is the address bound to
215
+
the socket on the other end of the connection.
216
+
When ``None`` is returned it means the connection didn't take place, in
217
+
which case the server should just ignore this event and keep listening
218
+
for further incoming connections.
216
219
217
220
218
221
.. method:: close()
@@ -222,6 +225,12 @@ any that have been added to the map during asynchronous service) is closed.
222
225
flushed). Sockets are automatically closed when they are
223
226
garbage-collected.
224
227
228
+
.. class:: dispatcher_with_send()
229
+
230
+
A :class:`dispatcher` subclass which adds simple buffered output capability,
231
+
useful for simple clients. For more sophisticated usage use
232
+
:class:`asynchat.async_chat`.
233
+
225
234
.. class:: file_dispatcher()
226
235
227
236
A file_dispatcher takes a file descriptor or :term:`file object` along
@@ -238,7 +247,7 @@ any that have been added to the map during asynchronous service) is closed.
238
247
socket for use by the :class:`file_dispatcher` class. Availability: UNIX.
239
248
240
249
241
-
.. _asyncore-example:
250
+
.. _asyncore-example-1:
242
251
243
252
asyncore Example basic HTTP client
244
253
----------------------------------
@@ -248,7 +257,7 @@ implement its socket handling::
248
257
249
258
import asyncore, socket
250
259
251
-
class http_client(asyncore.dispatcher):
260
+
class HTTPClient(asyncore.dispatcher):
252
261
253
262
def __init__(self, host, path):
254
263
asyncore.dispatcher.__init__(self)
@@ -272,6 +281,45 @@ implement its socket handling::
272
281
sent = self.send(self.buffer)
273
282
self.buffer = self.buffer[sent:]
274
283
275
-
c = http_client('www.python.org', '/')
276
284
277
-
asyncore.loop()
285
+
client = HTTPClient('www.python.org', '/')
286
+
asyncore.loop()
287
+
288
+
.. _asyncore-example-2:
289
+
290
+
asyncore Example basic echo server
291
+
----------------------------------
292
+
293
+
Here is abasic echo server that uses the :class:`dispatcher` class to accept
294
+
connections and dispatches the incoming connections to a handler::
295
+
296
+
import asyncore
297
+
import socket
298
+
299
+
class EchoHandler(asyncore.dispatcher_with_send):
300
+
301
+
def handle_read(self):
302
+
data = self.recv(8192)
303
+
self.send(data)
304
+
305
+
class EchoServer(asyncore.dispatcher):
306
+
307
+
def __init__(self, host, port):
308
+
asyncore.dispatcher.__init__(self)
309
+
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
310
+
self.set_reuse_addr()
311
+
self.bind((host, port))
312
+
self.listen(5)
313
+
314
+
def handle_accept(self):
315
+
pair = self.accept()
316
+
if pair is None:
317
+
return
318
+
else:
319
+
sock, addr = pair
320
+
print('Incoming connection from %s' % repr(addr))
321
+
handler = EchoHandler(sock)
322
+
323
+
server = EchoServer('localhost', 8080)
324
+
asyncore.loop()
325
+
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