Read the output of another program with python2019 Community Moderator ElectionRetrieving the output of subprocess.call()Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get time of a Python program's execution?Does Python have a string 'contains' substring method?How to print the full traceback without halting the program?Why is reading lines from stdin much slower in C++ than Python?
Maths symbols and unicode-math input inside siunitx commands
Have the tides ever turned twice on any open problem?
Can you move over difficult terrain with only 5 feet of movement?
gerund and noun applications
HP P840 HDD RAID 5 many strange drive failures
Does the attack bonus from a Masterwork weapon stack with the attack bonus from Masterwork ammunition?
Light propagating through a sound wave
Is it insecure to send a password in a `curl` command?
What are substitutions for coconut in curry?
Usage and meaning of "up" in "...worth at least a thousand pounds up in London"
Can a wizard cast a spell during their first turn of combat if they initiated combat by releasing a readied spell?
Worshiping one God at a time?
Matrix using tikz package
Do I need to be arrogant to get ahead?
Optimising a list searching algorithm
What favor did Moody owe Dumbledore?
Turning a hard to access nut?
Print a physical multiplication table
Calculate the frequency of characters in a string
Probably overheated black color SMD pads
Unfrosted light bulb
Bash - pair each line of file
What is the term when voters “dishonestly” choose something that they do not want to choose?
Knife as defense against stray dogs
Read the output of another program with python
2019 Community Moderator ElectionRetrieving the output of subprocess.call()Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How do I get time of a Python program's execution?Does Python have a string 'contains' substring method?How to print the full traceback without halting the program?Why is reading lines from stdin much slower in C++ than Python?
So lets say I open CMD or PowerShell and enter
ping google.com -t
I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?
Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)
Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in
Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.
python python-3.x cmd
add a comment |
So lets say I open CMD or PowerShell and enter
ping google.com -t
I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?
Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)
Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in
Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.
python python-3.x cmd
1
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
What operating system?
– MarkReedZ
Mar 7 at 17:13
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16
add a comment |
So lets say I open CMD or PowerShell and enter
ping google.com -t
I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?
Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)
Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in
Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.
python python-3.x cmd
So lets say I open CMD or PowerShell and enter
ping google.com -t
I will get a constant stream of pings. (I could obviously also write a script that gets pings within python but that is not the problem, I just want to know how to do this in theory.) What i want to do now is write a python script that gets those outputs from the CMD and prints them out, live. Is this even possible?
Example: I am running a server which prints logs into a command line and I want to read and save them (i know the save part, just the retrieve is my problem)
Example2: I am running my monero wallet (not the gui, the command line version) and want a script that notifies me when money is sent in
Summed up: an external program of which the UI is a Command line or whose logs are printed into CMD or similar programs is running, and i want to get all the things this program prints out.
python python-3.x cmd
python python-3.x cmd
edited Mar 7 at 17:12
Maritn Ge
asked Mar 7 at 16:11
Maritn GeMaritn Ge
168
168
1
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
What operating system?
– MarkReedZ
Mar 7 at 17:13
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16
add a comment |
1
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
What operating system?
– MarkReedZ
Mar 7 at 17:13
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16
1
1
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
What operating system?
– MarkReedZ
Mar 7 at 17:13
What operating system?
– MarkReedZ
Mar 7 at 17:13
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16
add a comment |
1 Answer
1
active
oldest
votes
On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).
import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )
You need to use sudo with strace
sudo python grab_output.py
Output with ping google is:
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82
So you'd have to clean it up a bit.
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048245%2fread-the-output-of-another-program-with-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).
import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )
You need to use sudo with strace
sudo python grab_output.py
Output with ping google is:
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82
So you'd have to clean it up a bit.
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
add a comment |
On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).
import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )
You need to use sudo with strace
sudo python grab_output.py
Output with ping google is:
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82
So you'd have to clean it up a bit.
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
add a comment |
On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).
import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )
You need to use sudo with strace
sudo python grab_output.py
Output with ping google is:
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82
So you'd have to clean it up a bit.
On linux this is the simplest way. 9590 is the pid of the process and -s says write out 500 characters from each output line (otherwise it truncates).
import os
stream = os.popen("strace -ewrite -p 9590 -s 500")
while 1:
print( stream.readline() )
You need to use sudo with strace
sudo python grab_output.py
Output with ping google is:
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=125 ttl=44 time=40.3 msn", 82) = 82
write(1, "64 bytes from 108.177.122.138 (108.177.122.138): icmp_seq=126 ttl=44 time=32.5 msn", 82) = 82
So you'd have to clean it up a bit.
edited Mar 7 at 17:15
answered Mar 7 at 16:53
MarkReedZMarkReedZ
1,0097
1,0097
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
add a comment |
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
Is the following what you want or are you asking to run the ping outside of python and attach to that process's output?
– MarkReedZ
Mar 7 at 16:56
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
the latter i think. run ping in cmd without python and the tell python to read the output of that command line
– Maritn Ge
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
I've updated the answer. What you are asking to do is "Use python to attach to the stdout of a running process". Using a debugger like gdb would also work.
– MarkReedZ
Mar 7 at 17:18
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
Perfect, thanks. Is there a similar way to do it on Windows? I need both but I think linux will suffice for now. Anyway, thank you!
– Maritn Ge
Mar 7 at 17:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55048245%2fread-the-output-of-another-program-with-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Possible duplicate of Retrieving the output of subprocess.call()
– SiHa
Mar 7 at 16:55
@SiHa as far as i understand it I need to call the program from within python for it to work. If possible I just want to enter the PID from task manager and read the output of an already running program which is running in the command line (like the command line monero wallet) or something like that
– Maritn Ge
Mar 7 at 17:04
What operating system?
– MarkReedZ
Mar 7 at 17:13
@MarkReedZ Windows for now, I will later on also need Ubuntu, but I think once I know how it works on one I can figure the rest out (if you know linux though you can also tell me)
– Maritn Ge
Mar 7 at 17:16