Running a script remotely

733 views
Skip to first unread message

cappy

unread,
May 4, 2011, 1:59:14 AM5/4/11
to rp...@googlegroups.com

Greetings rpycers,

I'm trying to get my head around using rpc, as I'm sure most of you are.

I've gone through the tutorials and I've connected to a remote machine over vpn.
I can write to sys.stdout and see the modules that are loaded, so the connection is working.

Normally, when I'm physically in front of the machine I'm not connected to remotely,
when I want to run a script I simply type python -i myscript.py

When the script finishes, I can examine variables (results, status, etc) in the interactive interpreter.

Now that I've found out about rpyc, I want to run that script remotely.

I've done conn.execute("os.chdir(mypath)" to get to the directory where the script is, to eliminate paths and \\ problems,
and I've verified that I'm in the correct directory via conn.modules.os.getcwd()
(I've omitted the import os step for brevity)

Now when I try to run my script using conn.execute("python -i myscript.py"), this  is returned

conn.execute("python -i myscript.py")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/RPyC-3.1.0-py2.7.egg/rpyc/core/netref.py", line 125, in __call__
    return syncreq(_self, consts.HANDLE_CALL, args, kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/RPyC-3.1.0-py2.7.egg/rpyc/core/netref.py", line 42, in syncreq
    return conn().sync_request(handler, oid, *args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/RPyC-3.1.0-py2.7.egg/rpyc/core/protocol.py", line 347, in sync_request
    raise obj
  File "<string>", line 1
    python -i myscript.py
                 ^
SyntaxError: invalid syntax

I've even tried running the script using conn.execute("myscript.py"), this results in 
NameError: name 'myscript' is not defined

I can see the script by doing a remote glob.glob, so I know I'm in the right place.
  

Tomer Filiba

unread,
May 4, 2011, 2:51:47 AM5/4/11
to rp...@googlegroups.com
conn.execute() is a function that invokes the exec statement remotely. see http://docs.python.org/reference/simple_stmts.html#grammar-token-exec_stmt

instead of 
conn.execute("os.chdir(mypath)")
use
conn.modules.os.chdir(mypath)

instead of 
conn.execute("python -i myscript.py")
use
conn.modules.os.system("python -i myscript.py")
or
conn.modules.subprocess.Popen(["python", "-i", "myscript"])

although you're likely to have problems with -i, since i'm pretty sure it tests for isatty().

conn.execute is mostly only required for defining remote functions or python executing statements (as opposed to functions) such as "print", "if", etc.
for example

>>> conn.execute("""def foo(x, y):
...    return x+y""")
>>> conn.namespace["foo"](4,5)
9



-tomer

An NCO and a Gentleman
Reply all
Reply to author
Forward
0 new messages