| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
r"""A class supporting chat-style (command/response) protocols. |
| 29 |
|
| 30 |
This class adds support for 'chat' style protocols - where one side |
| 31 |
sends a 'command', and the other sends a response (examples would be |
| 32 |
the common internet protocols - smtp, nntp, ftp, etc..). |
| 33 |
|
| 34 |
The handle_read() method looks at the input stream for the current |
| 35 |
'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n' |
| 36 |
for multi-line output), calling self.found_terminator() on its |
| 37 |
receipt. |
| 38 |
|
| 39 |
for example: |
| 40 |
Say you build an async nntp client using this class. At the start |
| 41 |
of the connection, you'll have self.terminator set to '\r\n', in |
| 42 |
order to process the single-line greeting. Just before issuing a |
| 43 |
'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST |
| 44 |
command will be accumulated (using your own 'collect_incoming_data' |
| 45 |
method) up to the terminator, and then control will be returned to |
| 46 |
you - by calling your self.found_terminator() method. |
| 47 |
""" |
| 48 |
|
| 49 |
import asyncore |
| 50 |
import errno |
| 51 |
import socket |
| 52 |
from collections import deque |
| 53 |
from sys import py3kwarning |
| 54 |
from warnings import filterwarnings, catch_warnings |
| 55 |
|
| 56 |
_BLOCKING_IO_ERRORS = (errno.EAGAIN, errno.EALREADY, errno.EINPROGRESS, |
| 57 |
errno.EWOULDBLOCK) |
| 58 |
|
| 59 |
|
| 60 |
class async_chat (asyncore.dispatcher): |
| 61 |
"""This is an abstract class. You must derive from this class, and add |
| 62 |
the two methods collect_incoming_data() and found_terminator()""" |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
ac_in_buffer_size = 4096 |
| 67 |
ac_out_buffer_size = 4096 |
| 68 |
|
| 69 |
def __init__ (self, sock=None, map=None): |
| 70 |
|
| 71 |
self.ac_in_buffer = '' |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
self.incoming = [] |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
self.producer_fifo = deque() |
| 84 |
asyncore.dispatcher.__init__ (self, sock, map) |
| 85 |
|
| 86 |
def collect_incoming_data(self, data): |
| 87 |
raise NotImplementedError("must be implemented in subclass") |
| 88 |
|
| 89 |
def _collect_incoming_data(self, data): |
| 90 |
self.incoming.append(data) |
| 91 |
|
| 92 |
def _get_data(self): |
| 93 |
d = ''.join(self.incoming) |
| 94 |
del self.incoming[:] |
| 95 |
return d |
| 96 |
|
| 97 |
def found_terminator(self): |
| 98 |
raise NotImplementedError("must be implemented in subclass") |
| 99 |
|
| 100 |
def set_terminator (self, term): |
| 101 |
"Set the input delimiter. Can be a fixed string of any length, an integer, or None" |
| 102 |
self.terminator = term |
| 103 |
|
| 104 |
def get_terminator (self): |
| 105 |
return self.terminator |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
def handle_read (self): |
| 113 |
|
| 114 |
try: |
| 115 |
data = self.recv (self.ac_in_buffer_size) |
| 116 |
except socket.error, why: |
| 117 |
if why.args[0] in _BLOCKING_IO_ERRORS: |
| 118 |
return |
| 119 |
self.handle_error() |
| 120 |
return |
| 121 |
|
| 122 |
self.ac_in_buffer = self.ac_in_buffer + data |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
while self.ac_in_buffer: |
| 130 |
lb = len(self.ac_in_buffer) |
| 131 |
terminator = self.get_terminator() |
| 132 |
if not terminator: |
| 133 |
|
| 134 |
self.collect_incoming_data (self.ac_in_buffer) |
| 135 |
self.ac_in_buffer = '' |
| 136 |
elif isinstance(terminator, (int, long)): |
| 137 |
|
| 138 |
n = terminator |
| 139 |
if lb < n: |
| 140 |
self.collect_incoming_data (self.ac_in_buffer) |
| 141 |
self.ac_in_buffer = '' |
| 142 |
self.terminator = self.terminator - lb |
| 143 |
else: |
| 144 |
self.collect_incoming_data (self.ac_in_buffer[:n]) |
| 145 |
self.ac_in_buffer = self.ac_in_buffer[n:] |
| 146 |
self.terminator = 0 |
| 147 |
self.found_terminator() |
| 148 |
else: |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
terminator_len = len(terminator) |
| 157 |
index = self.ac_in_buffer.find(terminator) |
| 158 |
if index != -1: |
| 159 |
|
| 160 |
if index > 0: |
| 161 |
|
| 162 |
self.collect_incoming_data (self.ac_in_buffer[:index]) |
| 163 |
self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:] |
| 164 |
|
| 165 |
self.found_terminator() |
| 166 |
else: |
| 167 |
|
| 168 |
index = find_prefix_at_end (self.ac_in_buffer, terminator) |
| 169 |
if index: |
| 170 |
if index != lb: |
| 171 |
|
| 172 |
self.collect_incoming_data (self.ac_in_buffer[:-index]) |
| 173 |
self.ac_in_buffer = self.ac_in_buffer[-index:] |
| 174 |
break |
| 175 |
else: |
| 176 |
|
| 177 |
self.collect_incoming_data (self.ac_in_buffer) |
| 178 |
self.ac_in_buffer = '' |
| 179 |
|
| 180 |
def handle_write (self): |
| 181 |
self.initiate_send() |
| 182 |
|
| 183 |
def handle_close (self): |
| 184 |
self.close() |
| 185 |
|
| 186 |
def push (self, data): |
| 187 |
sabs = self.ac_out_buffer_size |
| 188 |
if len(data) > sabs: |
| 189 |
for i in xrange(0, len(data), sabs): |
| 190 |
self.producer_fifo.append(data[i:i+sabs]) |
| 191 |
else: |
| 192 |
self.producer_fifo.append(data) |
| 193 |
self.initiate_send() |
| 194 |
|
| 195 |
def push_with_producer (self, producer): |
| 196 |
self.producer_fifo.append(producer) |
| 197 |
self.initiate_send() |
| 198 |
|
| 199 |
def readable (self): |
| 200 |
"predicate for inclusion in the readable for select()" |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
return 1 |
| 206 |
|
| 207 |
def writable (self): |
| 208 |
"predicate for inclusion in the writable for select()" |
| 209 |
return self.producer_fifo or (not self.connected) |
| 210 |
|
| 211 |
def close_when_done (self): |
| 212 |
"automatically close this channel once the outgoing queue is empty" |
| 213 |
self.producer_fifo.append(None) |
| 214 |
|
| 215 |
def initiate_send(self): |
| 216 |
while self.producer_fifo and self.connected: |
| 217 |
first = self.producer_fifo[0] |
| 218 |
|
| 219 |
if not first: |
| 220 |
del self.producer_fifo[0] |
| 221 |
if first is None: |
| 222 |
self.handle_close() |
| 223 |
return |
| 224 |
|
| 225 |
|
| 226 |
obs = self.ac_out_buffer_size |
| 227 |
try: |
| 228 |
with catch_warnings(): |
| 229 |
if py3kwarning: |
| 230 |
filterwarnings("ignore", ".*buffer", DeprecationWarning) |
| 231 |
data = buffer(first, 0, obs) |
| 232 |
except TypeError: |
| 233 |
data = first.more() |
| 234 |
if data: |
| 235 |
self.producer_fifo.appendleft(data) |
| 236 |
else: |
| 237 |
del self.producer_fifo[0] |
| 238 |
continue |
| 239 |
|
| 240 |
|
| 241 |
try: |
| 242 |
num_sent = self.send(data) |
| 243 |
except socket.error: |
| 244 |
self.handle_error() |
| 245 |
return |
| 246 |
|
| 247 |
if num_sent: |
| 248 |
if num_sent < len(data) or obs < len(first): |
| 249 |
self.producer_fifo[0] = first[num_sent:] |
| 250 |
else: |
| 251 |
del self.producer_fifo[0] |
| 252 |
|
| 253 |
return |
| 254 |
|
| 255 |
def discard_buffers (self): |
| 256 |
|
| 257 |
self.ac_in_buffer = '' |
| 258 |
del self.incoming[:] |
| 259 |
self.producer_fifo.clear() |
| 260 |
|
| 261 |
class simple_producer: |
| 262 |
|
| 263 |
def __init__ (self, data, buffer_size=512): |
| 264 |
self.data = data |
| 265 |
self.buffer_size = buffer_size |
| 266 |
|
| 267 |
def more (self): |
| 268 |
if len (self.data) > self.buffer_size: |
| 269 |
result = self.data[:self.buffer_size] |
| 270 |
self.data = self.data[self.buffer_size:] |
| 271 |
return result |
| 272 |
else: |
| 273 |
result = self.data |
| 274 |
self.data = '' |
| 275 |
return result |
| 276 |
|
| 277 |
class fifo: |
| 278 |
def __init__ (self, list=None): |
| 279 |
if not list: |
| 280 |
self.list = deque() |
| 281 |
else: |
| 282 |
self.list = deque(list) |
| 283 |
|
| 284 |
def __len__ (self): |
| 285 |
return len(self.list) |
| 286 |
|
| 287 |
def is_empty (self): |
| 288 |
return not self.list |
| 289 |
|
| 290 |
def first (self): |
| 291 |
return self.list[0] |
| 292 |
|
| 293 |
def push (self, data): |
| 294 |
self.list.append(data) |
| 295 |
|
| 296 |
def pop (self): |
| 297 |
if self.list: |
| 298 |
return (1, self.list.popleft()) |
| 299 |
else: |
| 300 |
return (0, None) |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
def find_prefix_at_end (haystack, needle): |
| 318 |
l = len(needle) - 1 |
| 319 |
while l and not haystack.endswith(needle[:l]): |
| 320 |
l -= 1 |
| 321 |
return l |