Running command lines within your Python script - python

So I have a bunch of aliases and Command Line prompt programs, and my main program works by inputting b into the cmd.exe, followed by some filepath names and what not. How would I run those arguments in my python script? So that it mimics the action i am doing in the cmd?

You should use the subprocess module. In particular, subprocess.call will run command line programs for you.

or you can use
import os
os.system('your_command')
for example:
import os
os.system('notepad')
will launch the notepad with the command line behind.
hope this helps

You can do this using subprocess
For example, this call bellow gets the output of the program and stores it as a string, using .call will help with calling it and for more accurate control use .Popen
subprocess.check_output(["ipconfig"])

Check out Sarge - a wrapper for subprocess which aims to make life easier for anyone who needs to interact with external applications from their Python code. and Plumbum - a small yet feature-rich library for shell script-like programs in Python.

Related

Preventing my script from os command injection python

i am using python 2.7.x
I automating my stuffs and in there i need run to another python program from my python script for that i am using the system function from the 'os' library.
for e.g:
import os
os.system("python anotherscript.py --data <USER_INPUT_FROM_MY_SCRIPT_HERE>")
so i know if any user inputs some other command in place of expected user input that will be converting to os command injection and that's what i want prevent in this case.
Thank you.
Since you need to run a Python script from Python, just import it the Python way and invoke the needed function normally
import anotherscript
anotherscript.<function>("<user_input>")
#Tenchi2xh's answer is the better way to do it, but if that doesn't work (e.g. your script only works on Python 2.x and the other one only works on Python 3.x) then you should use the subprocess module, passing the arguments as a list:
import subprocess
subprocess.call(['python', 'anotherscript.py', '--data', '<USER INPUT>'])
Also take a look at subprocess.check_call and subprocess.check_output to see if they are closer to what you need.
https://docs.python.org/2/library/subprocess.html#subprocess.call

Calling to a Sikuli script from Python (Selenium)

While running Selenium tests on a website, I have some Flash elements that I cannot test with Selenium/Python. I wanted to call out for a separate terminal window, run the Sikuli OCR tests, and then back into the Selenium/Python testing. I've not been able to figure this out exactly. I put XXX where I do not know the arguments for a new Terminal to open and run the Sikuli script.
def test_05(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_link_text("Home").click()
driver.find_element_by_id("open_popup").click()
driver.find_element_by_id("screen_name").send_keys("user")
driver.find_element_by_id("password").send_keys("pwd")
driver.find_element_by_id("login_submit").click()
driver.find_element_by_id("button").click()
time.sleep(120)
os.system('XXX')
os.system('./Sikuli/sikuli-script -r test.sikuli')
I am sure there are a couple items wrong here. Any help would be greatly appreciated. I've searched and read what I can find on this already, but can't get it all to work together.
I ran into a similar issue, so I wrote a CPython module for Sikuli. The module is hosted on GitHub and available via pip install sikuli. It's able to access an included Sikuli jar using pyjnius, so you don't have to use Jython or even install Sikuli itself (although I'd recommend it for recording purposes). The module currently covers most of the simpler Sikuli functions, so it should cover a lot of use cases.
After installing, a simple from sikuli import * will get you started, but as a best practice, I'd suggest only importing the functions you want to use. This is particularly important for this module, because sikuli has a type function which overrides Python's own type function.
If your sikuli script is completely independent and you just want to run it for once and then have control back to your python script.
Then you can create a batch file, which calls your sikuli script and call this batch file from your python script instead.
Once the batch file is done running, it exits and returns the control back to your python script.
Sample Batch file:
#echo off
call C:\Sikuli\runIDE.cmd -r C:\Automation\Test1.sikuli
exit
Code snippet to call Sikuli script from inside python:
import subprocess
def runSikuliScript(path):
filepath = path
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
print "Done Running Sikuli"
p = "C:\\Automation\\Test1\\test1.bat"
runSikuliScript(p)
// You can carry on writing your python code from here on
For calling Sikuli code from Selenium, my first choice would be TestAutomationEngr's suggestion of using Java, since Selenium and Sikuli both have native Java bindings.
Since you want to use Python, you should try running Selenium under Jython. It's important to remember that Sikuli is Jython, which is probably why you're not able to import it. (The other reason would be that you don't have it in Jython's module path.) I have not tried this myself, but there was a bug fixed last year in Selenium which indicates that it should be fine under Jython.
Note that if you call your Sikuli code directly from Jython, you need to add
from sikuli.Sikuli import *
to the top. This is because the Sikuli IDE implicitly adds that to all Sikuli code.
Finally, your last resort is to call Sikuli from the command line. There's an FAQ for that. You probably want the "without IDE" version, where you're calling Java and passing in the sikuli-script JAR file.

Inside python code, how do I run a .sh script?

Will it continue the code after it's run? Or will it stop at that line until the script is done?
Using subprocess.call is the easiest way. It will not return until the executed program has terminated. Have a look at the other methods of the subprocess module if you need different behaviour.
import os
os.system('./script.sh')
python script won't stop until sh is finished
You can use os.system or subprocess.Popen or subprocess.call but when using subprocess methods make sure you use shell=True. And executing it via system call in all these methods is blocking. The python script will complete and then go the next step.

Is it possible to use batch scripts in a GUI made with Python?

I was wondering if it was possible to write a GUI in python, and then somewhere in the python script, insert a script switch to temporarily change the language to accomodate for the batch snippet.
I know this can be done in html and vbscript but what about Python?
You can control other processes, written with any language, including bash using the subprocess module.
The subprocess module is the most powerful and complete method for executing other processes. However, there's also a very simple method using the os module: os.system(command) runs command just as if you were to type it into a command line.

Stop invoking of new Shell/cmd prompt , python

i have a python script which should invoke a .exe file to get some result. That .exe file is invoking a new windows command prompt(shell) . i dont need any output from the .exe file.
i 'm using
os.system('segwin.exe args') in my script where segwin is an executable.
now my question is : i need to stop invoking cmd prompt
kudos to all
sag
Try this (untested):
import subprocess
CREATE_NO_WINDOW = 0x08000000
args = [...]
subprocess.check_call(["segwin.exe"] + args, creationflags=CREATE_NO_WINDOW)
Note that check_call checks the return code of the launched subprocess and raises an exception if it's nonzero. If you don't want that, use call instead.
In general, avoid os.system() and use the subprocess module whenever possible. os.system() always starts a shell, which is nonportable unnecessary on most cases.
This is actually specific to Windows. Windows has decided that segwin.exe is a console-based application (uses the Console subsystem from the Windows C interface).
I know how to invoke an prompt for apps that don't necessarily want one, but not the reverse, you could try using this, or this.

Categories