本文整理汇总了Python中six.BytesIO.readline方法的典型用法代码示例。如果您正苦于以下问题:Python BytesIO.readline方法的具体用法?Python BytesIO.readline怎么用?Python BytesIO.readline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.BytesIO
的用法示例。
在下文中一共展示了BytesIO.readline方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_read_inventory_v1
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_read_inventory_v1():
f = BytesIO(inventory_v1)
f.readline()
invdata = read_inventory_v1(f, '/util', posixpath.join)
assert invdata['py:module']['module'] == \
('foo', '1.0', '/util/foo.html#module-module', '-')
assert invdata['py:class']['module.cls'] == \
('foo', '1.0', '/util/foo.html#module.cls', '-')
示例2: FakeResponse
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class FakeResponse(object):
"""A fake HTTPResponse object for testing."""
def __init__(self, code, body, headers=None):
self.code = code
self.msg = str(code)
if headers is None:
headers = {}
self.headers = headers
self.info = lambda: self.headers
if isinstance(body, six.text_type):
body = body.encode('utf-8')
self.body_file = BytesIO(body)
def read(self):
"""Read the entire response body."""
return self.body_file.read()
def readline(self):
"""Read a single line from the response body."""
return self.body_file.readline()
def close(self):
"""Close the connection."""
pass
示例3: test_read_inventory_v2
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_read_inventory_v2():
f = BytesIO(inventory_v2)
f.readline()
invdata1 = read_inventory_v2(f, "/util", posixpath.join)
# try again with a small buffer size to test the chunking algorithm
f = BytesIO(inventory_v2)
f.readline()
invdata2 = read_inventory_v2(f, "/util", posixpath.join, bufsize=5)
assert invdata1 == invdata2
assert len(invdata1["py:module"]) == 2
assert invdata1["py:module"]["module1"] == ("foo", "2.0", "/util/foo.html#module-module1", "Long Module desc")
assert invdata1["py:module"]["module2"] == ("foo", "2.0", "/util/foo.html#module-module2", "-")
assert invdata1["py:function"]["module1.func"][2] == "/util/sub/foo.html#module1.func"
assert invdata1["c:function"]["CFunc"][2] == "/util/cfunc.html#CFunc"
assert invdata1["std:term"]["a term"][2] == "/util/glossary.html#term-a-term"
示例4: test_handle_one_request_service_not_found
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_handle_one_request_service_not_found():
service = NoMod()
service.abs_path = '/notme'
handler_class = service.icap_handler_class()
request = MockSocket(req)
client_address = object()
server = object()
handler_class(request, client_address, server)
wfile = BytesIO(request.wfile.value)
assert wfile.readline() == 'ICAP/1.0 404 ICAP Service not found\r\n'
示例5: VCRHTTPResponse
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class VCRHTTPResponse(HTTPResponse):
"""
Stub reponse class that gets returned instead of a HTTPResponse
"""
def __init__(self, recorded_response):
self.recorded_response = recorded_response
self.reason = recorded_response['status']['message']
self.status = self.code = recorded_response['status']['code']
self.version = None
self._content = BytesIO(self.recorded_response['body']['string'])
self._closed = False
headers = self.recorded_response['headers']
self.msg = parse_headers(headers)
self.length = compat.get_header(self.msg, 'content-length') or None
@property
def closed(self):
# in python3, I can't change the value of self.closed. So I'
# twiddling self._closed and using this property to shadow the real
# self.closed from the superclas
return self._closed
def read(self, *args, **kwargs):
return self._content.read(*args, **kwargs)
def readline(self, *args, **kwargs):
return self._content.readline(*args, **kwargs)
def close(self):
self._closed = True
return True
def getcode(self):
return self.status
def isclosed(self):
return self.closed
def info(self):
return parse_headers(self.recorded_response['headers'])
def getheaders(self):
message = parse_headers(self.recorded_response['headers'])
return list(compat.get_header_items(message))
def getheader(self, header, default=None):
values = [v for (k, v) in self.getheaders() if k.lower() == header.lower()]
if values:
return ', '.join(values)
else:
return default
示例6: test_read_inventory_v2
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_read_inventory_v2():
f = BytesIO(inventory_v2)
f.readline()
invdata1 = read_inventory_v2(f, '/util', posixpath.join)
# try again with a small buffer size to test the chunking algorithm
f = BytesIO(inventory_v2)
f.readline()
invdata2 = read_inventory_v2(f, '/util', posixpath.join, bufsize=5)
assert invdata1 == invdata2
assert len(invdata1['py:module']) == 2
assert invdata1['py:module']['module1'] == \
('foo', '2.0', '/util/foo.html#module-module1', 'Long Module desc')
assert invdata1['py:module']['module2'] == \
('foo', '2.0', '/util/foo.html#module-module2', '-')
assert invdata1['py:function']['module1.func'][2] == \
'/util/sub/foo.html#module1.func'
assert invdata1['c:function']['CFunc'][2] == '/util/cfunc.html#CFunc'
assert invdata1['std:term']['a term'][2] == \
'/util/glossary.html#term-a-term'
示例7: RawHTTPRequest
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class RawHTTPRequest(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, raw_request):
if isinstance(raw_request, six.text_type):
raw_request = raw_request.encode("utf-8")
self.rfile = BytesIO(raw_request)
self.raw_requestline = self.rfile.readline()
self.error_code = None
self.error_message = None
self.parse_request()
def send_error(self, code, message):
self.error_code = code
self.error_message = message
示例8: test_handle_one_request
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_handle_one_request():
service = NoMod()
handler_class = service.icap_handler_class()
assert service_abs_path(service) in handler_class.service_map
request = MockSocket(req)
client_address = object()
server = object()
handler_class(request, client_address, server)
wfile = BytesIO(request.wfile.value)
assert wfile.readline() == 'ICAP/1.0 200 OK\r\n'
assert '\nMethods: RESPMOD\r\n' in request.wfile.value
assert '\nEncapsulated: null-body=0\r\n' in request.wfile.value
示例9: test_read_inventory_v1
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
def test_read_inventory_v1():
f = BytesIO(inventory_v1)
f.readline()
invdata = read_inventory_v1(f, "/util", posixpath.join)
assert invdata["py:module"]["module"] == ("foo", "1.0", "/util/foo.html#module-module", "-")
assert invdata["py:class"]["module.cls"] == ("foo", "1.0", "/util/foo.html#module.cls", "-")
示例10: VCRHTTPResponse
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class VCRHTTPResponse(HTTPResponse):
"""
Stub reponse class that gets returned instead of a HTTPResponse
"""
def __init__(self, recorded_response):
self.recorded_response = recorded_response
self.reason = recorded_response['status']['message']
self.status = self.code = recorded_response['status']['code']
self.version = None
self._content = BytesIO(self.recorded_response['body']['string'])
self._closed = False
headers = self.recorded_response['headers']
# Since we are loading a response that has already been serialized, our
# response is no longer chunked. That means we don't want any
# libraries trying to process a chunked response. By removing the
# transfer-encoding: chunked header, this should cause the downstream
# libraries to process this as a non-chunked response.
te_key = [h for h in headers.keys() if h.upper() == 'TRANSFER-ENCODING']
if te_key:
del headers[te_key[0]]
self.headers = self.msg = parse_headers(headers)
self.length = compat.get_header(self.msg, 'content-length') or None
@property
def closed(self):
# in python3, I can't change the value of self.closed. So I'
# twiddling self._closed and using this property to shadow the real
# self.closed from the superclas
return self._closed
def read(self, *args, **kwargs):
return self._content.read(*args, **kwargs)
def readline(self, *args, **kwargs):
return self._content.readline(*args, **kwargs)
def close(self):
self._closed = True
return True
def getcode(self):
return self.status
def isclosed(self):
return self.closed
def info(self):
return parse_headers(self.recorded_response['headers'])
def getheaders(self):
message = parse_headers(self.recorded_response['headers'])
return list(compat.get_header_items(message))
def getheader(self, header, default=None):
values = [v for (k, v) in self.getheaders() if k.lower() == header.lower()]
if values:
return ', '.join(values)
else:
return default
示例11: InputFile
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class InputFile(object):
max_buffer_size = 1024*1024
def __init__(self, rfile, length):
"""File-like object used to provide a seekable view of request body data"""
self._file = rfile
self.length = length
self._file_position = 0
if length > self.max_buffer_size:
self._buf = tempfile.TemporaryFile()
else:
self._buf = BytesIO()
@property
def _buf_position(self):
rv = self._buf.tell()
assert rv <= self._file_position
return rv
def read(self, bytes=-1):
assert self._buf_position <= self._file_position
if bytes < 0:
bytes = self.length - self._buf_position
bytes_remaining = min(bytes, self.length - self._buf_position)
if bytes_remaining == 0:
return b""
if self._buf_position != self._file_position:
buf_bytes = min(bytes_remaining, self._file_position - self._buf_position)
old_data = self._buf.read(buf_bytes)
bytes_remaining -= buf_bytes
else:
old_data = b""
assert bytes_remaining == 0 or self._buf_position == self._file_position, (
"Before reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))
new_data = self._file.read(bytes_remaining)
self._buf.write(new_data)
self._file_position += bytes_remaining
assert bytes_remaining == 0 or self._buf_position == self._file_position, (
"After reading buffer position (%i) didn't match file position (%i)" %
(self._buf_position, self._file_position))
return old_data + new_data
def tell(self):
return self._buf_position
def seek(self, offset):
if offset > self.length or offset < 0:
raise ValueError
if offset <= self._file_position:
self._buf.seek(offset)
else:
self.read(offset - self._file_position)
def readline(self, max_bytes=None):
if max_bytes is None:
max_bytes = self.length - self._buf_position
if self._buf_position < self._file_position:
data = self._buf.readline(max_bytes)
if data.endswith(b"\n") or len(data) == max_bytes:
return data
else:
data = b""
assert self._buf_position == self._file_position
initial_position = self._file_position
found = False
buf = []
max_bytes -= len(data)
while not found:
readahead = self.read(min(2, max_bytes))
max_bytes -= len(readahead)
for i, c in enumerate(readahead):
if c == b"\n"[0]:
buf.append(readahead[:i+1])
found = True
break
if not found:
buf.append(readahead)
if not readahead or not max_bytes:
break
new_data = b"".join(buf)
data += new_data
self.seek(initial_position + len(new_data))
return data
def readlines(self):
rv = []
while True:
data = self.readline()
if data:
#.........这里部分代码省略.........
示例12: TeeInput
# 需要导入模块: from six import BytesIO [as 别名]
# 或者: from six.BytesIO import readline [as 别名]
class TeeInput(object):
CHUNK_SIZE = conn.CHUNK_SIZE
def __init__(self, stream):
self.buf = StringIO()
self.eof = False
if isinstance(stream, six.string_types):
stream = StringIO(stream)
self.tmp = StringIO()
else:
self.tmp = tempfile.TemporaryFile()
self.stream = stream
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, traceback):
return
def seek(self, offset, whence=0):
""" naive implementation of seek """
current_size = self._tmp_size()
diff = 0
if whence == 0:
diff = offset - current_size
elif whence == 2:
diff = (self.tmp.tell() + offset) - current_size
elif whence == 3 and not self.eof:
# we read until the end
while True:
self.tmp.seek(0, 2)
if not self._tee(self.CHUNK_SIZE):
break
if not self.eof and diff > 0:
self._ensure_length(StringIO(), diff)
self.tmp.seek(offset, whence)
def flush(self):
self.tmp.flush()
def read(self, length=-1):
""" read """
if self.eof:
return self.tmp.read(length)
if length < 0:
buf = StringIO()
buf.write(self.tmp.read())
while True:
chunk = self._tee(self.CHUNK_SIZE)
if not chunk:
break
buf.write(chunk)
return buf.getvalue()
else:
dest = StringIO()
diff = self._tmp_size() - self.tmp.tell()
if not diff:
dest.write(self._tee(length))
return self._ensure_length(dest, length)
else:
l = min(diff, length)
dest.write(self.tmp.read(l))
return self._ensure_length(dest, length)
def readline(self, size=-1):
if self.eof:
return self.tmp.readline()
orig_size = self._tmp_size()
if self.tmp.tell() == orig_size:
if not self._tee(self.CHUNK_SIZE):
return ''
self.tmp.seek(orig_size)
# now we can get line
line = self.tmp.readline()
if line.find("\n") >=0:
return line
buf = StringIO()
buf.write(line)
while True:
orig_size = self.tmp.tell()
data = self._tee(self.CHUNK_SIZE)
if not data:
break
self.tmp.seek(orig_size)
buf.write(self.tmp.readline())
if data.find("\n") >= 0:
break
return buf.getvalue()
def readlines(self, sizehint=0):
total = 0
lines = []
line = self.readline()
#.........这里部分代码省略.........