stdin

How to redirect data to a “getpass” like password input?

自古美人都是妖i 提交于 2019-12-06 01:39:32
问题 I'm wring a python script for running some command. Some of those commands require user to input password, I did try to input data in their stdin, but it doesn't work, here is two simple python program represent the problem input.py import getpass print raw_input('text1:') print getpass.getpass('pass1:') print getpass.getpass('pass2:') put_data.py import subprocess import getpass def run(cmd, input=None): stdin=None if input: stdin=subprocess.PIPE p = subprocess.Popen(cmd, shell=True, stdin

How to convert STDIN contents to an array?

左心房为你撑大大i 提交于 2019-12-05 22:55:49
问题 I've got a file INPUT that has the following contents: 123\n 456\n 789 I want to run my script like so: script.rb < INPUT and have it convert the contents of the INPUT file to an array, splitting on the new line character. So, I'd having something like myArray = [123,456,789]. Here's what I've tried to do and am not having much luck: myArray = STDIN.to_s myArray.split(/\n/) puts field.size I'm expecting this to print 3, but I'm getting 15. I'm really confused here. Any pointers? 回答1: You want

python non-blocking non-messing-my-tty key press detection

筅森魡賤 提交于 2019-12-05 22:34:58
I have a loop that does some work and prints a lot of info to stdout. Over and over again (it's a loop...) What I'd like to do is to detect when / if user presses a key (it can be an arrow, enter, or a letter), and do some work when that happens. This should have been a very simple subsubtask, but I've spent last four hours trying different approaches and getting pretty much nowhere. This needs only work in Linux. Best I could get is something like this below. But that works partially, catching the keys only if within the 0.05 sec. import sys,tty,termios class _Getch: def __call__(self, n=1):

How can I tell if STDIN is connected to a terminal in Perl?

◇◆丶佛笑我妖孽 提交于 2019-12-05 21:30:30
问题 How can I tell if STDIN is connected to a terminal in Perl? 回答1: if (-t STDIN) { # stdin is connected } else { # stdin is not connected } I usually use this in conjunction with -t STDOUT, to find out if I'm running from an interactive shell, or from cron, to enable more output. 回答2: You might also be interested in IO::Interactive to figure out if Perl thinks it is interacting with a user. Simply being connected to a tty doesn't mean the user is going to see what you do. 回答3: One solution

difference between communicate() and .stdin.write, .stdout.read or .stderr.read - python

余生长醉 提交于 2019-12-05 21:19:05
I wan to create a pipe between 3 commands: cat = subprocess.Popen("cat /etc/passwd", stdout=subprocess.PIPE) grep = subprocess.Popen("grep '<usernamr>'", stdin=cat.stdout, stdout=subprocess.PIPE) cut = subprocess.Popen("cut -f 3 -d ':'", stdin=grep.stdout, stdout=subprocess.PIPE) for line in cut.stdout: # process each line here But python documentation says: Use communicate() rather than .stdin.write , .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process. then how should I use cut.stdout ? Can someone explain

python cat (echo) equivalent for stdin

我怕爱的太早我们不能终老 提交于 2019-12-05 19:57:09
I thought this program will echo my console input line by line: import os, sys for line in sys.stdin: print line Unfortunately it waits for EOF ( Ctrl + D ) and then it produces output. How should I modify my program to get output line by line? Python 2.x: for line in iter(sys.stdin.readline, ''): print line, Python 3.x: for line in iter(sys.stdin.readline, ''): print(line, end='') See the documentation on iter() with two arguments, it actually has reading from a file like this as one of the examples. Python 2.x: while True: sys.stdout.write(sys.stdin.readline()) Python 3.x: while True: print

How do I avoid processing an empty stdin with python?

非 Y 不嫁゛ 提交于 2019-12-05 19:05:36
The sys.stdin.readline() waits for an EOF (or new line) before returning, so if I have a console input, readline() waits for user input. Instead I want to print help and exit with an error if there is nothing to process, not wait for user input. Reason: I'm looking to write a python program with command line behaviour similar to grep . Test cases: No input and nothing piped, print help $ argparse.py argparse.py - prints arguments echo $? # UNIX echo %ERRORLEVEL% # WINDOWS 2 Command line args parsed $ argparse.py a b c 0 a 1 b 2 c Accept piped commands $ ls | argparse.py 0 argparse.py 1 aFile

How can I check if a CLI program is waiting for input from stdin?

依然范特西╮ 提交于 2019-12-05 18:40:38
How can I check if a CLI program, that I just started with CreateProcess(), is waiting for input from stdin with the Windows C API? as some of the comments above have said, you cannot check if your program is waiting for stdin once it has already started waiting. You could use an event handler or you could simply have a read from stdin with a timeout, where on occurrence of the timeout you flag that you are waiting for input and start waiting with a timeout again. 来源: https://stackoverflow.com/questions/10803122/how-can-i-check-if-a-cli-program-is-waiting-for-input-from-stdin

Continuously exchange data using python-shell

江枫思渺然 提交于 2019-12-05 18:06:57
I need to run some python scripts from node. Since my python scripts use complex structures I figured would be better if I only loaded those structures once and then run some the specific scripts (tasks) using the structures. On node I want to run a script forever (or until I say it can terminate) and keep sending on-demand messages to this script. This script would create a process, using python multiprocessing , to run the specific task and start listening again. A use case would be: Node starts, and wakes python script Some action on the node client causes it to send a command to the python

stdin, stdout and stderr are shared between?

非 Y 不嫁゛ 提交于 2019-12-05 17:56:48
I am trying to understand the behavior of the three streams - stdout , stdin and stderr . I couldn't get the answer from any textbook, so I came here. I know that these three are stored in file descriptor table with file descriptors 0 (stdin), 1 (stdout) and 2 (stderr). I am also aware that these are not merely file descriptors but I/O streams which can be redirected. Ok, so how about sharing? Consider the three cases: When a fork() is called : The child process and parent process shares file descriptors, but do they have the same stdin, stdout and stderr ? When a thread is created : Threads