Пример #1
0
    def recv(self, n, encoding="unicode", timeout=5):
        # Sanity checking.
        assert n

        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Get data.
            self.get_chunks(n, encoding=encoding)

            # Return the current buffer.
            ret = self.buf

            # Reset the old buffer.
            self.buf = b""

            # Return results.
            if encoding == "unicode":
                ret = encode_str(ret, encoding)

            return ret
        except Exception as e:
            self.debug_print("Recv closign e" + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()
            if encoding == "unicode":
                return u""
            else:
                return b""
Пример #2
0
    def recv(self, n, encoding="unicode", timeout=5):
        # Sanity checking.
        assert n

        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Get data.
            self.get_chunks(n, encoding=encoding)

            # Return the current buffer.
            ret = self.buf

            # Reset the old buffer.
            self.buf = b""

            # Return results.
            if encoding == "unicode":
                ret = encode_str(ret, encoding)

            return ret
        except Exception as e:
            self.debug_print("Recv closign e" + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()
            if encoding == "unicode":
                return u""
            else:
                return b""
Пример #3
0
    def parse_buf(self, encoding="unicode"):
        """
        Since TCP is a stream-orientated protocol, responses aren't guaranteed
        to be complete when they arrive. The buffer stores all the data and
        this function splits the data into replies based on the new line
        delimiter.
        """
        buf_len = len(self.buf)
        replies = []
        reply = b""
        chop = 0
        skip = 0
        i = 0
        buf_len = len(self.buf)
        for i in range(0, buf_len):
            ch = self.buf[i:i + 1]
            if skip:
                skip -= 1
                i += 1
                continue

            nxt = i + 1
            if nxt < buf_len:
                if ch == b"\r" and self.buf[nxt:nxt + 1] == b"\n":

                    # Append new reply.
                    if reply != b"":
                        if encoding == "unicode":
                            replies.append(encode_str(reply, encoding))
                        else:
                            replies.append(reply)
                        reply = b""

                    # Truncate the whole buf if chop is out of bounds.
                    chop = nxt + 1
                    skip = 1
                    i += 1
                    continue

            reply += ch
            i += 1

        # Truncate buf.
        if chop:
            self.buf = self.buf[chop:]

        return replies
Пример #4
0
    def parse_buf(self, encoding="unicode"):
        """
        Since TCP is a stream-orientated protocol, responses aren't guaranteed
        to be complete when they arrive. The buffer stores all the data and
        this function splits the data into replies based on the new line
        delimiter.
        """
        buf_len = len(self.buf)
        replies = []
        reply = b""
        chop = 0
        skip = 0
        i = 0
        buf_len = len(self.buf)
        for i in range(0, buf_len):
            ch = self.buf[i:i + 1]
            if skip:
                skip -= 1
                i += 1
                continue

            nxt = i + 1
            if nxt < buf_len:
                if ch == b"\r" and self.buf[nxt:nxt + 1] == b"\n":

                    # Append new reply.
                    if reply != b"":
                        if encoding == "unicode":
                            replies.append(encode_str(reply, encoding))
                        else:
                            replies.append(reply)
                        reply = b""

                    # Truncate the whole buf if chop is out of bounds.
                    chop = nxt + 1
                    skip = 1
                    i += 1
                    continue

            reply += ch
            i += 1

        # Truncate buf.
        if chop:
            self.buf = self.buf[chop:]

        return replies
Пример #5
0
    def send_line(self, msg, timeout=5):
        # Sanity checking.
        assert (len(msg))

        # Not connected.
        if not self.connected:
            return 0

        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Convert to bytes Python 2 & 3
            if type(msg) == type(u""):
                msg = encode_str(msg, "ascii")

            # Convert delimiter to bytes.
            msg += self.delimiter

            """
            The inclusion of the send_all flag makes this function behave like
            a blocking socket for the purposes of sending a full line even if
            the socket is non-blocking. It's assumed that lines will be small
            and if the network buffer is full this code won't end up as a
            bottleneck. (Otherwise you would have to check the number of bytes
            returned every time you sent a line which is quite annoying.)
            """
            ret = self.send(msg, send_all=1, timeout=timeout)

            return ret
        except Exception as e:
            self.debug_print("Send line closing" + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()
            return 0
Пример #6
0
    def send_line(self, msg, timeout=5):
        # Sanity checking.
        assert (len(msg))

        # Not connected.
        if not self.connected:
            return 0

        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Convert to bytes Python 2 & 3
            if type(msg) == type(u""):
                msg = encode_str(msg, "ascii")

            # Convert delimiter to bytes.
            msg += self.delimiter
            """
            The inclusion of the send_all flag makes this function behave like
            a blocking socket for the purposes of sending a full line even if
            the socket is non-blocking. It's assumed that lines will be small
            and if the network buffer is full this code won't end up as a
            bottleneck. (Otherwise you would have to check the number of bytes
            returned every time you sent a line which is quite annoying.)
            """
            ret = self.send(msg, send_all=1, timeout=timeout)

            return ret
        except Exception as e:
            self.debug_print("Send line closing" + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()
            return 0
Пример #7
0
    def send(self, msg, send_all=0, timeout=5, encoding="ascii"):
        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Convert to bytes Python 2 & 3
            # The caller should ensure correct encoding.
            if type(msg) == type(u""):
                msg = encode_str(msg, "ascii")

            # Work out stop time.
            if send_all:
                future = time.time() + (timeout or self.timeout)
            else:
                future = 0

            repeat = 1
            total_sent = 0
            msg_len = len(msg)
            while repeat:
                repeat = 0
                while True:
                    # Attempt to send all.
                    # This won't work if the network buffer is already full.
                    try:
                        bytes_sent = self.s.send(
                                msg[total_sent:self.chunk_size])
                    except socket.timeout as e:
                        err = e.args[0]
                        if err == "timed out":
                            return 0
                    except socket.error as e:
                        err = e.args[0]
                        if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                            break
                        else:
                            # Connection closed or other problem.
                            self.debug_print("Con send closing other")
                            self.close()
                            return 0

                    # Connection broken.
                    if not bytes_sent or bytes_sent is None:
                        self.close()
                        return 0

                    # How much has been sent?
                    total_sent += bytes_sent

                    # Avoid looping forever.
                    if self.blocking and not send_all:
                        break

                    # Everything sent.
                    if total_sent >= msg_len:
                        break

                    # Don't block.
                    if not send_all:
                        break

                    # Avoid 100% CPU.
                    time.sleep(0.001)

                # Avoid looping forever.
                if send_all:
                    if time.time() >= future:
                        repeat = 0
                        break

                # Send the rest if blocking:
                if total_sent < msg_len and send_all:
                    repeat = 1

            return total_sent
        except Exception as e:
            self.debug_print("Con send: " + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()
Пример #8
0
    def send(self, msg, send_all=0, timeout=5, encoding="ascii"):
        # Update timeout.
        if timeout != self.timeout and self.blocking:
            self.set_blocking(self.blocking, timeout)

        try:
            # Convert to bytes Python 2 & 3
            # The caller should ensure correct encoding.
            if type(msg) == type(u""):
                msg = encode_str(msg, "ascii")

            # Work out stop time.
            if send_all:
                future = time.time() + (timeout or self.timeout)
            else:
                future = 0

            repeat = 1
            total_sent = 0
            msg_len = len(msg)
            while repeat:
                repeat = 0
                while True:
                    # Attempt to send all.
                    # This won't work if the network buffer is already full.
                    try:
                        bytes_sent = self.s.send(
                            msg[total_sent:self.chunk_size])
                    except socket.timeout as e:
                        err = e.args[0]
                        if err == "timed out":
                            return 0
                    except socket.error as e:
                        err = e.args[0]
                        if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                            break
                        else:
                            # Connection closed or other problem.
                            self.debug_print("Con send closing other")
                            self.close()
                            return 0

                    # Connection broken.
                    if not bytes_sent or bytes_sent is None:
                        self.close()
                        return 0

                    # How much has been sent?
                    total_sent += bytes_sent

                    # Avoid looping forever.
                    if self.blocking and not send_all:
                        break

                    # Everything sent.
                    if total_sent >= msg_len:
                        break

                    # Don't block.
                    if not send_all:
                        break

                    # Avoid 100% CPU.
                    time.sleep(0.001)

                # Avoid looping forever.
                if send_all:
                    if time.time() >= future:
                        repeat = 0
                        break

                # Send the rest if blocking:
                if total_sent < msg_len and send_all:
                    repeat = 1

            return total_sent
        except Exception as e:
            self.debug_print("Con send: " + str(e))
            error = parse_exception(e)
            log_exception(error_log_path, error)
            self.close()