0

Say I have the following open_calc.py script on a machine that's on the network:

import subprocess
subprocess.call(['calc.exe'])

This simple script merely opens the Windows calculator and ends. Then, on a local machine, I have invoke_network_calc.py script that contains:

import imp
lecroyModule = imp.load_source('module', r'\\network_machine\c$\open_calc.py')

If I run invoke_network_calc.py from the local machine, the calulator opens on the local computer. My question is, is there a way to do this so that the calculator is open on the network computer? Or, more generally, is there somthing that can be done so that from that point on, when subprocess.call is called, it starts the process in a different environment? I know this can be done if I simply invoke the network's Python engine with open_calc.py, but is there a way to do this without starting a separate Python process?

EDIT: Perhaps a piece of wishful code will further specify what I'm looking for:

redirector = Redirector('network_machine')
redirector.start() # from here on, subprocesses are executed on the remote machine
lecroyModule = imp.load_source('module', r'\\network_machine\c$\open_calc.py') # calculator will be opened on remote computer
redirector.stop() # from here on, subprocesses are executed normally (on the local machine)
lecroyModule = imp.load_source('module', r'\\network_machine\c$\open_calc.py') # calculator will be opened on local computer

So basically, I'm asking if anyone knows of a way to implement the Redirector class.

asherbret
  • 5,439
  • 4
  • 38
  • 58
  • Usually there is a service running on a remote machine e.g., ssh server that accepts requests from a client e.g., a Python script that runs on your local machine and submits commands via ssh using subprocess, or paramiko, or fabric modules. There could be a specific to Windows command that allows to run commands on a remote computer. Or you could use something like [`execnet`](http://codespeak.net/execnet/) where both server and client are written in Python. – jfs Feb 26 '15 at 18:04
  • @J.F.Sebastian Thanks for your comment. Perhaps my question wasn't clear enough. I'm looking for a way to "redirect" subprocesses to open on a remote machine. Since I may not know what these processes may be, I cannot simply invoke them remotely. I have added some "wishful code" to the question to add clarity. – asherbret Mar 01 '15 at 08:09
  • In what way `execnet` does not work for you? It can run python code (that runs processes that your "may not know") on a remote machine. – jfs Mar 01 '15 at 08:14
  • @J.F.Sebastian I'm looking for a way so that the output (stdout) of the script that is run on the remote machine will be displayed on the local machine. It seems that with `execnet`, the output will be displayed on the remote. – asherbret Mar 01 '15 at 14:21
  • where do you want GUI applications to show their windows? Do you want these processes to accept input from the local keyboard/mouse? – jfs Mar 01 '15 at 14:27
  • I want all subprocesses to be opened on the remote machine (including GUI windows. These processes are not expected to accept any input from the local keyboard/mouse) but any python output (like that printed by the `print` statement) will be displayed on the local machine for monitoring reasons. – asherbret Mar 01 '15 at 19:13
  • [the very first example shows how to send the output back](http://codespeak.net/execnet/example/test_info.html) – jfs Mar 01 '15 at 19:31
  • Yes, but that will require me to change the code on the remote machine, which is code I do not have access to. – asherbret Mar 02 '15 at 08:58
  • no, it won't. If you don't know how to do it, ask – jfs Mar 02 '15 at 09:00
  • I read through the docs and tried a few examples myself, but did not succeed with what I'm trying to do. If you can assist with a small example, that would be very helpful. I appreciate your help! – asherbret Mar 02 '15 at 09:04
  • Have you tried the first example I've linked above? Does it work (unmodified) exactly as described in the docs on your machine? 1. Learn how to redirect the output of a *local* Python script that might run other processes, [example](http://stackoverflow.com/q/4675728/4279). make sure that you can get the output of `open_calc.py` *locally* without modifying `open_calc.py` itself 2. Run that code via execnet on the remote machine, use `channel.send()` to send the output back. – jfs Mar 02 '15 at 09:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72031/discussion-between-user2016436-and-j-f-sebastian). – asherbret Mar 02 '15 at 09:47

0 Answers0