def readline(self): while not b'\n' in self.data: new_data = recv_with_retry(self.sock) if not new_data: raise socket.error('done') self.data += new_data one_line, self.data = self.data.partition(b'\n')[::2] return cast_string(one_line).strip()
def read_bytes(self, size): data = self.data while len(data) < size: new_data = recv_with_retry(self.sock, size - len(data)) if not new_data: raise utils.Error('Lost connection') data += new_data data, self.data = data[:size], data[size:] return data
def get_full_response(self): data = self.data self.data = b'' # noinspection PyTypeChecker while not data.endswith(b'\n> '): new_data = recv_with_retry(self.sock) if not new_data: raise utils.Error('Lost connection') data += new_data data = s(data[:-3]) # noinspection PyTypeChecker if data.startswith('Error:'): raise utils.Error(data[7:]) return data
def get_one_line_response(self, alternate_terminator=None): data = self.data while b'\n' not in data: if alternate_terminator and data.endswith(alternate_terminator): break new_data = recv_with_retry(self.sock) if not new_data: raise utils.Error('Lost connection') data += new_data if data.startswith(b'Error:'): self.data = data return self.get_full_response() data, self.data = data.partition(b'\n')[::2] return s(data)
import sys import socket from papa.utils import cast_string, send_with_retry, recv_with_retry __author__ = 'Scott Maxwell' if len(sys.argv) != 2: sys.stderr.write('Need one port number\n') sys.exit(1) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('127.0.0.1', int(sys.argv[1]))) send_with_retry(sock, b'howdy\n') data = recv_with_retry(sock) sys.stdout.write(cast_string(data)) sock.close()