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