def testCompatibleReadLines(self): for chunk_size in [4, 8, 16, 32, 64, 128]: b = Buffer(1024) test_stream = TestStream('hello world!\nTest\n\nPiet\nKlaas\nPietBlaat\n', chunk_size = chunk_size) f = BufferedReader(test_stream, b).file() lines = f.readlines() self.assertEquals('hello world!\n', lines.next()) self.assertEquals('Test\n', lines.next()) self.assertEquals('\n', lines.next()) self.assertEquals('Piet\n', lines.next()) self.assertEquals('Klaas\n', lines.next()) self.assertEquals('PietBlaat\n', lines.next()) self.assertEquals('', lines.next()) #line without newline at end of buffer, should report without newline test_stream = TestStream('hello world!\nTest', chunk_size = chunk_size) f = BufferedReader(test_stream, b).file() lines = f.readlines() self.assertEquals('hello world!\n', lines.next()) self.assertEquals('Test', lines.next()) self.assertEquals('', lines.next())
def _send(self, identifier, body, timeout=30): try: conn = Socket.connect((self.host, self.port), timeout) try: req = u"POST / HTTP/1.1\r\nHost: %s\r\nContent-length: %d\r\nX-Realplexor: %s=%s%s\r\n\r\n%s" % (self.host, len(body), (self.identifier or ""), ("%s:%s@" % (self.login, self.password) if self.login else ""), identifier or "", body) if type(req) == unicode: req = req.encode("utf-8") reader = BufferedReader(conn, Buffer(1024)) writer = BufferedWriter(conn, Buffer(1024)) writer.write_bytes(req) writer.flush() conn.socket.shutdown(socket.SHUT_WR) response = "" while True: try: chunk = reader.read_bytes_available() except EOFError: chunk = None if not chunk: break response += chunk if response: m = re_split_headers.split(response, 1) if not m: raise RealplexorError("Non-HTTP response received:\n%s" % response) headers = m[0] body = m[1] m = re_http_status_line.match(headers) if not m: raise RealplexorError("Non-HTTP response received:\n%s" % response) status, code = m.group(1, 2) if code != "200": raise RealplexorError("Request failed: %s\n%s" % (status, body)) m = re_content_length.search(headers) if not m: raise RealplexorError("No Content-Length header in response headers:\n%s" % headers) expected_len = int(m.group(1)) if len(body) != expected_len: raise RealplexorError("Response length (%d) is different from one specified in the Content-Length header (%d): possibly broken response" % (len(body), expected_len)) return body return response finally: conn.close() except socket.error as e: raise RealplexorSocketError("Socket error: %s" % e) except socket.gaierror as e: raise RealplexorError("DNS error: %s" % e) except socket.timeout as e: raise RealplexorError("Timeout error: %s" % e) except IOError as e: raise RealplexorSocketError("IOError: %s" % e)
class SMTP(smtplib.SMTP): """ This is a subclass derived from SMTP that connects over a Concurrence socket """ def _get_socket(self, host, port, timeout): new_socket = Socket.connect((host, port)) self._reader = BufferedReader(new_socket, Buffer(1024)) self._writer = BufferedWriter(new_socket, Buffer(1024)) self.file = self._reader.file() return new_socket def send(self, str): if self.debuglevel > 0: print >> sys.stderr, 'send:', repr(str) if hasattr(self, 'sock') and self.sock: try: self._writer.write_bytes(str) self._writer.flush() except IOError: self.close() raise SMTPServerDisconnected('Server not connected') else: raise SMTPServerDisconnected('please run connect() first') def close(self): if self.sock: self.sock.close() self.sock = None self._reader = None self._writer = None self.file = None
class SMTP(smtplib.SMTP): """ This is a subclass derived from SMTP that connects over a Concurrence socket """ def _get_socket(self, host, port, timeout): new_socket = Socket.connect((host, port)) self._reader = BufferedReader(new_socket, Buffer(1024)) self._writer = BufferedWriter(new_socket, Buffer(1024)) self.file = self._reader.file() return new_socket def send(self, str): if self.debuglevel > 0: print>>sys.stderr, 'send:', repr(str) if hasattr(self, 'sock') and self.sock: try: self._writer.write_bytes(str) self._writer.flush() except IOError: self.close() raise SMTPServerDisconnected('Server not connected') else: raise SMTPServerDisconnected('please run connect() first') def close(self): if self.sock: self.sock.close() self.sock = None self._reader = None self._writer = None self.file = None
def testCompatibleReadLines(self): for chunk_size in [4, 8, 16, 32, 64, 128]: b = Buffer(1024) test_stream = TestStream( 'hello world!\nTest\n\nPiet\nKlaas\nPietBlaat\n', chunk_size=chunk_size) f = BufferedReader(test_stream, b).file() lines = f.readlines() self.assertEquals('hello world!\n', lines.next()) self.assertEquals('Test\n', lines.next()) self.assertEquals('\n', lines.next()) self.assertEquals('Piet\n', lines.next()) self.assertEquals('Klaas\n', lines.next()) self.assertEquals('PietBlaat\n', lines.next()) self.assertEquals('', lines.next()) #line without newline at end of buffer, should report without newline test_stream = TestStream('hello world!\nTest', chunk_size=chunk_size) f = BufferedReader(test_stream, b).file() lines = f.readlines() self.assertEquals('hello world!\n', lines.next()) self.assertEquals('Test', lines.next()) self.assertEquals('', lines.next())
def _get_socket(self, host, port, timeout): new_socket = Socket.connect((host, port)) self._reader = BufferedReader(new_socket, Buffer(1024)) self._writer = BufferedWriter(new_socket, Buffer(1024)) self.file = self._reader.file() return new_socket
def __init__(self, stream, buffer): BufferedReader.__init__(self, stream, buffer) self.stream = stream self.buffer = buffer self.reader = PacketReader(buffer)
def test_stream(s): b = Buffer(buffer_size) f = BufferedReader(TestStream(s, chunk_size=chunk_size), b).file() i = cStringIO.StringIO(s) return (i, f)
def _send(self, identifier, body, timeout=30): try: conn = Socket.connect((self.host, self.port), timeout) try: req = u"POST / HTTP/1.1\r\nHost: %s\r\nContent-length: %d\r\nX-Realplexor: %s=%s%s\r\n\r\n%s" % ( self.host, len(body), (self.identifier or ""), ("%s:%s@" % (self.login, self.password) if self.login else ""), identifier or "", body) if type(req) == unicode: req = req.encode("utf-8") reader = BufferedReader(conn, Buffer(1024)) writer = BufferedWriter(conn, Buffer(1024)) writer.write_bytes(req) writer.flush() conn.socket.shutdown(socket.SHUT_WR) response = "" while True: try: chunk = reader.read_bytes_available() except EOFError: chunk = None if not chunk: break response += chunk if response: m = re_split_headers.split(response, 1) if not m: raise RealplexorError( "Non-HTTP response received:\n%s" % response) headers = m[0] body = m[1] m = re_http_status_line.match(headers) if not m: raise RealplexorError( "Non-HTTP response received:\n%s" % response) status, code = m.group(1, 2) if code != "200": raise RealplexorError("Request failed: %s\n%s" % (status, body)) m = re_content_length.search(headers) if not m: raise RealplexorError( "No Content-Length header in response headers:\n%s" % headers) expected_len = int(m.group(1)) if len(body) != expected_len: raise RealplexorError( "Response length (%d) is different from one specified in the Content-Length header (%d): possibly broken response" % (len(body), expected_len)) return body return response finally: conn.close() except socket.error as e: raise RealplexorSocketError("Socket error: %s" % e) except socket.gaierror as e: raise RealplexorError("DNS error: %s" % e) except socket.timeout as e: raise RealplexorError("Timeout error: %s" % e) except IOError as e: raise RealplexorSocketError("IOError: %s" % e)