我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用sys.stdin.readlines()。
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)
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)))