本文整理汇总了Python中six.StringIO.tell方法的典型用法代码示例。如果您正苦于以下问题:Python StringIO.tell方法的具体用法?Python StringIO.tell怎么用?Python StringIO.tell使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.StringIO
的用法示例。
在下文中一共展示了StringIO.tell方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_data
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def fetch_data(self):
# create a data frame directly from the full text of
# the response from the returned file-descriptor.
data = self.fetch_url(self.url)
fd = StringIO()
if isinstance(data, str):
fd.write(data)
else:
for chunk in data:
fd.write(chunk)
self.fetch_size = fd.tell()
fd.seek(0)
try:
# see if pandas can parse csv data
frames = read_csv(fd, **self.pandas_kwargs)
frames_hash = hashlib.md5(str(fd.getvalue()).encode('utf-8'))
self.fetch_hash = frames_hash.hexdigest()
except pd.parser.CParserError:
# could not parse the data, raise exception
raise Exception('Error parsing remote CSV data.')
finally:
fd.close()
return frames
示例2: do_POST
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def do_POST(self):
"""Serve a POST request."""
r, info = self.deal_post_data()
print(r, info, "by: ", self.client_address)
f = StringIO()
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>Upload Result Page</title>\n")
f.write("<body>\n<h2>Upload Result Page</h2>\n")
f.write("<hr>\n")
if r:
f.write("<strong>Success:</strong>")
else:
f.write("<strong>Failed:</strong>")
f.write(info)
f.write("<br><a href=\"%s\">back</a>" % self.headers['referer'])
f.write("<hr><small>Powerd By: bones7456, check new version at ")
f.write("<a href=\"http://li2z.cn/?s=SimpleHTTPServerWithUpload\">")
f.write("here</a>.</small></body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
if f:
self.copyfile(f, self.wfile)
f.close()
示例3: sitemap_generator
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def sitemap_generator(request, maps, page, current_site):
output = StringIO()
protocol = request.is_secure() and 'https' or 'http'
xml = SimplerXMLGenerator(output, settings.DEFAULT_CHARSET)
xml.startDocument()
xml.startElement('urlset', {'xmlns':'http://www.sitemaps.org/schemas/sitemap/0.9'})
yield output.getvalue()
pos = output.tell()
for site in maps:
if callable(site):
if issubclass(site, RequestSitemap):
site = site(request=request)
else:
site = site()
elif hasattr(site, 'request'):
site.request = request
for url in site.get_urls(page=page, site=current_site, protocol=protocol):
xml.startElement('url', {})
xml.addQuickElement('loc', url['location'])
try:
if url['lastmod']:
xml.addQuickElement('lastmod', url['lastmod'].strftime('%Y-%m-%d'))
except (KeyError, AttributeError):
pass
try:
if url['changefreq']:
xml.addQuickElement('changefreq', url['changefreq'])
except KeyError:
pass
try:
if url['priority']:
xml.addQuickElement('priority', url['priority'])
except KeyError:
pass
xml.endElement('url')
output.seek(pos)
yield output.read()
pos = output.tell()
xml.endElement('urlset')
xml.endDocument()
output.seek(pos)
last = output.read()
output.close()
yield last
示例4: build_csv_response
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def build_csv_response(self, wb, title="report"):
""" Take a workbook and return a csv file response """
title = generate_filename(title, ".csv")
myfile = StringIO()
sh = wb.get_active_sheet()
c = csv.writer(myfile)
for r in sh.rows:
c.writerow([cell.value for cell in r])
response = HttpResponse(myfile.getvalue(), content_type="text/csv")
response["Content-Disposition"] = "attachment; filename=%s" % title
response["Content-Length"] = myfile.tell()
return response
示例5: build_csv_response
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def build_csv_response(self, wb, title="report"):
""" Take a workbook and return a csv file response """
title = generate_filename(title, '.csv')
myfile = StringIO()
sh = wb.active
c = csv.writer(myfile)
for r in sh.rows:
c.writerow([cell.value for cell in r])
response = HttpResponse(
myfile.getvalue(),
content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=%s' % title
response['Content-Length'] = myfile.tell()
return response
示例6: check
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def check(self, instance):
host = instance.get('host', 'localhost')
port = int(instance.get('port', 2181))
timeout = float(instance.get('timeout', 3.0))
dimensions = self._set_dimensions(
{'component': 'zookeeper', 'service': 'zookeeper'}, instance)
sock = socket.socket()
sock.settimeout(timeout)
buf = StringIO()
chunk_size = 1024
# try-finally and try-except to stay compatible with python 2.4
try:
try:
# Connect to the zk client port and send the stat command
sock.connect((host, port))
sock.sendall(b'stat')
# Read the response into a StringIO buffer
chunk = encodeutils.safe_decode(sock.recv(chunk_size), 'utf-8')
buf.write(chunk)
num_reads = 1
max_reads = 10000
while chunk:
if num_reads > max_reads:
# Safeguard against an infinite loop
raise Exception(
"Read %s bytes before exceeding max reads of %s. " %
(buf.tell(), max_reads))
chunk = encodeutils.safe_decode(sock.recv(chunk_size), 'utf-8')
buf.write(chunk)
num_reads += 1
except socket.timeout:
buf = None
finally:
sock.close()
if buf is not None:
# Parse the response
metrics, new_dimensions = self.parse_stat(buf)
if new_dimensions is not None:
dimensions.update(new_dimensions.copy())
# Write the data
for metric, value in metrics:
self.gauge(metric, value, dimensions=dimensions)
else:
# Reading from the client port timed out, track it as a metric
self.increment('zookeeper.timeouts', dimensions=dimensions)
示例7: list_directory
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def list_directory(self, path):
"""Helper to produce a directory listing (absent index.html).
Return value is either a file object, or None (indicating an
error). In either case, the headers are sent, making the
interface the same as for send_head().
"""
try:
list = os.listdir(path)
except os.error:
self.send_error(404, "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
f = StringIO()
displaypath = cgi.escape(unquote(self.path))
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>Directory listing for %s</title>\n" % displaypath)
f.write("<body>\n<h2>Directory listing for %s</h2>\n" % displaypath)
f.write("<hr>\n")
f.write("<form ENCTYPE=\"multipart/form-data\" method=\"post\">")
f.write("<input name=\"file\" type=\"file\"/>")
f.write("<input type=\"submit\" value=\"upload\"/></form>\n")
f.write("<hr>\n<ul>\n")
for name in list:
fullname = os.path.join(path, name)
displayname = linkname = name
# Append / for directories or @ for symbolic links
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
# Note: a link to a directory displays with @ and links with /
f.write('<li><a href="%s">%s</a>\n'
% (quote(linkname), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-Length", str(length))
self.end_headers()
return f
示例8: FormatterTest
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
class FormatterTest(unittest.TestCase):
def setUp(self):
self.position = 0
self.logger = structuredlog.StructuredLogger(
"test_%s" % type(self).__name__)
self.output_file = StringIO()
self.handler = handlers.StreamHandler(
self.output_file, self.get_formatter())
self.logger.add_handler(self.handler)
def set_position(self, pos=None):
if pos is None:
pos = self.output_file.tell()
self.position = pos
def get_formatter(self):
raise NotImplementedError(
"FormatterTest subclasses must implement get_formatter")
@property
def loglines(self):
self.output_file.seek(self.position)
return [line.rstrip() for line in self.output_file.readlines()]
示例9: BufferedFile
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
#.........这里部分代码省略.........
"""
lines = []
bytes = 0
while True:
line = self.readline()
if len(line) == 0:
break
lines.append(line)
bytes += len(line)
if (sizehint is not None) and (bytes >= sizehint):
break
return lines
def seek(self, offset, whence=0):
"""
Set the file's current position, like stdio's C{fseek}. Not all file
objects support seeking.
@note: If a file is opened in append mode (C{'a'} or C{'a+'}), any seek
operations will be undone at the next write (as the file position
will move back to the end of the file).
@param offset: position to move to within the file, relative to
C{whence}.
@type offset: int
@param whence: type of movement: 0 = absolute; 1 = relative to the
current position; 2 = relative to the end of the file.
@type whence: int
@raise IOError: if the file doesn't support random access.
"""
raise IOError('File does not support seeking.')
def tell(self):
"""
Return the file's current position. This may not be accurate or
useful if the underlying file doesn't support random access, or was
opened in append mode.
@return: file position (in bytes).
@rtype: int
"""
return self._pos
def write(self, data):
"""
Write data to the file. If write buffering is on (C{bufsize} was
specified and non-zero), some or all of the data may not actually be
written yet. (Use L{flush} or L{close} to force buffered data to be
written out.)
@param data: data to write.
@type data: str
"""
if self._closed:
raise IOError('File is closed')
if not (self._flags & self.FLAG_WRITE):
raise IOError('File not open for writing')
if not (self._flags & self.FLAG_BUFFERED):
self._write_all(data)
return
self._wbuffer.write(data)
if self._flags & self.FLAG_LINE_BUFFERED:
# only scan the new data for linefeed, to avoid wasting time.
last_newline_pos = data.rfind('\n')
if last_newline_pos >= 0:
示例10: _flushHeaderMessage
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def _flushHeaderMessage(self, buf, wout, wsz):
"""Write a message for self.HEADERS_CLIENT_TYPE
This method writes a message using the same logic as
THeaderTransport._flushHeaderMessage
but mutates fields included in self._fuzz_fields
"""
cls = self.__class__
transform_data = StringIO()
num_transforms = len(self._THeaderTransport__write_transforms)
for trans_id in self._THeaderTransport__write_transforms:
trans_id = self._get_fuzzy_field('transform_id', trans_id, 'i32')
transform_data.write(getVarint(trans_id))
if self._THeaderTransport__hmac_func:
num_transforms += 1
transform_data.write(getVarint(self.HMAC_TRANSFORM))
transform_data.write(b'\0') # size of hmac, fixup later.
# Add in special flags.
if self._THeaderTransport__identity:
id_version = self._get_fuzzy_field(
'version', self.ID_VERSION, 'str')
self._THeaderTransport__write_headers[self.ID_VERSION_HEADER] = (
id_version)
identity = self._get_fuzzy_field(
'identity', self._THeaderTransport__identity, 'str')
self._THeaderTransport__write_headers[self.IDENTITY_HEADER] = (
identity)
info_data = StringIO()
# Write persistent kv-headers
cls._flush_info_headers(
info_data,
self._THeaderTransport__write_persistent_headers,
self.INFO_PKEYVALUE)
# Write non-persistent kv-headers
cls._flush_info_headers(
info_data,
self._THeaderTransport__write_headers,
self.INFO_KEYVALUE)
header_data = StringIO()
proto_id = self._get_fuzzy_field(
'proto_id', self._THeaderTransport__proto_id, 'i32')
header_data.write(getVarint(proto_id))
num_transforms = self._get_fuzzy_field(
'num_transforms', num_transforms, 'i32')
header_data.write(getVarint(num_transforms))
header_size = (transform_data.tell() +
header_data.tell() +
info_data.tell())
padding_size = 4 - (header_size % 4)
# Fuzz padding size, but do not let total header size exceed 2**16 - 1
padding_size = min((2 ** 16 - 1) - header_size,
self._get_fuzzy_field('padding', padding_size, 'i16'))
header_size = header_size + padding_size
wsz += header_size + 10
self._write_fuzzy_field(buf, 'length', wsz, 'i32')
self._write_fuzzy_field(buf, 'magic', self.HEADER_MAGIC, 'i16')
self._write_fuzzy_field(
buf, 'flags', self._THeaderTransport__flags, 'i16')
self._write_fuzzy_field(
buf, 'seq_id', self._THeaderTransport__seq_id, 'i32')
self._write_fuzzy_field(buf, 'header_size', header_size // 4, 'i16')
buf.write(header_data.getvalue())
buf.write(transform_data.getvalue())
hmac_loc = buf.tell() - 1 # Fixup hmac size later
buf.write(info_data.getvalue())
# Pad out the header with 0x00
if 'padding_bytes' in self._fuzz_fields:
# Print that padding bytes are being fuzzed in favor of printing
# the value of each individual padding byte
self._print("Fuzzing %d padding bytes" % padding_size)
old_verbose, self._verbose = self._verbose, False
for _ in sm.xrange(padding_size):
self._write_fuzzy_field(
buf, 'padding_bytes', six.int2byte(0), 'char')
self._verbose = old_verbose
self._write_fuzzy_field(buf, 'payload', wout, 'str')
# HMAC calculation should always be last.
if self._THeaderTransport__hmac_func:
hmac_data = buf.getvalue()[4:]
hmac = self._THeaderTransport__hmac_func(hmac_data)
# Fill in hmac size.
buf.seek(hmac_loc)
self._write_hmac_size(buf, hmac)
#.........这里部分代码省略.........
示例11: File
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
class File(object):
def __init__(self, name, data=None, type_=None):
self._name = name
self.id = None
self._url = None
self._acl = None
self.current_user = None # TODO
self._metadata = {
'owner': 'unknown'
}
if self.current_user and self.current_user is not None:
self._metadata['owner'] = self.current_user.id
pattern = re.compile('\.([^.]*)$')
extension = pattern.findall(name)
if extension:
self.extension = extension[0].lower()
else:
self.extension = ''
if type_:
self._type = type_
else:
self._type = mime_types.get(self.extension, 'text/plain')
if data is None:
self._source = None
elif isinstance(data, StringIO):
self._source = data
elif isinstance(data, file):
data.seek(0, os.SEEK_SET)
self._source = StringIO(data.read())
elif isinstance(data, buffer):
self._source = StringIO(data)
elif six.PY2:
import cStringIO
if isinstance(data, cStringIO.OutputType):
data.seek(0, os.SEEK_SET)
self._source = StringIO(data.getvalue())
else:
raise TypeError('data must be a StringIO / buffer / file instance')
else:
raise TypeError('data must be a StringIO / buffer / file instance')
if self._source:
self._source.seek(0, os.SEEK_END)
self._metadata['size'] = self._source.tell()
self._source.seek(0, os.SEEK_SET)
@classmethod
def create_with_url(cls, name, url, meta_data=None, type_=None):
f = File(name, None, type_)
if meta_data:
f._metadata.update(meta_data)
f._url = url
f._metadata['__source'] = 'external'
return f
@classmethod
def create_without_data(cls, object_id):
f = File('')
f.id = object_id
return f
def get_acl(self):
return self._acl
def set_acl(self, acl):
if not isinstance(acl, leancloud.ACL):
raise TypeError('acl must be a leancloud.ACL instance')
self._acl = acl
@property
def name(self):
return self._name
@property
def url(self):
return self._url
@property
def size(self):
return self._metadata['size']
@property
def owner_id(self):
return self._metadata['owner']
@property
def metadata(self):
return self._metadata
def get_thumbnail_url(self, width, height, quality=100, scale_to_fit=True, fmt='png'):
if not self._url:
raise ValueError('invalid url')
if width < 0 or height < 0:
raise ValueError('invalid height or width params')
#.........这里部分代码省略.........
示例12: Message
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
class Message(object):
"""
An SSH2 I{Message} is a stream of bytes that encodes some combination of
strings, integers, bools, and infinite-precision integers (known in python
as I{long}s). This class builds or breaks down such a byte stream.
Normally you don't need to deal with anything this low-level, but it's
exposed for people implementing custom extensions, or features that
paramiko doesn't support yet.
"""
def __init__(self, content=None):
"""
Create a new SSH2 Message.
@param content: the byte stream to use as the Message content (passed
in only when decomposing a Message).
@type content: string
"""
if content != None:
self.packet = StringIO(content)
else:
self.packet = StringIO()
def __str__(self):
"""
Return the byte stream content of this Message, as a string.
@return: the contents of this Message.
@rtype: string
"""
return self.packet.getvalue()
def __repr__(self):
"""
Returns a string representation of this object, for debugging.
@rtype: string
"""
return "paramiko.Message(" + repr(self.packet.getvalue()) + ")"
def rewind(self):
"""
Rewind the message to the beginning as if no items had been parsed
out of it yet.
"""
self.packet.seek(0)
def get_remainder(self):
"""
Return the bytes of this Message that haven't already been parsed and
returned.
@return: a string of the bytes not parsed yet.
@rtype: string
"""
position = self.packet.tell()
remainder = self.packet.read()
self.packet.seek(position)
return remainder
def get_so_far(self):
"""
Returns the bytes of this Message that have been parsed and returned.
The string passed into a Message's constructor can be regenerated by
concatenating C{get_so_far} and L{get_remainder}.
@return: a string of the bytes parsed so far.
@rtype: string
"""
position = self.packet.tell()
self.rewind()
return self.packet.read(position)
def get_bytes(self, n):
"""
Return the next C{n} bytes of the Message, without decomposing into
an int, string, etc. Just the raw bytes are returned.
@return: a string of the next C{n} bytes of the Message, or a string
of C{n} zero bytes, if there aren't C{n} bytes remaining.
@rtype: string
"""
b = self.packet.read(n)
if len(b) < n:
return b + "\x00" * (n - len(b))
return b
def get_byte(self):
"""
Return the next byte of the Message, without decomposing it. This
is equivalent to L{get_bytes(1)<get_bytes>}.
@return: the next byte of the Message, or C{'\000'} if there aren't
any bytes remaining.
@rtype: string
"""
return self.get_bytes(1)
def get_boolean(self):
#.........这里部分代码省略.........
示例13: DocStreamReceiver
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
class DocStreamReceiver(ReadBodyProtocol):
"""
A protocol implementation that can parse incoming data from server based
on a line format specified on u1db implementation. Except that we split doc
attributes from content to ease parsing and increment throughput for larger
documents.
[\r\n
{metadata},\r\n
{doc_info},\r\n
{content},\r\n
...
{doc_info},\r\n
{content},\r\n
]
"""
def __init__(self, response, deferred, doc_reader):
self.deferred = deferred
self.status = response.code if response else None
self.message = response.phrase if response else None
self.headers = response.headers if response else {}
self.delimiter = '\r\n'
self.metadata = ''
self._doc_reader = doc_reader
self.reset()
def reset(self):
self._line = 0
self._buffer = StringIO()
self._properly_finished = False
def connectionLost(self, reason):
"""
Deliver the accumulated response bytes to the waiting L{Deferred}, if
the response body has been completely received without error.
"""
if self.deferred.called:
return
try:
if reason.check(ResponseDone):
self.dataBuffer = self.metadata
else:
self.dataBuffer = self.finish()
except errors.BrokenSyncStream as e:
return self.deferred.errback(e)
return ReadBodyProtocol.connectionLost(self, reason)
def consumeBufferLines(self):
"""
Consumes lines from buffer and rewind it, writing remaining data
that didn't formed a line back into buffer.
"""
content = self._buffer.getvalue()[0:self._buffer.tell()]
self._buffer.seek(0)
lines = content.split(self.delimiter)
self._buffer.write(lines.pop(-1))
return lines
def dataReceived(self, data):
"""
Buffer incoming data until a line breaks comes in. We check only
the incoming data for efficiency.
"""
self._buffer.write(data)
if '\n' not in data:
return
lines = self.consumeBufferLines()
while lines:
line, _ = utils.check_and_strip_comma(lines.pop(0))
self.lineReceived(line)
self._line += 1
def lineReceived(self, line):
"""
Protocol implementation.
0: [\r\n
1: {metadata},\r\n
(even): {doc_info},\r\n
(odd): {data},\r\n
(last): ]
"""
if self._properly_finished:
raise errors.BrokenSyncStream("Reading a finished stream")
if ']' == line:
self._properly_finished = True
elif self._line == 0:
if line is not '[':
raise errors.BrokenSyncStream("Invalid start")
elif self._line == 1:
self.metadata = line
if 'error' in self.metadata:
raise errors.BrokenSyncStream("Error from server: %s" % line)
self.total = json.loads(line).get('number_of_changes', -1)
elif (self._line % 2) == 0:
self.current_doc = json.loads(line)
if 'error' in self.current_doc:
raise errors.BrokenSyncStream("Error from server: %s" % line)
else:
d = self._doc_reader(
self.current_doc, line.strip() or None, self.total)
#.........这里部分代码省略.........
示例14: _flushHeaderMessage
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
def _flushHeaderMessage(self, buf, wout, wsz):
"""Write a message for CLIENT_TYPE.HEADER
This method writes a message using the same logic as
THeaderTransport._flushHeaderMessage
but mutates fields included in self._fuzz_fields
"""
transform_data = StringIO()
num_transforms = len(self._THeaderTransport__write_transforms)
for trans_id in self._THeaderTransport__write_transforms:
trans_id = self._get_fuzzy_field('transform_id', trans_id, 'i32')
transform_data.write(getVarint(trans_id))
# Add in special flags.
if self._THeaderTransport__identity:
id_version = self._get_fuzzy_field(
'version', self.ID_VERSION, 'str')
self._THeaderTransport__write_headers[self.ID_VERSION_HEADER] = (
id_version)
identity = self._get_fuzzy_field(
'identity', self._THeaderTransport__identity, 'str')
self._THeaderTransport__write_headers[self.IDENTITY_HEADER] = (
identity)
info_data = StringIO()
# Write persistent kv-headers
_flush_info_headers(
info_data,
self._THeaderTransport__write_persistent_headers,
INFO.PERSISTENT)
# Write non-persistent kv-headers
_flush_info_headers(
info_data,
self._THeaderTransport__write_headers,
INFO.NORMAL)
header_data = StringIO()
proto_id = self._get_fuzzy_field(
'proto_id', self._THeaderTransport__proto_id, 'i32')
header_data.write(getVarint(proto_id))
num_transforms = self._get_fuzzy_field(
'num_transforms', num_transforms, 'i32')
header_data.write(getVarint(num_transforms))
header_size = (transform_data.tell() +
header_data.tell() +
info_data.tell())
padding_size = 4 - (header_size % 4)
# Fuzz padding size, but do not let total header size exceed 2**16 - 1
padding_size = min((2 ** 16 - 1) - header_size,
self._get_fuzzy_field('padding', padding_size, 'i16'))
header_size = header_size + padding_size
wsz += header_size + 10
self._write_fuzzy_field(buf, 'length', wsz, 'i32')
self._write_fuzzy_field(buf, 'magic', HEADER_MAGIC >> 16, 'i16')
self._write_fuzzy_field(
buf, 'flags', self.header_flags(), 'i16')
self._write_fuzzy_field(
buf, 'seq_id', self.seq_id, 'i32')
self._write_fuzzy_field(buf, 'header_size', header_size // 4, 'i16')
buf.write(header_data.getvalue())
buf.write(transform_data.getvalue())
buf.write(info_data.getvalue())
# Pad out the header with 0x00
if 'padding_bytes' in self._fuzz_fields:
# Print that padding bytes are being fuzzed in favor of printing
# the value of each individual padding byte
self._print("Fuzzing %d padding bytes" % padding_size)
old_verbose, self._verbose = self._verbose, False
for _ in sm.xrange(padding_size):
self._write_fuzzy_field(
buf, 'padding_bytes', six.int2byte(0), 'char')
self._verbose = old_verbose
self._write_fuzzy_field(buf, 'payload', wout, 'str')
示例15: SSHSession
# 需要导入模块: from six import StringIO [as 别名]
# 或者: from six.StringIO import tell [as 别名]
class SSHSession(Session):
"Implements a :rfc:`4742` NETCONF session over SSH."
def __init__(self, device_handler):
capabilities = Capabilities(device_handler.get_capabilities())
Session.__init__(self, capabilities)
self._host_keys = paramiko.HostKeys()
self._transport = None
self._connected = False
self._channel = None
self._channel_id = None
self._channel_name = None
self._buffer = StringIO()
# parsing-related, see _parse()
self._device_handler = device_handler
self._parsing_state10 = 0
self._parsing_pos10 = 0
self._parsing_pos11 = 0
self._parsing_state11 = 0
self._expchunksize = 0
self._curchunksize = 0
self._inendpos = 0
self._size_num_list = []
self._message_list = []
def _parse(self):
"Messages ae delimited by MSG_DELIM. The buffer could have grown by a maximum of BUF_SIZE bytes everytime this method is called. Retains state across method calls and if a byte has been read it will not be considered again."
return self._parse10()
def _parse10(self):
"""Messages are delimited by MSG_DELIM. The buffer could have grown by
a maximum of BUF_SIZE bytes everytime this method is called. Retains
state across method calls and if a chunk has been read it will not be
considered again."""
logger.debug("parsing netconf v1.0")
buf = self._buffer
buf.seek(self._parsing_pos10)
if MSG_DELIM in buf.read().decode('UTF-8'):
buf.seek(0)
msg, _, remaining = buf.read().decode('UTF-8').partition(MSG_DELIM)
msg = msg.strip()
if sys.version < '3':
self._dispatch_message(msg.encode())
else:
self._dispatch_message(msg)
# create new buffer which contains remaining of old buffer
self._buffer = StringIO()
self._buffer.write(remaining.encode())
self._parsing_pos10 = 0
if len(remaining) > 0:
# There could be another entire message in the
# buffer, so we should try to parse again.
logger.debug('Trying another round of parsing since there is still data')
self._parse10()
else:
# handle case that MSG_DELIM is split over two chunks
self._parsing_pos10 = buf.tell() - MSG_DELIM_LEN
if self._parsing_pos10 < 0:
self._parsing_pos10 = 0
def _parse11(self):
logger.debug("parsing netconf v1.1")
expchunksize = self._expchunksize
curchunksize = self._curchunksize
idle, instart, inmsg, inbetween, inend = range(5)
state = self._parsing_state11
inendpos = self._inendpos
num_list = self._size_num_list
MAX_STARTCHUNK_SIZE = 12 # \#+4294967295+\n
pre = 'invalid base:1:1 frame'
buf = self._buffer
buf.seek(self._parsing_pos11)
message_list = self._message_list # a message is a list of chunks
chunk_list = [] # a chunk is a list of characters
should_recurse = False
while True:
x = buf.read(1)
if not x:
logger.debug('No more data to read')
# Store the current chunk to the message list
chunk = b''.join(chunk_list)
message_list.append(textify(chunk))
break # done reading
logger.debug('x: %s', x)
if state == idle:
if x == b'\n':
state = instart
inendpos = 1
else:
logger.debug('%s (%s: expect newline)'%(pre, state))
raise Exception
elif state == instart:
if inendpos == 1:
if x == b'#':
inendpos += 1
#.........这里部分代码省略.........