Python Websocket-Code doesn't work - need Help

Hello everybody,
I have a python code with which I turn on a light. That works great on the console,
but not when I paste it as code snippets.
Here is my Python code:>

#! / usr / bin / env python3

import asyncio
import websockets

async def hello ():
uri = "ws: //192.168.178.102: 8080"
addr = "00_0_002"
val = 1
payload = "Update GA:" + addr + "=" + str (val)
async with websockets.connect (uri) as websocket:
await websocket.send (payload)
asyncio.get_event_loop (). run_until_complete (hello ())

If I insert that as code snippets and I get the following error message when running:

Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err] Traceback (most recent call last):
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]   File "_ctypes/callbacks.c", line 234, in 'calling callback function'
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]   File "/var/lib/snips/skills/cetax.Spot_On/venv/lib/python3.5/site-packages/hermes_python/ffi/wrappers.py", line 61, in convert_arguments_when_invoking_function
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]     return func(hermes_client, *parsed_args)
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]   File "./action-cetax-Spot_On-cetax.Spot_On.py", line 29, in subscribe_intent_callback
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]     action_wrapper(hermes, intentMessage, conf)
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]   File "./action-cetax-Spot_On-cetax.Spot_On.py", line 34, in action_wrapper
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err]     import websockets
Nov 01 00:48:49 raspberrypi snips-skill-server[12219]: INFO:snips_skill_server_lib::runner            : [cetax-Spot_On-cetax.Spot_On][err] ImportError: No module named 'websockets'

In the skills looks like this:

nano /var/lib/snips/skills/cetax.Spot_On/action-cetax-Spot_On-cetax.Spot_On.py

#! / usr / bin / env python3
# - * - coding: utf-8 - * -

import configparser
from hermes_python.hermes import Hermes
from hermes_python.ffi.utils import MqttOptions
from hermes_python.ontology import *
import io

CONFIGURATION_ENCODING_FORMAT = "utf-8"
CONFIG_INI = "config.ini"

class SnipsConfigParser (configparser.SafeConfigParser):
    def to_dict (self):
        return {section: {option_name: option for option_name, option in self.items (section)} for section in self.sections ()}


def read_configuration_file (configuration_file):
    try:
        with io.open (configuration_file, encoding = CONFIGURATION_ENCODING_FORMAT) as f:
            conf_parser = SnipsConfigParser ()
            conf_parser.readfp (f)
            return conf_parser.to_dict ()
    except (IOError, configparser.Error) as e:
        return dict ()

def subscribe_intent_callback (hermes, intentMessage):
    conf = read_configuration_file (CONFIG_INI)
    action_wrapper (hermes, intentMessage, conf)


def action_wrapper (hermes, intentMessage, conf):
        import asyncio
        import websockets

        async def hello ():
            uri = "ws: //192.168.178.102: 8080"
            addr = "00_0_002"
            val = 1
            payload = "Update GA:" + addr + "=" + str (val)

            async with websockets.connect (uri) as websocket:
                await websocket.send (payload)
        asyncio.get_event_loop (). run_until_complete (hello ())

if __name__ == "__main__":
    mqtt_opts = MqttOptions ()
    with Hermes (mqtt_options = mqtt_opts) as h:
        h.subscribe_intent ("cetax: Spot_On", subscribe_intent_callback) \
         .begin()

I do not understand the two error messages: frowning:
websockets is installed:

pi@raspberrypi:~/TEST $ pip list
Package Version


chardet 2.3.0
cryptography 1.7.1
enum34 1.1.6
future 0.18.2
gevent 1.4.0
greenlet 0.4.15
hermes-python 0.8.1
idna 2.2
keyring 10.1
keyrings.alt 1.3
numpy 1.12.1
picamera 1.13
pip 19.3.1
pyasn1 0.1.9
pycrypto 2.6.1
pygobject 3.22.0
python-apt 1.1.0b5
pyxdg 0.25
requests 2.12.4
SecretStorage 2.3.1
setuptools 33.1.1
six 1.12.0
ssh-import-id 5.6
typing 3.7.4.1
urllib3 1.19.1
websocket 0.2.1
websocket-client 0.56.0
websockets 7.0
wheel 0.29.0

Does any of you have any idea why my Python code calls:

pi @ raspberrypi: ~ / TEST $ python Stube_Table.py

Works, but not with snips?

Many thanks for your help.

greeting
Stefan

I answered you to your German question :wink:

1 Like