python stringIO

StringIO经常被用来作字符串的缓存,因为StringIO的一些接口和文件操作是一致的,也就是说同样的代码,可以同时当成文件操作或者StringIO操作。

一、StringIO中的常用方法

1、read

用法:

s.read([n]):参数n用于限定读取的长度,类型为int,默认为从当前位置读取对象s中所有的数据。读取结束后,位置被移动。

2、readline

用法:

s.readline([length]):length用于限定读取的结束位置,类型为int,缺省为None,即从当前位置读取至下一个以' '为结束符的当前行。读位置被移动。

3、readlines

用法:

s.readlines():读取所有行

4、write

用法:

s.write(s):从读写位置将参数s写入到对象s。参数为str或unicode类型,读写位置被移动。

5、writeline

用法:

s.writeline(s):从读写位置将list写入给对象s。参数list为一个列表,列表的成员为str或unicode类型。读写位置被移动

6、getvalue

用法:

s.getvalue():返回对象s中的所有数据

7、truncate

用法:

s.truncate([size]):从读写位置起切断数据,参数size限定裁剪长度,默认为None

8、tell

用法:

s.tell()  #返回当前读写位置

9、seek

用法:

s.seek(pos[,mode]):移动当前读写位置至pos处,可选参数mode为0时将读写位置移动到pos处,为1时将读写位置从当前位置移动pos个长度,为2时读写位置置于末尾处再向后移动pos个长度。默认为0

10、close

用法:

s.close():释放缓冲区,执行此函数后,数据将被释放,也不可再进行操作。

11、isatty

用法:

s.isatty():此函数总是返回0。不论StringIO对象是否已被close。

12、flush

用法:

s.flush():刷新缓冲区。

二、String使用示例

下面是一个利用StringIO缓冲及paramiko的RSAKey生成密钥对函数:

#!/usr/bin/env python
#coding: utf-8

import StringIO
import os
from paramiko import RSAKey
def gen_keys(key=""):
    """
    生成公钥 私钥
    """

    output = StringIO.StringIO()
    sbuffer = StringIO.StringIO()
    key_content = {}
    #如果私钥不存在,生成一个私钥,并将私钥缓存到output中 
    if not key:
        try:
            key = RSAKey.generate(2048)
            key.write_private_key(output)
            private_key = output.getvalue()
        except IOError:
            raise IOError('gen_keys: there was an error writing to the file')
        except SSHException:
            raise SSHException('gen_keys: the key is invalid')
    #如果私钥存在,直接获取该私钥
    else:
        private_key = key
        output.write(private_key)
        print output.getvalue()
        try:
            key = RSAKey.from_private_key(output)
        except SSHException, e:
            #raise SSHException(e)
            print e
    #利用上面的私钥生成公钥
    for data in [key.get_name(),
                 " ",
                 key.get_base64(),
                 " %s@%s" % ("yap", os.uname()[1])]:
        sbuffer.write(data)
    public_key = sbuffer.getvalue()
    key_content['public_key'] = public_key
    key_content['private_key'] = private_key
    #logger.info('gen_keys:   key content:%s' % key_content)
    return key_content

ga = gen_keys()

print ga
原文地址:https://www.cnblogs.com/wangbaihan/p/9466612.html