Run remote Python script on Gateway

Hi,
I would like to run a Python script on the gateway machine from a button on Perspective client.
I am sending a message to Gateway to fire the script. The gateway event script is:

Blockquote
def handleMessage(payload):
# F3 message handler
logger = system.util.getLogger("F3_log")

# Logic to execute the external Python program
param1 = payload.get("param1")
param2 = payload.get("param2")


command = ["C:\Users\Ross\AppData\Local\Programs\Python\Python311\python.exe", r"C:\Users\Ross\source\repos\PythonApplication1\PythonApplication1.py"]

# Execute the external script
result = system.util.execute(command)  # Remove timeout=5000
            
    
# Process the result as needed   
logger.info("GATEWAY HANDLER _> " + str(param1) + " ...rd f3.... " + str(param2))
logger.info(result)	
logger.info(str(command))

Blockquote

This execute without error and the logger.info works as expected.
But the Python script does not run

Any ideas

I think the recommendation for doing this kind of thing wuld be to configure a Python flask server, and then using that to run remote python scripts.

jrd, see tips on How to post code on this forum.

Also, you don't need to use a message. Perspective button jython scripts run on the gateway anyways.

1 Like

backslashes are escape characters, unless it already got escaped because of the bad formating xd

You're never going to get anything in your results variable as system.util.execute returns nothing, it will always come back as a None - system.util.execute - Ignition User Manual 8.1 - Ignition Documentation

system.util.execute is fire and forget.

You will have to use a different method to get the result out of your python script most likely ProcessBuilder. There's a few examples on the forum of people doing this you can search for.

It's very possible your python script is running now but you don't think it is because your results are None.

Thank you [bkarabinchak.psi]
I am not relying on the return to determine whether the script fired. As you point out the doco explain that system.util.execute returns nothing. I do not require an results. The script reads and writes to a database. It works when fired from Visual Studio, but when called from Ignition the database is not modified

Thank you pturmel
Is this different from Vision?

Hi [Hayden_Watson]
Where do I find this recommendation?

victordcq
I tried various methods to eliminate the backslash issue
Can anyone clarify exactly this should be written, for Windows
Thanks

@jrd, to ping someone or have notification of a reply in their inbox hit the reply button under their post or use the @username syntax. I'm not sure if any of your previous four posts will have generated notifications. You can edit each with the pencil icon link.

1 Like

This is a good read, the flask method is in a section about running python 3 code:

in the docs they escape the escaped backslash

\ => \\

On your second var you used r"" which also works
So that is probably easier if you dont need any other escaped characters

Worth asking to get the simple thing out of the way - this path - this is the exact command you would run on the computer on that the ignition gateway is installed on? -"C:\Users\Ross\AppData\Local\Programs\Python\Python311\python.exe", r"C:\Users\Ross\source\repos\PythonApplication1\PythonApplication1.py"

You're running it on a gateway event script, so it's using the file system of the computer that the Ignition Gateway is installed on for one, and for two, then it would need permissions to the User\Ross folder if it doesn't have it. Perhaps all you need is to make sure the file path exists on the gateway or move it to a location the Ignition gateway has permissions for.

2 Likes

I asume the file is on the gateway, But the rights to the file is indeed something else to look for.

Running an execute with unescaped "\" does not work, so that was definitly one thing wrong.

1 Like