1 diff -ruN Python-2.6.7-orig/Doc/library/asyncore.rst Python-2.6.7/Doc/library/asyncore.rst
2 --- Python-2.6.7-orig/Doc/library/asyncore.rst 2010-05-19 14:14:45.000000000 +0000
3 +++ Python-2.6.7/Doc/library/asyncore.rst 2012-02-15 20:47:05.176388857 +0000
4 @@ -211,10 +211,13 @@
5 .. method:: accept()
6
7 Accept a connection. The socket must be bound to an address and listening
8 - for connections. The return value is a pair ``(conn, address)`` where
9 - *conn* is a *new* socket object usable to send and receive data on the
10 - connection, and *address* is the address bound to the socket on the other
11 - end of the connection.
12 + for connections. The return value can be either ``None`` or a pair
13 + ``(conn, address)`` where *conn* is a *new* socket object usable to send
14 + and receive data on the connection, and *address* is the address bound to
15 + the socket on the other end of the connection.
16 + When ``None`` is returned it means the connection didn't take place, in
17 + which case the server should just ignore this event and keep listening
18 + for further incoming connections.
19
20
21 .. method:: close()
22 @@ -224,6 +227,12 @@
23 flushed). Sockets are automatically closed when they are
24 garbage-collected.
25
26 +.. class:: dispatcher_with_send()
27 +
28 + A :class:`dispatcher` subclass which adds simple buffered output capability,
29 + useful for simple clients. For more sophisticated usage use
30 + :class:`asynchat.async_chat`.
31 +
32 .. class:: file_dispatcher()
33
34 A file_dispatcher takes a file descriptor or file object along with an
35 @@ -240,7 +249,7 @@
36 socket for use by the :class:`file_dispatcher` class. Availability: UNIX.
37
38
39 -.. _asyncore-example:
40 +.. _asyncore-example-1:
41
42 asyncore Example basic HTTP client
43 ----------------------------------
44 @@ -250,7 +259,7 @@
45
46 import asyncore, socket
47
48 - class http_client(asyncore.dispatcher):
49 + class HTTPClient(asyncore.dispatcher):
50
51 def __init__(self, host, path):
52 asyncore.dispatcher.__init__(self)
53 @@ -274,6 +283,45 @@
54 sent = self.send(self.buffer)
55 self.buffer = self.buffer[sent:]
56
57 - c = http_client('www.python.org', '/')
58
59 + client = HTTPClient('www.python.org', '/')
60 asyncore.loop()
61 +
62 +.. _asyncore-example-2:
63 +
64 +asyncore Example basic echo server
65 +----------------------------------
66 +
67 +Here is abasic echo server that uses the :class:`dispatcher` class to accept
68 +connections and dispatches the incoming connections to a handler::
69 +
70 + import asyncore
71 + import socket
72 +
73 + class EchoHandler(asyncore.dispatcher_with_send):
74 +
75 + def handle_read(self):
76 + data = self.recv(8192)
77 + self.send(data)
78 +
79 + class EchoServer(asyncore.dispatcher):
80 +
81 + def __init__(self, host, port):
82 + asyncore.dispatcher.__init__(self)
83 + self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
84 + self.set_reuse_addr()
85 + self.bind((host, port))
86 + self.listen(5)
87 +
88 + def handle_accept(self):
89 + pair = self.accept()
90 + if pair is None:
91 + pass
92 + else:
93 + sock, addr = pair
94 + print 'Incoming connection from %s' % repr(addr)
95 + handler = EchoHandler(sock)
96 +
97 + server = EchoServer('localhost', 8080)
98 + asyncore.loop()
99 +
100 diff -ruN Python-2.6.7-orig/Lib/asyncore.py Python-2.6.7/Lib/asyncore.py
101 --- Python-2.6.7-orig/Lib/asyncore.py 2010-08-13 01:30:39.000000000 +0000
102 +++ Python-2.6.7/Lib/asyncore.py 2012-02-15 20:47:05.175726769 +0000
103 @@ -348,12 +348,15 @@
104 # XXX can return either an address pair or None
105 try:
106 conn, addr = self.socket.accept()
107 - return conn, addr
108 - except socket.error, why:
109 - if why.args[0] == EWOULDBLOCK:
110 - pass
111 + except TypeError:
112 + return None
113 + except socket.error as why:
114 + if why.args[0] in (EWOULDBLOCK, ECONNABORTED):
115 + return None
116 else:
117 raise
118 + else:
119 + return conn, addr
120
121 def send(self, data):
122 try: