Python asynchat.async_chat() Examples

The following are 30 code examples of asynchat.async_chat(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asynchat , or try the search function .
Example #1
Source File: smtpd.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, server, conn, addr):
        asynchat.async_chat.__init__(self, conn)
        self.__server = server
        self.__conn = conn
        self.__addr = addr
        self.__line = []
        self.__state = self.COMMAND
        self.__greeting = 0
        self.__mailfrom = None
        self.__rcpttos = []
        self.__data = ''
        self.__fqdn = socket.getfqdn()
        try:
            self.__peer = conn.getpeername()
        except socket.error, err:
            # a race condition  may occur if the other end is closing
            # before we can get the peername
            self.close()
            if err[0] != errno.ENOTCONN:
                raise
            return 
Example #2
Source File: ioloop.py    From oss-ftp with MIT License 6 votes vote down vote up
def initiate_send(self):
        asynchat.async_chat.initiate_send(self)
        if not self._closed:
            # if there's still data to send we want to be ready
            # for writing, else we're only intereseted in reading
            if not self.producer_fifo:
                wanted = self.ioloop.READ
            else:
                # In FTPHandler, we also want to listen for user input
                # hence the READ. DTPHandler has its own initiate_send()
                # which will either READ or WRITE.
                wanted = self.ioloop.READ | self.ioloop.WRITE
            if self._wanted_io_events != wanted:
                self.ioloop.modify(self._fileno, wanted)
                self._wanted_io_events = wanted
        else:
            debug("call: initiate_send(); called with no connection",
                  inst=self) 
Example #3
Source File: smtpd.py    From meddle with MIT License 6 votes vote down vote up
def __init__(self, server, conn, addr):
        asynchat.async_chat.__init__(self, conn)
        self.__server = server
        self.__conn = conn
        self.__addr = addr
        self.__line = []
        self.__state = self.COMMAND
        self.__greeting = 0
        self.__mailfrom = None
        self.__rcpttos = []
        self.__data = ''
        self.__fqdn = socket.getfqdn()
        try:
            self.__peer = conn.getpeername()
        except socket.error, err:
            # a race condition  may occur if the other end is closing
            # before we can get the peername
            self.close()
            if err[0] != errno.ENOTCONN:
                raise
            return 
Example #4
Source File: ioloop.py    From script-languages with MIT License 6 votes vote down vote up
def initiate_send(self):
        asynchat.async_chat.initiate_send(self)
        if not self._closed:
            # if there's still data to send we want to be ready
            # for writing, else we're only intereseted in reading
            if not self.producer_fifo:
                wanted = self.ioloop.READ
            else:
                # In FTPHandler, we also want to listen for user input
                # hence the READ. DTPHandler has its own initiate_send()
                # which will either READ or WRITE.
                wanted = self.ioloop.READ | self.ioloop.WRITE
            if self._wanted_io_events != wanted:
                self.ioloop.modify(self._fileno, wanted)
                self._wanted_io_events = wanted
        else:
            debug("call: initiate_send(); called with no connection",
                  inst=self) 
Example #5
Source File: smtpd.py    From oss-ftp with MIT License 6 votes vote down vote up
def __init__(self, server, conn, addr):
        asynchat.async_chat.__init__(self, conn)
        self.__server = server
        self.__conn = conn
        self.__addr = addr
        self.__line = []
        self.__state = self.COMMAND
        self.__greeting = 0
        self.__mailfrom = None
        self.__rcpttos = []
        self.__data = ''
        self.__fqdn = socket.getfqdn()
        try:
            self.__peer = conn.getpeername()
        except socket.error, err:
            # a race condition  may occur if the other end is closing
            # before we can get the peername
            self.close()
            if err[0] != errno.ENOTCONN:
                raise
            return 
Example #6
Source File: test_asynchat.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def line_terminator_check(self, term, server_chunk):
        event = threading.Event()
        s = echo_server(event)
        s.chunk_size = server_chunk
        s.start()
        event.wait()
        event.clear()
        time.sleep(0.01)   # Give server time to start accepting.
        c = echo_client(term, s.port)
        c.push(b"hello ")
        c.push(b"world" + term)
        c.push(b"I'm not dead yet!" + term)
        c.push(SERVER_QUIT)
        asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
        s.join(timeout=TIMEOUT)
        if s.is_alive():
            self.fail("join() timed out")

        self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])

    # the line terminator tests below check receiving variously-sized
    # chunks back from the server in order to exercise all branches of
    # async_chat.handle_read 
Example #7
Source File: test_asynchat.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, terminator, server_port):
            asynchat.async_chat.__init__(self)
            self.contents = []
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.connect((HOST, server_port))
            self.set_terminator(terminator)
            self.buffer = b""

            def handle_connect(self):
                pass

            if sys.platform == 'darwin':
                # select.poll returns a select.POLLHUP at the end of the tests
                # on darwin, so just ignore it
                def handle_expt(self):
                    pass 
Example #8
Source File: ioloop.py    From oss-ftp with MIT License 6 votes vote down vote up
def initiate_send(self):
        asynchat.async_chat.initiate_send(self)
        if not self._closed:
            # if there's still data to send we want to be ready
            # for writing, else we're only intereseted in reading
            if not self.producer_fifo:
                wanted = self.ioloop.READ
            else:
                # In FTPHandler, we also want to listen for user input
                # hence the READ. DTPHandler has its own initiate_send()
                # which will either READ or WRITE.
                wanted = self.ioloop.READ | self.ioloop.WRITE
            if self._wanted_io_events != wanted:
                self.ioloop.modify(self._fileno, wanted)
                self._wanted_io_events = wanted
        else:
            debug("call: initiate_send(); called with no connection",
                  inst=self) 
Example #9
Source File: test_asynchat.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_blockingioerror(self):
        # Issue #16133: handle_read() must ignore blocking I/O errors like
        # EAGAIN
        class fake_socket:
            def fileno(self):
                return 0

            def recv(self, size):
                raise socket.error(errno.EAGAIN, "EAGAIN")

        class MyChat(asynchat.async_chat):
            def handle_error(self):
                raise Exception("error")

        sock = fake_socket()
        dispatcher = MyChat()
        dispatcher.set_socket(sock)
        self.addCleanup(dispatcher.del_channel)

        # must not call handle_error()
        dispatcher.handle_read() 
Example #10
Source File: test_asynchat.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_blockingioerror(self):
        # Issue #16133: handle_read() must ignore blocking I/O errors like
        # EAGAIN
        class fake_socket:
            def fileno(self):
                return 0

            def recv(self, size):
                raise socket.error(errno.EAGAIN, "EAGAIN")

        class MyChat(asynchat.async_chat):
            def handle_error(self):
                raise Exception("error")

        sock = fake_socket()
        dispatcher = MyChat()
        dispatcher.set_socket(sock)
        self.addCleanup(dispatcher.del_channel)

        # must not call handle_error()
        dispatcher.handle_read() 
Example #11
Source File: smtpd.py    From BinderFilter with MIT License 6 votes vote down vote up
def __init__(self, server, conn, addr):
        asynchat.async_chat.__init__(self, conn)
        self.__server = server
        self.__conn = conn
        self.__addr = addr
        self.__line = []
        self.__state = self.COMMAND
        self.__greeting = 0
        self.__mailfrom = None
        self.__rcpttos = []
        self.__data = ''
        self.__fqdn = socket.getfqdn()
        try:
            self.__peer = conn.getpeername()
        except socket.error, err:
            # a race condition  may occur if the other end is closing
            # before we can get the peername
            self.close()
            if err[0] != errno.ENOTCONN:
                raise
            return 
Example #12
Source File: test_poplib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, conn):
            asynchat.async_chat.__init__(self, conn)
            self.secure_connection()
            self.set_terminator(b"\r\n")
            self.in_buffer = []
            self.push('+OK dummy pop3 server ready. <timestamp>')
            self.tls_active = True
            self.tls_starting = False 
Example #13
Source File: ftpserver.py    From script-languages with MIT License 5 votes vote down vote up
def readable(self):
        # Checking for self.connected seems to be necessary as per:
        # http://code.google.com/p/pyftpdlib/issues/detail?id=188#c18
        # In contrast to DTPHandler, here we are not interested in
        # attempting to receive any further data from a closed socket.
        return not self.sleeping and self.connected \
                                 and asynchat.async_chat.readable(self) 
Example #14
Source File: ftpserver.py    From script-languages with MIT License 5 votes vote down vote up
def writable(self):
        return not self.sleeping and self.connected \
                                 and asynchat.async_chat.writable(self) 
Example #15
Source File: ioloop.py    From script-languages with MIT License 5 votes vote down vote up
def __init__(self, sock=None, ioloop=None):
        self.ioloop = ioloop or IOLoop.instance()
        self._wanted_io_events = self.ioloop.READ
        self._current_io_events = self.ioloop.READ
        self._closed = False
        self._closing = False
        self._fileno = sock.fileno() if sock else None
        self._tasks = []
        asynchat.async_chat.__init__(self, sock)

    # --- IO loop related methods 
Example #16
Source File: backdoros.py    From backdoros with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        asynchat.async_chat.__init__(self, *args, **kwargs)
        self._childs = {}
        self._in_repl = False
        self._repl_instance = code.InteractiveConsole()
        self._stdout = None
        self._stderr = None
        self._in_cat = False
        self._in_cat_buffer = ""
        self._in_cat_filename = ""
        self.buffer = ""
        self.set_terminator('\n')
        # Welcome to the Jungle!
        self.push("BackdorOS release %s on an %s\n" % (__version__, platform.platform()))
        self.push("%> ") 
Example #17
Source File: test_asynchat.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_blockingioerror(self):
        # Issue #16133: handle_read() must ignore BlockingIOError
        sock = unittest.mock.Mock()
        sock.recv.side_effect = BlockingIOError(errno.EAGAIN)

        dispatcher = asynchat.async_chat()
        dispatcher.set_socket(sock)
        self.addCleanup(dispatcher.del_channel)

        with unittest.mock.patch.object(dispatcher, 'handle_error') as error:
            dispatcher.handle_read()
        self.assertFalse(error.called) 
Example #18
Source File: test_asynchat.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_numeric_terminator1(self):
        # check that ints & longs both work (since type is
        # explicitly checked in async_chat.handle_read)
        self.numeric_terminator_check(1) 
Example #19
Source File: test_asynchat.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_disallow_negative_terminator(self):
        # Issue #11259
        client = asynchat.async_chat()
        self.assertRaises(ValueError, client.set_terminator, -1) 
Example #20
Source File: test_poplib.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def handle_read(self):
            if self.tls_starting:
                self._do_tls_handshake()
            else:
                try:
                    asynchat.async_chat.handle_read(self)
                except ssl.SSLEOFError:
                    self.handle_close() 
Example #21
Source File: ftpserver.py    From script-languages with MIT License 5 votes vote down vote up
def writable(self):
        """Predicate for inclusion in the writable for select()."""
        return not self.receive and asynchat.async_chat.writable(self) 
Example #22
Source File: ftpserver.py    From script-languages with MIT License 5 votes vote down vote up
def push_with_producer(self, producer):
        if self._use_sendfile(producer):
            self._offset = producer.file.tell()
            self._filefd = self.file_obj.fileno()
            self.initiate_sendfile()
            self.initiate_send = self.initiate_sendfile
        else:
            asynchat.async_chat.push_with_producer(self, producer) 
Example #23
Source File: StratumSource.py    From poclbm with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, socket, map_, parent):
		asynchat.async_chat.__init__(self, socket, map_)
		self.parent = parent
		self.data = ''
		self.set_terminator('\n') 
Example #24
Source File: plac_ext.py    From plac with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, socket, interpreter):
        asynchat.async_chat.__init__(self, socket)
        self.set_terminator(self.terminator)
        self.i = interpreter
        self.i.__enter__()
        self.data = []
        self.write(self.prompt) 
Example #25
Source File: ioloop.py    From script-languages with MIT License 5 votes vote down vote up
def close_when_done(self):
        if len(self.producer_fifo) == 0:
            self.handle_close()
        else:
            self._closing = True
            asynchat.async_chat.close_when_done(self) 
Example #26
Source File: test_asynchat.py    From oss-ftp with MIT License 5 votes vote down vote up
def test_numeric_terminator1(self):
        # check that ints & longs both work (since type is
        # explicitly checked in async_chat.handle_read)
        self.numeric_terminator_check(1)
        self.numeric_terminator_check(1L) 
Example #27
Source File: test_asynchat.py    From oss-ftp with MIT License 5 votes vote down vote up
def __init__(self, terminator, server_port):
            asynchat.async_chat.__init__(self)
            self.contents = []
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.connect((HOST, server_port))
            self.set_terminator(terminator)
            self.buffer = '' 
Example #28
Source File: test_poplib.py    From oss-ftp with MIT License 5 votes vote down vote up
def __init__(self, conn):
            asynchat.async_chat.__init__(self, conn)
            self.socket = ssl.wrap_socket(self.socket, certfile=CERTFILE,
                                          server_side=True,
                                          do_handshake_on_connect=False)
            # Must try handshake before calling push()
            self._ssl_accepting = True
            self._do_ssl_handshake()
            self.set_terminator("\r\n")
            self.in_buffer = []
            self.push('+OK dummy pop3 server ready.') 
Example #29
Source File: test_poplib.py    From oss-ftp with MIT License 5 votes vote down vote up
def cmd_retr(self, arg):
        self.push('+OK %s bytes' %len(RETR_RESP))
        asynchat.async_chat.push(self, RETR_RESP) 
Example #30
Source File: test_poplib.py    From oss-ftp with MIT License 5 votes vote down vote up
def cmd_list(self, arg):
        if arg:
            self.push('+OK %s %s' %(arg, arg))
        else:
            self.push('+OK')
            asynchat.async_chat.push(self, LIST_RESP)