Pyro4: Failed to locate the nameserver
Asked Answered
L

3

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.

Lothair answered 28/12, 2015 at 19:14 Comment(0)
L
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

Lothair answered 28/12, 2015 at 20:26 Comment(2)
Works for me. But what is the reason behind this?Eleusis
@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 of Dfranc3373 is more elegant than my answerLothair
P
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
Plasm answered 29/6, 2018 at 22:15 Comment(2)
Let me clarify that this solution works ok with python3, but with python2 the client have an error File "<string>", line 1, in <module> 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.Lothair
Yes the code above was written with python3 in mind, I'll put a note on the answer as well.Plasm
B
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.

Borzoi answered 2/1, 2016 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.