Pyro4:无法找到名称服务器

Pyro4: Failed to locate the nameserver

我对 Python 和 Pyro4 比较陌生。所以我尝试按照此页面的第二个示例 Pyro - Python Remote Objects - 4.41,但是当我 运行 服务器抛出此异常时:

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

代码服务器:

# 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

运行 pyro-ns 在另一个终端首先:

$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.

备注:我在 Debian 8 上工作并且我已经安装了:

到运行这个例子

也许我错过了什么。任何想法为什么这不起作用,或者我做错了什么? 提前致谢。

这对我有用:

运行 python -m Pyro4.naming 首先在另一个终端:

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

而不是 pyro-ns 我之前为 pyro4 做过,因为你看到这个过程发生了变化

我认为您在这里混合了 python 3 和 python 2 版本,因为您写道您必须同时安装 'pyro4' 和 'python2-pyro4' 软件包。 我怀疑前者适用于 python 3,而后者是遗留的 python 2 版本。

'pyro-ns' shell 命令似乎启动了一个旧的、不兼容的名称服务器版本。

虽然文档中的 URI 方法很棒,但另一种连接方法是使用 Pyro4 注册域/IP SimpleServe

编辑: 这是为与 Python 3 一起使用而编写的,感谢@Cyber​​guille 指出应该使用 raw_input 而不是 input 在客户端代码中使用 Python 2.x

服务器

请注意,0.0.0.0 将其公开给全世界

# 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)

然后运行python问候-server.py启动脚本

客户端

# 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