/Lib/asynchat.py

http://unladen-swallow.googlecode.com/ · Python · 314 lines · 235 code · 27 blank · 52 comment · 32 complexity · 899e2ea1eb98a636469eb97f4d95bc1c MD5 · raw file

  1. # -*- Mode: Python; tab-width: 4 -*-
  2. # Id: asynchat.py,v 2.26 2000/09/07 22:29:26 rushing Exp
  3. # Author: Sam Rushing <rushing@nightmare.com>
  4. # ======================================================================
  5. # Copyright 1996 by Sam Rushing
  6. #
  7. # All Rights Reserved
  8. #
  9. # Permission to use, copy, modify, and distribute this software and
  10. # its documentation for any purpose and without fee is hereby
  11. # granted, provided that the above copyright notice appear in all
  12. # copies and that both that copyright notice and this permission
  13. # notice appear in supporting documentation, and that the name of Sam
  14. # Rushing not be used in advertising or publicity pertaining to
  15. # distribution of the software without specific, written prior
  16. # permission.
  17. #
  18. # SAM RUSHING DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
  20. # NO EVENT SHALL SAM RUSHING BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. # CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  22. # OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  23. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  24. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  25. # ======================================================================
  26. r"""A class supporting chat-style (command/response) protocols.
  27. This class adds support for 'chat' style protocols - where one side
  28. sends a 'command', and the other sends a response (examples would be
  29. the common internet protocols - smtp, nntp, ftp, etc..).
  30. The handle_read() method looks at the input stream for the current
  31. 'terminator' (usually '\r\n' for single-line responses, '\r\n.\r\n'
  32. for multi-line output), calling self.found_terminator() on its
  33. receipt.
  34. for example:
  35. Say you build an async nntp client using this class. At the start
  36. of the connection, you'll have self.terminator set to '\r\n', in
  37. order to process the single-line greeting. Just before issuing a
  38. 'LIST' command you'll set it to '\r\n.\r\n'. The output of the LIST
  39. command will be accumulated (using your own 'collect_incoming_data'
  40. method) up to the terminator, and then control will be returned to
  41. you - by calling your self.found_terminator() method.
  42. """
  43. import socket
  44. import asyncore
  45. from collections import deque
  46. from sys import py3kwarning
  47. from warnings import filterwarnings, catch_warnings
  48. class async_chat (asyncore.dispatcher):
  49. """This is an abstract class. You must derive from this class, and add
  50. the two methods collect_incoming_data() and found_terminator()"""
  51. # these are overridable defaults
  52. ac_in_buffer_size = 4096
  53. ac_out_buffer_size = 4096
  54. def __init__ (self, sock=None, map=None):
  55. # for string terminator matching
  56. self.ac_in_buffer = ''
  57. # we use a list here rather than cStringIO for a few reasons...
  58. # del lst[:] is faster than sio.truncate(0)
  59. # lst = [] is faster than sio.truncate(0)
  60. # cStringIO will be gaining unicode support in py3k, which
  61. # will negatively affect the performance of bytes compared to
  62. # a ''.join() equivalent
  63. self.incoming = []
  64. # we toss the use of the "simple producer" and replace it with
  65. # a pure deque, which the original fifo was a wrapping of
  66. self.producer_fifo = deque()
  67. asyncore.dispatcher.__init__ (self, sock, map)
  68. def collect_incoming_data(self, data):
  69. raise NotImplementedError("must be implemented in subclass")
  70. def _collect_incoming_data(self, data):
  71. self.incoming.append(data)
  72. def _get_data(self):
  73. d = ''.join(self.incoming)
  74. del self.incoming[:]
  75. return d
  76. def found_terminator(self):
  77. raise NotImplementedError("must be implemented in subclass")
  78. def set_terminator (self, term):
  79. "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
  80. self.terminator = term
  81. def get_terminator (self):
  82. return self.terminator
  83. # grab some more data from the socket,
  84. # throw it to the collector method,
  85. # check for the terminator,
  86. # if found, transition to the next state.
  87. def handle_read (self):
  88. try:
  89. data = self.recv (self.ac_in_buffer_size)
  90. except socket.error, why:
  91. self.handle_error()
  92. return
  93. self.ac_in_buffer = self.ac_in_buffer + data
  94. # Continue to search for self.terminator in self.ac_in_buffer,
  95. # while calling self.collect_incoming_data. The while loop
  96. # is necessary because we might read several data+terminator
  97. # combos with a single recv(4096).
  98. while self.ac_in_buffer:
  99. lb = len(self.ac_in_buffer)
  100. terminator = self.get_terminator()
  101. if not terminator:
  102. # no terminator, collect it all
  103. self.collect_incoming_data (self.ac_in_buffer)
  104. self.ac_in_buffer = ''
  105. elif isinstance(terminator, int) or isinstance(terminator, long):
  106. # numeric terminator
  107. n = terminator
  108. if lb < n:
  109. self.collect_incoming_data (self.ac_in_buffer)
  110. self.ac_in_buffer = ''
  111. self.terminator = self.terminator - lb
  112. else:
  113. self.collect_incoming_data (self.ac_in_buffer[:n])
  114. self.ac_in_buffer = self.ac_in_buffer[n:]
  115. self.terminator = 0
  116. self.found_terminator()
  117. else:
  118. # 3 cases:
  119. # 1) end of buffer matches terminator exactly:
  120. # collect data, transition
  121. # 2) end of buffer matches some prefix:
  122. # collect data to the prefix
  123. # 3) end of buffer does not match any prefix:
  124. # collect data
  125. terminator_len = len(terminator)
  126. index = self.ac_in_buffer.find(terminator)
  127. if index != -1:
  128. # we found the terminator
  129. if index > 0:
  130. # don't bother reporting the empty string (source of subtle bugs)
  131. self.collect_incoming_data (self.ac_in_buffer[:index])
  132. self.ac_in_buffer = self.ac_in_buffer[index+terminator_len:]
  133. # This does the Right Thing if the terminator is changed here.
  134. self.found_terminator()
  135. else:
  136. # check for a prefix of the terminator
  137. index = find_prefix_at_end (self.ac_in_buffer, terminator)
  138. if index:
  139. if index != lb:
  140. # we found a prefix, collect up to the prefix
  141. self.collect_incoming_data (self.ac_in_buffer[:-index])
  142. self.ac_in_buffer = self.ac_in_buffer[-index:]
  143. break
  144. else:
  145. # no prefix, collect it all
  146. self.collect_incoming_data (self.ac_in_buffer)
  147. self.ac_in_buffer = ''
  148. def handle_write (self):
  149. self.initiate_send()
  150. def handle_close (self):
  151. self.close()
  152. def push (self, data):
  153. sabs = self.ac_out_buffer_size
  154. if len(data) > sabs:
  155. for i in xrange(0, len(data), sabs):
  156. self.producer_fifo.append(data[i:i+sabs])
  157. else:
  158. self.producer_fifo.append(data)
  159. self.initiate_send()
  160. def push_with_producer (self, producer):
  161. self.producer_fifo.append(producer)
  162. self.initiate_send()
  163. def readable (self):
  164. "predicate for inclusion in the readable for select()"
  165. # cannot use the old predicate, it violates the claim of the
  166. # set_terminator method.
  167. # return (len(self.ac_in_buffer) <= self.ac_in_buffer_size)
  168. return 1
  169. def writable (self):
  170. "predicate for inclusion in the writable for select()"
  171. return self.producer_fifo or (not self.connected)
  172. def close_when_done (self):
  173. "automatically close this channel once the outgoing queue is empty"
  174. self.producer_fifo.append(None)
  175. def initiate_send(self):
  176. while self.producer_fifo and self.connected:
  177. first = self.producer_fifo[0]
  178. # handle empty string/buffer or None entry
  179. if not first:
  180. del self.producer_fifo[0]
  181. if first is None:
  182. self.handle_close()
  183. return
  184. # handle classic producer behavior
  185. obs = self.ac_out_buffer_size
  186. try:
  187. with catch_warnings():
  188. if py3kwarning:
  189. filterwarnings("ignore", ".*buffer", DeprecationWarning)
  190. data = buffer(first, 0, obs)
  191. except TypeError:
  192. data = first.more()
  193. if data:
  194. self.producer_fifo.appendleft(data)
  195. else:
  196. del self.producer_fifo[0]
  197. continue
  198. # send the data
  199. try:
  200. num_sent = self.send(data)
  201. except socket.error:
  202. self.handle_error()
  203. return
  204. if num_sent:
  205. if num_sent < len(data) or obs < len(first):
  206. self.producer_fifo[0] = first[num_sent:]
  207. else:
  208. del self.producer_fifo[0]
  209. # we tried to send some actual data
  210. return
  211. def discard_buffers (self):
  212. # Emergencies only!
  213. self.ac_in_buffer = ''
  214. del self.incoming[:]
  215. self.producer_fifo.clear()
  216. class simple_producer:
  217. def __init__ (self, data, buffer_size=512):
  218. self.data = data
  219. self.buffer_size = buffer_size
  220. def more (self):
  221. if len (self.data) > self.buffer_size:
  222. result = self.data[:self.buffer_size]
  223. self.data = self.data[self.buffer_size:]
  224. return result
  225. else:
  226. result = self.data
  227. self.data = ''
  228. return result
  229. class fifo:
  230. def __init__ (self, list=None):
  231. if not list:
  232. self.list = deque()
  233. else:
  234. self.list = deque(list)
  235. def __len__ (self):
  236. return len(self.list)
  237. def is_empty (self):
  238. return not self.list
  239. def first (self):
  240. return self.list[0]
  241. def push (self, data):
  242. self.list.append(data)
  243. def pop (self):
  244. if self.list:
  245. return (1, self.list.popleft())
  246. else:
  247. return (0, None)
  248. # Given 'haystack', see if any prefix of 'needle' is at its end. This
  249. # assumes an exact match has already been checked. Return the number of
  250. # characters matched.
  251. # for example:
  252. # f_p_a_e ("qwerty\r", "\r\n") => 1
  253. # f_p_a_e ("qwertydkjf", "\r\n") => 0
  254. # f_p_a_e ("qwerty\r\n", "\r\n") => <undefined>
  255. # this could maybe be made faster with a computed regex?
  256. # [answer: no; circa Python-2.0, Jan 2001]
  257. # new python: 28961/s
  258. # old python: 18307/s
  259. # re: 12820/s
  260. # regex: 14035/s
  261. def find_prefix_at_end (haystack, needle):
  262. l = len(needle) - 1
  263. while l and not haystack.endswith(needle[:l]):
  264. l -= 1
  265. return l