7

I'm rather new to Python and Pyro4. So I try to follow the second example of this page Pyro - Python Remote Objects - 4.41, but when I run the server throw this exception:

Traceback (most recent call last):
  File "greeting-server.py", line 10, in <module>
    ns = Pyro4.locateNS()                  # find the name server
  File "/usr/lib/python2.7/dist-packages/Pyro4/naming.py", line 344, in locateNS
    raise e
Pyro4.errors.NamingError: Failed to locate the nameserver

Code Server:

# saved as greeting-server.py
import Pyro4

class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Tomorrow's lucky number is 12345678.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
ns = Pyro4.locateNS()                  # find the name server
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object
ns.register("example.greeting", uri)   # register the object with a name in the name server

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

Run pyro-ns in another terminial first:

$pyro-ns
*** Pyro Name Server ***
Name server listening on: ('0.0.0.0', 9090)

WARNING: daemon bound on hostname that resolves to loopback address 127.0.x.x 

URI is: PYRO://127.0.1.1:9090/7f0001011d2a21ca9fb63702dd216e1143
URI written to: /home/guille/Documents/pyro examples/Pyro4-master/examples/banks/Pyro_NS_URI
Name Server started.

Remark: I work on Debian 8 and I've installed:

to run this example

Maybe I missed something. Any ideas why this is not working, or things that I'm doing wrong? thanks in advance.

Community
  • 1
  • 1
Cyberguille
  • 1,552
  • 3
  • 28
  • 58

3 Answers3

10

This work for me:

Run python -m Pyro4.naming in another terminial first:

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

and not pyro-ns I've done before for pyro4 as you see this procedure change

Cyberguille
  • 1,552
  • 3
  • 28
  • 58
  • Works for me. But what is the reason behind this? – alyssaeliyah Oct 14 '18 at 01:19
  • 1
    @Eliyah As you can see in my question you need to run a name server to register your daemon, I fallow the example in the the documentation but in Pyro4 the way to generate the name server changed, and that is that I answered, I suggest you the [answer](https://stackoverflow.com/a/51110040/2399444) of Dfranc3373 is more elegant than my answer – Cyberguille Oct 14 '18 at 02:16
5

While the URI method in the docs is great, another way to connect is register the domain / IP using Pyro4 SimpleServe

Edit: This was written for use with Python 3, thanks to @Cyberguille for pointing out that raw_input should be used instead of input on the client code when using Python 2.x

Server

Note that 0.0.0.0 exposes it to the world

# saved as greeting-server.py
import Pyro4

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)

Pyro4.Daemon.serveSimple({
    GreetingMaker: 'Greeting',
}, host="0.0.0.0", port=9090, ns=False, verbose=True)

Then running python greeting-server.py to start the script

Client

# saved as greeting-client.py
import Pyro4

ipAddressServer = "" # TODO add your server remote IP here

# Works for Python3, see edit above for notes on Python 2.x
name = input("What is your name? ").strip()

greetingMaker = Pyro4.core.Proxy('PYRO:Greeting@' + ipAddressServer + ':9090')
print(greetingMaker.get_fortune(name))   # call method normally
Dfranc3373
  • 2,048
  • 4
  • 30
  • 44
  • 1
    Let me clarify that this solution works ok with python3, but with python2 the client have an error `File "", line 1, in ` `NameError: name 'guille' is not defined` so in python 2 you should use `raw_input` instead of `input`. thanks for your answer, is more elegant than my answer, and I will update my code, thanks a lot. – Cyberguille Jun 29 '18 at 23:08
  • Yes the code above was written with python3 in mind, I'll put a note on the answer as well. – Dfranc3373 Jul 02 '18 at 20:46
2

I think you are mixing python 3 and python 2 versions here, because you wrote you had to install both 'pyro4' and 'python2-pyro4' packages. I suspect the former is for python 3 and the latter is the legacy python 2 version.

The 'pyro-ns' shell command seems to launch an older, incompatible version of the name server.

Irmen de Jong
  • 2,739
  • 1
  • 14
  • 26