how to run local python script on remote machine

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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.

回答1:

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



回答2:

You can find a solution for a similar question in here

For my specific problem, which you may also face, I had this local script that I wanted to run at a remote machine using one of its virtual enviroments. I added the following to my bashrc:

rpython (){     cat $1 | ssh user@remote source /home/user/venv/bin/activate \; python - }

This way I can run rpython local_script.py. So here is how it works: cat $1 | pipes the first argument and the ssh user@remote source /home/user/venv/bin/activate \; python - part copies through ssh what was piped, launches the remote machines's virtual enviroment venv and runs local_script.py. Note that everything that local_script.py may eventually print goes back to the local stdout.

The TL;DR here is everything that you write after ssh user@remote runs in the remote. If you want to pass several shell instructions, separate them with \; to not confuse your local shell.

There are other possible solutions involving python modules like Fabric or Plumbum that you may find useful.



回答3:

If you want to execute a python script on a remote machine, the script must reside on the remote machine.

If you have SSH access to the remote machine you need to first copy the file from your local machine to the remote machine:

scp -r /this/file/location/my_script.py user@server:/home/newlocation

Then simply SSH into the remote machine and run the script.

ssh user@server cd /home/newlocation python my_script.py


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!