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