我在运行Wheezy的Raspberry Pi上使用Pyro4和Python 2.7
当我用
启动Pyro Nameserver时pyro4-ns &
或
python -m Pyro4.naming &
我的代码按预期工作,没有任何错误。但是,当我使用follow命令
启动Pyro名称服务器守护程序时/etc/init.d/pyro-nsd start
当我启动lamp_daemon.py作为后台进程时
/home/pi/Wakeup-Lamp/lamp_daemon.py &
我收到以下错误
文件" /home/pi/Wakeup-Lamp/lamp_daemon.py" ;,第27行,中
nameServer = Pyro4.locateNS()
文件" /usr/local/lib/python2.7/dist-packages/Pyro4/naming.py",第358行,在locateNS中 提高e
Pyro4.errors.NamingError:找不到名称服务器
lamp_daemon.py代码是
#!/usr/bin/python
from current_lamp_state import CurrentLampState
from lamp_state import LampState
from pwm import Pwm
import Pyro4
import Pyro.core
import Pyro.naming
class LampSwitch(Pyro.core.ObjBase):
__currentLampState = CurrentLampState()
__pwm = Pwm()
def get_lamp_state(self):
return self.__currentLampState.get()
def set_lamp_state(self, new_lamp_state):
self.__currentLampState.set(new_lamp_state)
self.__pwm.update()
lampSwitch = LampSwitch()
daemon = Pyro4.Daemon()
nameServer = Pyro4.locateNS()
uri = daemon.register(lampSwitch)
nameServer.register("lamp.daemon", uri)
daemon.requestLoop()
我已经谷歌了很多但我似乎无法解决问题。有没有人知道我做错了什么?
答案 0 :(得分:1)
如果服务器上的pyro版本与客户端上的pyro版本不同,则无法找到名称服务器,这里有一种检查Pyro版本的方法:
python -c"将Pyro4.constants导入为c;打印(c.PROTOCOL_VERSION)" 强>
如果不一样,请卸载pyro模块并重新安装。
我遇到了类似的问题,当我按照上述步骤修复了这个问题。
答案 1 :(得分:0)
我不确定这是否能彻底解决您的问题,但这是我学会尝试让pyro-nsd
使用python2.7。在这种情况下我使用了Ubuntu 14.04。 Wheezy版本可能有所不同。
sudo apt-get install pyro4
安装,因为pyro4-nsd
未通过pip安装。pyro4-nsc list
未被识别。sudo pip install pyro4
来解析pyro4。pyro4-nsc list
有效,但我收到Failed to locate the nameserver
错误。所以我看了/etc/init.d/pyro4-nsd
的配置,发现了一些有趣的事情。
<强> 1 强>
脚本检查是否安装了python3。如果是,它将使用python3版本的pyro4,它将作为sudo apt-get install pyro4
的依赖项安装。
我在这里使用python2.7
。
现在pyro4-nsc list
确实有效,但我收到此错误:Error: CommunicationError - cannot connect: hmac key config not symmetric
,这会导致数字2
2。
我注意到的另一件事是export PYRO_HMAC_KEY=12345
中的pyro4-nsd
行。
在Pyro4/configuration.py
文件中,似乎这仅用于python3:(https://github.com/delmic/Pyro4/blob/ccea9c2870a1280010bcc56f4146bc1617ec6e8d/src/Pyro4/configuration.py#L81)。请在此处查看此代码段:
if self.HMAC_KEY and sys.version_info>=(3,0):
if type(self.HMAC_KEY) is not bytes:
self.HMAC_KEY=bytes(self.HMAC_KEY, "utf-8") # convert to bytes
所以,基本上我只是删除了PYRO_HMAC_KEY
导出行。
第3 强>
做sudo service pyro4-nsd restart
的小事,在服务停止服务然后启动服务时启动然后停止服务。
以下是修改后的pyro4-nsd文件:
#!/bin/sh
### BEGIN INIT INFO
# Provides: pyro4-nsd
# Required-Start: $time $local_fs $remote_fs $network
# Required-Stop: $time $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Pyro4 name server daemon
# Description: Debian init script for pyro4-nsd (Pyro4 name server daemon)
### END INIT INFO
# -------------------------------------------------------------------------
# <Pyro4 NameServer Daemon Script>
# Copyright (C) <2011> <Pierre PACORY> - ppacory@gmail.com
# Licensed under the "MIT Software License" for inclusion in Pyro4.
# -------------------------------------------------------------------------
LISTEN_ADDRESS=0.0.0.0
LISTEN_PORT=9999
MESSAGEDIR=/var/log/Pyro4
MESSAGELOG=/var/log/Pyro4/NameServer.log
PID=/var/run/Pyro4-NameServer.pid
# Defaults - don't touch, edit /etc/default/pyro-nsd
ENABLED=0
if [ -f /etc/default/pyro4-nsd ] ; then
. /etc/default/pyro4-nsd
fi
if [ "$ENABLED" = "0" ]; then
echo "pyro4-nsd: disabled, see /etc/default/pyro4-nsd"
exit 0
fi
# Add Pyro Config
# here you can add others ...
# NOTE: Comment out PYRO_HMAC_KEY since it appears to be used only for Python3
#export PYRO_HMAC_KEY=12345
export PYRO_LOGFILE="$MESSAGELOG"
export PYRO_LOGLEVEL=DEBUG
. /lib/lsb/init-functions
# Check the script is being run by root user
if [ "$(id -u)" != "0" ]; then
echo 1>&2 "ERROR: The $0 script must be run as root"
exit 1
fi
# Create the PID File
touch $PID
# Detect if Python 2.x or Python 3.y is installed
# NOTE: For the use of python2.7 here
PYTHON=python2.7
[ -x /usr/bin/$PYTHON ] || PYTHON=python
case "$1" in
start)
# create the log directory if not exist
[ ! -d "$MESSAGEDIR" ] && mkdir -p "$MESSAGEDIR"
echo "Starting Pyro4 Name Server"
# test if not already running
if [ ! -f "/proc/$(cat $PID)/exe" ]; then
$PYTHON -m Pyro4.naming -n "$LISTEN_ADDRESS" -p "$LISTEN_PORT" >/dev/null 2>&1 &
echo $!>"$PID"
else
echo "Pyro4 Name Server already running"
fi
;;
stop)
echo "Stopping Pyro4 Name Server"
# test if running
if [ -f "/proc/$(cat $PID)/exe" ]; then
kill -9 "$(cat $PID)"
rm -rf "$PID"
else
echo "Pyro4 Name Server already stopped"
fi
;;
restart)
# Stop, then Start
$0 stop
$0 start
;;
force-reload)
# Stop, then Start
$0 stop
$0 start
;;
*)
echo "usage: $0 {start|stop|restart|force-reload}"
esac
exit 0