irmen / Pyro5

Pyro 5 - Python remote objects
https://pyro5.readthedocs.io
MIT License
312 stars 36 forks source link

Run simple example using two computers #99

Open wigging opened 2 days ago

wigging commented 2 days ago

I'm submitting this question as an issue because I'm not aware of a Pyro5 forum to ask questions.

Anyway, I'm trying to run the Simple Example where the server is on one computer and the client is on another computer. Both computers are on the same network. Below are the steps I have done to run the example with two computers.

Computer A

On this computer I run pyro5-ns in a terminal. The output from the name server is shown below. This appears to be running on localhost so I guess I need to change that to use it with multiple computers. How do I run this name server so it can be seen by other computers on the network?

Not starting broadcast server for IPv6.
NS running on localhost:9090 (::1)
URI = PYRO:Pyro.NameServer@localhost:9090

In another terminal on this computer, I run the following server code:

import Pyro5.api

@Pyro5.api.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        line1 = f"Hello, {name}! Here is your fortune message:\n"
        line2 = "Tomorrow's lucky number is 12345678."
        return line1 + line2

def main():
    """Run the greeting server."""

    # Make a pyro daemon and register greeting maker object
    daemon = Pyro5.api.Daemon()
    uri = daemon.register(GreetingMaker)

    # Find the name server and register the object with a name
    ns = Pyro5.api.locate_ns()
    ns.register("example.greeting", uri)

    # Start the event loop of the server and wait for calls
    print("Ready.")
    daemon.requestLoop()

if __name__ == "__main__":
    main()

Computer B

On this computer I run the client code shown below. This fails because the name server was not located. How can I locate the name server that is running on Computer A?

import Pyro5.api

def main():
    """Run the greeting client."""
    name = input("What is your name? ").strip()

    # Use name server object lookup to get response
    greeting_maker = Pyro5.api.Proxy("PYRONAME:example.greeting")
    response = greeting_maker.get_fortune(name)
    print(response)

if __name__ == "__main__":
    main()
irmen commented 1 day ago

https://pyro5.readthedocs.io/en/latest/security.html

Pyro binds on localhost by default for security reasons. You have to override that configuration to bind on other network addresses that are visible from outside. For the name server, you can do this via the '-n' command line parameter , you can try -n "" or -n $HOST
Your daemons also need to bind on an externally visible network interface instead of localhost. The manual tells you how to do this https://pyro5.readthedocs.io/en/latest/servercode.html#creating-a-daemon

wigging commented 1 day ago

So let me make sure I understand this... If my host name on Computer A is mymac123, then I need to run the name server as pyro5-ns -n mymac123. And for the server code on Computer A, daemon = Pyro5.api.Daemon(host="mymac123") is also needed. I don't need to change anything in the client code on Computer B. Is there anything else I need to change?

wigging commented 1 day ago

@irmen I made the changes mentioned above and everything works fine. Thanks for the help. Also, can you enable the GitHub Discussions feature for this repository so I don't have to submit questions as issues?

irmen commented 23 hours ago

Nah, I'm not going to. I think it's perfectly fine to use the issue tracker for this. The volume of discourse is extremely low to warrant another channel