0

So imagine two computers that I have full control on both, I will call them computer A and computer B.

On computer A there is a python script that is running in the background and has multiple functions, one of them being able to create and show a message box: (this is just an example)

def msg_box(text,title):
    MessageBox = ctypes.windll.user32.MessageBoxW
    MessageBox(None, text, title, 0)

What I want to do, is to access the functions of the script running on computer A from computer B, in the case of the msg_box function, I want to be able to call it on the computer B with any parameters that I want, and it will get executed on computer A. I am a beginner and I don't know how to do this link between the computers, I remind you that I have full control over both of them and they are connected to my local network. Some have suggested me using a ssh server, can someone give me some ideas ?

4
  • You can use a socket and communicate. Commented Aug 23, 2018 at 0:24
  • Can you give me some tips on how to use sockets for this particular task ? Just for start, I'll go and research more after Commented Aug 23, 2018 at 0:25
  • You can look up something like how to make a simple chat server/client with Python. Instead of chatting, you'll just be sending commands. Commented Aug 23, 2018 at 0:27
  • That's a great idea, thanks, I'll look into that and see what I can do Commented Aug 23, 2018 at 0:27

1 Answer 1

0

This sounds like a job for the Pyro4 package. Here's an example based on their "Simple Example", with some extra code to start the nameserver automatically and listen over a network.

First use this command on each machine to install Pyro4:

pip install pyro4

On the server, save this script as server.py and then run it in a terminal window via python server.py:

# saved as server.py
import Pyro4, Pyro4.naming
import socket, threading

# Define an object that will be accessible over the network.
# This is where all your code should go...
@Pyro4.expose
class MessageServer(object):
    def show_message(self, msg):
        print("Message received: {}".format(msg))


# Start a Pyro nameserver and daemon (server process) that are accessible
# over the network. This has security risks; see 
# https://pyro4.readthedocs.io/en/stable/security.html
hostname = socket.gethostname()
ns_thread = threading.Thread(
    target=Pyro4.naming.startNSloop, kwargs={'host': hostname}
)
ns_thread.daemon = True   # automatically exit when main program finishes
ns_thread.start()
main_daemon = Pyro4.Daemon(host=hostname)

# find the name server
ns = Pyro4.locateNS()
# register the message server as a Pyro object
main_daemon_uri = main_daemon.register(MessageServer)
# register a name for the object in the name server
ns.register("example.message", main_daemon_uri)

# start the event loop of the main_daemon to wait for calls
print("Message server ready.")
main_daemon.requestLoop()

On the client, save this as client.py and run it with python client.py:

# saved as client.py
import Pyro4
import sys

print("What is your message?")
msg = sys.stdin.readline().strip()

# lookup object uri on name server and create a proxy for it
message_server = Pyro4.Proxy("PYRONAME:example.message")
# call method on remote object
message_server.show_message(msg)

Note that setting up Pyro to listen over your network is a security risk. So you should read their section on security before you go ahead with this. But this should be enough to get you started.

If you want a solution that just uses the standard library, you could take a look at my socket-based client-server setup in a different answer. Or you might consider setting up a flask webserver on the server, and then using urllib2 on the client to access the server and invoke the correct action (probably via a GET or POST request). But those are both more difficult than this.

Another option would be to use a different interprocess communication package such as PyZMQ, redis, mpi4py or maybe zmq_object_exchanger. See this question for some ideas.

Sign up to request clarification or add additional context in comments.

3 Comments

you're linking to a deprecated location, the correct new location of Pyro4's documentation is here: pyro4.readthedocs.io
Thanks, fixed it!
almost :) the 'simple example' and the 'chapter on security' links still point to the old url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.