Pyro4:找不到名称服务器

时间:2015-12-28 19:14:56

标签: python pyro

我是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上工作,我已安装:

运行此示例

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

3 个答案:

答案 0 :(得分: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做过了,因为你看到这个程序改变了

答案 1 :(得分:2)

尽管docs中的URI方法很棒,但是另一种连接方式是使用Pyro4 SimpleServe

注册域/ IP

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

服务器

请注意,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 greeting-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

答案 2 :(得分:1)

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

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