微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python sys.stdin 模块-readlines() 实例源码

Python sys.stdin 模块,readlines() 实例源码

我们从Python开源项目中,提取了以下2代码示例,用于说明如何使用sys.stdin.readlines()

项目:piku    作者:rcarmo    | 项目源码 | 文件源码
def add_key(public_key_file):
    """Set up a new SSH key (use - for stdin)"""

    def add_helper(key_file):
        if exists(key_file):
            try:
                fingerprint = str(check_output('ssh-keygen -lf ' + key_file, shell=True)).split(' ', 4)[1]
                key = open(key_file, 'r').read().strip()
                echo("Adding key '%s'." % fingerprint, fg='white')
                setup_authorized_keys(fingerprint, realpath(__file__), key)
            except Exception as e:
                echo("Error: invalid public key file '%s': %s" % (key_file, format_exc()), fg='red')
        elif '-' == public_key_file:
            buffer = "".join(stdin.readlines())
            with NamedTemporaryFile(mode="w") as f:
                f.write(buffer)
                f.flush()
                add_helper(f.name)
        else:
            echo("Error: public key file '%s' not found." % key_file, fg='red')

    add_helper(public_key_file)
项目:tellsticknet    作者:molobrakos    | 项目源码 | 文件源码
def parse_stdin():
    """Parse protocol data passed on stdin,prevIoUsly captured

    example to print all captured sensor id:s
    script/listen > /tmp/packets.log
    cat /tmp/packets.log  | ./script/parse | jq ".sensorId" | sort | uniq
    """
    for line in stdin.readlines():
        line = line.strip()
        if " " in line:
            # assume we have date + raw data separated by space
            timestamp, line = line.split(' ', 1)
            timestamp = parse_isoformat(timestamp)
            lastUpdated = int(timestamp.timestamp())
            packet = decode_packet(line)
            if packet is None:
                continue
            packet.update(lastUpdated=lastUpdated, time=timestamp.isoformat())
            print(to_json(packet))
        else:
            print(to_json(decode_packet(line)))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐