2

I'm using Pyro4 with Python 2.7 on a Raspberry Pi running Wheezy

When I start the Pyro Nameserver with either

pyro4-ns &

or

python -m Pyro4.naming &

My code works as expected without any errors. However When I start the Pyro name server daemon with the follow command

/etc/init.d/pyro-nsd start

when I start the lamp_daemon.py as a background process

/home/pi/Wakeup-Lamp/lamp_daemon.py &

I get the following error

File "/home/pi/Wakeup-Lamp/lamp_daemon.py", line 27, in
nameServer = Pyro4.locateNS()
File "/usr/local/lib/python2.7/dist-packages/Pyro4/naming.py", line 358, in locateNS
raise e
Pyro4.errors.NamingError: Failed to locate the nameserver

The lamp_daemon.py code is

#!/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()

I've google quite a bit but I can't seem to resolve the problem. Does anyone have any clues on what I'm doing wrong?

TheLukeMcCarthy
  • 2,253
  • 2
  • 25
  • 34
  • Maybe try running this command to see if the nameserver is running: `pyro4-nsc list` – Gohn67 Oct 21 '14 at 21:31
  • Interesting, `/etc/init.d/pyro-nsd` status gives the result, Server process 3110 is running. However `pyro4-nsc list` gives the result, Failed to locate the name server: Failed to locate the nameserver – TheLukeMcCarthy Oct 21 '14 at 21:38
  • I haven't used the pyro-nsd yet. But I'd check the contents of that file to see how it is initializing the name server. – Gohn67 Oct 21 '14 at 21:56
  • I think a possible issue is because the pyro-nsd script checks if python3 is installed. If it python3 is installed, it will use the python3 version of pyro4. – Gohn67 Oct 21 '14 at 22:31

2 Answers2

1

If the pyro version on the server is different than that on client, the it fails to find the name server, here is a way to check the Pyro version:

python -c "import Pyro4.constants as c; print(c.PROTOCOL_VERSION)"

If it is not the same, then please uninstall the pyro module and reinstall it.

I faced a similar issue which was fixed when I followed the above steps.

abhijitjaiswal
  • 139
  • 1
  • 8
0

I'm not sure if this will completely solve your problem, but here is what I learned trying to get pyro-nsd working with python2.7. I used Ubuntu 14.04 in this case. It may be different on the Wheezy version.

  1. I installed using sudo apt-get install pyro4 since pyro4-nsd is not installed via pip.
  2. First thing I notice is that pyro4-nsc list was not recognized.
  3. So then I pyro4 using sudo pip install pyro4.
  4. Now pyro4-nsc list works, but I get the Failed to locate the nameserver error.

So I took a look at the configuration for /etc/init.d/pyro4-nsd and found some interesting things.

1.

The script checks if python3 is installed. If it is, it will use the python3 version of pyro4, which gets installed as a dependency with sudo apt-get install pyro4.

Here I just make it use python2.7.

Now pyro4-nsc list actually works, but I get this error: Error: CommunicationError - cannot connect: hmac key config not symmetric, which leads to number 2

2.

Next thing I notice is the export PYRO_HMAC_KEY=12345 line in the pyro4-nsd.

In the Pyro4/configuration.py file, it seems this is only used for python3: (https://github.com/delmic/Pyro4/blob/ccea9c2870a1280010bcc56f4146bc1617ec6e8d/src/Pyro4/configuration.py#L81). See this snippet here:

    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

So, basically I just removed the PYRO_HMAC_KEY export line.

3.

Minor thing but doing sudo service pyro4-nsd restart, starts and then stops the service when it should stop the service and then start it.

Here is the modified pyro4-nsd file:

#!/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
Gohn67
  • 10,608
  • 2
  • 29
  • 35