problem in using subprocess module - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: problem in using subprocess module (/thread-40782.html) |
problem in using subprocess module - akbarza - Sep-23-2023 hi when i was reading the page https://realpython.com/python-eval-function/#minimizing-the-security-issues-of-eval about eval() in python, i encounterd a problem. in the code (the below code is on the above page): # Run the echo command import subprocess eval("subprocess.getoutput('echo Hello, World')") # Launch Firefox (if available) eval("subprocess.getoutput('firefox')")when I ran it, I took the below : I think the code is used for running Firefox from inside Python. is it true?why did not the code run successfully? I have another question: is it possible to take information about something in Python? For example, suppose I want to know about % usage in Python. when I am in idle , I wrote help(%), and it takes an error as . how can I take information about something in idle?thanks RE: problem in using subprocess module - snippsat - Sep-23-2023 (Sep-23-2023, 12:01 PM)akbarza Wrote: I think the code is used for running Firefox from inside Python. is it true?Yes it will try to run the FireFox binary,but i think you misunderstand how this work. As the Firefox(Browser) is a long running process and may give no output to stdout at all.The eval make no sense here,can only use Python eval if output make any sense for this and are secure for task like this.To give example of that make some sense,so eg when run ping on command line,it give output about request messages to a target Ip. import subprocess response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8') print(response.stdout) So now in subprocess running the ping command and capture is output.Also subprocess.getoutput() is not common used at all.Doc Wrote:Using the subprocess Module RE: problem in using subprocess module - akbarza - Sep-24-2023 (Sep-23-2023, 01:32 PM)snippsat Wrote:(Sep-23-2023, 12:01 PM)akbarza Wrote: I think the code is used for running Firefox from inside Python. is it true?Yes it will try to run the FireFox binary,but i think you misunderstand how this work. hi snippet I am not familiar with the subprocess library and your code: response = subprocess.run(['ping', '-n' ,'4', 'python-forum.io'], capture_output=True, encoding='utf-8')how(where) can I gain more info about the above code? do you have any opinion about my second question? thanks a lot. RE: problem in using subprocess module - akbarza - Sep-24-2023 (Sep-24-2023, 11:09 AM)akbarza Wrote:(Sep-23-2023, 01:32 PM)snippsat Wrote: Yes it will try to run the FireFox binary,but i think you misunderstand how this work. hi I ran your code in idle. the print line without the stdout statement printed messy but with it, the output is clean. when must the stdout statement be used? thanks RE: problem in using subprocess module - deanhystad - Sep-24-2023 stdout is a method, not a statement. This is a statement. print(response.stdout)The statement calls the print function, passing response.stdout as the argument. If you read the documentation for subprocess: https://docs.python.org/3/library/subprocess.html response is a CompletedProcess object. response.stdout is the captured stdout from the child process.Python documentation is pretty good. RE: problem in using subprocess module - snippsat - Sep-24-2023 (Sep-24-2023, 11:09 AM)akbarza Wrote: hi snippetTo explain the basic of this,so ping is a external program ping.exe that you have on your OS Windows .From cmd :C:\>ping -n 4 python-forum.io Pinging python-forum.io [172.67.168.227] with 32 bytes of data: Reply from 172.67.168.227: bytes=32 time=21ms TTL=53 Reply from 172.67.168.227: bytes=32 time=37ms TTL=53 Reply from 172.67.168.227: bytes=32 time=32ms TTL=53 Reply from 172.67.168.227: bytes=32 time=35ms TTL=53 Ping statistics for 172.67.168.227: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 21ms, Maximum = 37ms, Average = 31msSo it's a way to take a external programs into Python,and run it if program have output can also catch that. Like this will just start FireFox from Python. import subprocess subprocess.run(['C:/Program Files (x86)/Mozilla Firefox/firefox.exe'])Look at Doc for eg run(). |