假设我想从不同位置ping某些内容,所以我将ping命令行工具包装到python中并使用pyro4 prc库来调用它。
我有一个python Pyro4名称服务器
import Pyro4
Pyro4.config.COMPRESSION = True
Pyro4.naming.startNSloop("ipofnameserver")
简单的ping服务器:
class Pinger(object):
def ping(self, host):
return subprocess.check_output(["ping", host, "-c 4"])
pinger = Pinger()
daemon = Pyro4.Daemon() # make a Pyro daemon
ns = Pyro4.locateNS(host = "ipofnameservre", port=9090) # find the name server
uri = daemon.register(pinger) # register the greeting object as a Pyro object
print ns.register("location1", uri)
print "Ready. Object uri =", uri # print the uri so we can use it in the client later
daemon.requestLoop()
只要我只有两个ping服务器一切正常,但在我添加第三个名称服务器后停止响应。每个ping服务器当然都有唯一的名称。
例如,我想检查服务器的可用性:
ns = Pyro4.locateNS(host = "nameserverip", port=9090)
names = ns.list().keys()
print names
print ns.list()
for n in names:
if n == 'Pyro.NameServer': continue
proxy = Pyro4.Proxy("PYRONAME:"+n)
try:
print n, proxy._Proxy__pyroCreateConnection()
except:
print "offline"
这适用于两个ping服务器,但有三个它只是等待一些东西。此脚本的回溯以ctrl + C终止:
ns = Pyro4.locateNS(host = "nameserverip", port=9090)
File "/usr/local/lib/python2.7/dist-packages/Pyro4/naming.py", line 319, in locateNS
proxy.ping()
File "/usr/local/lib/python2.7/dist-packages/Pyro4/core.py", line 146, in __call__
return self.__send(self.__name, args, kwargs)
File "/usr/local/lib/python2.7/dist-packages/Pyro4/core.py", line 250, in _pyroInvoke
self.__pyroCreateConnection()
File "/usr/local/lib/python2.7/dist-packages/Pyro4/core.py", line 312, in __pyroCreateConnection
msgType, flags, seq, data = MessageFactory.getMessage(conn, None)
File "/usr/local/lib/python2.7/dist-packages/Pyro4/core.py", line 665, in getMessage
headerdata = connection.recv(cls.HEADERSIZE)
File "/usr/local/lib/python2.7/dist-packages/Pyro4/socketutil.py", line 323, in recv
return receiveData(self.sock, size)
File "/usr/local/lib/python2.7/dist-packages/Pyro4/socketutil.py", line 104, in receiveData
data=sock.recv(size, socket.MSG_WAITALL)
strace显示以下内容:
插座(PF_INET,SOCK_STREAM,IPPROTO_IP)= 3 fcntl(3,F_GETFL)
= 0x2(标志O_RDWR)fcntl(3,F_SETFL,O_RDWR)= 0 connect(3,{sa_family = AF_INET,sin_port = htons(9090), sin_addr = inet_addr(“ipofnameserver”)},16)= 0 setsockopt(3, SOL_SOCKET,SO_KEEPALIVE,[1],4)= 0 recvfrom(3,
以下示例也不起作用,因为它无法将名称解析为pyro_uri,因为它只是等待前面示例中的内容。关于这个例子的有趣的事情,它打印fls,其中包含所有远程ping服务器的名称。然后添加第四个ping服务器我甚至无法打印已注册的ping服务器的名称。
def _ping((host, firing_location)):
pinger = Pyro4.Proxy("PYRONAME:" + firing_location)
return pinger.ping(host)
def ping(host):
ns = Pyro4.locateNS(host = "178.209.52.240", port=9090)
names = ns.list().keys()
fls = []
for name in names:
if name == 'Pyro.NameServer': continue
fls.append(name)
print fls
p = Pool(len(fls))
jobs = p.map(_ping, zip([host]*len(fls), fls) )
for j in jobs:
print j.split("/")[-3], "±", j.split("/")[-1][:-1]
return jobs
我正在苦苦挣扎两天,并且不知道我的代码有什么问题。