28 r"""A class supporting chat-style (command/response) protocols.
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..).
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
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.
53 """This is an abstract class. You must derive from this class, and add
54 the two methods collect_incoming_data() and found_terminator()"""
58 ac_in_buffer_size = 4096
59 ac_out_buffer_size = 4096
65 asyncore.dispatcher.__init__ (self, conn)
68 "Set the input delimiter. Can be a fixed string of any length, an integer, or None"
83 except socket.error, why:
97 if terminator
is None:
101 elif type(terminator) == type(0):
112 self.found_terminator()
121 terminator_len = len(terminator)
122 index = self.ac_in_buffer.find(terminator)
130 self.found_terminator()
133 index = find_prefix_at_end (self.
ac_in_buffer, terminator)
152 self.producer_fifo.push (simple_producer (data))
156 self.producer_fifo.push (producer)
160 "predicate for inclusion in the readable for select()"
164 "predicate for inclusion in the writable for select()"
169 self.producer_fifo.is_empty()
and
174 "automatically close this channel once the outgoing queue is empty"
175 self.producer_fifo.push (
None)
180 _string_type = type(
'')
183 p = self.producer_fifo.first()
188 self.producer_fifo.pop()
191 elif type(p)
is _string_type:
192 self.producer_fifo.pop()
200 self.producer_fifo.pop()
217 except socket.error, why:
226 self.producer_fifo.pop()
253 return len(self.
list)
256 return self.
list == []
262 self.list.append (data)
266 result = self.
list[0]
289 for i
in range (1,nl):
290 if haystack[-(nl-i):] == needle[:(nl-i)]: