GithubHelp home page GithubHelp logo

nyamuk's Introduction

Nyamuk

Nyamuk is a python MQTT library, originally based on libmosquitto.
It implements both 3.1 and 3.1.1 versions of MQTT protocol.
Currently only supporting python 2.7

Features

  • MQTT v3.1
  • MQTT v3.1.1
  • SSL
  • Qos 0, 1 & 2 support
  • docstring & documentation
  • python3 support
  •  advanced logging

Install

from sources:

$> python setup.py install

using pypi package:

$> pip install nyamuk

Example

Publishing a message with Qos 1 (with MQTT v3.1.1)

import sys
from nyamuk import *

def nloop(client):
    client.packet_write()     # flush write buffer (messages sent to MQTT server)
    client.loop()             # fill read buffer   (enqueue received messages)
    return client.pop_event() # return 1st received message (dequeued)

client = Nyamuk("test_nyamuk", server="test.mosquitto.org")
ret = client.connect(version=4)
ret = nloop(client) # ret should be EventConnack object
if not isinstance(ret, EventConnack) or ret.ret_code != 0:
    print 'connection failed'; sys.exit(1)

client.publish('foo/bar', 'this is a test', qos=1)
ret = nloop(client) # ret should be EventPuback

client.disconnect()

Subscribing a topic

import sys
from nyamuk import *

def nloop(client):
    client.packet_write()     # flush write buffer (messages sent to MQTT server)
    client.loop()             # fill read buffer   (enqueue received messages)
    return client.pop_event() # return 1st received message (dequeued)

client = Nyamuk("test_nyamuk", server="test.mosquitto.org")
ret = client.connect(version=4)
ret = nloop(client) # ret should be EventConnack object
if not isinstance(ret, EventConnack) or ret.ret_code != 0:
    print 'connection failed'; sys.exit(1)

client.subscribe('foo/bar', qos=1)
ret = nloop(client)
if not isinstance(ret, EventSuback):
    print 'SUBACK not received'; sys.exit(2)
print 'granted qos is', ret.granted_qos[0]

try:
    while True:
        evt = nloop(client)
        if isinstance(evt, EventPublish):
            print 'we received a message: {0} (topic= {1})'.format(evt.msg.payload, evt.msg.topic)
            
            # received message is either qos 0 or 1
            # in case of qos 1, we must send back PUBACK message with same packet-id
            if evt.msg.qos == 1:
                client.puback(evt.msg.mid)

except KeyboardInterrupt:
    pass

client.disconnect()

Authors

Original creator: Iwan B. Kusnanto
Current maintainer: Guillaume Bour

License

Nyamuk is distributed under BSD license

nyamuk's People

Contributors

iwanbk avatar mharj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nyamuk's Issues

ValueError: file descriptor cannot be a negative integer (-1)

ERROR 2016-10-27 11:12:50,669 server 16802 140596291487488 Traceback (most recent call last):
File "/ali/cloudring/systemserver/server.py", line 52, in
ret = client.nloop()
File "/ali/cloudring/utils/emqttd.py", line 78, in nloop
return self.loop()
File "/ali/cloudring/utils/emqttd.py", line 34, in loop
ret = self.client.loop()
File "/ali/cloudring/nyamuk/nyamuk.py", line 56, in loop
to_read, to_write, _ = select.select(rlist, wlist, [], timeout)
ValueError: file descriptor cannot be a negative integer (-1)

Socket error handling which causes a crash

This is a great contribution, thanks for making this available. I have one issue which has popped up.

I have a Python 2.7 program which runs successfully for some time listening to MQTT messages, then it spits and error:

socket.error. Errno = 50 err msg = Network is down
Traceback (most recent call last):
File "plant_mqtt_thingspeak.py", line 87, in
start_nyamuk(server, client_id, topic)
File "plant_mqtt_thingspeak.py", line 76, in start_nyamuk
rc = ny.loop()
File "build/bdist.macosx-10.7-intel/egg/nyamuk/nyamuk.py", line 47, in loop
File "build/bdist.macosx-10.7-intel/egg/nyamuk/nyamuk.py", line 61, in loop_read
File "build/bdist.macosx-10.7-intel/egg/nyamuk/base_nyamuk.py", line 142, in packet_read
TypeError: object of type 'int' has no len()

So the error arises in nyamuk_net.py in function 'read' starting on line 29. The function returns '-1, errno' if there is an error, or 'len, None' if successful. However, in base_nyamuk.py, line 166 where this routine was called, after calling this routine we then check on the length of string returned in line 167. In the case of an error this is not a string, this was an int of value -1. (on line 178 you do check for a string of length 0, but we never get there. Line 167 then generates the error messages seen above. Looks like the code around here needs a little more work.

Have I got this right, or is there something I have missed here?

Cheers
Mark

Logger cleanup

class Nyamuk(base_nyamuk.BaseNyamuk):
    def __del__(self):
        self.logger.handlers=[]

or even delete logger to cleanup logs when deleting class itself.
This is visible when connection fails and make new instance of class which gets old log data and each connection failure adds content to log.

ImportError: cannot import name 'Nyamuk' from 'nyamuk'

Thank you for writing/maintaining this code. I'm brand new (today) to Python. Running in Windows 10 Pro, just downloaded Python 3.7.0. Eventually, I will be running this on a Raspberry PI in a Docker container, but I want to learn a bit of Python first and see if I can communicate with a Mosquitto MQTT server.

I have pip installed nyamuk:
Collecting nyamuk Downloading https://files.pythonhosted.org/packages/de/44/1a25ae956b13341cb2e3d54d1dc3d3cdd3a687f6e226f5f850848133767a/nyamuk-v0.2.0.tar.gz Installing collected packages: nyamuk Running setup.py install for nyamuk ... done Successfully installed nyamuk-0.2.0

When I run the Publish sample it throws
ImportError: cannot import name 'Nyamuk' from 'nyamuk' (C:\Users\Rajinder\AppData\Local\Programs\Python\Python37-32\lib\site-packages\nyamuk\__init__.py)

I'm hoping you can help me out. Thanks very much

in handle_publish message.msg.topic = ba_data.decode('utf8')

Subscriber recive payload head is 0x31 and [ret=2, ba_data=None] while too much publish
error:
File "D:\python_pj\testpy\nyamuk\nyamuk.py", line 401, in handle_publish
message.msg.topic = ba_data.decode('utf8')
AttributeError: 'NoneType' object has no attribute 'decode'

what can i do ?

The test demo on README

I can't run it well when "ret = client.connect(version=4)" .
"ret = client.connect(version=3)" will be OK.

pip install for v0.2.0 fails with "IOError: [Errno 2] No such file or directory: 'README'"

$ pip install https://github.com/iwanbk/nyamuk/archive/v0.2.0.zip
Collecting https://github.com/iwanbk/nyamuk/archive/v0.2.0.zip
Downloading https://github.com/iwanbk/nyamuk/archive/v0.2.0.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    File "/tmp/pip-9ZSylS-build/setup.py", line 4, in <module>
        long_description = open('README').read()
    IOError: [Errno 2] No such file or directory: 'README'

Apparently, the README file was renamed to README.md and setup.py was not updated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.