Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python automation
#1
i have a python file which when run opens a new python shell of its own.then i need to run commands in the python shell.collect results ,parse it and conclude it is pass or fail.
how i can accomplish this.appreciate any help/guidance on this

thanks
Reply
#2
If the code is coming from an untrusted source (ie: anyone on the internet can send random python code for you to run), you should run that code in a way that won't jeopardize the system it's running on. Either within a docker container, a chroot jail, a virtual machine you can just restart after the code's run to undo whatever changes it made, etc.

As to how you'd actually run it, compile() is a builtin function that compiles a string into a code object/ast, which you can then pass to exec().

Just make sure you pass empty dicts to exec() for globals() and locals...
>>> code = '''
... for i in range(2):
...     code = 'print("new code")'
...     print(i)
... '''
>>> block = compile(code, '<string>', 'exec')
>>> block
<code object <module> at 0x000001C7A2AC4A80, file "<string>", line 2>
>>> code
'\nfor i in range(2):\n    code = \'print("new code")\'\n    print(i)\n'
>>> exec(block)
0
1
>>> code
'print("new code")'
>>> # !!! the code body was overwritten by the dynamic code
>>> code = '''
... for i in range(2):
...     code = 'print("new code")'
...     print(i)
... '''
>>> # for a little more protection, pass empty globals and locals
>>> exec(block, {}, {})
0
1
>>> code
'\nfor i in range(2):\n    code = \'print("new code")\'\n    print(i)\n'
Reply
#3
Hi,

thanks for the reply
let me explain in a more clear way

I have a file abc.py.
From abc.py i run xyz.py file

abc.py:-
process = subprocess.Popen("python C:\\path\\xyz.py", stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd="C:\\path")
outputfull = process.communicate()
output = outputfull[0]+outputfull[1]

the file xyz.py opens and runs in a python shell.
now from abc.py:- I need to send commands to python shell,collect the output of the command from python shell
How to do it please guide on this
Reply
#4
Let's say I have two files, spam.py and eggs.py.

Here's spam.py:
import sys

for line in sys.stdin:
    print(line.upper())
And here's eggs.py:
import subprocess

process = subprocess.Popen("python ./spam.py", stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE, stdin=subprocess.PIPE)
output, errors = process.communicate(b'testing')
print(output)
Running eggs.py gives this output:
Output:
> python eggs.py b'TESTING\r\n'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Automation of GUI by python function dghosal 4 818 Aug-18-2023, 10:23 AM
Last Post: dghosal
  home automation using python barryjo 1 1,186 Jul-24-2022, 09:09 PM
Last Post: Larz60+
  Python Help, New User, Computer Automation hexagonalyeti 2 1,651 Jun-25-2021, 11:43 AM
Last Post: hexagonalyeti
  Automation using python Santhosh_Sangar 0 1,599 Mar-11-2020, 01:04 AM
Last Post: Santhosh_Sangar
  loop in pyautogui (python automation GUI application) pyprogrammer 0 4,782 Feb-12-2020, 02:52 PM
Last Post: pyprogrammer
  Python Based Data QA Automation tool suggestion Sonia567 1 1,989 Nov-19-2019, 04:46 PM
Last Post: Larz60+
  Create hybrid automation framework using python. janeho 2 2,423 Mar-20-2019, 05:27 PM
Last Post: DeaD_EyE
  Python automation GET/POST/REQUEST Raki 1 3,083 Sep-22-2017, 06:24 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020