Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run local python script on remote machine

Tags:

python

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.

like image 827
user3305569 Avatar asked Mar 11 '14 08:03

user3305569


People also ask

How do I run a python script locally?

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!

How do I run a python file on a server?

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.


1 Answers

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

like image 180
Mike McKerns Avatar answered Sep 22 '22 11:09

Mike McKerns