I have a python script on my local machine.Is there any way to run this script on remote machine.I mean python script should on the local machine but execution should happen on remote machine and get the output back to the local machine.
To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World! If everything works okay, after you press Enter , you'll see the phrase Hello World!
Open the file and add the necessary code. NOTE: The file should start with the path to the Python scripts that is /usr/bin/python on our servers, but you can run the whereis python command via SSH to check the directory. To save the changes, click Crtl+O and press Enter for Windows or Command+O for Mac OS.
The pathos
package has tools that make it easy to interact with remote machines, all directly from python… and you can also easily capture stdout
or other piped responses and return them to your calling script.
So, let's say you have a local script hello.py
that looks like this:
# 'hello.py'
import os
print os.system('hostname')
They you can push the script and execute it like this:
>>> import pathos
>>> c = pathos.core.copy('hello.py', destination='guido.remote.com:~/hello.py')
>>> s = pathos.core.execute('python hello.py', host='guido.remote.com')
>>> print s.response()
guido
0
>>> s.pid()
37429
There's also ssh-tunneling, setting up daemon processes, remote port pickling, and dealing with killing remote processes… if you need that stuff.
pathos
provides an abstraction on remote commands, the default being ssh
and scp
… but it's easy to see what it's doing.
>>> s.message
'ssh -q guido.remote.com "python hello.py"'
>>> c.message
'scp -q -r hello.py guido.remote.com:~/hello.py'
Get pathos
here: https://github.com/uqfoundation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With