# Problem using Paho mqtt from Jsr223 (Jython)

**URL:** <https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792>\
**Category:** Scripts & Rules\
**Tags:** jsr223, scripts, jython\
**Created:** [July 30, 2016, 10:55am UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792 "2016-07-30T10:55:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 10:55am UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/1 "2016-07-30T10:55:15Z")

</div>

I am trying to set up a mqtt client from my jsr223 script. The story behind why is a longer story, which I will omit here.

I have found a sample which work in Python, but when I am calling the same code from my OpenHAB Jsr223 Python script, it thows an exception. I have tried all combinations here below, and none of them seem to work in Jython, where all of them works in Python.

```auto
#Work in Python
pahoClient = paho.Client()

#Work in Python
pahoClient = paho.Client("", True, None, paho.MQTTv31)

#Work in Python
pahoClient = paho.Client("", True, None, paho.MQTTv31, "tcp")

#Work in Python
pahoClient = paho.Client("", True, None, paho.MQTTv311, "tcp")

```

I receive the following exception:

````auto
sys.exc_info()[0]: '<class '_socket.error'>'
sys.exc_info()[1]: '[Errno 92] Protocol not available'```

If I use "websocket" instead of "tcp" it fails in Python. But as far as I remeber I havn't configured that in mosquitto..., 

OpenHAB runs as root. 
Any suggestions?
````

---

<div class="post-metadata">

**Author:** ![steve1](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@steve1](https://community.openhab.org/u/steve1)\
**Post date:** [July 30, 2016, 2:06pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/2 "2016-07-30T14:06:47Z")

</div>

> [@mr\_eskildsen](#):
>
> [Errno 92] Protocol not available

It could be a Jython bug. Doing a quick search for `jython "[Errno 92] Protocol not available"` I found the following reports that mention that type of error.

[https://hg.python.org/jython/rev/b2c98eeaa744](https://hg.python.org/jython/rev/b2c98eeaa744)  
[https://hg.python.org/jython/rev/b3b82ef080a9](https://hg.python.org/jython/rev/b3b82ef080a9)

The good news is that there was a bug fix. The bad news is that the fix was made _after_ the latest release of Jython. However, the somewhat good news is that the fix is in Python code so you can patch it yourself relatively easily based on the information in the bug reports. The file to patch is in your \<python.home\>/Lib directory and it’s called ‘\_socket.py’. I’d try changing just changing the \_realsocket and \_socketobject proto argument default value in the **init** methods. I wouldn’t try to apply the entire patch since it may depend on other patches. Move the \_socket.pyc (compiled file) to a backup file and try running your code again after restarting openHAB. It should compile your patch and use that module.

Another option is to create the MQTT client in the Jython command line interpreter first. This would avoid waiting around for openHAB to restart as you are trying some experiments.

---

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 2:51pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/3 "2016-07-30T14:51:30Z")

</div>

Hmmm. No success. **Might be because of my lack of python knowledge.**  
I changed argument default of proto two places in \_realsocket from None to 0. In \_socketobject it was already 0. I also tried changing it in exported socket class, which didn’t help.

I didn’t find any .pyc files in the directory. Instead the name seems to be: **\_socket$py.class**

But anyway thanks for your help. I will take a closer look, when I have better time. Unless you can see some obvious mistake

---

<div class="post-metadata">

**Author:** ![steve1](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@steve1](https://community.openhab.org/u/steve1)\
**Post date:** [July 30, 2016, 3:23pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/4 "2016-07-30T15:23:44Z")

</div>

Yeah, that’s right. For Jython the compiled files have a “$py.class” suffix. If you see a new \_socket$py.class file that means it was recompiled.

The patch I saw changed the default in \_socketobject from 0 to None (the opposite of \_realsocket).

---

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 3:41pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/5 "2016-07-30T15:41:42Z")

</div>

I have now tried to change it the two places you suggested, but i still get the error, so it might be something else.

Thanks for your suggestion.  
Is it possible to combine the usage of python and javascripts in OpenHAB? That might be a solution for me.

---

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 4:02pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/6 "2016-07-30T16:02:29Z")

</div>

> [@mr\_eskildsen](#):
>
> Is it possible to combine the usage of python and javascripts in OpenHAB?

The answer to this is yes, so I believe that I will go ahead using that approach 🙂

---

<div class="post-metadata">

**Author:** ![steve1](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@steve1](https://community.openhab.org/u/steve1)\
**Post date:** [July 30, 2016, 7:27pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/7 "2016-07-30T19:27:51Z")

</div>

In case you still want to use Jython, I found a workaround for the problem with a small change to the paho/mqtt/client.py file. Typically this is located in the Lib/site-packages directory of the Jython installation but it may be elsewhere if you did a nonstandard installation.

There’s a line that looks like (line 267 in my file)…

```python
    listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)

```

It worked for me to remove the third argument:

```python
    listensock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

```

After that, I was able to connect to an MQTT broker and receive messages. This was the test program:

```python
from paho.mqtt.client import Client

class TestClient(Client):
  def __init__ (self, *args, **kwargs):
    super(TestClient, self). __init__ (*args, **kwargs)
    self.monitor('on_connect')
    self.monitor('on_subscribe')
    self.monitor('on_message')

  def monitor(self, name):
    def dump_info(self, *args):
      print name, args
    setattr(self, name, dump_info)

client = TestClient()
client.connect("broker.hivemq.com")
client.subscribe("ohtest")

client.loop_forever()

```

---

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 7:45pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/8 "2016-07-30T19:45:02Z")

</div>

Cool. That would definately make life a lot easier.

---

<div class="post-metadata">

**Author:** ![mr\_eskildsen](https://community.openhab.org/user_avatar/community.openhab.org/mr_eskildsen/32/37806_2.png) [@mr\_eskildsen](https://community.openhab.org/u/mr_eskildsen)\
**Post date:** [July 30, 2016, 8:02pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/9 "2016-07-30T20:02:49Z")

</div>

It works like a charm.  
**Thanks for your time!**

---

<div class="post-metadata">

**Author:** ![steve1](https://community.openhab.org/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@steve1](https://community.openhab.org/u/steve1)\
**Post date:** [July 30, 2016, 8:31pm UTC](https://community.openhab.org/t/problem-using-paho-mqtt-from-jsr223-jython/12792/10 "2016-07-30T20:31:42Z")

</div>

Great! I’m glad I could help.
