diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..1de8e726 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,50 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' +--- + +# Prerequisites + +*Note: You may remove this section prior to submitting your report.* + +A small team of volunteers monitors issues. Please help us to help you by making it simple to understand and, if possible, +replicate your issue. Prior to reporting a bug please: + + - [ ] Test the latest release of the library. + - [ ] Search existing issues. + - [ ] Read the relevant documentation. + - [ ] Review your server configuration and logs. + - [ ] Consider testing against a different server (e.g. [mqtt.eclipseprojects.io](https://mqtt.eclipseprojects.io/) or [test.mosquitto.org](https://test.mosquitto.org/)) + - [ ] If possible, test using another tool (e.g. [MQTTX](https://mqttx.app/) / [mosquitto_sub](https://mosquitto.org/man/mosquitto_sub-1.html)) + to confirm the issue is specific to this client. + - [ ] If you are unsure if you have found a bug, please consider asking on [stackoverflow](https://stackoverflow.com/) for a quicker response. + +# Bug Description + +*Please provide a clear and concise description of the bug.* + +# Reproduction + +*Please provide detailed steps showing how to replicate the issue (it's difficult to fix an issue we cannot replicate). +If errors are output then include the full error (including any stack trace).* +*Most issues should include a [minimal example](https://stackoverflow.com/help/minimal-reproducible-example) that +demonstrates the issue (ideally one that can be run without modification, i.e. runnable code using a public broker).* + +# Environment + +* Python version: +* Library version: +* Operating system (including version): +* MQTT server (name, version, configuration, hosting details): + +# Logs + +For many issues, especially when you cannot provide code to replicate the issue, it's helpful to include logs. Please +consider including: + * library logs; see [the readme](https://github.com/eclipse/paho.mqtt.python#enable_logger) and [logger example](https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client_logger.py). + * broker logs (availability will depend the server in use) + + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..77339316 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,37 @@ +--- +name: Feature request +about: Suggest an idea for this project +title: '' +labels: '' +assignees: '' +--- + +# Prerequisites + +*Note: You may remove this section prior to submitting your report.* + +A small team of volunteers monitors issues, so it's important that we can quickly understand what you are requesting, and +why it would be of benefit. Prior to requesting a feature, please: + +- [ ] Search existing issues (the feature may have been requested previously). +- [ ] Read the relevant documentation. +- [ ] Consider the impact your feature/idea might have on other users of the library (both positive and negative). +- [ ] Decide if you are able to implement the feature yourself (please do raise an issue before submitting a pull request). + +# Feature Description + +*Please provide a clear and concise description of the feature you are requesting. Include details of the benefits you +believe the feature will deliver (i.e. problems it will solve, user groups it will help etc).* + +# Requested Solution + +*Describe how you would like this feature delivered (for example, if adding functionality, show mock code using the new +feature)* + +# Alternatives + +*Have you considered alternative ways of accomplishing your goal? If so please provide details.* + +# Additional Information + +*Is there any other information you can provide that would help us understand your idea?* diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md new file mode 100644 index 00000000..039ce8b8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/question.md @@ -0,0 +1,43 @@ +--- +name: Question +about: Ask a question +title: '' +labels: '' +assignees: '' +--- + +# Prerequisites + +*Note: You may remove this section prior to submitting your question.* + +A small team of volunteers monitors issues; this is the same team that develops the library. Whilst we are keen to help, +there are often better places to ask questions, including: + +- [Stack Overflow](https://stackoverflow.com/questions/tagged/mqtt) - well written questions are generally answered +within a day. Please use the tags MQTT, Python and Paho. +- [Reddit](https://www.reddit.com/r/MQTT/) - great for questions requiring discussion. +- [MQTT Google Group](https://groups.google.com/g/mqtt) - fairly quiet but questions about the protocol are generally answered quickly. +- [Eclipse paho-dev mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev) - for general discussion about the paho project. + +Prior to asking a question here, please: + +- [ ] Search the resources mentioned above (it's likely someone else has asked the same question) +- [ ] Read the [readme](https://github.com/eclipse/paho.mqtt.python/blob/master/README.rst) (especially the +"Known limitations" section) and look at the [examples](https://github.com/eclipse/paho.mqtt.python/tree/master/examples). +- [ ] Search through the [project issues](https://github.com/eclipse/paho.mqtt.python/issues). +- [ ] Confirm that you are using the latest release of the library. +- [ ] Ensure your question is specific to this project; consider using another tool (e.g. [MQTTX](https://mqttx.app/) / [mosquitto_sub](https://mosquitto.org/man/mosquitto_sub-1.html)) +to test your assumptions. + +# Question + +*Please clearly and concisely state your question.* + + +# Environment +*It's often helpful for us to know how you are using the library so please provide:* + +* Python version: +* Library version: +* Operating system (including version): +* MQTT server (name, version, configuration, hosting details): \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 00000000..d3240c20 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,21 @@ +name: build +on: + pull_request: + branches: [master] + push: + branches: [master] +jobs: + build: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 + with: + python-version: 3 + - run: pip install build + - run: python -m build . + - uses: actions/upload-artifact@v4 + with: + name: dist + path: dist diff --git a/.github/workflows/label-issue.yml b/.github/workflows/label-issue.yml index 47320bbd..4fafaff5 100644 --- a/.github/workflows/label-issue.yml +++ b/.github/workflows/label-issue.yml @@ -12,6 +12,7 @@ on: jobs: label_issues: runs-on: ubuntu-latest + timeout-minutes: 5 permissions: issues: write steps: diff --git a/.github/workflows/lint-python.yml b/.github/workflows/lint-python.yml index 548038f1..afbb69ab 100644 --- a/.github/workflows/lint-python.yml +++ b/.github/workflows/lint-python.yml @@ -1,21 +1,15 @@ name: lint_python -on: [pull_request, push] +on: + pull_request: + branches: [master] + push: + branches: [master] jobs: lint_python: runs-on: ubuntu-latest + timeout-minutes: 5 steps: - uses: actions/checkout@v2 - uses: actions/setup-python@v2 - - run: pip install bandit black codespell flake8 isort mypy pytest pyupgrade safety - - run: bandit --recursive --skip B101,B105,B106,B110,B303,B404,B603 . - - run: black --check . || true - - run: codespell || true # --ignore-words-list="" --skip="" - - run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - - run: flake8 . --count --exit-zero --max-complexity=29 --max-line-length=167 --show-source --statistics - - run: isort --check-only --profile black . - - run: pip install -e . - - run: mypy --ignore-missing-imports . || true - - run: mv setup.cfg setup.cfg.disabled - - run: pytest . - - run: shopt -s globstar && pyupgrade --py36-plus **/*.py || true - - run: safety check + - run: pip install tox + - run: tox -e lint diff --git a/.github/workflows/precommit.yml b/.github/workflows/precommit.yml new file mode 100644 index 00000000..6cb979ac --- /dev/null +++ b/.github/workflows/precommit.yml @@ -0,0 +1,19 @@ +# https://pre-commit.com +# This GitHub Action assumes that the repo contains a valid .pre-commit-config.yaml file. +# Using pre-commit.ci is even better that using GitHub Actions for pre-commit. +name: pre-commit +on: + pull_request: + branches: [master] + push: + branches: [master] +jobs: + pre-commit: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: 3.x + - uses: pre-commit/action@v3.0.0 diff --git a/.github/workflows/tox.yml b/.github/workflows/tox.yml index 37186bf5..fa939797 100644 --- a/.github/workflows/tox.yml +++ b/.github/workflows/tox.yml @@ -1,24 +1,34 @@ name: tox -on: [push, pull_request] +on: + pull_request: + branches: [master] + push: + branches: [master] jobs: tox: strategy: fail-fast: false max-parallel: 4 matrix: - python: [3.6, 3.7, 3.8, 3.9] + python: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] runs-on: ubuntu-latest + timeout-minutes: 5 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 + - uses: actions/checkout@v4 + - uses: actions/checkout@v4 + with: + repository: eclipse/paho.mqtt.testing + ref: a4dc694010217b291ee78ee13a6d1db812f9babd + path: paho.mqtt.testing + - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} + cache: pip + cache-dependency-path: | + tox.ini + setup.py - run: pip install tox - - if: matrix.python == '3.6' - run: TOXENV=py36 tox - - if: matrix.python == '3.7' - run: TOXENV=py37 tox - - if: matrix.python == '3.8' - run: TOXENV=py38 tox - - if: matrix.python == '3.9' - run: TOXENV=py39 tox + - run: tox -e py + - uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore index 84665a27..6bb6bc11 100644 --- a/.gitignore +++ b/.gitignore @@ -45,9 +45,9 @@ nosetests.xml coverage.xml *,cover .hypothesis/ -test/ssl/demoCA -test/ssl/rootCA -test/ssl/signingCA +tests/ssl/demoCA +tests/ssl/rootCA +tests/ssl/signingCA *.csr # Translations diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..f4b9137d --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,37 @@ +# Learn more about this config here: https://pre-commit.com/ + +# To enable these pre-commit hooks run: +# `brew install pre-commit` or `python3 -m pip install pre-commit` +# Then in the project root directory run `pre-commit install` + +# default_language_version: +# python: python3 +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-builtin-literals + # - id: check-executables-have-shebangs + # - id: check-shebang-scripts-are-executable + - id: check-toml + - id: check-xml + - id: check-yaml + # - id: detect-private-key + # - id: end-of-file-fixer + # - id: mixed-line-ending + # - id: trailing-whitespace + + - repo: https://github.com/crate-ci/typos + rev: v1.17.0 + hooks: + - id: typos + + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.1.9 + hooks: + - id: ruff # See pyproject.toml for args + + - repo: https://github.com/abravalheri/validate-pyproject + rev: v0.15 + hooks: + - id: validate-pyproject diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 00000000..faa735b3 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,93 @@ +# Community Code of Conduct + +**Version 2.0 +January 1, 2023** + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as community members, contributors, Committers[^1], and Project Leads (collectively "Contributors") pledge to make participation in our projects and our community a harassment-free and inclusive experience for everyone. + +This Community Code of Conduct ("Code") outlines our behavior expectations as members of our community in all Eclipse Foundation activities, both offline and online. It is not intended to govern scenarios or behaviors outside of the scope of Eclipse Foundation activities. Nor is it intended to replace or supersede the protections offered to all our community members under the law. Please follow both the spirit and letter of this Code and encourage other Contributors to follow these principles into our work. Failure to read or acknowledge this Code does not excuse a Contributor from compliance with the Code. + +## Our Standards + +Examples of behavior that contribute to creating a positive and professional environment include: + +- Using welcoming and inclusive language; +- Actively encouraging all voices; +- Helping others bring their perspectives and listening actively. If you find yourself dominating a discussion, it is especially important to encourage other voices to join in; +- Being respectful of differing viewpoints and experiences; +- Gracefully accepting constructive criticism; +- Focusing on what is best for the community; +- Showing empathy towards other community members; +- Being direct but professional; and +- Leading by example by holding yourself and others accountable + +Examples of unacceptable behavior by Contributors include: + +- The use of sexualized language or imagery; +- Unwelcome sexual attention or advances; +- Trolling, insulting/derogatory comments, and personal or political attacks; +- Public or private harassment, repeated harassment; +- Publishing others' private information, such as a physical or electronic address, without explicit permission; +- Violent threats or language directed against another person; +- Sexist, racist, or otherwise discriminatory jokes and language; +- Posting sexually explicit or violent material; +- Sharing private content, such as emails sent privately or non-publicly, or unlogged forums such as IRC channel history; +- Personal insults, especially those using racist or sexist terms; +- Excessive or unnecessary profanity; +- Advocating for, or encouraging, any of the above behavior; and +- Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +With the support of the Eclipse Foundation employees, consultants, officers, and directors (collectively, the "Staff"), Committers, and Project Leads, the Eclipse Foundation Conduct Committee (the "Conduct Committee") is responsible for clarifying the standards of acceptable behavior. The Conduct Committee takes appropriate and fair corrective action in response to any instances of unacceptable behavior. + +## Scope + +This Code applies within all Project, Working Group, and Interest Group spaces and communication channels of the Eclipse Foundation (collectively, "Eclipse spaces"), within any Eclipse-organized event or meeting, and in public spaces when an individual is representing an Eclipse Foundation Project, Working Group, Interest Group, or their communities. Examples of representing a Project or community include posting via an official social media account, personal accounts, or acting as an appointed representative at an online or offline event. Representation of Projects, Working Groups, and Interest Groups may be further defined and clarified by Committers, Project Leads, or the Staff. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the Conduct Committee via conduct@eclipse-foundation.org. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. Without the explicit consent of the reporter, the Conduct Committee is obligated to maintain confidentiality with regard to the reporter of an incident. The Conduct Committee is further obligated to ensure that the respondent is provided with sufficient information about the complaint to reply. If such details cannot be provided while maintaining confidentiality, the Conduct Committee will take the respondent‘s inability to provide a defense into account in its deliberations and decisions. Further details of enforcement guidelines may be posted separately. + +Staff, Committers and Project Leads have the right to report, remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code, or to block temporarily or permanently any Contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. Any such actions will be reported to the Conduct Committee for transparency and record keeping. + +Any Staff (including officers and directors of the Eclipse Foundation), Committers, Project Leads, or Conduct Committee members who are the subject of a complaint to the Conduct Committee will be recused from the process of resolving any such complaint. + +## Responsibility + +The responsibility for administering this Code rests with the Conduct Committee, with oversight by the Executive Director and the Board of Directors. For additional information on the Conduct Committee and its process, please write to . + +## Investigation of Potential Code Violations + +All conflict is not bad as a healthy debate may sometimes be necessary to push us to do our best. It is, however, unacceptable to be disrespectful or offensive, or violate this Code. If you see someone engaging in objectionable behavior violating this Code, we encourage you to address the behavior directly with those involved. If for some reason, you are unable to resolve the matter or feel uncomfortable doing so, or if the behavior is threatening or harassing, please report it following the procedure laid out below. + +Reports should be directed to . It is the Conduct Committee’s role to receive and address reported violations of this Code and to ensure a fair and speedy resolution. + +The Eclipse Foundation takes all reports of potential Code violations seriously and is committed to confidentiality and a full investigation of all allegations. The identity of the reporter will be omitted from the details of the report supplied to the accused. Contributors who are being investigated for a potential Code violation will have an opportunity to be heard prior to any final determination. Those found to have violated the Code can seek reconsideration of the violation and disciplinary action decisions. Every effort will be made to have all matters disposed of within 60 days of the receipt of the complaint. + +## Actions +Contributors who do not follow this Code in good faith may face temporary or permanent repercussions as determined by the Conduct Committee. + +This Code does not address all conduct. It works in conjunction with our [Communication Channel Guidelines](https://www.eclipse.org/org/documents/communication-channel-guidelines/), [Social Media Guidelines](https://www.eclipse.org/org/documents/social_media_guidelines.php), [Bylaws](https://www.eclipse.org/org/documents/eclipse-foundation-be-bylaws-en.pdf), and [Internal Rules](https://www.eclipse.org/org/documents/ef-be-internal-rules.pdf) which set out additional protections for, and obligations of, all contributors. The Foundation has additional policies that provide further guidance on other matters. + +It’s impossible to spell out every possible scenario that might be deemed a violation of this Code. Instead, we rely on one another’s good judgment to uphold a high standard of integrity within all Eclipse Spaces. Sometimes, identifying the right thing to do isn’t an easy call. In such a scenario, raise the issue as early as possible. + +## No Retaliation + +The Eclipse community relies upon and values the help of Contributors who identify potential problems that may need to be addressed within an Eclipse Space. Any retaliation against a Contributor who raises an issue honestly is a violation of this Code. That a Contributor has raised a concern honestly or participated in an investigation, cannot be the basis for any adverse action, including threats, harassment, or discrimination. If you work with someone who has raised a concern or provided information in an investigation, you should continue to treat the person with courtesy and respect. If you believe someone has retaliated against you, report the matter as described by this Code. Honest reporting does not mean that you have to be right when you raise a concern; you just have to believe that the information you are providing is accurate. + +False reporting, especially when intended to retaliate or exclude, is itself a violation of this Code and will not be accepted or tolerated. + +Everyone is encouraged to ask questions about this Code. Your feedback is welcome, and you will get a response within three business days. Write to . + +## Amendments + +The Eclipse Foundation Board of Directors may amend this Code from time to time and may vary the procedures it sets out where appropriate in a particular case. + +### Attribution + +This Code was inspired by the [Contributor Covenant](https://www.contributor-covenant.org/), version 1.4, available [here](https://www.contributor-covenant.org/version/1/4/code-of-conduct/). + +[^1]: Capitalized terms used herein without definition shall have the meanings assigned to them in the Bylaws. \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 468cef87..3b0632c1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,7 +28,7 @@ Please read the [Eclipse Foundation policy on accepting contributions via Git](h cherry-picked to the release branch. The only changes that goes directly to the release branch (``1.4``, - ``1.5``, ...) are bug fixe that does not apply to ``master`` (e.g. because + ``1.5``, ...) are bug fixes that does not apply to ``master`` (e.g. because there are fixed on master by a refactoring, or any other huge change we do not want to cherry-pick to the release branch). 4. Create a new branch from the latest ```master``` branch @@ -82,3 +82,35 @@ Be sure to search for existing bugs before you create another one. Remember that contributions are always welcome! - [Create new Paho bug](https://github.com/eclipse/paho.mqtt.python/issues) + + +## Committer resources: + +Making a release +---------------- + +The process to make a release is the following: +* Using a virtual env with the following tool installed: `pip install build sphinx twine` +* In that same virtual env, install paho itself (required for docs): `pip install -e .` +* Update the Changelog with the release version and date. Ensure it's up-to-date with latest fixes & PRs merged. +* Make sure test pass, check that Github actions are green. +* Check that documentation build (`cd docs; make clean html`) +* Bump the version number in ``paho/mqtt/__init__.py``, commit the change. +* Make a dry-run of build: + * Build release: ``python -m build .`` + * Check with twine for common errors: ``python -m twine check dist/*`` + * Try uploading it to testpypi: ``python3 -m twine upload --repository testpypi dist/*`` +* Do a GPG signed tag (assuming your GPG is correctly configured, it's ``git tag -s -m "Version 1.2.3" v1.2.3``) +* Push the commit and it's tag to Github +* Make sure your git is clean, especially the ``dist/`` folder. +* Build a release: ``python -m build .`` +* You can also get the latest build from Github action. It should be identical to your local build: + https://github.com/eclipse/paho.mqtt.python/actions/workflows/build.yml?query=branch%3Amaster +* Then upload the dist file, you can follow instruction on https://packaging.python.org/en/latest/tutorials/packaging-projects/#uploading-the-distribution-archives + It should mostly be ``python -m twine upload dist/*`` +* Create a release on Github, copy-pasting the release note from Changelog. +* Build and publish the documentation + * To build the documentation, run `make clean html` in `docs` folder + * Copy `_build/html/` to https://github.com/eclipse/paho-website/tree/master/files/paho.mqtt.python/html +* Announce the release on the Mailing list. +* To allow installing from a git clone, update the version in ``paho/mqtt/__init__.py`` to next number WITH .dev0 (example ``1.2.3.dev0``) diff --git a/ChangeLog.txt b/ChangeLog.txt index ab57c1a8..61ecc4d7 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,13 +1,84 @@ +v2.1.0 - 2024-04-29 +=================== + +- Make transition from 1.x to 2.x version smoother (Closes #831) +- Fix "protocol" property (Closes #820) +- Fix publish() a bytearray payload (Closes #833) +- Fix some type annotations (Closes #828) +- Fix loop_stop() not stopping thread when called from callback (Closes #809) +- Fix some documentation errors (Closes #817, #823, #832, #838) +- Add support for Unix socket (Closes #829) +- Fix flaky test (Closes #789) + + +v2.0.0 - 2024-02-10 +=================== + +This release include breaking change. See `migrations `_ for more details on how to upgrade. + +- **BREAKING** Added callback_api_version. This break *ALL* users of paho-mqtt Client class. + See docs/migrations.rst or `online version `_ for details on how to upgrade. + tl; dr; add CallbackAPIVersion.VERSION1 to first argument of Client() +- **BREAKING** Drop support for Python 2.7, Python 3.5 and Python 3.6 + Minimum tested version is Python 3.7 + Python version up to Python 3.12 are tested. +- **BREAKING** connect_srv changed it signature to take an additional bind_port parameter. + This is a breaking change, but in previous version connect_srv was broken anyway. + Closes #493. +- **BREAKING** Remove some deprecated argument and method: + + * ``max_packets`` argument in loop(), loop_write() and loop_forever() is removed + * ``force`` argument in loop_stop() is removed + * method ``message_retry_set()`` is removed +- **BREAKING** Remove the base62, WebsocketWrapper and ConnectionState, as user shouldn't directly use them. +- Possible breaking change: Add properties to access most Client attribute. Closes #764. + Since this add new properties like `logger`, if a sub-class defined `logger`, the two `logger` + will conflict. +- Add version 2 of user-callback which allow to access MQTTv5 reason code & properties that were + missing from on_publish callback. Also it's more consistent in parameter order or between + MQTTv3 and MQTTv5. +- Add types to Client class, which caused few change which should be compatible. + Known risk of breaking changes: + + - Use enum for returned error code (like MQTT_ERR_SUCCESS). It use an IntEnum + which should be a drop-in replacement. Excepted if someone is doing "rc is 0" instead of "rc == 0". + - reason in on_connect callback when using MQTTv5 is now always a ReasonCode object. It used to possibly be + an integer with the value 132. + - MQTTMessage field "dup" and "retain" used to be integer with value 0 and 1. They are now boolean. +- Add support for ALPN protocols on TLS connection. Closes #790 & #648. +- Add on_pre_connect() callback, which is called immediately before a + connection attempt is made. +- Fix subscribe.simple with MQTTv5. Closes #707. +- Use better name for thread started by loop_start. Closes #617. +- Fix possible bug during disconnection where self._sock is unexpectedly None. Closes #686 & #505. +- Fix loading too weak TLS CA file but setting allowed ciphers before loading CA. Closes #676. +- Allow to manually ack QoS > 0 messages. Closes #753 & #348. +- Improve tests & linters. Modernize build (drop setup.py, use pyproject.toml) +- Fix is_connected property to correctly return False when connection is lost + and loop_start/loop_forever isn't used. Closes #525. +- Fix wait_for_publish that could hang with QoS == 0 message on reconnection + or publish during connection. Closes #549. +- Correctly mark connection as broken on SSL error and don't crash loop_forever. + Closes #750. +- Fix handling of MQTT v5.0 PUBREL messages with remaining length not equal to + 2. Closes #696. +- Raise error on ``subscribe()`` when `topic` is an empty list. Closes #690. +- Raise error on `publish.multiple()` when ``msgs`` is an empty list. Closes #684. +- Don't add port to Host: header for websockets connections when the port if the default port. Closes #666. + + v1.6.1 - 2021-10-21 =================== -- Fix Python 2.7 compatilibity. +- Fix Python 2.7 compatibility. v1.6.0 - 2021-10-20 =================== - Changed default TLS version to 1.2 instead of 1.0. +- MQTT connection attempts now use a timeout of 5 seconds rather than the + configured keepalive interval - Fix incoming MQTT v5 messages with overall property length > 127 bytes being incorrectly decoded. Closes #541. - MQTTMessageInfo.wait_for_publish() and MQTTMessageInfo.is_published() will @@ -52,7 +123,7 @@ v1.5.1 - 2020-09-22 =================== - Exceptions that occur in callbacks are no longer suppressed by default. They - can optionally be suppressed by setting `client.suppress_exceptions = True`. + can optionally be suppressed by setting ``client.suppress_exceptions = True``. Closes #365. - Fix PUBREL remaining length of > 2 not being accepted for MQTT v5 message flows. Closes #481. @@ -194,7 +265,7 @@ v1.2 - 2016-06-03 - Allow ^C to interrupt client loop. - Fix for keepalive=0 causing an infinite disconnect/reconnect loop. Closes #42. -- Modify callbacks definition/structure to allow classical inheritence. Closes +- Modify callbacks definition/structure to allow classical inheritance. Closes #53, #54. - Add websockets support. - Default MQTT version is again changed to v3.1.1. diff --git a/MANIFEST.in b/MANIFEST.in deleted file mode 100644 index 2e8e0978..00000000 --- a/MANIFEST.in +++ /dev/null @@ -1,10 +0,0 @@ -include edl-v10 epl-v10 -include README.rst -include CONTRIBUTING.md -include setup.py -include notice.html -include LICENSE.txt -include about.html - -recursive-include src *.py -recursive-include examples *.py diff --git a/Makefile b/Makefile index 16cc9179..dbd1e4b2 100644 --- a/Makefile +++ b/Makefile @@ -5,10 +5,9 @@ PYTHON?=python3 .PHONY : all clean clean-build clean-pyc clean-test install test upload all : - $(PYTHON) ./setup.py build install : all - $(PYTHON) ./setup.py install --root=${DESTDIR} + $(PYTHON) -m pip install -e . clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts @@ -32,8 +31,8 @@ clean-test: ## remove test and coverage artifacts rm -fr htmlcov/ test : - $(PYTHON) setup.py test - $(MAKE) -C test test + $(PYTHON) -m pytest . upload : test - $(PYTHON) ./setup.py sdist upload + $(PYTHON) -m hatch build + $(PYTHON) -m hatch publish diff --git a/README.rst b/README.rst index e060cef2..72639cc8 100644 --- a/README.rst +++ b/README.rst @@ -1,31 +1,31 @@ Eclipse Paho™ MQTT Python Client ================================ +The `full documentation is available here `_. + +**Warning breaking change** - Release 2.0 contains a breaking change; see the `release notes `_ and `migration details `_. + This document describes the source code for the `Eclipse Paho `_ MQTT Python client library, which implements versions 5.0, 3.1.1, and 3.1 of the MQTT protocol. -This code provides a client class which enable applications to connect to an `MQTT `_ broker to publish messages, and to subscribe to topics and receive published messages. It also provides some helper functions to make publishing one off messages to an MQTT server very straightforward. +This code provides a client class which enables applications to connect to an `MQTT `_ broker to publish messages, and to subscribe to topics and receive published messages. It also provides some helper functions to make publishing one off messages to an MQTT server very straightforward. -It supports Python 2.7.9+ or 3.6+. +It supports Python 3.7+. The MQTT protocol is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. Designed as an extremely lightweight publish/subscribe messaging transport, it is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. Paho is an `Eclipse Foundation `_ project. - Contents -------- * Installation_ * `Known limitations`_ * `Usage and API`_ + * `Getting Started`_ * `Client`_ - * `Constructor / reinitialise`_ - * `Option functions`_ - * `Connect / reconnect / disconnect`_ * `Network loop`_ - * `Publishing`_ - * `Subscribe / Unsubscribe`_ * `Callbacks`_ + * `Logger`_ * `External event loop support`_ * `Global helper functions`_ * `Publish`_ @@ -67,48 +67,51 @@ Once you have the code, it can be installed from your repository as well: :: cd paho.mqtt.python - python setup.py install + pip install -e . -To perform all test (including MQTT v5 test), you also need to clone paho.mqtt.testing in paho.mqtt.python folder:: +To perform all tests (including MQTT v5 tests), you also need to clone paho.mqtt.testing in paho.mqtt.python folder:: git clone https://github.com/eclipse/paho.mqtt.testing.git + cd paho.mqtt.testing + git checkout a4dc694010217b291ee78ee13a6d1db812f9babd Known limitations ----------------- -The following are the known unimplemented MQTT feature. +The following are the known unimplemented MQTT features. -When clean_session is False, the session is only stored in memory not persisted. This means that -when client is restarted (not just reconnected, the object is recreated usually because the -program was restarted) the session is lost. This result in possible message lost. +When ``clean_session`` is False, the session is only stored in memory and not persisted. This means that +when the client is restarted (not just reconnected, the object is recreated usually because the +program was restarted) the session is lost. This results in a possible message loss. -The following part of client session is lost: +The following part of the client session is lost: -* QoS 2 messages which have been received from the Server, but have not been completely acknowledged. +* QoS 2 messages which have been received from the server, but have not been completely acknowledged. Since the client will blindly acknowledge any PUBCOMP (last message of a QoS 2 transaction), it - won't hang but will lost this QoS 2 message. + won't hang but will lose this QoS 2 message. -* QoS 1 and QoS 2 messages which have been sent to the Server, but have not been completely acknowledged. +* QoS 1 and QoS 2 messages which have been sent to the server, but have not been completely acknowledged. - This means that message passed to publish() may be lost. This could be mitigated by taking care - that all message passed to publish() has a corresponding on_publish() call. + This means that messages passed to ``publish()`` may be lost. This could be mitigated by taking care + that all messages passed to ``publish()`` have a corresponding ``on_publish()`` call or use `wait_for_publish`. - It also means that the broker may have the Qos2 message in the session. Since the client start - with an empty session it don't know it and will re-use the mid. This is not yet fixed. + It also means that the broker may have the QoS2 message in the session. Since the client starts + with an empty session it don't know it and will reuse the mid. This is not yet fixed. -Also when clean_session is True, this library will republish QoS > 0 message accross network -reconnection. This means that QoS > 0 message won't be lost. But the standard say that -if we should discard any message for which the publish packet was sent. Our choice means that +Also, when ``clean_session`` is True, this library will republish QoS > 0 message across network +reconnection. This means that QoS > 0 message won't be lost. But the standard says that +we should discard any message for which the publish packet was sent. Our choice means that we are not compliant with the standard and it's possible for QoS 2 to be received twice. -You should you clean_session = False if you need the QoS 2 guarantee of only one delivery. + +You should set ``clean_session = False`` if you need the QoS 2 guarantee of only one delivery. Usage and API ------------- -Detailed API documentation is available through **pydoc**. Samples are available in the **examples** directory. +Detailed API documentation `is available online `_ or could be built from ``docs/`` and samples are available in the `examples`_ directory. -The package provides two modules, a full client and a helper for simple publishing. +The package provides two modules, a full `Client` and few `helpers` for simple publishing or subscribing. Getting Started *************** @@ -120,9 +123,8 @@ Here is a very simple example that subscribes to the broker $SYS topic tree and import paho.mqtt.client as mqtt # The callback for when the client receives a CONNACK response from the server. - def on_connect(client, userdata, flags, rc): - print("Connected with result code "+str(rc)) - + def on_connect(client, userdata, flags, reason_code, properties): + print(f"Connected with result code {reason_code}") # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("$SYS/#") @@ -131,17 +133,17 @@ Here is a very simple example that subscribes to the broker $SYS topic tree and def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) - client = mqtt.Client() - client.on_connect = on_connect - client.on_message = on_message + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.on_connect = on_connect + mqttc.on_message = on_message - client.connect("mqtt.eclipseprojects.io", 1883, 60) + mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) # Blocking call that processes network traffic, dispatches callbacks and # handles reconnecting. # Other loop*() functions are available that give a threaded interface and a # manual interface. - client.loop_forever() + mqttc.loop_forever() Client ****** @@ -157,993 +159,328 @@ You can use the client class as an instance, within a class or by subclassing. T Callbacks will be called to allow the application to process events as necessary. These callbacks are described below. -Constructor / reinitialise -`````````````````````````` - -Client() -'''''''' - -.. code:: python - - Client(client_id="", clean_session=True, userdata=None, protocol=MQTTv311, transport="tcp") - -The ``Client()`` constructor takes the following arguments: - -client_id - the unique client id string used when connecting to the broker. If - ``client_id`` is zero length or ``None``, then one will be randomly - generated. In this case the ``clean_session`` parameter must be ``True``. - -clean_session - a boolean that determines the client type. If ``True``, the broker will - remove all information about this client when it disconnects. If ``False``, - the client is a durable client and subscription information and queued - messages will be retained when the client disconnects. - - Note that a client will never discard its own outgoing messages on - disconnect. Calling connect() or reconnect() will cause the messages to be - resent. Use reinitialise() to reset a client to its original state. - -userdata - user defined data of any type that is passed as the ``userdata`` parameter - to callbacks. It may be updated at a later point with the - ``user_data_set()`` function. - -protocol - the version of the MQTT protocol to use for this client. Can be either - ``MQTTv31``, ``MQTTv311`` or ``MQTTv5`` - -transport - set to "websockets" to send MQTT over WebSockets. Leave at the default of - "tcp" to use raw TCP. - - -Constructor Example -................... - -.. code:: python - - import paho.mqtt.client as mqtt - - mqttc = mqtt.Client() - - -reinitialise() -'''''''''''''' - -.. code:: python - - reinitialise(client_id="", clean_session=True, userdata=None) - -The ``reinitialise()`` function resets the client to its starting state as if it had just been created. It takes the same arguments as the ``Client()`` constructor. - -Reinitialise Example -.................... - -.. code:: python - - mqttc.reinitialise() - -Option functions -```````````````` - -These functions represent options that can be set on the client to modify its behaviour. In the majority of cases this must be done *before* connecting to a broker. - -max_inflight_messages_set() -''''''''''''''''''''''''''' - -.. code:: python - - max_inflight_messages_set(self, inflight) - -Set the maximum number of messages with QoS>0 that can be part way through their network flow at once. - -Defaults to 20. Increasing this value will consume more memory but can increase throughput. - -max_queued_messages_set() -''''''''''''''''''''''''' - -.. code:: python - - max_queued_messages_set(self, queue_size) - -Set the maximum number of outgoing messages with QoS>0 that can be pending in the outgoing message queue. - -Defaults to 0. 0 means unlimited, but due to implementation currently limited to 65555 (65535 messages in queue + 20 in flight). When the queue is full, any further outgoing messages would be dropped. - -message_retry_set() -''''''''''''''''''' - -.. code:: python - - message_retry_set(retry) - -Set the time in seconds before a message with QoS>0 is retried, if the broker does not respond. - -This is set to 5 seconds by default and should not normally need changing. - -ws_set_options() -'''''''''''''''' - -.. code:: python - - ws_set_options(self, path="/mqtt", headers=None) - -Set websocket connection options. These options will only be used if ``transport="websockets"`` was passed into the ``Client()`` constructor. - -path - The mqtt path to use on the broker. - -headers - Either a dictionary specifying a list of extra headers which should be appended to the standard websocket headers, or a callable that takes the normal websocket headers and returns a new dictionary with a set of headers to connect to the broker. - -Must be called before ``connect*()``. An example of how this can be used with the AWS IoT platform is in the **examples** folder. - - -tls_set() -''''''''' - -.. code:: python - - tls_set(ca_certs=None, certfile=None, keyfile=None, cert_reqs=ssl.CERT_REQUIRED, - tls_version=ssl.PROTOCOL_TLS, ciphers=None) - -Configure network encryption and authentication options. Enables SSL/TLS support. - -ca_certs - a string path to the Certificate Authority certificate files that are to be treated as trusted by this client. If this is the only option given then the client will operate in a similar manner to a web browser. That is to say it will require the broker to have a certificate signed by the Certificate Authorities in ``ca_certs`` and will communicate using TLS v1.2, but will not attempt any form of authentication. This provides basic network encryption but may not be sufficient depending on how the broker is configured. By default, on Python 2.7.9+ or 3.4+, the default certification authority of the system is used. On older Python version this parameter is mandatory. - -certfile, keyfile - strings pointing to the PEM encoded client certificate and private keys respectively. If these arguments are not ``None`` then they will be used as client information for TLS based authentication. Support for this feature is broker dependent. Note that if either of these files in encrypted and needs a password to decrypt it, Python will ask for the password at the command line. It is not currently possible to define a callback to provide the password. - -cert_reqs - defines the certificate requirements that the client imposes on the broker. By default this is ``ssl.CERT_REQUIRED``, which means that the broker must provide a certificate. See the ssl pydoc for more information on this parameter. - -tls_version - specifies the version of the SSL/TLS protocol to be used. By default (if the python version supports it) the highest TLS version is detected. If unavailable, TLS v1.2 is used. Previous versions (all versions beginning with SSL) are possible but not recommended due to possible security problems. - -ciphers - a string specifying which encryption ciphers are allowable for this connection, or ``None`` to use the defaults. See the ssl pydoc for more information. - -Must be called before ``connect*()``. - -tls_set_context() -''''''''''''''''' - -.. code:: python - - tls_set_context(context=None) - -Configure network encryption and authentication context. Enables SSL/TLS support. - -context - an ssl.SSLContext object. By default, this is given by ``ssl.create_default_context()``, if available (added in Python 3.4). - -If you're unsure about using this method, then either use the default context, or use the ``tls_set`` method. See the ssl module documentation section about `security considerations `_ for more information. - -Must be called before ``connect*()``. - -tls_insecure_set() -'''''''''''''''''' - -.. code:: python - - tls_insecure_set(value) - -Configure verification of the server hostname in the server certificate. - -If ``value`` is set to ``True``, it is impossible to guarantee that the host you are connecting to is not impersonating your server. This can be useful in initial server testing, but makes it possible for a malicious third party to impersonate your server through DNS spoofing, for example. - -Do not use this function in a real system. Setting value to True means there is no point using encryption. - -Must be called before ``connect*()`` and after ``tls_set()`` or ``tls_set_context()``. - -enable_logger() -''''''''''''''' - -.. code:: python - - enable_logger(logger=None) - -Enable logging using the standard python logging package (See PEP 282). This may be used at the same time as the ``on_log`` callback method. - -If ``logger`` is specified, then that ``logging.Logger`` object will be used, otherwise one will be created automatically. - -Paho logging levels are converted to standard ones according to the following mapping: - -==================== =============== -Paho logging -==================== =============== -``MQTT_LOG_ERR`` ``logging.ERROR`` -``MQTT_LOG_WARNING`` ``logging.WARNING`` -``MQTT_LOG_NOTICE`` ``logging.INFO`` *(no direct equivalent)* -``MQTT_LOG_INFO`` ``logging.INFO`` -``MQTT_LOG_DEBUG`` ``logging.DEBUG`` -==================== =============== - -disable_logger() -'''''''''''''''' - -.. code:: python - - disable_logger() - -Disable logging using standard python logging package. This has no effect on the ``on_log`` callback. - -username_pw_set() -''''''''''''''''' - -.. code:: python - - username_pw_set(username, password=None) - -Set a username and optionally a password for broker authentication. Must be called before ``connect*()``. - -user_data_set() -''''''''''''''' - -.. code:: python - - user_data_set(userdata) - -Set the private user data that will be passed to callbacks when events are generated. Use this for your own purpose to support your application. - -will_set() -'''''''''' - -.. code:: python - - will_set(topic, payload=None, qos=0, retain=False) - -Set a Will to be sent to the broker. If the client disconnects without calling -``disconnect()``, the broker will publish the message on its behalf. - -topic - the topic that the will message should be published on. - -payload - the message to send as a will. If not given, or set to ``None`` a zero - length message will be used as the will. Passing an int or float will - result in the payload being converted to a string representing that number. - If you wish to send a true int/float, use ``struct.pack()`` to create the - payload you require. - -qos - the quality of service level to use for the will. - -retain - if set to ``True``, the will message will be set as the "last known - good"/retained message for the topic. - -Raises a ``ValueError`` if ``qos`` is not 0, 1 or 2, or if ``topic`` is -``None`` or has zero string length. - -reconnect_delay_set -''''''''''''''''''' - -.. code:: python - - reconnect_delay_set(min_delay=1, max_delay=120) - -The client will automatically retry connection. Between each attempt -it will wait a number of seconds between ``min_delay`` and ``max_delay``. - -When the connection is lost, initially the reconnection attempt is delayed of -``min_delay`` seconds. It's doubled between subsequent attempt up to ``max_delay``. - -The delay is reset to ``min_delay`` when the connection complete (e.g. the CONNACK is -received, not just the TCP connection is established). - - -Connect / reconnect / disconnect -```````````````````````````````` - -connect() -''''''''' - -.. code:: python - - connect(host, port=1883, keepalive=60, bind_address="") - -The ``connect()`` function connects the client to a broker. This is a blocking -function. It takes the following arguments: - -host - the hostname or IP address of the remote broker - -port - the network port of the server host to connect to. Defaults to 1883. Note - that the default port for MQTT over SSL/TLS is 8883 so if you are using - ``tls_set()`` or ``tls_set_context()``, the port may need providing manually - -keepalive - maximum period in seconds allowed between communications with the broker. - If no other messages are being exchanged, this controls the rate at which - the client will send ping messages to the broker - -bind_address - the IP address of a local network interface to bind this client to, - assuming multiple interfaces exist - -Callback -........ - -When the client receives a CONNACK message from the broker in response to the -connect it generates an ``on_connect()`` callback. - -Connect Example -............... - -.. code:: python - - mqttc.connect("mqtt.eclipseprojects.io") - -connect_async() -''''''''''''''' - -.. code:: python - - connect_async(host, port=1883, keepalive=60, bind_address="") - -Use in conjunction with ``loop_start()`` to connect in a non-blocking manner. -The connection will not complete until ``loop_start()`` is called. - -Callback (connect) -.................. - -When the client receives a CONNACK message from the broker in response to the -connect it generates an ``on_connect()`` callback. - -connect_srv() -''''''''''''' - -.. code:: python - - connect_srv(domain, keepalive=60, bind_address="") - -Connect to a broker using an SRV DNS lookup to obtain the broker address. Takes -the following arguments: - -domain - the DNS domain to search for SRV records. If ``None``, try to determine the - local domain name. - -See ``connect()`` for a description of the ``keepalive`` and ``bind_address`` -arguments. - -Callback (connect_srv) -...................... - -When the client receives a CONNACK message from the broker in response to the -connect it generates an ``on_connect()`` callback. - -SRV Connect Example -................... - -.. code:: python - - mqttc.connect_srv("eclipse.org") - -reconnect() -''''''''''' - -.. code:: python - - reconnect() - -Reconnect to a broker using the previously provided details. You must have -called ``connect*()`` before calling this function. - -Callback (reconnect) -.................... - -When the client receives a CONNACK message from the broker in response to the -connect it generates an ``on_connect()`` callback. - -disconnect() -'''''''''''' - -.. code:: python - - disconnect() - -Disconnect from the broker cleanly. Using ``disconnect()`` will not result in a -will message being sent by the broker. - -Disconnect will not wait for all queued message to be sent, to ensure all messages -are delivered, ``wait_for_publish()`` from ``MQTTMessageInfo`` should be used. -See ``publish()`` for details. - -Callback (disconnect) -..................... - -When the client has sent the disconnect message it generates an -``on_disconnect()`` callback. - Network loop ```````````` These functions are the driving force behind the client. If they are not called, incoming network data will not be processed and outgoing network data -may not be sent in a timely fashion. There are four options for managing the +will not be sent. There are four options for managing the network loop. Three are described here, the fourth in "External event loop support" below. Do not mix the different loop functions. -loop() -'''''' - -.. code:: python - - loop(timeout=1.0, max_packets=1) - -Call regularly to process network events. This call waits in ``select()`` until -the network socket is available for reading or writing, if appropriate, then -handles the incoming/outgoing data. This function blocks for up to ``timeout`` -seconds. ``timeout`` must not exceed the ``keepalive`` value for the client or -your client will be regularly disconnected by the broker. - -The ``max_packets`` argument is obsolete and should be left unset. - -Loop Example -............ - -.. code:: python - - run = True - while run: - mqttc.loop() - loop_start() / loop_stop() '''''''''''''''''''''''''' .. code:: python - loop_start() - loop_stop(force=False) - -These functions implement a threaded interface to the network loop. Calling -``loop_start()`` once, before or after ``connect*()``, runs a thread in the -background to call ``loop()`` automatically. This frees up the main thread for -other work that may be blocking. This call also handles reconnecting to the -broker. Call ``loop_stop()`` to stop the background thread. The ``force`` -argument is currently ignored. - -Loop Start/Stop Example -....................... - -.. code:: python - - mqttc.connect("mqtt.eclipseprojects.io") mqttc.loop_start() while True: temperature = sensor.blocking_read() mqttc.publish("paho/temperature", temperature) + mqttc.loop_stop() + +These functions implement a threaded interface to the network loop. Calling +`loop_start()` once, before or after ``connect*()``, runs a thread in the +background to call `loop()` automatically. This frees up the main thread for +other work that may be blocking. This call also handles reconnecting to the +broker. Call `loop_stop()` to stop the background thread. +The loop is also stopped if you call `disconnect()`. + loop_forever() '''''''''''''' .. code:: python - loop_forever(timeout=1.0, max_packets=1, retry_first_connection=False) + mqttc.loop_forever(retry_first_connection=False) This is a blocking form of the network loop and will not return until the -client calls ``disconnect()``. It automatically handles reconnecting. +client calls `disconnect()`. It automatically handles reconnecting. -Except for the first connection attempt when using connect_async, use +Except for the first connection attempt when using `connect_async`, use ``retry_first_connection=True`` to make it retry the first connection. -Warning: This might lead to situations where the client keeps connecting to an -non existing host without failing. - -The ``timeout`` and ``max_packets`` arguments are obsolete and should be left -unset. - -Publishing -`````````` - -Send a message from the client to the broker. - -publish() -''''''''' - -.. code:: python - - publish(topic, payload=None, qos=0, retain=False) - -This causes a message to be sent to the broker and subsequently from the broker -to any clients subscribing to matching topics. It takes the following -arguments: - -topic - the topic that the message should be published on - -payload - the actual message to send. If not given, or set to ``None`` a zero length - message will be used. Passing an int or float will result in the payload - being converted to a string representing that number. If you wish to send a - true int/float, use ``struct.pack()`` to create the payload you require - -qos - the quality of service level to use - -retain - if set to ``True``, the message will be set as the "last known - good"/retained message for the topic. - -Returns a MQTTMessageInfo which expose the following attributes and methods: - -* ``rc``, the result of the publishing. It could be ``MQTT_ERR_SUCCESS`` to - indicate success, ``MQTT_ERR_NO_CONN`` if the client is not currently connected, - or ``MQTT_ERR_QUEUE_SIZE`` when ``max_queued_messages_set`` is used to indicate - that message is neither queued nor sent. -* ``mid`` is the message ID for the publish request. The mid value can be used to - track the publish request by checking against the mid argument in the - ``on_publish()`` callback if it is defined. ``wait_for_publish`` may be easier - depending on your use-case. -* ``wait_for_publish()`` will block until the message is published. It will - raise ValueError if the message is not queued (rc == - ``MQTT_ERR_QUEUE_SIZE``), or a RuntimeError if there was an error when - publishing, most likely due to the client not being connected. -* ``is_published`` returns True if the message has been published. It will - raise ValueError if the message is not queued (rc == - ``MQTT_ERR_QUEUE_SIZE``), or a RuntimeError if there was an error when - publishing, most likely due to the client not being connected. - -A ``ValueError`` will be raised if topic is ``None``, has zero length or is -invalid (contains a wildcard), if ``qos`` is not one of 0, 1 or 2, or if the -length of the payload is greater than 268435455 bytes. -Callback (publish) -.................. - -When the message has been sent to the broker an ``on_publish()`` callback will -be generated. - - -Subscribe / Unsubscribe -``````````````````````` - -subscribe() -''''''''''' - -.. code:: python - - subscribe(topic, qos=0) - -Subscribe the client to one or more topics. - -This function may be called in three different ways: - -Simple string and integer -......................... - -e.g. ``subscribe("my/topic", 2)`` - -topic - a string specifying the subscription topic to subscribe to. - -qos - the desired quality of service level for the subscription. Defaults to 0. - -String and integer tuple -........................ - -e.g. ``subscribe(("my/topic", 1))`` - -topic - a tuple of ``(topic, qos)``. Both topic and qos must be present in the tuple. - -qos - not used. - -List of string and integer tuples -................................. - -e.g. ``subscribe([("my/topic", 0), ("another/topic", 2)])`` - -This allows multiple topic subscriptions in a single SUBSCRIPTION command, -which is more efficient than using multiple calls to ``subscribe()``. - -topic - a list of tuple of format ``(topic, qos)``. Both topic and qos must be - present in all of the tuples. - -qos - not used. - -The function returns a tuple ``(result, mid)``, where ``result`` is -``MQTT_ERR_SUCCESS`` to indicate success or ``(MQTT_ERR_NO_CONN, None)`` if the -client is not currently connected. ``mid`` is the message ID for the subscribe -request. The mid value can be used to track the subscribe request by checking -against the mid argument in the ``on_subscribe()`` callback if it is defined. - -Raises a ``ValueError`` if ``qos`` is not 0, 1 or 2, or if topic is ``None`` or -has zero string length, or if ``topic`` is not a string, tuple or list. - -Callback (subscribe) -.................... - -When the broker has acknowledged the subscription, an ``on_subscribe()`` -callback will be generated. +*Warning*: This might lead to situations where the client keeps connecting to an +non existing host without failing. -unsubscribe() -''''''''''''' +loop() +'''''' .. code:: python - unsubscribe(topic) - -Unsubscribe the client from one or more topics. - -topic - a single string, or list of strings that are the subscription topics to - unsubscribe from. - -Returns a tuple ``(result, mid)``, where ``result`` is ``MQTT_ERR_SUCCESS`` to -indicate success, or ``(MQTT_ERR_NO_CONN, None)`` if the client is not -currently connected. ``mid`` is the message ID for the unsubscribe request. The -mid value can be used to track the unsubscribe request by checking against the -mid argument in the ``on_unsubscribe()`` callback if it is defined. + run = True + while run: + rc = mqttc.loop(timeout=1.0) + if rc != 0: + # need to handle error, possible reconnecting or stopping the application -Raises a ``ValueError`` if ``topic`` is ``None`` or has zero string length, or -is not a string or list. +Call regularly to process network events. This call waits in ``select()`` until +the network socket is available for reading or writing, if appropriate, then +handles the incoming/outgoing data. This function blocks for up to ``timeout`` +seconds. ``timeout`` must not exceed the ``keepalive`` value for the client or +your client will be regularly disconnected by the broker. -Callback (unsubscribe) -...................... +Using this kind of loop, require you to handle reconnection strategie. -When the broker has acknowledged the unsubscribe, an ``on_unsubscribe()`` -callback will be generated. Callbacks ````````` -on_connect() -'''''''''''' - -.. code:: python - - on_connect(client, userdata, flags, rc) - -Called when the broker responds to our connection request. - -client - the client instance for this callback - -userdata - the private user data as set in ``Client()`` or ``user_data_set()`` - -flags - response flags sent by the broker -rc - the connection result - - -flags is a dict that contains response flags from the broker: - flags['session present'] - this flag is useful for clients that are - using clean session set to 0 only. If a client with clean - session=0, that reconnects to a broker that it has previously - connected to, this flag indicates whether the broker still has the - session information for the client. If 1, the session still exists. - -The value of rc indicates success or not: - - 0: Connection successful - 1: Connection refused - incorrect protocol version - 2: Connection refused - invalid client identifier - 3: Connection refused - server unavailable - 4: Connection refused - bad username or password - 5: Connection refused - not authorised - 6-255: Currently unused. - -On Connect Example -.................. - -.. code:: python - - def on_connect(client, userdata, flags, rc): - print("Connection returned result: "+connack_string(rc)) - - mqttc.on_connect = on_connect - ... - -on_disconnect() -''''''''''''''' - -.. code:: python - - on_disconnect(client, userdata, rc) - -Called when the client disconnects from the broker. - -client - the client instance for this callback - -userdata - the private user data as set in ``Client()`` or ``user_data_set()`` - -rc - the disconnection result - -The rc parameter indicates the disconnection state. If ``MQTT_ERR_SUCCESS`` -(0), the callback was called in response to a ``disconnect()`` call. If any -other value the disconnection was unexpected, such as might be caused by a -network error. - -On Disconnect Example -..................... - -.. code:: python - - def on_disconnect(client, userdata, rc): - if rc != 0: - print("Unexpected disconnection.") - - mqttc.on_disconnect = on_disconnect - ... - -on_message() -'''''''''''' +The interface to interact with paho-mqtt include various callback that are called by +the library when some events occur. + +The callbacks are functions defined in your code, to implement the require action on those events. This could +be simply printing received message or much more complex behaviour. + +Callbacks API is versioned, and the selected version is the `CallbackAPIVersion` you provided to `Client` +constructor. Currently two version are supported: + +* ``CallbackAPIVersion.VERSION1``: it's the historical version used in paho-mqtt before version 2.0. + It's the API used before the introduction of `CallbackAPIVersion`. + This version is deprecated and will be removed in paho-mqtt version 3.0. +* ``CallbackAPIVersion.VERSION2``: This version is more consistent between protocol MQTT 3.x and MQTT 5.x. It's also + much more usable with MQTT 5.x since reason code and properties are always provided when available. + It's recommended for all user to upgrade to this version. It's highly recommended for MQTT 5.x user. + +The following callbacks exists: + +* `on_connect()`: called when the CONNACK from the broker is received. The call could be for a refused connection, + check the reason_code to see if the connection is successful or rejected. +* `on_connect_fail()`: called by `loop_forever()` and `loop_start()` when the TCP connection failed to establish. + This callback is not called when using `connect()` or `reconnect()` directly. It's only called following + an automatic (re)connection made by `loop_start()` and `loop_forever()` +* `on_disconnect()`: called when the connection is closed. +* `on_message()`: called when a MQTT message is received from the broker. +* `on_publish()`: called when an MQTT message was sent to the broker. Depending on QoS level the callback is called + at different moment: + + * For QoS == 0, it's called as soon as the message is sent over the network. This could be before the corresponding ``publish()`` return. + * For QoS == 1, it's called when the corresponding PUBACK is received from the broker + * For QoS == 2, it's called when the corresponding PUBCOMP is received from the broker +* `on_subscribe()`: called when the SUBACK is received from the broker +* `on_unsubscribe()`: called when the UNSUBACK is received from the broker +* `on_log()`: called when the library log a message +* `on_socket_open`, `on_socket_close`, `on_socket_register_write`, `on_socket_unregister_write`: callbacks used for external loop support. See below for details. + +For the signature of each callback, see the `online documentation `_. + +Subscriber example +'''''''''''''''''' .. code:: python - on_message(client, userdata, message) - -Called when a message has been received on a topic that the client subscribes -to and the message does not match an existing topic filter callback. -Use ``message_callback_add()`` to define a callback that will be called for -specific topic filters. ``on_message`` will serve as fallback when none matched. - -client - the client instance for this callback - -userdata - the private user data as set in ``Client()`` or ``user_data_set()`` - -message - an instance of MQTTMessage. This is a class with members ``topic``, ``payload``, ``qos``, ``retain``. - -On Message Example -.................. + import paho.mqtt.client as mqtt -.. code:: python + def on_subscribe(client, userdata, mid, reason_code_list, properties): + # Since we subscribed only for a single channel, reason_code_list contains + # a single entry + if reason_code_list[0].is_failure: + print(f"Broker rejected you subscription: {reason_code_list[0]}") + else: + print(f"Broker granted the following QoS: {reason_code_list[0].value}") + + def on_unsubscribe(client, userdata, mid, reason_code_list, properties): + # Be careful, the reason_code_list is only present in MQTTv5. + # In MQTTv3 it will always be empty + if len(reason_code_list) == 0 or not reason_code_list[0].is_failure: + print("unsubscribe succeeded (if SUBACK is received in MQTTv3 it success)") + else: + print(f"Broker replied with failure: {reason_code_list[0]}") + client.disconnect() def on_message(client, userdata, message): - print("Received message '" + str(message.payload) + "' on topic '" - + message.topic + "' with QoS " + str(message.qos)) - + # userdata is the structure we choose to provide, here it's a list() + userdata.append(message.payload) + # We only want to process 10 messages + if len(userdata) >= 10: + client.unsubscribe("$SYS/#") + + def on_connect(client, userdata, flags, reason_code, properties): + if reason_code.is_failure: + print(f"Failed to connect: {reason_code}. loop_forever() will retry connection") + else: + # we should always subscribe from on_connect callback to be sure + # our subscribed is persisted across reconnections. + client.subscribe("$SYS/#") + + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.on_connect = on_connect mqttc.on_message = on_message - ... - -message_callback_add() -'''''''''''''''''''''' + mqttc.on_subscribe = on_subscribe + mqttc.on_unsubscribe = on_unsubscribe + + mqttc.user_data_set([]) + mqttc.connect("mqtt.eclipseprojects.io") + mqttc.loop_forever() + print(f"Received the following message: {mqttc.user_data_get()}") -This function allows you to define callbacks that handle incoming messages for -specific subscription filters, including with wildcards. This lets you, for -example, subscribe to ``sensors/#`` and have one callback to handle -``sensors/temperature`` and another to handle ``sensors/humidity``. +publisher example +''''''''''''''''' .. code:: python - message_callback_add(sub, callback) - -sub - the subscription filter to match against for this callback. Only one - callback may be defined per literal sub string - -callback - the callback to be used. Takes the same form as the ``on_message`` - callback. - -If using ``message_callback_add()`` and ``on_message``, only messages that do -not match a subscription specific filter will be passed to the ``on_message`` -callback. - -If multiple sub match a topic, each callback will be called (e.g. sub ``sensors/#`` -and sub ``+/humidity`` both match a message with a topic ``sensors/humidity``, so both -callbacks will handle this message). - -message_callback_remove() -''''''''''''''''''''''''' - -Remove a topic/subscription specific callback previously registered using -``message_callback_add()``. + import time + import paho.mqtt.client as mqtt -.. code:: python + def on_publish(client, userdata, mid, reason_code, properties): + # reason_code and properties will only be present in MQTTv5. It's always unset in MQTTv3 + try: + userdata.remove(mid) + except KeyError: + print("on_publish() is called with a mid not present in unacked_publish") + print("This is due to an unavoidable race-condition:") + print("* publish() return the mid of the message sent.") + print("* mid from publish() is added to unacked_publish by the main thread") + print("* on_publish() is called by the loop_start thread") + print("While unlikely (because on_publish() will be called after a network round-trip),") + print(" this is a race-condition that COULD happen") + print("") + print("The best solution to avoid race-condition is using the msg_info from publish()") + print("We could also try using a list of acknowledged mid rather than removing from pending list,") + print("but remember that mid could be re-used !") + + unacked_publish = set() + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.on_publish = on_publish + + mqttc.user_data_set(unacked_publish) + mqttc.connect("mqtt.eclipseprojects.io") + mqttc.loop_start() - message_callback_remove(sub) + # Our application produce some messages + msg_info = mqttc.publish("paho/test/topic", "my message", qos=1) + unacked_publish.add(msg_info.mid) -sub - the subscription filter to remove + msg_info2 = mqttc.publish("paho/test/topic", "my message2", qos=1) + unacked_publish.add(msg_info2.mid) + + # Wait for all message to be published + while len(unacked_publish): + time.sleep(0.1) -on_publish() -'''''''''''' + # Due to race-condition described above, the following way to wait for all publish is safer + msg_info.wait_for_publish() + msg_info2.wait_for_publish() -.. code:: python + mqttc.disconnect() + mqttc.loop_stop() - on_publish(client, userdata, mid) -Called when a message that was to be sent using the ``publish()`` call has -completed transmission to the broker. For messages with QoS levels 1 and 2, -this means that the appropriate handshakes have completed. For QoS 0, this -simply means that the message has left the client. The ``mid`` variable matches -the mid variable returned from the corresponding ``publish()`` call, to allow -outgoing messages to be tracked. +Logger +`````` -This callback is important because even if the publish() call returns success, -it does not always mean that the message has been sent. +The Client emit some log message that could be useful during troubleshooting. The easiest way to +enable logs is the call `enable_logger()`. It's possible to provide a custom logger or let the +default logger being used. -on_subscribe() -'''''''''''''' +Example: .. code:: python - on_subscribe(client, userdata, mid, granted_qos) - -Called when the broker responds to a subscribe request. The ``mid`` variable -matches the mid variable returned from the corresponding ``subscribe()`` call. -The ``granted_qos`` variable is a list of integers that give the QoS level the -broker has granted for each of the different subscription requests. + import logging + import paho.mqtt.client as mqtt -on_unsubscribe() -'''''''''''''''' + logging.basicConfig(level=logging.DEBUG) -.. code:: python + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.enable_logger() - on_unsubscribe(client, userdata, mid) + mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) + mqttc.loop_start() -Called when the broker responds to an unsubscribe request. The ``mid`` variable -matches the mid variable returned from the corresponding ``unsubscribe()`` -call. + # Do additional action needed, publish, subscribe, ... + [...] -on_log() -'''''''' +It's also possible to define a on_log callback that will receive a copy of all log messages. Example: .. code:: python - on_log(client, userdata, level, buf) - -Called when the client has log information. Define to allow debugging. The -``level`` variable gives the severity of the message and will be one of -``MQTT_LOG_INFO``, ``MQTT_LOG_NOTICE``, ``MQTT_LOG_WARNING``, ``MQTT_LOG_ERR``, -and ``MQTT_LOG_DEBUG``. The message itself is in ``buf``. - -This may be used at the same time as the standard Python logging, which can be -enabled via the ``enable_logger`` method. - -on_socket_open() -'''''''''''''''' - -:: - - on_socket_open(client, userdata, sock) - -Called when the socket has been opened. -Use this to register the socket with an external event loop for reading. - -on_socket_close() -''''''''''''''''' - -:: - - on_socket_close(client, userdata, sock) + import paho.mqtt.client as mqtt -Called when the socket is about to be closed. -Use this to unregister a socket from an external event loop for reading. + def on_log(client, userdata, paho_log_level, messages): + if paho_log_level == mqtt.LogLevel.MQTT_LOG_ERR: + print(message) -on_socket_register_write() -'''''''''''''''''''''''''' + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + mqttc.on_log = on_log -:: - - on_socket_register_write(client, userdata, sock) + mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) + mqttc.loop_start() -Called when a write operation to the socket failed because it would have blocked, e.g. output buffer full. -Use this to register the socket with an external event loop for writing. + # Do additional action needed, publish, subscribe, ... + [...] -on_socket_unregister_write() -'''''''''''''''''''''''''''' -:: +The correspondence with Paho logging levels and standard ones is the following: - on_socket_unregister_write(client, userdata, sock) +==================== =============== +Paho logging +==================== =============== +``MQTT_LOG_ERR`` ``logging.ERROR`` +``MQTT_LOG_WARNING`` ``logging.WARNING`` +``MQTT_LOG_NOTICE`` ``logging.INFO`` *(no direct equivalent)* +``MQTT_LOG_INFO`` ``logging.INFO`` +``MQTT_LOG_DEBUG`` ``logging.DEBUG`` +==================== =============== -Called when a write operation to the socket succeeded after it had previously failed. -Use this to unregister the socket from an external event loop for writing. External event loop support ``````````````````````````` -loop_read() -''''''''''' - -.. code:: python - - loop_read(max_packets=1) - -Call when the socket is ready for reading. ``max_packets`` is obsolete and -should be left unset. - -loop_write() -'''''''''''' - -.. code:: python +To support other network loop like asyncio (see examples_), the library expose some +method and callback to support those use-case. - loop_write(max_packets=1) +The following loop method exists: -Call when the socket is ready for writing. ``max_packets`` is obsolete and -should be left unset. +* `loop_read`: should be called when the socket is ready for reading. +* `loop_write`: should be called when the socket is ready for writing AND the library want to write data. +* `loop_misc`: should be called every few seconds to handle message retrying and pings. -loop_misc() -''''''''''' +In pseudo code, it give the following: .. code:: python - loop_misc() - -Call every few seconds to handle message retrying and pings. - -socket() -'''''''' - -.. code:: python - - socket() - -Returns the socket object in use in the client to allow interfacing with other -event loops. -This call is particularly useful for select_ based loops. See ``examples/loop_select.py``. - -.. _select: https://docs.python.org/3/library/select.html#select.select - -want_write() -'''''''''''' - -.. code:: python - - want_write() - -Returns true if there is data waiting to be written, to allow interfacing the -client with other event loops. -This call is particularly useful for select_ based loops. See ``examples/loop_select.py``. + while run: + if need_read: + mqttc.loop_read() + if need_write: + mqttc.loop_write() + mqttc.loop_misc() + + if not need_read and not need_write: + # But don't wait more than few seconds, loop_misc() need to be called regularly + wait_for_change_in_need_read_or_write() + updated_need_read_and_write() + +The tricky part is implementing the update of need_read / need_write and wait for condition change. To support +this, the following method exists: + +* `socket()`: which return the socket object when the TCP connection is open. + This call is particularly useful for select_ based loops. See ``examples/loop_select.py``. +* `want_write()`: return true if there is data waiting to be written. This is close to the + ``need_writew`` of above pseudo-code, but you should also check whether the socket is ready for writing. +* callbacks ``on_socket_*``: + + * `on_socket_open`: called when the socket is opened. + * `on_socket_close`: called when the socket is about to be closed. + * `on_socket_register_write`: called when there is data the client want to write on the socket + * `on_socket_unregister_write`: called when there is no more data to write on the socket. + + Callbacks are particularly useful for event loops where you register or unregister a socket + for reading+writing. See ``examples/loop_asyncio.py`` for an example. .. _select: https://docs.python.org/3/library/select.html#select.select -state callbacks -''''''''''''''' - -:: - - on_socket_open - on_socket_close - on_socket_register_write - on_socket_unregister_write - -Use these callbacks to get notified about state changes in the socket. -This is particularly useful for event loops where you register or unregister a socket -for reading+writing. See ``examples/loop_asyncio.py`` for an example. - -When the socket is opened, ``on_socket_open`` is called. -Register the socket with your event loop for reading. - -When the socket is about to be closed, ``on_socket_close`` is called. -Unregister the socket from your event loop for reading. - -When a write to the socket failed because it would have blocked, e.g. output buffer full, -``on_socket_register_write`` is called. -Register the socket with your event loop for writing. - -When the next write to the socket succeeded, ``on_socket_unregister_write`` is called. -Unregister the socket from your event loop for writing. - The callbacks are always called in this order: -- ``on_socket_open`` +- `on_socket_open` - Zero or more times: - - ``on_socket_register_write`` - - ``on_socket_unregister_write`` + - `on_socket_register_write` + - `on_socket_unregister_write` -- ``on_socket_close`` +- `on_socket_close` Global helper functions ``````````````````````` @@ -1160,13 +497,6 @@ For example: the topic ``non/matching`` would not match the subscription ``non/+/+`` -``connack_string(connack_code)`` returns the error string associated with a -CONNACK result. - - -``error_string(mqtt_errno)`` returns the error string associated with a Paho -MQTT error number. - Publish ******* @@ -1175,7 +505,7 @@ of messages in a one-shot manner. In other words, they are useful for the situation where you have a single/multiple messages you want to publish to a broker, then disconnect with nothing else required. -The two functions provided are ``single()`` and ``multiple()``. +The two functions provided are `single()` and `multiple()`. Both functions include support for MQTT v5.0, but do not currently let you set any properties on connection or when sending messages. @@ -1185,132 +515,29 @@ Single Publish a single message to a broker, then disconnect cleanly. -.. code:: python - - single(topic, payload=None, qos=0, retain=False, hostname="localhost", - port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, - protocol=mqtt.MQTTv311, transport="tcp") - - -Publish Single Function arguments -''''''''''''''''''''''''''''''''' - -topic - the only required argument must be the topic string to which the payload - will be published. - -payload - the payload to be published. If "" or None, a zero length payload will be - published. - -qos - the qos to use when publishing, default to 0. - -retain - set the message to be retained (True) or not (False). - -hostname - a string containing the address of the broker to connect to. Defaults to - localhost. - -port - the port to connect to the broker on. Defaults to 1883. - -client_id - the MQTT client id to use. If "" or None, the Paho library will - generate a client id automatically. - -keepalive - the keepalive timeout value for the client. Defaults to 60 seconds. - -will - a dict containing will parameters for the client: - - will = {'topic': "", 'payload':", 'qos':, 'retain':}. - - Topic is required, all other parameters are optional and will default to - None, 0 and False respectively. - - Defaults to None, which indicates no will should be used. - -auth - a dict containing authentication parameters for the client: - - auth = {'username':"", 'password':""} - - Username is required, password is optional and will default to None if not provided. - - Defaults to None, which indicates no authentication is to be used. - -tls - a dict containing TLS configuration parameters for the client: - - dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':"} - - ca_certs is required, all other parameters are optional and will default to None if not provided, which results in the client using the default behaviour - see the paho.mqtt.client documentation. - - Defaults to None, which indicates that TLS should not be used. - -protocol - choose the version of the MQTT protocol to use. Use either ``MQTTv31``, - ``MQTTv311``, or ``MQTTv5`. - -transport - set to "websockets" to send MQTT over WebSockets. Leave at the default of - "tcp" to use raw TCP. - -Publish Single Example -'''''''''''''''''''''' +Example: .. code:: python import paho.mqtt.publish as publish - publish.single("paho/test/single", "payload", hostname="mqtt.eclipseprojects.io") + publish.single("paho/test/topic", "payload", hostname="mqtt.eclipseprojects.io") Multiple ```````` Publish multiple messages to a broker, then disconnect cleanly. -This function includes support for MQTT v5.0, but does not currently let you -set any properties on connection or when sending messages. - -.. code:: python - - multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, - will=None, auth=None, tls=None, protocol=mqtt.MQTTv311, transport="tcp") - -Publish Multiple Function arguments -''''''''''''''''''''''''''''''''''' - -msgs - a list of messages to publish. Each message is either a dict or a tuple. - - If a dict, only the topic must be present. Default values will be - used for any missing arguments. The dict must be of the form: - - msg = {'topic':"", 'payload':"", 'qos':, 'retain':} - - topic must be present and may not be empty. - If payload is "", None or not present then a zero length payload will be published. If qos is not present, the default of 0 is used. If retain is not present, the default of False is used. - - If a tuple, then it must be of the form: - - ("", "", qos, retain) - -See ``single()`` for the description of ``hostname``, ``port``, ``client_id``, ``keepalive``, ``will``, ``auth``, ``tls``, ``protocol``, ``transport``. - -Publish Multiple Example -'''''''''''''''''''''''' +Example: .. code:: python + from paho.mqtt.enums import MQTTProtocolVersion import paho.mqtt.publish as publish - msgs = [{'topic':"paho/test/multiple", 'payload':"multiple 1"}, - ("paho/test/multiple", "multiple 2", 0, False)] - publish.multiple(msgs, hostname="mqtt.eclipseprojects.io") + msgs = [{'topic':"paho/test/topic", 'payload':"multiple 1"}, + ("paho/test/topic", "multiple 2", 0, False)] + publish.multiple(msgs, hostname="mqtt.eclipseprojects.io", protocol=MQTTProtocolVersion.MQTTv5) Subscribe @@ -1319,7 +546,7 @@ Subscribe This module provides some helper functions to allow straightforward subscribing and processing of messages. -The two functions provided are ``simple()`` and ``callback()``. +The two functions provided are `simple()` and `callback()`. Both functions include support for MQTT v5.0, but do not currently let you set any properties on connection or when subscribing. @@ -1330,90 +557,13 @@ Simple Subscribe to a set of topics and return the messages received. This is a blocking function. -.. code:: python - - simple(topics, qos=0, msg_count=1, retained=False, hostname="localhost", - port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, - protocol=mqtt.MQTTv311) - - -Simple Subscribe Function arguments -''''''''''''''''''''''''''''''''''' - -topics - the only required argument is the topic string to which the client will - subscribe. This can either be a string or a list of strings if multiple - topics should be subscribed to. - -qos - the qos to use when subscribing, defaults to 0. - -msg_count - the number of messages to retrieve from the broker. Defaults to 1. If 1, a - single MQTTMessage object will be returned. If >1, a list of MQTTMessages - will be returned. - -retained - set to True to consider retained messages, set to False to ignore messages - with the retained flag set. - -hostname - a string containing the address of the broker to connect to. Defaults to localhost. - -port - the port to connect to the broker on. Defaults to 1883. - -client_id - the MQTT client id to use. If "" or None, the Paho library will - generate a client id automatically. - -keepalive - the keepalive timeout value for the client. Defaults to 60 seconds. - -will - a dict containing will parameters for the client: - - will = {'topic': "", 'payload':", 'qos':, 'retain':}. - - Topic is required, all other parameters are optional and will default to - None, 0 and False respectively. - - Defaults to None, which indicates no will should be used. - -auth - a dict containing authentication parameters for the client: - - auth = {'username':"", 'password':""} - - Username is required, password is optional and will default to None if not - provided. - - Defaults to None, which indicates no authentication is to be used. - -tls - a dict containing TLS configuration parameters for the client: - - dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':"} - - ca_certs is required, all other parameters are optional and will default to - None if not provided, which results in the client using the default - behaviour - see the paho.mqtt.client documentation. - - Defaults to None, which indicates that TLS should not be used. - -protocol - choose the version of the MQTT protocol to use. Use either ``MQTTv31``, - ``MQTTv311``, or ``MQTTv5``. - - -Simple Example -'''''''''''''' +Example: .. code:: python import paho.mqtt.subscribe as subscribe - msg = subscribe.simple("paho/test/simple", hostname="mqtt.eclipseprojects.io") + msg = subscribe.simple("paho/test/topic", hostname="mqtt.eclipseprojects.io") print("%s %s" % (msg.topic, msg.payload)) Using Callback @@ -1422,38 +572,7 @@ Using Callback Subscribe to a set of topics and process the messages received using a user provided callback. -.. code:: python - - callback(callback, topics, qos=0, userdata=None, hostname="localhost", - port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, - protocol=mqtt.MQTTv311) - -Callback Subscribe Function arguments -''''''''''''''''''''''''''''''''''''' - -callback - an "on_message" callback that will be used for each message received, and - of the form - - .. code:: python - - def on_message(client, userdata, message) - -topics - the topic string to which the client will subscribe. This can either be a - string or a list of strings if multiple topics should be subscribed to. - -qos - the qos to use when subscribing, defaults to 0. - -userdata - a user provided object that will be passed to the on_message callback when - a message is received. - -See ``simple()`` for the description of ``hostname``, ``port``, ``client_id``, ``keepalive``, ``will``, ``auth``, ``tls``, ``protocol``. - -Callback Example -'''''''''''''''' +Example: .. code:: python @@ -1461,8 +580,12 @@ Callback Example def on_message_print(client, userdata, message): print("%s %s" % (message.topic, message.payload)) + userdata["message_count"] += 1 + if userdata["message_count"] >= 5: + # it's possible to stop the program by disconnecting + client.disconnect() - subscribe.callback(on_message_print, "paho/test/callback", hostname="mqtt.eclipseprojects.io") + subscribe.callback(on_message_print, "paho/test/topic", hostname="mqtt.eclipseprojects.io", userdata={"message_count": 0}) Reporting bugs @@ -1478,3 +601,6 @@ Discussion of the Paho clients takes place on the `Eclipse paho-dev mailing list General questions about the MQTT protocol itself (not this library) are discussed in the `MQTT Google Group `_. There is much more information available via the `MQTT community site `_. + +.. _examples: https://github.com/eclipse/paho.mqtt.python/tree/master/examples +.. _documentation: https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 00000000..15208760 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,13 @@ +# Security Policy + +This project implements the Eclipse Foundation Security Policy + +* https://www.eclipse.org/security + +## Supported Versions + +Only the most recent release of the client will be supported with security updates. + +## Reporting a Vulnerability + +Please report vulnerabilities to the Eclipse Foundation Security Team at security@eclipse.org \ No newline at end of file diff --git a/conftest.py b/conftest.py new file mode 100644 index 00000000..81a88214 --- /dev/null +++ b/conftest.py @@ -0,0 +1,2 @@ +# This file ensures pytest keeps this directory in sys.path, +# so you can run `pytest tests/lib`. diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 00000000..d4bb2cbb --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/changelog.rst b/docs/changelog.rst new file mode 100644 index 00000000..69a64391 --- /dev/null +++ b/docs/changelog.rst @@ -0,0 +1,4 @@ +Changelog +********* + +.. include:: ../ChangeLog.txt diff --git a/docs/client.rst b/docs/client.rst new file mode 100644 index 00000000..62dbf80c --- /dev/null +++ b/docs/client.rst @@ -0,0 +1,14 @@ +client module +============= + +.. + exclude-members exclude decorator for callback, because decorator are + documented in there respective on_XXX + +.. automodule:: paho.mqtt.client + :members: + :exclude-members: connect_callback, connect_fail_callback, disconnect_callback, log_callback, + message_callback, topic_callback, pre_connect_callback, publish_callback, + socket_close_callback, socket_open_callback, socket_register_write_callback, + socket_unregister_write_callback, subscribe_callback, unsubscribe_callback + :undoc-members: diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 00000000..2546d6f5 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,33 @@ +# Configuration file for the Sphinx documentation builder. +# +# For the full list of built-in configuration values, see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Project information ----------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information + +project = 'Eclipse paho-mqtt' +copyright = '2024, Eclipse' +author = 'Eclipse' + +# -- General configuration --------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration + +extensions = [ + "sphinx.ext.autodoc", +] + +# autodoc_class_signature = "separated" + +templates_path = ['_templates'] +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +# -- Options for HTML output ------------------------------------------------- +# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output + +html_theme = 'alabaster' +html_static_path = ['_static'] + +# This allow to reference class, method... just using `Client` and don't need +# :class:`Client`. It reduce markup overhead. +default_role="any" diff --git a/docs/helpers.rst b/docs/helpers.rst new file mode 100644 index 00000000..be41a3c4 --- /dev/null +++ b/docs/helpers.rst @@ -0,0 +1,10 @@ +helpers +======= + +.. automodule:: paho.mqtt.publish + :members: + :undoc-members: + +.. automodule:: paho.mqtt.subscribe + :members: + :undoc-members: diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 00000000..551ebe64 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,22 @@ +.. Eclipse paho-mqtt documentation master file, created by + sphinx-quickstart on Sun Jan 28 11:00:26 2024. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +.. note:: + + This is the documentation of the upcoming 2.0 release of paho-mqtt (already + available as pre-release). For 1.6.x release of paho-mqtt, see the + Github README. + +.. include:: ../README.rst + +.. toctree:: + :hidden: + :maxdepth: 3 + + client + helpers + types + changelog + migrations diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 00000000..32bb2452 --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=. +set BUILDDIR=_build + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/migrations.rst b/docs/migrations.rst new file mode 100644 index 00000000..21caf712 --- /dev/null +++ b/docs/migrations.rst @@ -0,0 +1,282 @@ +Migrations +========== + +Change between version 1.x and 2.0 +---------------------------------- + +Most breaking change should break loudly and should not be missed. The +most significant one which affect everyone is the versioned user callbacks. +Other breaking change might not effect your usage of paho-mqtt. + +The list of breaking change (detailed below) are: + +* Add version to user callbacks (on_publish, on_connect...). + tl; dr: add ``mqtt.CallbackAPIVersion.VERSION1`` as first argument to `Client()` +* Drop support for older Python. +* Dropped some deprecated and no used argument or method. If you used them, you can just drop them. +* Removed from public interface few function/class +* Renamed ReasonCodes to ReasonCode +* Improved typing which resulted in few type change. It might no affect you, see below for detail. +* Fixed connect_srv, which changed its signature. +* Added new properties, which could conflict with sub-class + +Versioned the user callbacks +**************************** + +Version 2.0 of paho-mqtt introduced versioning of the user-callback. To fix some inconsistency in callback +arguments and to provide better support for MQTTv5, version 2.0 changed the arguments passed to the user-callback. + +You can still use the old version of the callback, you are just required to tell paho-mqtt that you opt for this +version. For that just change your client creation from:: + + # OLD code + >>> mqttc = mqtt.Client() + +to:: + + # NEW code + >>> mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) + + +That's it, the remaining of the code can stay unchanged. + +Version 1 of the callback is deprecated, but is still supported in version 2.x. If you want to upgrade to the newer version of the API callback, you will need to update your callbacks: + +on_connect +`````````` + +:: + + # OLD code for MQTTv3 + def on_connect(client, userdata, flags, rc): + if flags["session present"] == 1: + # ... + if rc == 0: + # success connect + if rc > 0: + # error processing + + # OLD code for MQTTv5 + def on_connect(client, userdata, flags, reason_code, properties): + if flags["session present"] == 1: + # ... + if reason_code == 0: + # success connect + + # NEW code for both version + def on_connect(client, userdata, flags, reason_code, properties): + if flags.session_present: + # ... + if reason_code == 0: + # success connect + if reason_code > 0: + # error processing + + +Be careful that for MQTTv3, ``rc`` (an integer) changed to ``reason_code`` (an instance of `ReasonCode`), and the numeric value changed. +The numeric value 0 means success for both, so as in above example, using ``reason_code == 0``, ``reason_code != 0`` or other comparison with zero +is fine. +But if you had comparison with other value, you will need to update the code. It's recommended to compare to string value:: + + # OLD code for MQTTv3 + def on_connect(client, userdata, flags, rc): + if rc == 1: + # handle bad protocol version + if rc == CONNACK_REFUSED_IDENTIFIER_REJECTED: + # handle bad identifier + + # NEW code + def on_connect(client, userdata, flags, reason_code, properties): + if reason_code == "Unsupported protocol version": + # handle bad protocol version + if reason_code == "Client identifier not valid": + # handle bad identifier + +on_disconnect +````````````` + +:: + + # OLD code for MQTTv3 + def on_disconnect(client, userdata, rc): + if rc == 0: + # success disconnect + if rc > 0: + # error processing + + # OLD code for MQTTv5 + def on_disconnect(client, userdata, reason_code, properties): + if reason_code: + # error processing + + # NEW code for both version + def on_disconnect(client, userdata, flags, reason_code, properties): + if reason_code == 0: + # success disconnect + if reason_code > 0: + # error processing + + + +on_subscribe +```````````` + +:: + + # OLD code for MQTTv3 + def on_subscribe(client, userdata, mid, granted_qos): + for sub_result in granted_qos: + if sub_result == 1: + # process QoS == 1 + if sub_result == 0x80: + # error processing + + # OLD code for MQTTv5 + def on_disconnect(client, userdata, mid, reason_codes, properties): + for sub_result in reason_codes: + if sub_result == 1: + # process QoS == 1 + # Any reason code >= 128 is a failure. + if sub_result >= 128: + # error processing + + # NEW code for both version + def on_subscribe(client, userdata, mid, reason_codes, properties): + for sub_result in reason_codes: + if sub_result == 1: + # process QoS == 1 + # Any reason code >= 128 is a failure. + if sub_result >= 128: + # error processing + + + +on_unsubscribe +`````````````` + +:: + + # OLD code for MQTTv3 + def on_unsubscribe(client, userdata, mid): + # ... + + # OLD code for MQTTv5 + def on_unsubscribe(client, userdata, mid, properties, reason_codes): + # In OLD version, reason_codes could be a list or a single ReasonCode object + if isinstance(reason_codes, list): + for unsub_result in reason_codes: + # Any reason code >= 128 is a failure. + if reason_codes[0] >= 128: + # error processing + else: + # Any reason code >= 128 is a failure. + if reason_codes > 128: + # error processing + + + # NEW code for both version + def on_subscribe(client, userdata, mid, reason_codes, properties): + # In NEW version, reason_codes is always a list. Empty for MQTTv3 + for unsub_result in reason_codes: + # Any reason code >= 128 is a failure. + if reason_codes[0] >= 128: + # error processing + + +on_publish +`````````` + +:: + + # OLD code + def on_publish(client, userdata, mid): + # ... + + + # NEW code + def on_publish(client, userdata, mid, reason_codes, properties): + # ... + + + +on_message +`````````` + +No change for this callback:: + + # OLD & NEW code + def on_message(client, userdata, message): + # ... + + +Drop support for older Python +***************************** + +paho-mqtt support Python 3.7 to 3.12. If you are using an older Python version, including +Python 2.x you will need to kept running the 1.x version of paho-mqtt. + +Drop deprecated argument and method +*********************************** + +The following are dropped: + +* ``max_packets`` argument in `loop()`, `loop_write()` and `loop_forever()` is removed +* ``force`` argument in `loop_stop()` is removed +* method ``message_retry_set()`` is removed + +They were not used in previous version, so you can just remove them if you used them. + +Stop exposing private function/class +************************************ + +Some private function or class are not longer exposed. The following are removed: + +* function base62 +* class WebsocketWrapper +* enum ConnectionState + +Renamed ReasonCodes to ReasonCode +********************************* + +The class ReasonCodes that was used to represent one reason code response from +broker or generated by the library is now named `ReasonCode`. + +This should work without any change as ReasonCodes (plural, the old name) is still +present but deprecated. + +Improved typing +*************** + +Version 2.0 improved typing, but this would be compatible with existing code. +The most likely issue are some integer that are now better type, like `dup` on MQTTMessage. + +That means that code that used ``if msg.dup == 1:`` will need to be change to ``if msg.dup:`` (the later version +for with both paho-mqtt 1.x and 2.0). + +Fix connect_srv +*************** + +`connect_srv()` didn't took the same argument as `connect()`. Fixed this, which means the signaure +changed. But since connect_srv was broken in previous version, this should not have any negative impact. + +Added new properties +******************** + +The Client class added few new properties. If you are using a sub-class of Client and also defined a +attribute, method or properties with the same name, it will conflict. + +The added properties are: + +* `host` +* `port` +* `keepalive` +* `transport` +* `protocol` +* `connect_timeout` +* `username` +* `password` +* `max_inflight_messages` +* `max_queued_messages` +* `will_topic` +* `will_payload` +* `logger` diff --git a/docs/types.rst b/docs/types.rst new file mode 100644 index 00000000..dfe65b54 --- /dev/null +++ b/docs/types.rst @@ -0,0 +1,14 @@ +Types and enums +=============== + +.. automodule:: paho.mqtt.enums + :members: + :undoc-members: + +.. automodule:: paho.mqtt.properties + :members: + :undoc-members: + +.. automodule:: paho.mqtt.reasoncodes + :members: + :undoc-members: diff --git a/examples/aws_iot.py b/examples/aws_iot.py index e65aab71..6917c825 100755 --- a/examples/aws_iot.py +++ b/examples/aws_iot.py @@ -6,7 +6,7 @@ import os import uuid -from paho.mqtt.client import Client +from paho.mqtt.client import Client, CallbackAPIVersion def get_amazon_auth_headers(access_key, secret_key, region, host, port, headers=None): @@ -122,7 +122,7 @@ def example_use(): port, ) - client = Client(transport="websockets") + client = Client(CallbackAPIVersion.VERSION2, transport="websockets") client.ws_set_options(headers=extra_headers) diff --git a/examples/client_logger.py b/examples/client_logger.py index 7cbb87fd..6b984a79 100755 --- a/examples/client_logger.py +++ b/examples/client_logger.py @@ -27,7 +27,7 @@ # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = mqtt.Client() +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) logger = logging.getLogger(__name__) mqttc.enable_logger(logger) diff --git a/examples/client_mqtt_clear_retain.py b/examples/client_mqtt_clear_retain.py index 62f7c885..996c7d66 100755 --- a/examples/client_mqtt_clear_retain.py +++ b/examples/client_mqtt_clear_retain.py @@ -27,9 +27,9 @@ final_mid = 0 -def on_connect(mqttc, userdata, flags, rc): - if userdata == True: - print("rc: " + str(rc)) +def on_connect(mqttc, userdata, flags, reason_code, properties): + if userdata: + print(f"reason_code: {reason_code}") def on_message(mqttc, userdata, msg): @@ -38,12 +38,12 @@ def on_message(mqttc, userdata, msg): pass # sys.exit() else: - if userdata == True: + if userdata: print("Clearing topic " + msg.topic) (rc, final_mid) = mqttc.publish(msg.topic, None, 1, True) -def on_publish(mqttc, userdata, mid): +def on_publish(mqttc, userdata, mid, reason_code, properties): global final_mid if mid == final_mid: sys.exit() @@ -96,12 +96,12 @@ def main(argv): elif opt in ("-v", "--verbose"): verbose = True - if topic == None: + if not topic: print("You must provide a topic to clear.\n") print_usage() sys.exit(2) - mqttc = mqtt.Client(client_id) + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id) mqttc._userdata = verbose mqttc.on_message = on_message mqttc.on_publish = on_publish diff --git a/examples/client_pub-wait.py b/examples/client_pub-wait.py index caeb1a5f..729119d6 100755 --- a/examples/client_pub-wait.py +++ b/examples/client_pub-wait.py @@ -22,21 +22,16 @@ import paho.mqtt.client as mqtt -def on_connect(mqttc, obj, flags, rc): - print("rc: " + str(rc)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("reason_code: " + str(reason_code)) def on_message(mqttc, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) -def on_publish(mqttc, obj, mid): +def on_publish(mqttc, obj, mid, reason_code, properties): print("mid: " + str(mid)) - pass - - -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: " + str(mid) + " " + str(granted_qos)) def on_log(mqttc, obj, level, string): @@ -47,11 +42,10 @@ def on_log(mqttc, obj, level, string): # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = mqtt.Client() +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish -mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages # mqttc.on_log = on_log mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) diff --git a/examples/client_pub_opts.py b/examples/client_pub_opts.py index a850288d..40e49cde 100755 --- a/examples/client_pub_opts.py +++ b/examples/client_pub_opts.py @@ -32,8 +32,8 @@ parser.add_argument('-d', '--disable-clean-session', action='store_true', help="disable 'clean session' (sub + msgs not cleared when client disconnects)") parser.add_argument('-p', '--password', required=False, default=None) parser.add_argument('-P', '--port', required=False, type=int, default=None, help='Defaults to 8883 for TLS or 1883 for non-TLS') -parser.add_argument('-N', '--nummsgs', required=False, type=int, default=1, help='send this many messages before disconnecting') -parser.add_argument('-S', '--delay', required=False, type=float, default=1, help='number of seconds to sleep between msgs') +parser.add_argument('-N', '--nummsgs', required=False, type=int, default=1, help='send this many messages before disconnecting') +parser.add_argument('-S', '--delay', required=False, type=float, default=1, help='number of seconds to sleep between msgs') parser.add_argument('-k', '--keepalive', required=False, type=int, default=60) parser.add_argument('-s', '--use-tls', action='store_true') parser.add_argument('--insecure', action='store_true') @@ -44,22 +44,18 @@ args, unknown = parser.parse_known_args() -def on_connect(mqttc, obj, flags, rc): - print("connect rc: " + str(rc)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("connect reason_code: " + str(reason_code)) def on_message(mqttc, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) -def on_publish(mqttc, obj, mid): +def on_publish(mqttc, obj, mid, reason_code, properties): print("mid: " + str(mid)) -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: " + str(mid) + " " + str(granted_qos)) - - def on_log(mqttc, obj, level, string): print(string) @@ -68,14 +64,14 @@ def on_log(mqttc, obj, level, string): if args.cacerts: usetls = True -port = args.port +port = args.port if port is None: if usetls: port = 8883 else: port = 1883 -mqttc = mqtt.Client(args.clientid,clean_session = not args.disable_clean_session) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, args.clientid, clean_session = not args.disable_clean_session) if usetls: if args.tls_version == "tlsv1.2": @@ -94,7 +90,7 @@ def on_log(mqttc, obj, level, string): cert_required = ssl.CERT_REQUIRED else: cert_required = ssl.CERT_NONE - + mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion) if args.insecure: @@ -106,7 +102,6 @@ def on_log(mqttc, obj, level, string): mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish -mqttc.on_subscribe = on_subscribe if args.debug: mqttc.on_log = on_log @@ -125,4 +120,4 @@ def on_log(mqttc, obj, level, string): time.sleep(args.delay) mqttc.disconnect() - + diff --git a/examples/client_rpc_math.py b/examples/client_rpc_math.py index af534fcb..74a47d98 100755 --- a/examples/client_rpc_math.py +++ b/examples/client_rpc_math.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- # Copyright (c) 2020 Frank Pagliughi -# All rights reserved. -# -# This program and the accompanying materials are made available +# All rights reserved. +# +# This program and the accompanying materials are made available # under the terms of the Eclipse Distribution License v1.0 # which accompanies this distribution. # @@ -16,6 +16,8 @@ # # This shows an example of an MQTTv5 Remote Procedure Call (RPC) client. +# You should run the server_rpc_math.py before. + import json import sys @@ -33,14 +35,14 @@ # This correlates the outbound request with the returned reply corr_id = b"1" -# This is sent in the message callback when we get the respone +# This is sent in the message callback when we get the response reply = None # The MQTTv5 callback takes the additional 'props' parameter. -def on_connect(mqttc, userdata, flags, rc, props): +def on_connect(mqttc, userdata, flags, reason_code, props): global client_id, reply_to - print("Connected: '"+str(flags)+"', '"+str(rc)+"', '"+str(props)) + print(f"Connected: '{flags}', '{reason_code}', '{props}'") if hasattr(props, 'AssignedClientIdentifier'): client_id = props.AssignedClientIdentifier reply_to = "replies/math/" + client_id @@ -65,11 +67,11 @@ def on_message(mqttc, userdata, msg): print("USAGE: client_rpc_math.py [add|mult] n1 n2 ...") sys.exit(1) -mqttc = mqtt.Client(client_id="", protocol=mqtt.MQTTv5) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="", protocol=mqtt.MQTTv5) mqttc.on_message = on_message mqttc.on_connect = on_connect -mqttc.connect(host='localhost', clean_start=True) +mqttc.connect(host="mqtt.eclipseprojects.io", clean_start=True) mqttc.loop_start() # Wait for connection to set `client_id`, etc. @@ -96,7 +98,7 @@ def on_message(mqttc, userdata, msg): args.append(float(s)) # Send the request -topic = "requests/math/" + func +topic = "requests/math/" + func payload = json.dumps(args) mqttc.publish(topic, payload, qos=1, properties=props) diff --git a/examples/client_session_present.py b/examples/client_session_present.py index 806d1aff..b4552cc8 100755 --- a/examples/client_session_present.py +++ b/examples/client_session_present.py @@ -22,19 +22,19 @@ import paho.mqtt.client as mqtt -def on_connect(mqttc, obj, flags, rc): +def on_connect(mqttc, obj, flags, reason_code, properties): if obj == 0: print("First connection:") elif obj == 1: print("Second connection:") elif obj == 2: print("Third connection (with clean session=True):") - print(" Session present: " + str(flags['session present'])) - print(" Connection result: " + str(rc)) + print(" Session present: " + str(flags.session_present)) + print(" Connection result: " + str(reason_code)) mqttc.disconnect() -def on_disconnect(mqttc, obj, rc): +def on_disconnect(mqttc, obj, flags, reason_code, properties): mqttc.user_data_set(obj + 1) if obj == 0: mqttc.reconnect() @@ -44,7 +44,7 @@ def on_log(mqttc, obj, level, string): print(string) -mqttc = mqtt.Client(client_id="asdfj", clean_session=False) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="asdfj", clean_session=False) mqttc.on_connect = on_connect mqttc.on_disconnect = on_disconnect # Uncomment to enable debug messages @@ -55,7 +55,7 @@ def on_log(mqttc, obj, level, string): mqttc.loop_forever() # Clear session -mqttc = mqtt.Client(client_id="asdfj", clean_session=True) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="asdfj", clean_session=True) mqttc.on_connect = on_connect mqttc.user_data_set(2) mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) diff --git a/examples/client_sub-class.py b/examples/client_sub-class.py index d5776253..93740061 100755 --- a/examples/client_sub-class.py +++ b/examples/client_sub-class.py @@ -22,8 +22,8 @@ class MyMQTTClass(mqtt.Client): - def on_connect(self, mqttc, obj, flags, rc): - print("rc: "+str(rc)) + def on_connect(self, mqttc, obj, flags, reason_code, properties): + print("rc: "+str(reason_code)) def on_connect_fail(self, mqttc, obj): print("Connect failed") @@ -31,11 +31,11 @@ def on_connect_fail(self, mqttc, obj): def on_message(self, mqttc, obj, msg): print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) - def on_publish(self, mqttc, obj, mid): + def on_publish(self, mqttc, obj, mid, reason_codes, properties): print("mid: "+str(mid)) - def on_subscribe(self, mqttc, obj, mid, granted_qos): - print("Subscribed: "+str(mid)+" "+str(granted_qos)) + def on_subscribe(self, mqttc, obj, mid, reason_code_list, properties): + print("Subscribed: "+str(mid)+" "+str(reason_code_list)) def on_log(self, mqttc, obj, level, string): print(string) @@ -54,7 +54,7 @@ def run(self): # mqttc = MyMQTTClass("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = MyMQTTClass() +mqttc = MyMQTTClass(mqtt.CallbackAPIVersion.VERSION2) rc = mqttc.run() print("rc: "+str(rc)) diff --git a/examples/client_sub-multiple-callback.py b/examples/client_sub-multiple-callback.py index d8126c72..26a911e9 100755 --- a/examples/client_sub-multiple-callback.py +++ b/examples/client_sub-multiple-callback.py @@ -41,7 +41,7 @@ def on_message(mosq, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) -mqttc = mqtt.Client() +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) # Add message callbacks that will only trigger on a specific subscription match. mqttc.message_callback_add("$SYS/broker/messages/#", on_message_msgs) diff --git a/examples/client_sub-srv.py b/examples/client_sub-srv.py index f3ebd386..df95c911 100755 --- a/examples/client_sub-srv.py +++ b/examples/client_sub-srv.py @@ -22,17 +22,14 @@ import paho.mqtt.client as mqtt -def on_connect(mqttc, obj, flags, rc): - print("Connected to %s:%s" % (mqttc._host, mqttc._port)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("Connected to %s:%s" % (mqttc.host, mqttc.port)) def on_message(mqttc, obj, msg): print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) -def on_publish(mqttc, obj, mid): - print("mid: "+str(mid)) - -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: "+str(mid)+" "+str(granted_qos)) +def on_subscribe(mqttc, obj, mid, reason_code_list, properties): + print("Subscribed: "+str(mid)+" "+str(reason_code_list)) def on_log(mqttc, obj, level, string): print(string) @@ -41,14 +38,13 @@ def on_log(mqttc, obj, level, string): # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = mqtt.Client() +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect -mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages #mqttc.on_log = on_log -mqttc.connect_srv("mosquitto.org", 60) +mqttc.connect_srv("eclipseprojects.io", 60) mqttc.subscribe("$SYS/broker/version", 0) diff --git a/examples/client_sub-ws.py b/examples/client_sub-ws.py index 085d4850..e2950850 100755 --- a/examples/client_sub-ws.py +++ b/examples/client_sub-ws.py @@ -22,17 +22,14 @@ import paho.mqtt.client as mqtt -def on_connect(mqttc, obj, flags, rc): - print("rc: "+str(rc)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("reason_code: "+str(reason_code)) def on_message(mqttc, obj, msg): print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) -def on_publish(mqttc, obj, mid): - print("mid: "+str(mid)) - -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: "+str(mid)+" "+str(granted_qos)) +def on_subscribe(mqttc, obj, mid, reason_code_list, properties): + print("Subscribed: "+str(mid)+" "+str(reason_code_list)) def on_log(mqttc, obj, level, string): print(string) @@ -41,14 +38,13 @@ def on_log(mqttc, obj, level, string): # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = mqtt.Client(transport="websockets") +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, transport="websockets") mqttc.on_message = on_message mqttc.on_connect = on_connect -mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages mqttc.on_log = on_log -mqttc.connect("test.mosquitto.org", 8080, 60) +mqttc.connect("mqtt.eclipseprojects.io", 80, 60) mqttc.subscribe("$SYS/broker/version", 0) mqttc.loop_forever() diff --git a/examples/client_sub.py b/examples/client_sub.py index 9cd54e21..739fa029 100755 --- a/examples/client_sub.py +++ b/examples/client_sub.py @@ -22,20 +22,16 @@ import paho.mqtt.client as mqtt -def on_connect(mqttc, obj, flags, rc): - print("rc: " + str(rc)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("reason_code: " + str(reason_code)) def on_message(mqttc, obj, msg): print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) -def on_publish(mqttc, obj, mid): - print("mid: " + str(mid)) - - -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: " + str(mid) + " " + str(granted_qos)) +def on_subscribe(mqttc, obj, mid, reason_code_list, properties): + print("Subscribed: " + str(mid) + " " + str(reason_code_list)) def on_log(mqttc, obj, level, string): @@ -46,14 +42,13 @@ def on_log(mqttc, obj, level, string): # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. -mqttc = mqtt.Client() +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect -mqttc.on_publish = on_publish mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages # mqttc.on_log = on_log mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) -mqttc.subscribe("$SYS/#", 0) +mqttc.subscribe("$SYS/#") mqttc.loop_forever() diff --git a/examples/client_sub_opts.py b/examples/client_sub_opts.py index 4aa6664f..2968ab5f 100755 --- a/examples/client_sub_opts.py +++ b/examples/client_sub_opts.py @@ -41,8 +41,8 @@ args, unknown = parser.parse_known_args() -def on_connect(mqttc, obj, flags, rc): - print("rc: " + str(rc)) +def on_connect(mqttc, obj, flags, reason_code, properties): + print("reason_code: " + str(reason_code)) def on_message(mqttc, obj, msg): @@ -53,8 +53,8 @@ def on_publish(mqttc, obj, mid): print("mid: " + str(mid)) -def on_subscribe(mqttc, obj, mid, granted_qos): - print("Subscribed: " + str(mid) + " " + str(granted_qos)) +def on_subscribe(mqttc, obj, mid, reason_code_list, properties): + print("Subscribed: " + str(mid) + " " + str(reason_code_list)) def on_log(mqttc, obj, level, string): @@ -65,14 +65,14 @@ def on_log(mqttc, obj, level, string): if args.cacerts: usetls = True -port = args.port +port = args.port if port is None: if usetls: port = 8883 else: port = 1883 -mqttc = mqtt.Client(args.clientid,clean_session = not args.disable_clean_session) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, args.clientid,clean_session = not args.disable_clean_session) if usetls: if args.tls_version == "tlsv1.2": @@ -91,7 +91,7 @@ def on_log(mqttc, obj, level, string): cert_required = ssl.CERT_REQUIRED else: cert_required = ssl.CERT_NONE - + mqttc.tls_set(ca_certs=args.cacerts, certfile=None, keyfile=None, cert_reqs=cert_required, tls_version=tlsVersion) if args.insecure: diff --git a/examples/loop_asyncio.py b/examples/loop_asyncio.py index f4f22c79..42ab04a2 100755 --- a/examples/loop_asyncio.py +++ b/examples/loop_asyncio.py @@ -64,7 +64,7 @@ class AsyncMqttExample: def __init__(self, loop): self.loop = loop - def on_connect(self, client, userdata, flags, rc): + def on_connect(self, client, userdata, flags, reason_code, properties): print("Subscribing") client.subscribe(topic) @@ -74,14 +74,14 @@ def on_message(self, client, userdata, msg): else: self.got_message.set_result(msg.payload) - def on_disconnect(self, client, userdata, rc): - self.disconnected.set_result(rc) + def on_disconnect(self, client, userdata, flags, reason_code, properties): + self.disconnected.set_result(reason_code) async def main(self): self.disconnected = self.loop.create_future() self.got_message = None - self.client = mqtt.Client(client_id=client_id) + self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id) self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_disconnect = self.on_disconnect diff --git a/examples/loop_select.py b/examples/loop_select.py index 328b1030..255c9026 100755 --- a/examples/loop_select.py +++ b/examples/loop_select.py @@ -16,7 +16,7 @@ class SelectMqttExample: def __init__(self): pass - def on_connect(self, client, userdata, flags, rc): + def on_connect(self, client, userdata, flags, reason_code, properties): print("Subscribing") client.subscribe(topic) @@ -29,8 +29,8 @@ def on_message(self, client, userdata, msg): self.state += 1 self.t = time() - def on_disconnect(self, client, userdata, rc): - self.disconnected = True, rc + def on_disconnect(self, client, userdata, flags, reason_code, properties): + self.disconnected = True, reason_code def do_select(self): sock = self.client.socket() @@ -60,7 +60,7 @@ def main(self): self.t = time() self.state = 0 - self.client = mqtt.Client(client_id=client_id) + self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id) self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_disconnect = self.on_disconnect diff --git a/examples/loop_trio.py b/examples/loop_trio.py index 0567d130..aa71c3fc 100755 --- a/examples/loop_trio.py +++ b/examples/loop_trio.py @@ -24,13 +24,13 @@ def __init__(self, client): async def read_loop(self): while True: - await trio.hazmat.wait_readable(self.sock) + await trio.lowlevel.wait_readable(self.sock) self.client.loop_read() async def write_loop(self): while True: await self._event_large_write.wait() - await trio.hazmat.wait_writable(self.sock) + await trio.lowlevel.wait_writable(self.sock) self.client.loop_write() async def misc_loop(self): @@ -54,15 +54,15 @@ def on_socket_unregister_write(self, client, userdata, sock): class TrioAsyncMqttExample: - def on_connect(self, client, userdata, flags, rc): + def on_connect(self, client, userdata, flags, reason_code, properties): print("Subscribing") client.subscribe(topic) def on_message(self, client, userdata, msg): print("Got response with {} bytes".format(len(msg.payload))) - def on_disconnect(self, client, userdata, rc): - print('Disconnect result {}'.format(rc)) + def on_disconnect(self, client, userdata, flags, reason_code, properties): + print('Disconnect result {}'.format(reason_code)) async def test_write(self, cancel_scope: trio.CancelScope): for c in range(3): @@ -72,7 +72,7 @@ async def test_write(self, cancel_scope: trio.CancelScope): cancel_scope.cancel() async def main(self): - self.client = mqtt.Client(client_id=client_id) + self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id=client_id) self.client.on_connect = self.on_connect self.client.on_message = self.on_message self.client.on_disconnect = self.on_disconnect diff --git a/examples/publish_utf8-3.py b/examples/publish_utf8-3.py index 76c11b4d..e7bb46cf 100755 --- a/examples/publish_utf8-3.py +++ b/examples/publish_utf8-3.py @@ -21,4 +21,4 @@ topic = u"paho/test/single/ô" payload = u'German umlauts like "ä" ü"ö" are not supported' -publish.single(topic, payload, hostname="test.mosquitto.org") +publish.single(topic, payload, hostname="mqtt.eclipseprojects.io") diff --git a/examples/server_rpc_math.py b/examples/server_rpc_math.py index 5f3613e4..2fba6d9a 100755 --- a/examples/server_rpc_math.py +++ b/examples/server_rpc_math.py @@ -2,9 +2,9 @@ # -*- coding: utf-8 -*- # Copyright (c) 2020 Frank Pagliughi -# All rights reserved. -# -# This program and the accompanying materials are made available +# All rights reserved. +# +# This program and the accompanying materials are made available # under the terms of the Eclipse Distribution License v1.0 # which accompanies this distribution. # @@ -39,13 +39,13 @@ def mult(nums): return prod # Remember that the MQTTv5 callback takes the additional 'props' parameter. -def on_connect(mqttc, userdata, flags, rc, props): - print("Connected: '"+str(flags)+"', '"+str(rc)+"', '"+str(props)) - if not flags["session present"]: +def on_connect(mqttc, userdata, flags, reason_code, props): + print(f"Connected: '{flags}', '{reason_code}', '{props}'") + if not flags.session_present: print("Subscribing to math requests") mqttc.subscribe("requests/math/#") -# Each incoming message should be an RPC request on the +# Each incoming message should be an RPC request on the # 'requests/math/#' topic. def on_message(mqttc, userdata, msg): print(msg.topic + " " + str(msg.payload)) @@ -83,15 +83,14 @@ def on_log(mqttc, obj, level, string): # Typically with an RPC service, you want to make sure that you're the only -# client answering requests for specific topics. Using a known client ID +# client answering requests for specific topics. Using a known client ID # might help. -mqttc = mqtt.Client(client_id="paho_rpc_math_srvr", protocol=mqtt.MQTTv5) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="paho_rpc_math_srvr", protocol=mqtt.MQTTv5) mqttc.on_message = on_message mqttc.on_connect = on_connect # Uncomment to enable debug messages #mqttc.on_log = on_log -#mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) -mqttc.connect(host="localhost", clean_start=False) +mqttc.connect(host="mqtt.eclipseprojects.io", clean_start=False) mqttc.loop_forever() diff --git a/notice.html b/notice.html index f26d6406..c201955b 100644 --- a/notice.html +++ b/notice.html @@ -22,7 +22,7 @@

Usage Of Content

Applicable Licenses

Unless otherwise indicated, all Content made available by the Eclipse Foundation is provided to you under the terms and conditions of the Eclipse Public License Version 2.0 - ("EPL"). A copy of the EPL is provided with this Content and is also available at http://www.eclipse.org/legal/epl-v10.html. + ("EPL"). A copy of the EPL is provided with this Content and is also available at http://www.eclipse.org/legal/epl-v20.html. For purposes of the EPL, "Program" will mean the Content.

Content includes, but is not limited to, source code, object code, documentation and other files maintained in the Eclipse Foundation source code diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..e990812d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,146 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "paho-mqtt" +dynamic = ["version"] +description = "MQTT version 5.0/3.1.1 client class" +readme = "README.rst" +# see https://lists.spdx.org/g/Spdx-legal/topic/request_for_adding_eclipse/67981884 +# for why Eclipse Distribution License v1.0 is listed as BSD-3-Clause +license = { text = "EPL-2.0 OR BSD-3-Clause" } +requires-python = ">=3.7" +authors = [ + { name = "Roger Light", email = "roger@atchoo.org" }, +] +keywords = [ + "paho", +] +classifiers = [ + "Development Status :: 4 - Beta", + "Intended Audience :: Developers", + "License :: OSI Approved", + "Natural Language :: English", + "Operating System :: MacOS :: MacOS X", + "Operating System :: Microsoft :: Windows", + "Operating System :: POSIX", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Topic :: Communications", + "Topic :: Internet", +] +dependencies = [] + +[project.optional-dependencies] +proxy = [ + "PySocks", +] + +[project.urls] +Homepage = "http://eclipse.org/paho" + +[tool.coverage.report] +exclude_also = [ + "@(abc\\.)?abstractmethod", + "def __repr__", + "except ImportError:", + "if TYPE_CHECKING:", + "raise AssertionError", + "raise NotImplementedError", +] + +[tool.hatch.build.targets.sdist] +include = [ + "src/paho", + "/examples", + "/tests", + "about.html", + "CONTRIBUTING.md", + "edl-v10", + "epl-v20", + "LICENSE.txt", + "notice.html", + "README.rst", +] + +[tool.hatch.build.targets.wheel] +sources = ["src"] +include = [ + "src/paho", +] + +[tool.hatch.version] +path = "src/paho/mqtt/__init__.py" + + +[tool.mypy] + +[[tool.mypy.overrides]] +module = "paho.mqtt.client" +# check_untyped_defs = true +# disallow_untyped_calls = true +# disallow_incomplete_defs = true +disallow_untyped_defs = true + +[tool.pytest.ini_options] +addopts = ["-r", "xs"] +testpaths = "tests src" +filterwarnings = [ + "ignore:Callback API version 1 is deprecated, update to latest version" +] + +[tool.ruff] +exclude = ["test/lib/python/*"] +extend-select = [ + "B", + "C4", + "E", + "E9", + "F63", + "F7", + "F82", + "FLY", # flynt + "I", + "ISC", + "PERF", + "S", # Bandit + "UP", + "RUF", + "W", +] +ignore = [] +line-length = 167 + +[tool.ruff.per-file-ignores] +"examples/**/*.py" = [ + "B", + "E402", + "E711", + "E741", + "F401", + "F811", + "F841", + "I", + "PERF", + "S", + "UP", +] +"tests/**/*.py" = [ + "F811", + "PERF203", + "S101", + "S105", + "S106", +] + +[tool.typos.default.extend-words] +Mosquitto = "Mosquitto" + +[tool.typos.type.sh.extend-words] +# gen.sh use the openssl option pass(word) in +passin = "passin" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index bd0dd2ba..00000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -mock==3.0.5 -pylama==7.7.1 -pytest==4.6.6; python_version < '3.0' -pytest==5.2.2; python_version >= '3.0' -pytest-runner==5.2 -tox==3.14.0 diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 71ce170d..00000000 --- a/setup.cfg +++ /dev/null @@ -1,8 +0,0 @@ -[aliases] -test=pytest -[tool:pytest] -addopts=-r xs -testpaths=tests src -[pylama] -linters=pyflakes -skip=tests/* diff --git a/setup.py b/setup.py deleted file mode 100644 index 038c1e1f..00000000 --- a/setup.py +++ /dev/null @@ -1,62 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -import sys - -from setuptools import find_packages, setup - -sys.path.insert(0, 'src') -from paho.mqtt import __version__ - -with open('README.rst', 'rb') as readme_file: - readme = readme_file.read().decode('utf-8') - -requirements = [] -test_requirements = ['pytest', 'pylama', 'six'] -needs_pytest = {'pytest', 'test', 'ptr'}.intersection(sys.argv) -setup_requirements = ['pytest-runner'] if needs_pytest else [] -extra_requirements = {'proxy': ['PySocks']} - -if sys.version_info < (3, 0): - test_requirements += ['mock'] - -setup( - name='paho-mqtt', - version=__version__, - description='MQTT version 5.0/3.1.1 client class', - long_description=readme, - author='Roger Light', - author_email='roger@atchoo.org', - url='http://eclipse.org/paho', - packages=find_packages('src'), - package_dir={'': 'src'}, - include_package_data=True, - install_requires=requirements, - license='Eclipse Public License v2.0 / Eclipse Distribution License v1.0', - zip_safe=False, - keywords='paho', - classifiers=[ - 'Development Status :: 4 - Beta', - 'Intended Audience :: Developers', - 'License :: OSI Approved', - 'Operating System :: MacOS :: MacOS X', - 'Operating System :: Microsoft :: Windows', - 'Operating System :: POSIX', - 'Natural Language :: English', - 'Programming Language :: Python', - 'Programming Language :: Python :: 2', - 'Programming Language :: Python :: 2.7', - 'Programming Language :: Python :: 3', - 'Programming Language :: Python :: 3.6', - 'Programming Language :: Python :: 3.7', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', - 'Programming Language :: Python :: 3.10', - 'Topic :: Communications', - 'Topic :: Internet', - ], - test_suite='tests', - tests_require=test_requirements, - setup_requires=setup_requirements, - extras_require=extra_requirements -) diff --git a/src/paho/mqtt/__init__.py b/src/paho/mqtt/__init__.py index 0d349fc3..9372c8f8 100644 --- a/src/paho/mqtt/__init__.py +++ b/src/paho/mqtt/__init__.py @@ -1,4 +1,4 @@ -__version__ = "1.6.1" +__version__ = "2.1.1.dev0" class MQTTException(Exception): diff --git a/src/paho/mqtt/client.py b/src/paho/mqtt/client.py index 1c0236e4..4ccc8696 100644 --- a/src/paho/mqtt/client.py +++ b/src/paho/mqtt/client.py @@ -5,61 +5,105 @@ # and Eclipse Distribution License v1.0 which accompany this distribution. # # The Eclipse Public License is available at -# http://www.eclipse.org/legal/epl-v10.html +# http://www.eclipse.org/legal/epl-v20.html # and the Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial API and implementation # Ian Craggs - MQTT V5 support +""" +This is an MQTT client module. MQTT is a lightweight pub/sub messaging +protocol that is easy to implement and suitable for low powered devices. +""" +from __future__ import annotations import base64 +import collections +import errno import hashlib import logging +import os +import platform +import select +import socket import string import struct -import sys import threading import time +import urllib.parse +import urllib.request import uuid +import warnings +from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, NamedTuple, Sequence, Tuple, Union, cast +from paho.mqtt.packettypes import PacketTypes + +from .enums import CallbackAPIVersion, ConnackCode, LogLevel, MessageState, MessageType, MQTTErrorCode, MQTTProtocolVersion, PahoClientMode, _ConnectionState from .matcher import MQTTMatcher from .properties import Properties -from .reasoncodes import ReasonCodes +from .reasoncodes import ReasonCode, ReasonCodes from .subscribeoptions import SubscribeOptions -""" -This is an MQTT client module. MQTT is a lightweight pub/sub messaging -protocol that is easy to implement and suitable for low powered devices. -""" -import collections -import errno -import os -import platform -import select -import socket - -ssl = None try: - import ssl + from typing import Literal except ImportError: - pass + from typing_extensions import Literal # type: ignore + +if TYPE_CHECKING: + try: + from typing import TypedDict # type: ignore + except ImportError: + from typing_extensions import TypedDict + + try: + from typing import Protocol # type: ignore + except ImportError: + from typing_extensions import Protocol # type: ignore + + class _InPacket(TypedDict): + command: int + have_remaining: int + remaining_count: list[int] + remaining_mult: int + remaining_length: int + packet: bytearray + to_process: int + pos: int + + + class _OutPacket(TypedDict): + command: int + mid: int + qos: int + pos: int + to_process: int + packet: bytes + info: MQTTMessageInfo | None + + class SocketLike(Protocol): + def recv(self, buffer_size: int) -> bytes: + ... + def send(self, buffer: bytes) -> int: + ... + def close(self) -> None: + ... + def fileno(self) -> int: + ... + def setblocking(self, flag: bool) -> None: + ... + -socks = None try: - import socks + import ssl except ImportError: - pass + ssl = None # type: ignore[assignment] + try: - # Python 3 - from urllib import parse as urllib_dot_parse - from urllib import request as urllib_dot_request + import socks # type: ignore[import-untyped] except ImportError: - # Python 2 - import urllib as urllib_dot_request - - import urlparse as urllib_dot_parse + socks = None # type: ignore[assignment] try: @@ -70,123 +114,181 @@ try: import dns.resolver + + HAVE_DNS = True except ImportError: HAVE_DNS = False -else: - HAVE_DNS = True if platform.system() == 'Windows': - EAGAIN = errno.WSAEWOULDBLOCK + EAGAIN = errno.WSAEWOULDBLOCK # type: ignore[attr-defined] else: EAGAIN = errno.EAGAIN -# Python 2.7 does not have BlockingIOError. Fall back to IOError -try: - BlockingIOError -except NameError: - BlockingIOError = IOError - -MQTTv31 = 3 -MQTTv311 = 4 -MQTTv5 = 5 - -if sys.version_info[0] >= 3: - # define some alias for python2 compatibility - unicode = str - basestring = str - -# Message types -CONNECT = 0x10 -CONNACK = 0x20 -PUBLISH = 0x30 -PUBACK = 0x40 -PUBREC = 0x50 -PUBREL = 0x60 -PUBCOMP = 0x70 -SUBSCRIBE = 0x80 -SUBACK = 0x90 -UNSUBSCRIBE = 0xA0 -UNSUBACK = 0xB0 -PINGREQ = 0xC0 -PINGRESP = 0xD0 -DISCONNECT = 0xE0 -AUTH = 0xF0 +# Avoid linter complain. We kept importing it as ReasonCodes (plural) for compatibility +_ = ReasonCodes + +# Keep copy of enums values for compatibility. +CONNECT = MessageType.CONNECT +CONNACK = MessageType.CONNACK +PUBLISH = MessageType.PUBLISH +PUBACK = MessageType.PUBACK +PUBREC = MessageType.PUBREC +PUBREL = MessageType.PUBREL +PUBCOMP = MessageType.PUBCOMP +SUBSCRIBE = MessageType.SUBSCRIBE +SUBACK = MessageType.SUBACK +UNSUBSCRIBE = MessageType.UNSUBSCRIBE +UNSUBACK = MessageType.UNSUBACK +PINGREQ = MessageType.PINGREQ +PINGRESP = MessageType.PINGRESP +DISCONNECT = MessageType.DISCONNECT +AUTH = MessageType.AUTH # Log levels -MQTT_LOG_INFO = 0x01 -MQTT_LOG_NOTICE = 0x02 -MQTT_LOG_WARNING = 0x04 -MQTT_LOG_ERR = 0x08 -MQTT_LOG_DEBUG = 0x10 +MQTT_LOG_INFO = LogLevel.MQTT_LOG_INFO +MQTT_LOG_NOTICE = LogLevel.MQTT_LOG_NOTICE +MQTT_LOG_WARNING = LogLevel.MQTT_LOG_WARNING +MQTT_LOG_ERR = LogLevel.MQTT_LOG_ERR +MQTT_LOG_DEBUG = LogLevel.MQTT_LOG_DEBUG LOGGING_LEVEL = { - MQTT_LOG_DEBUG: logging.DEBUG, - MQTT_LOG_INFO: logging.INFO, - MQTT_LOG_NOTICE: logging.INFO, # This has no direct equivalent level - MQTT_LOG_WARNING: logging.WARNING, - MQTT_LOG_ERR: logging.ERROR, + LogLevel.MQTT_LOG_DEBUG: logging.DEBUG, + LogLevel.MQTT_LOG_INFO: logging.INFO, + LogLevel.MQTT_LOG_NOTICE: logging.INFO, # This has no direct equivalent level + LogLevel.MQTT_LOG_WARNING: logging.WARNING, + LogLevel.MQTT_LOG_ERR: logging.ERROR, } # CONNACK codes -CONNACK_ACCEPTED = 0 -CONNACK_REFUSED_PROTOCOL_VERSION = 1 -CONNACK_REFUSED_IDENTIFIER_REJECTED = 2 -CONNACK_REFUSED_SERVER_UNAVAILABLE = 3 -CONNACK_REFUSED_BAD_USERNAME_PASSWORD = 4 -CONNACK_REFUSED_NOT_AUTHORIZED = 5 - -# Connection state -mqtt_cs_new = 0 -mqtt_cs_connected = 1 -mqtt_cs_disconnecting = 2 -mqtt_cs_connect_async = 3 +CONNACK_ACCEPTED = ConnackCode.CONNACK_ACCEPTED +CONNACK_REFUSED_PROTOCOL_VERSION = ConnackCode.CONNACK_REFUSED_PROTOCOL_VERSION +CONNACK_REFUSED_IDENTIFIER_REJECTED = ConnackCode.CONNACK_REFUSED_IDENTIFIER_REJECTED +CONNACK_REFUSED_SERVER_UNAVAILABLE = ConnackCode.CONNACK_REFUSED_SERVER_UNAVAILABLE +CONNACK_REFUSED_BAD_USERNAME_PASSWORD = ConnackCode.CONNACK_REFUSED_BAD_USERNAME_PASSWORD +CONNACK_REFUSED_NOT_AUTHORIZED = ConnackCode.CONNACK_REFUSED_NOT_AUTHORIZED # Message state -mqtt_ms_invalid = 0 -mqtt_ms_publish = 1 -mqtt_ms_wait_for_puback = 2 -mqtt_ms_wait_for_pubrec = 3 -mqtt_ms_resend_pubrel = 4 -mqtt_ms_wait_for_pubrel = 5 -mqtt_ms_resend_pubcomp = 6 -mqtt_ms_wait_for_pubcomp = 7 -mqtt_ms_send_pubrec = 8 -mqtt_ms_queued = 9 - -# Error values -MQTT_ERR_AGAIN = -1 -MQTT_ERR_SUCCESS = 0 -MQTT_ERR_NOMEM = 1 -MQTT_ERR_PROTOCOL = 2 -MQTT_ERR_INVAL = 3 -MQTT_ERR_NO_CONN = 4 -MQTT_ERR_CONN_REFUSED = 5 -MQTT_ERR_NOT_FOUND = 6 -MQTT_ERR_CONN_LOST = 7 -MQTT_ERR_TLS = 8 -MQTT_ERR_PAYLOAD_SIZE = 9 -MQTT_ERR_NOT_SUPPORTED = 10 -MQTT_ERR_AUTH = 11 -MQTT_ERR_ACL_DENIED = 12 -MQTT_ERR_UNKNOWN = 13 -MQTT_ERR_ERRNO = 14 -MQTT_ERR_QUEUE_SIZE = 15 -MQTT_ERR_KEEPALIVE = 16 - -MQTT_CLIENT = 0 -MQTT_BRIDGE = 1 +mqtt_ms_invalid = MessageState.MQTT_MS_INVALID +mqtt_ms_publish = MessageState.MQTT_MS_PUBLISH +mqtt_ms_wait_for_puback = MessageState.MQTT_MS_WAIT_FOR_PUBACK +mqtt_ms_wait_for_pubrec = MessageState.MQTT_MS_WAIT_FOR_PUBREC +mqtt_ms_resend_pubrel = MessageState.MQTT_MS_RESEND_PUBREL +mqtt_ms_wait_for_pubrel = MessageState.MQTT_MS_WAIT_FOR_PUBREL +mqtt_ms_resend_pubcomp = MessageState.MQTT_MS_RESEND_PUBCOMP +mqtt_ms_wait_for_pubcomp = MessageState.MQTT_MS_WAIT_FOR_PUBCOMP +mqtt_ms_send_pubrec = MessageState.MQTT_MS_SEND_PUBREC +mqtt_ms_queued = MessageState.MQTT_MS_QUEUED + +MQTT_ERR_AGAIN = MQTTErrorCode.MQTT_ERR_AGAIN +MQTT_ERR_SUCCESS = MQTTErrorCode.MQTT_ERR_SUCCESS +MQTT_ERR_NOMEM = MQTTErrorCode.MQTT_ERR_NOMEM +MQTT_ERR_PROTOCOL = MQTTErrorCode.MQTT_ERR_PROTOCOL +MQTT_ERR_INVAL = MQTTErrorCode.MQTT_ERR_INVAL +MQTT_ERR_NO_CONN = MQTTErrorCode.MQTT_ERR_NO_CONN +MQTT_ERR_CONN_REFUSED = MQTTErrorCode.MQTT_ERR_CONN_REFUSED +MQTT_ERR_NOT_FOUND = MQTTErrorCode.MQTT_ERR_NOT_FOUND +MQTT_ERR_CONN_LOST = MQTTErrorCode.MQTT_ERR_CONN_LOST +MQTT_ERR_TLS = MQTTErrorCode.MQTT_ERR_TLS +MQTT_ERR_PAYLOAD_SIZE = MQTTErrorCode.MQTT_ERR_PAYLOAD_SIZE +MQTT_ERR_NOT_SUPPORTED = MQTTErrorCode.MQTT_ERR_NOT_SUPPORTED +MQTT_ERR_AUTH = MQTTErrorCode.MQTT_ERR_AUTH +MQTT_ERR_ACL_DENIED = MQTTErrorCode.MQTT_ERR_ACL_DENIED +MQTT_ERR_UNKNOWN = MQTTErrorCode.MQTT_ERR_UNKNOWN +MQTT_ERR_ERRNO = MQTTErrorCode.MQTT_ERR_ERRNO +MQTT_ERR_QUEUE_SIZE = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE +MQTT_ERR_KEEPALIVE = MQTTErrorCode.MQTT_ERR_KEEPALIVE + +MQTTv31 = MQTTProtocolVersion.MQTTv31 +MQTTv311 = MQTTProtocolVersion.MQTTv311 +MQTTv5 = MQTTProtocolVersion.MQTTv5 + +MQTT_CLIENT = PahoClientMode.MQTT_CLIENT +MQTT_BRIDGE = PahoClientMode.MQTT_BRIDGE # For MQTT V5, use the clean start flag only on the first successful connect -MQTT_CLEAN_START_FIRST_ONLY = 3 +MQTT_CLEAN_START_FIRST_ONLY: CleanStartOption = 3 sockpair_data = b"0" +# Payload support all those type and will be converted to bytes: +# * str are utf8 encoded +# * int/float are converted to string and utf8 encoded (e.g. 1 is converted to b"1") +# * None is converted to a zero-length payload (i.e. b"") +PayloadType = Union[str, bytes, bytearray, int, float, None] -class WebsocketConnectionError(ValueError): +HTTPHeader = Dict[str, str] +WebSocketHeaders = Union[Callable[[HTTPHeader], HTTPHeader], HTTPHeader] + +CleanStartOption = Union[bool, Literal[3]] + + +class ConnectFlags(NamedTuple): + """Contains additional information passed to `on_connect` callback""" + + session_present: bool + """ + this flag is useful for clients that are + using clean session set to False only (MQTTv3) or clean_start = False (MQTTv5). + In that case, if client that reconnects to a broker that it has previously + connected to, this flag indicates whether the broker still has the + session information for the client. If true, the session still exists. + """ + + +class DisconnectFlags(NamedTuple): + """Contains additional information passed to `on_disconnect` callback""" + + is_disconnect_packet_from_server: bool + """ + tells whether this on_disconnect call is the result + of receiving an DISCONNECT packet from the broker or if the on_disconnect is only + generated by the client library. + When true, the reason code is generated by the broker. + """ + + +CallbackOnConnect_v1_mqtt3 = Callable[["Client", Any, Dict[str, Any], MQTTErrorCode], None] +CallbackOnConnect_v1_mqtt5 = Callable[["Client", Any, Dict[str, Any], ReasonCode, Union[Properties, None]], None] +CallbackOnConnect_v1 = Union[CallbackOnConnect_v1_mqtt5, CallbackOnConnect_v1_mqtt3] +CallbackOnConnect_v2 = Callable[["Client", Any, ConnectFlags, ReasonCode, Union[Properties, None]], None] +CallbackOnConnect = Union[CallbackOnConnect_v1, CallbackOnConnect_v2] +CallbackOnConnectFail = Callable[["Client", Any], None] +CallbackOnDisconnect_v1_mqtt3 = Callable[["Client", Any, MQTTErrorCode], None] +CallbackOnDisconnect_v1_mqtt5 = Callable[["Client", Any, Union[ReasonCode, int, None], Union[Properties, None]], None] +CallbackOnDisconnect_v1 = Union[CallbackOnDisconnect_v1_mqtt3, CallbackOnDisconnect_v1_mqtt5] +CallbackOnDisconnect_v2 = Callable[["Client", Any, DisconnectFlags, ReasonCode, Union[Properties, None]], None] +CallbackOnDisconnect = Union[CallbackOnDisconnect_v1, CallbackOnDisconnect_v2] +CallbackOnLog = Callable[["Client", Any, int, str], None] +CallbackOnMessage = Callable[["Client", Any, "MQTTMessage"], None] +CallbackOnPreConnect = Callable[["Client", Any], None] +CallbackOnPublish_v1 = Callable[["Client", Any, int], None] +CallbackOnPublish_v2 = Callable[["Client", Any, int, ReasonCode, Properties], None] +CallbackOnPublish = Union[CallbackOnPublish_v1, CallbackOnPublish_v2] +CallbackOnSocket = Callable[["Client", Any, "SocketLike"], None] +CallbackOnSubscribe_v1_mqtt3 = Callable[["Client", Any, int, Tuple[int, ...]], None] +CallbackOnSubscribe_v1_mqtt5 = Callable[["Client", Any, int, List[ReasonCode], Properties], None] +CallbackOnSubscribe_v1 = Union[CallbackOnSubscribe_v1_mqtt3, CallbackOnSubscribe_v1_mqtt5] +CallbackOnSubscribe_v2 = Callable[["Client", Any, int, List[ReasonCode], Union[Properties, None]], None] +CallbackOnSubscribe = Union[CallbackOnSubscribe_v1, CallbackOnSubscribe_v2] +CallbackOnUnsubscribe_v1_mqtt3 = Callable[["Client", Any, int], None] +CallbackOnUnsubscribe_v1_mqtt5 = Callable[["Client", Any, int, Properties, Union[ReasonCode, List[ReasonCode]]], None] +CallbackOnUnsubscribe_v1 = Union[CallbackOnUnsubscribe_v1_mqtt3, CallbackOnUnsubscribe_v1_mqtt5] +CallbackOnUnsubscribe_v2 = Callable[["Client", Any, int, List[ReasonCode], Union[Properties, None]], None] +CallbackOnUnsubscribe = Union[CallbackOnUnsubscribe_v1, CallbackOnUnsubscribe_v2] + +# This is needed for typing because class Client redefined the name "socket" +_socket = socket + + +class WebsocketConnectionError(ConnectionError): + """ WebsocketConnectionError is a subclass of ConnectionError. + + It's raised when unable to perform the Websocket handshake. + """ pass -def error_string(mqtt_errno): +def error_string(mqtt_errno: MQTTErrorCode | int) -> str: """Return the error string associated with an mqtt error number.""" if mqtt_errno == MQTT_ERR_SUCCESS: return "No error." @@ -226,8 +328,11 @@ def error_string(mqtt_errno): return "Unknown error." -def connack_string(connack_code): - """Return the string associated with a CONNACK result.""" +def connack_string(connack_code: int|ReasonCode) -> str: + """Return the string associated with a CONNACK result or CONNACK reason code.""" + if isinstance(connack_code, ReasonCode): + return str(connack_code) + if connack_code == CONNACK_ACCEPTED: return "Connection Accepted." elif connack_code == CONNACK_REFUSED_PROTOCOL_VERSION: @@ -244,9 +349,69 @@ def connack_string(connack_code): return "Connection Refused: unknown reason." -def base62(num, base=string.digits + string.ascii_letters, padding=1): +def convert_connack_rc_to_reason_code(connack_code: ConnackCode) -> ReasonCode: + """Convert a MQTTv3 / MQTTv3.1.1 connack result to `ReasonCode`. + + This is used in `on_connect` callback to have a consistent API. + + Be careful that the numeric value isn't the same, for example: + + >>> ConnackCode.CONNACK_REFUSED_SERVER_UNAVAILABLE == 3 + >>> convert_connack_rc_to_reason_code(ConnackCode.CONNACK_REFUSED_SERVER_UNAVAILABLE) == 136 + + It's recommended to compare by names + + >>> code_to_test = ReasonCode(PacketTypes.CONNACK, "Server unavailable") + >>> convert_connack_rc_to_reason_code(ConnackCode.CONNACK_REFUSED_SERVER_UNAVAILABLE) == code_to_test + """ + if connack_code == ConnackCode.CONNACK_ACCEPTED: + return ReasonCode(PacketTypes.CONNACK, "Success") + if connack_code == ConnackCode.CONNACK_REFUSED_PROTOCOL_VERSION: + return ReasonCode(PacketTypes.CONNACK, "Unsupported protocol version") + if connack_code == ConnackCode.CONNACK_REFUSED_IDENTIFIER_REJECTED: + return ReasonCode(PacketTypes.CONNACK, "Client identifier not valid") + if connack_code == ConnackCode.CONNACK_REFUSED_SERVER_UNAVAILABLE: + return ReasonCode(PacketTypes.CONNACK, "Server unavailable") + if connack_code == ConnackCode.CONNACK_REFUSED_BAD_USERNAME_PASSWORD: + return ReasonCode(PacketTypes.CONNACK, "Bad user name or password") + if connack_code == ConnackCode.CONNACK_REFUSED_NOT_AUTHORIZED: + return ReasonCode(PacketTypes.CONNACK, "Not authorized") + + return ReasonCode(PacketTypes.CONNACK, "Unspecified error") + + +def convert_disconnect_error_code_to_reason_code(rc: MQTTErrorCode) -> ReasonCode: + """Convert an MQTTErrorCode to Reason code. + + This is used in `on_disconnect` callback to have a consistent API. + + Be careful that the numeric value isn't the same, for example: + + >>> MQTTErrorCode.MQTT_ERR_PROTOCOL == 2 + >>> convert_disconnect_error_code_to_reason_code(MQTTErrorCode.MQTT_ERR_PROTOCOL) == 130 + + It's recommended to compare by names + + >>> code_to_test = ReasonCode(PacketTypes.DISCONNECT, "Protocol error") + >>> convert_disconnect_error_code_to_reason_code(MQTTErrorCode.MQTT_ERR_PROTOCOL) == code_to_test + """ + if rc == MQTTErrorCode.MQTT_ERR_SUCCESS: + return ReasonCode(PacketTypes.DISCONNECT, "Success") + if rc == MQTTErrorCode.MQTT_ERR_KEEPALIVE: + return ReasonCode(PacketTypes.DISCONNECT, "Keep alive timeout") + if rc == MQTTErrorCode.MQTT_ERR_CONN_LOST: + return ReasonCode(PacketTypes.DISCONNECT, "Unspecified error") + return ReasonCode(PacketTypes.DISCONNECT, "Unspecified error") + + +def _base62( + num: int, + base: str = string.digits + string.ascii_letters, + padding: int = 1, +) -> str: """Convert a number to base-62 representation.""" - assert num >= 0 + if num < 0: + raise ValueError("Number must be positive or zero") digits = [] while num: num, rest = divmod(num, 62) @@ -255,13 +420,13 @@ def base62(num, base=string.digits + string.ascii_letters, padding=1): return ''.join(reversed(digits)) -def topic_matches_sub(sub, topic): +def topic_matches_sub(sub: str, topic: str) -> bool: """Check whether a topic matches a subscription. For example: - foo/bar would match the subscription foo/# or +/bar - non/matching would not match the subscription non/+/+ + * Topic "foo/bar" would match the subscription "foo/#" or "+/bar" + * Topic "non/matching" would not match the subscription "non/+/+" """ matcher = MQTTMatcher() matcher[sub] = True @@ -272,7 +437,7 @@ def topic_matches_sub(sub, topic): return False -def _socketpair_compat(): +def _socketpair_compat() -> tuple[socket.socket, socket.socket]: """TCP/IP socketpair including Windows support""" listensock = socket.socket( socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) @@ -283,43 +448,70 @@ def _socketpair_compat(): iface, port = listensock.getsockname() sock1 = socket.socket( socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) - sock1.setblocking(0) + sock1.setblocking(False) try: sock1.connect(("127.0.0.1", port)) except BlockingIOError: pass sock2, address = listensock.accept() - sock2.setblocking(0) + sock2.setblocking(False) listensock.close() return (sock1, sock2) -class MQTTMessageInfo(object): - """This is a class returned from Client.publish() and can be used to find +def _force_bytes(s: str | bytes) -> bytes: + if isinstance(s, str): + return s.encode("utf-8") + return s + + +def _encode_payload(payload: str | bytes | bytearray | int | float | None) -> bytes|bytearray: + if isinstance(payload, str): + return payload.encode("utf-8") + + if isinstance(payload, (int, float)): + return str(payload).encode("ascii") + + if payload is None: + return b"" + + if not isinstance(payload, (bytes, bytearray)): + raise TypeError( + "payload must be a string, bytearray, int, float or None." + ) + + return payload + + +class MQTTMessageInfo: + """This is a class returned from `Client.publish()` and can be used to find out the mid of the message that was published, and to determine whether the message has been published, and/or wait until it is published. """ __slots__ = 'mid', '_published', '_condition', 'rc', '_iterpos' - def __init__(self, mid): + def __init__(self, mid: int): self.mid = mid + """ The message Id (int)""" self._published = False self._condition = threading.Condition() - self.rc = 0 + self.rc: MQTTErrorCode = MQTTErrorCode.MQTT_ERR_SUCCESS + """ The `MQTTErrorCode` that give status for this message. + This value could change until the message `is_published`""" self._iterpos = 0 - def __str__(self): + def __str__(self) -> str: return str((self.rc, self.mid)) - def __iter__(self): + def __iter__(self) -> Iterator[MQTTErrorCode | int]: self._iterpos = 0 return self - def __next__(self): + def __next__(self) -> MQTTErrorCode | int: return self.next() - def next(self): + def next(self) -> MQTTErrorCode | int: if self._iterpos == 0: self._iterpos = 1 return self.rc @@ -329,7 +521,7 @@ def next(self): else: raise StopIteration - def __getitem__(self, index): + def __getitem__(self, index: int) -> MQTTErrorCode | int: if index == 0: return self.rc elif index == 1: @@ -337,162 +529,129 @@ def __getitem__(self, index): else: raise IndexError("index out of range") - def _set_as_published(self): + def _set_as_published(self) -> None: with self._condition: self._published = True self._condition.notify() - def wait_for_publish(self, timeout=None): + def wait_for_publish(self, timeout: float | None = None) -> None: """Block until the message associated with this object is published, or until the timeout occurs. If timeout is None, this will never time out. Set timeout to a positive number of seconds, e.g. 1.2, to enable the timeout. - Raises ValueError if the message was not queued due to the outgoing - queue being full. + :raises ValueError: if the message was not queued due to the outgoing + queue being full. - Raises RuntimeError if the message was not published for another - reason. + :raises RuntimeError: if the message was not published for another + reason. """ if self.rc == MQTT_ERR_QUEUE_SIZE: raise ValueError('Message is not queued due to ERR_QUEUE_SIZE') elif self.rc == MQTT_ERR_AGAIN: pass elif self.rc > 0: - raise RuntimeError('Message publish failed: %s' % (error_string(self.rc))) + raise RuntimeError(f'Message publish failed: {error_string(self.rc)}') - timeout_time = None if timeout is None else time.time() + timeout + timeout_time = None if timeout is None else time_func() + timeout timeout_tenth = None if timeout is None else timeout / 10. - def timed_out(): - return False if timeout is None else time.time() > timeout_time + def timed_out() -> bool: + return False if timeout_time is None else time_func() > timeout_time with self._condition: while not self._published and not timed_out(): self._condition.wait(timeout_tenth) - def is_published(self): + if self.rc > 0: + raise RuntimeError(f'Message publish failed: {error_string(self.rc)}') + + def is_published(self) -> bool: """Returns True if the message associated with this object has been - published, else returns False.""" - if self.rc == MQTT_ERR_QUEUE_SIZE: + published, else returns False. + + To wait for this to become true, look at `wait_for_publish`. + """ + if self.rc == MQTTErrorCode.MQTT_ERR_QUEUE_SIZE: raise ValueError('Message is not queued due to ERR_QUEUE_SIZE') - elif self.rc == MQTT_ERR_AGAIN: + elif self.rc == MQTTErrorCode.MQTT_ERR_AGAIN: pass elif self.rc > 0: - raise RuntimeError('Message publish failed: %s' % (error_string(self.rc))) + raise RuntimeError(f'Message publish failed: {error_string(self.rc)}') with self._condition: return self._published -class MQTTMessage(object): - """ This is a class that describes an incoming or outgoing message. It is - passed to the on_message callback as the message parameter. - - Members: - - topic : String. topic that the message was published on. - payload : Bytes/Byte array. the message payload. - qos : Integer. The message Quality of Service 0, 1 or 2. - retain : Boolean. If true, the message is a retained message and not fresh. - mid : Integer. The message id. - properties: Properties class. In MQTT v5.0, the properties associated with the message. +class MQTTMessage: + """ This is a class that describes an incoming message. It is + passed to the `on_message` callback as the message parameter. """ - __slots__ = 'timestamp', 'state', 'dup', 'mid', '_topic', 'payload', 'qos', 'retain', 'info', 'properties' - def __init__(self, mid=0, topic=b""): - self.timestamp = 0 + def __init__(self, mid: int = 0, topic: bytes = b""): + self.timestamp = 0.0 self.state = mqtt_ms_invalid self.dup = False self.mid = mid + """ The message id (int).""" self._topic = topic self.payload = b"" + """the message payload (bytes)""" self.qos = 0 + """ The message Quality of Service (0, 1 or 2).""" self.retain = False + """ If true, the message is a retained message and not fresh.""" self.info = MQTTMessageInfo(mid) + self.properties: Properties | None = None + """ In MQTT v5.0, the properties associated with the message. (`Properties`)""" - def __eq__(self, other): + def __eq__(self, other: object) -> bool: """Override the default Equals behavior""" if isinstance(other, self.__class__): return self.mid == other.mid return False - def __ne__(self, other): + def __ne__(self, other: object) -> bool: """Define a non-equality test""" return not self.__eq__(other) @property - def topic(self): + def topic(self) -> str: + """topic that the message was published on. + + This property is read-only. + """ return self._topic.decode('utf-8') @topic.setter - def topic(self, value): + def topic(self, value: bytes) -> None: self._topic = value -class Client(object): +class Client: """MQTT version 3.1/3.1.1/5.0 client class. This is the main class for use communicating with an MQTT broker. General usage flow: - * Use connect()/connect_async() to connect to a broker - * Call loop() frequently to maintain network traffic flow with the broker - * Or use loop_start() to set a thread running to call loop() for you. - * Or use loop_forever() to handle calling loop() for you in a blocking - * function. - * Use subscribe() to subscribe to a topic and receive messages - * Use publish() to send messages - * Use disconnect() to disconnect from the broker + * Use `connect()`, `connect_async()` or `connect_srv()` to connect to a broker + * Use `loop_start()` to set a thread running to call `loop()` for you. + * Or use `loop_forever()` to handle calling `loop()` for you in a blocking function. + * Or call `loop()` frequently to maintain network traffic flow with the broker + * Use `subscribe()` to subscribe to a topic and receive messages + * Use `publish()` to send messages + * Use `disconnect()` to disconnect from the broker Data returned from the broker is made available with the use of callback functions as described below. - Callbacks - ========= - - A number of callback functions are available to receive data back from the - broker. To use a callback, define a function and then assign it to the - client: - - def on_connect(client, userdata, flags, rc): - print("Connection returned " + str(rc)) - - client.on_connect = on_connect + :param CallbackAPIVersion callback_api_version: define the API version for user-callback (on_connect, on_publish,...). + This field is required and it's recommended to use the latest version (CallbackAPIVersion.API_VERSION2). + See each callback for description of API for each version. The file docs/migrations.rst contains details on + how to migrate between version. - Callbacks can also be attached using decorators: - - client = paho.mqtt.Client() - - @client.connect_callback() - def on_connect(client, userdata, flags, rc): - print("Connection returned " + str(rc)) - - - **IMPORTANT** the required function signature for a callback can differ - depending on whether you are using MQTT v5 or MQTT v3.1.1/v3.1. See the - documentation for each callback. - - All of the callbacks as described below have a "client" and an "userdata" - argument. "client" is the Client instance that is calling the callback. - "userdata" is user data of any type and can be set when creating a new client - instance or with user_data_set(userdata). - - If you wish to suppress exceptions within a callback, you should set - `client.suppress_exceptions = True` - - The callbacks are listed below, documentation for each of them can be found - at the same function name: - - on_connect, on_connect_fail, on_disconnect, on_message, on_publish, - on_subscribe, on_unsubscribe, on_log, on_socket_open, on_socket_close, - on_socket_register_write, on_socket_unregister_write - """ - - def __init__(self, client_id="", clean_session=None, userdata=None, - protocol=MQTTv311, transport="tcp", reconnect_on_failure=True): - """client_id is the unique client id string used when connecting to the + :param str client_id: the unique client id string used when connecting to the broker. If client_id is zero length or None, then the behaviour is defined by which protocol version is in use. If using MQTT v3.1.1, then a zero length client id will be sent to the broker and the broker will @@ -500,7 +659,7 @@ def __init__(self, client_id="", clean_session=None, userdata=None, randomly generated. In both cases, clean_session must be True. If this is not the case a ValueError will be raised. - clean_session is a boolean that determines the client type. If True, + :param bool clean_session: a boolean that determines the client type. If True, the broker will remove all information about this client when it disconnects. If False, the client is a persistent client and subscription information and queued messages will be retained when the @@ -512,30 +671,111 @@ def __init__(self, client_id="", clean_session=None, userdata=None, It is not accepted if the MQTT version is v5.0 - use the clean_start argument on connect() instead. - userdata is user defined data of any type that is passed as the "userdata" + :param userdata: user defined data of any type that is passed as the "userdata" parameter to callbacks. It may be updated at a later point with the user_data_set() function. - The protocol argument allows explicit setting of the MQTT version to + :param int protocol: allows explicit setting of the MQTT version to use for this client. Can be paho.mqtt.client.MQTTv311 (v3.1.1), paho.mqtt.client.MQTTv31 (v3.1) or paho.mqtt.client.MQTTv5 (v5.0), with the default being v3.1.1. - Set transport to "websockets" to use WebSockets as the transport + :param transport: use "websockets" to use WebSockets as the transport mechanism. Set to "tcp" to use raw TCP, which is the default. - """ + Use "unix" to use Unix sockets as the transport mechanism; note that + this option is only available on platforms that support Unix sockets, + and the "host" argument is interpreted as the path to the Unix socket + file in this case. + + :param bool manual_ack: normally, when a message is received, the library automatically + acknowledges after on_message callback returns. manual_ack=True allows the application to + acknowledge receipt after it has completed processing of a message + using a the ack() method. This addresses vulnerability to message loss + if applications fails while processing a message, or while it pending + locally. - if transport.lower() not in ('websockets', 'tcp'): + Callbacks + ========= + + A number of callback functions are available to receive data back from the + broker. To use a callback, define a function and then assign it to the + client:: + + def on_connect(client, userdata, flags, reason_code, properties): + print(f"Connected with result code {reason_code}") + + client.on_connect = on_connect + + Callbacks can also be attached using decorators:: + + mqttc = paho.mqtt.Client() + + @mqttc.connect_callback() + def on_connect(client, userdata, flags, reason_code, properties): + print(f"Connected with result code {reason_code}") + + All of the callbacks as described below have a "client" and an "userdata" + argument. "client" is the `Client` instance that is calling the callback. + userdata" is user data of any type and can be set when creating a new client + instance or with `user_data_set()`. + + If you wish to suppress exceptions within a callback, you should set + ``mqttc.suppress_exceptions = True`` + + The callbacks are listed below, documentation for each of them can be found + at the same function name: + + `on_connect`, `on_connect_fail`, `on_disconnect`, `on_message`, `on_publish`, + `on_subscribe`, `on_unsubscribe`, `on_log`, `on_socket_open`, `on_socket_close`, + `on_socket_register_write`, `on_socket_unregister_write` + """ + + def __init__( + self, + callback_api_version: CallbackAPIVersion = CallbackAPIVersion.VERSION1, + client_id: str | None = "", + clean_session: bool | None = None, + userdata: Any = None, + protocol: MQTTProtocolVersion = MQTTv311, + transport: Literal["tcp", "websockets", "unix"] = "tcp", + reconnect_on_failure: bool = True, + manual_ack: bool = False, + ) -> None: + transport = transport.lower() # type: ignore + if transport == "unix" and not hasattr(socket, "AF_UNIX"): + raise ValueError('"unix" transport not supported') + elif transport not in ("websockets", "tcp", "unix"): raise ValueError( - 'transport must be "websockets" or "tcp", not %s' % transport) - self._transport = transport.lower() + f'transport must be "websockets", "tcp" or "unix", not {transport}') + + self._manual_ack = manual_ack + self._transport = transport self._protocol = protocol self._userdata = userdata - self._sock = None - self._sockpairR, self._sockpairW = (None, None,) + self._sock: SocketLike | None = None + self._sockpairR: socket.socket | None = None + self._sockpairW: socket.socket | None = None self._keepalive = 60 self._connect_timeout = 5.0 self._client_mode = MQTT_CLIENT + self._callback_api_version = callback_api_version + + if self._callback_api_version == CallbackAPIVersion.VERSION1: + warnings.warn( + "Callback API version 1 is deprecated, update to latest version", + category=DeprecationWarning, + stacklevel=2, + ) + if isinstance(self._callback_api_version, str): + # Help user to migrate, it probably provided a client id + # as first arguments + raise ValueError( + "Unsupported callback API version: version 2.0 added a callback_api_version, see docs/migrations.rst for details" + ) + if self._callback_api_version not in CallbackAPIVersion: + raise ValueError("Unsupported callback API version") + + self._clean_start: int = MQTT_CLEAN_START_FIRST_ONLY if protocol == MQTTv5: if clean_session is not None: @@ -551,17 +791,15 @@ def __init__(self, client_id="", clean_session=None, userdata=None, # [MQTT-3.1.3-4] Client Id must be UTF-8 encoded string. if client_id == "" or client_id is None: if protocol == MQTTv31: - self._client_id = base62(uuid.uuid4().int, padding=22) + self._client_id = _base62(uuid.uuid4().int, padding=22).encode("utf8") else: self._client_id = b"" else: - self._client_id = client_id - if isinstance(self._client_id, unicode): - self._client_id = self._client_id.encode('utf-8') + self._client_id = _force_bytes(client_id) - self._username = None - self._password = None - self._in_packet = { + self._username: bytes | None = None + self._password: bytes | None = None + self._in_packet: _InPacket = { "command": 0, "have_remaining": 0, "remaining_count": [], @@ -569,24 +807,29 @@ def __init__(self, client_id="", clean_session=None, userdata=None, "remaining_length": 0, "packet": bytearray(b""), "to_process": 0, - "pos": 0} - self._out_packet = collections.deque() + "pos": 0, + } + self._out_packet: collections.deque[_OutPacket] = collections.deque() self._last_msg_in = time_func() self._last_msg_out = time_func() self._reconnect_min_delay = 1 self._reconnect_max_delay = 120 - self._reconnect_delay = None + self._reconnect_delay: int | None = None self._reconnect_on_failure = reconnect_on_failure - self._ping_t = 0 + self._ping_t = 0.0 self._last_mid = 0 - self._state = mqtt_cs_new - self._out_messages = collections.OrderedDict() - self._in_messages = collections.OrderedDict() + self._state = _ConnectionState.MQTT_CS_NEW + self._out_messages: collections.OrderedDict[ + int, MQTTMessage + ] = collections.OrderedDict() + self._in_messages: collections.OrderedDict[ + int, MQTTMessage + ] = collections.OrderedDict() self._max_inflight_messages = 20 self._inflight_messages = 0 self._max_queued_messages = 0 - self._connect_properties = None - self._will_properties = None + self._connect_properties: Properties | None = None + self._will_properties: Properties | None = None self._will = False self._will_topic = b"" self._will_payload = b"" @@ -597,7 +840,7 @@ def __init__(self, client_id="", clean_session=None, userdata=None, self._port = 1883 self._bind_address = "" self._bind_port = 0 - self._proxy = {} + self._proxy: Any = {} self._in_callback_mutex = threading.Lock() self._callback_mutex = threading.RLock() self._msgtime_mutex = threading.Lock() @@ -605,58 +848,279 @@ def __init__(self, client_id="", clean_session=None, userdata=None, self._in_message_mutex = threading.Lock() self._reconnect_delay_mutex = threading.Lock() self._mid_generate_mutex = threading.Lock() - self._thread = None + self._thread: threading.Thread | None = None self._thread_terminate = False self._ssl = False - self._ssl_context = None + self._ssl_context: ssl.SSLContext | None = None # Only used when SSL context does not have check_hostname attribute self._tls_insecure = False - self._logger = None + self._logger: logging.Logger | None = None self._registered_write = False # No default callbacks - self._on_log = None - self._on_connect = None - self._on_connect_fail = None - self._on_subscribe = None - self._on_message = None - self._on_publish = None - self._on_unsubscribe = None - self._on_disconnect = None - self._on_socket_open = None - self._on_socket_close = None - self._on_socket_register_write = None - self._on_socket_unregister_write = None + self._on_log: CallbackOnLog | None = None + self._on_pre_connect: CallbackOnPreConnect | None = None + self._on_connect: CallbackOnConnect | None = None + self._on_connect_fail: CallbackOnConnectFail | None = None + self._on_subscribe: CallbackOnSubscribe | None = None + self._on_message: CallbackOnMessage | None = None + self._on_publish: CallbackOnPublish | None = None + self._on_unsubscribe: CallbackOnUnsubscribe | None = None + self._on_disconnect: CallbackOnDisconnect | None = None + self._on_socket_open: CallbackOnSocket | None = None + self._on_socket_close: CallbackOnSocket | None = None + self._on_socket_register_write: CallbackOnSocket | None = None + self._on_socket_unregister_write: CallbackOnSocket | None = None self._websocket_path = "/mqtt" - self._websocket_extra_headers = None + self._websocket_extra_headers: WebSocketHeaders | None = None # for clean_start == MQTT_CLEAN_START_FIRST_ONLY self._mqttv5_first_connect = True self.suppress_exceptions = False # For callbacks - def __del__(self): + def __del__(self) -> None: self._reset_sockets() - def _sock_recv(self, bufsize): + @property + def host(self) -> str: + """ + Host to connect to. If `connect()` hasn't been called yet, returns an empty string. + + This property may not be changed if the connection is already open. + """ + return self._host + + @host.setter + def host(self, value: str) -> None: + if not self._connection_closed(): + raise RuntimeError("updating host on established connection is not supported") + + if not value: + raise ValueError("Invalid host.") + self._host = value + + @property + def port(self) -> int: + """ + Broker TCP port to connect to. + + This property may not be changed if the connection is already open. + """ + return self._port + + @port.setter + def port(self, value: int) -> None: + if not self._connection_closed(): + raise RuntimeError("updating port on established connection is not supported") + + if value <= 0: + raise ValueError("Invalid port number.") + self._port = value + + @property + def keepalive(self) -> int: + """ + Client keepalive interval (in seconds). + + This property may not be changed if the connection is already open. + """ + return self._keepalive + + @keepalive.setter + def keepalive(self, value: int) -> None: + if not self._connection_closed(): + # The issue here is that the previous value of keepalive matter to possibly + # sent ping packet. + raise RuntimeError("updating keepalive on established connection is not supported") + + if value < 0: + raise ValueError("Keepalive must be >=0.") + + self._keepalive = value + + @property + def transport(self) -> Literal["tcp", "websockets", "unix"]: + """ + Transport method used for the connection ("tcp" or "websockets"). + + This property may not be changed if the connection is already open. + """ + return self._transport + + @transport.setter + def transport(self, value: Literal["tcp", "websockets"]) -> None: + if not self._connection_closed(): + raise RuntimeError("updating transport on established connection is not supported") + + self._transport = value + + @property + def protocol(self) -> MQTTProtocolVersion: + """ + Protocol version used (MQTT v3, MQTT v3.11, MQTTv5) + + This property is read-only. + """ + return self._protocol + + @property + def connect_timeout(self) -> float: + """ + Connection establishment timeout in seconds. + + This property may not be changed if the connection is already open. + """ + return self._connect_timeout + + @connect_timeout.setter + def connect_timeout(self, value: float) -> None: + if not self._connection_closed(): + raise RuntimeError("updating connect_timeout on established connection is not supported") + + if value <= 0.0: + raise ValueError("timeout must be a positive number") + + self._connect_timeout = value + + @property + def username(self) -> str | None: + """The username used to connect to the MQTT broker, or None if no username is used. + + This property may not be changed if the connection is already open. + """ + if self._username is None: + return None + return self._username.decode("utf-8") + + @username.setter + def username(self, value: str | None) -> None: + if not self._connection_closed(): + raise RuntimeError("updating username on established connection is not supported") + + if value is None: + self._username = None + else: + self._username = value.encode("utf-8") + + @property + def password(self) -> str | None: + """The password used to connect to the MQTT broker, or None if no password is used. + + This property may not be changed if the connection is already open. + """ + if self._password is None: + return None + return self._password.decode("utf-8") + + @password.setter + def password(self, value: str | None) -> None: + if not self._connection_closed(): + raise RuntimeError("updating password on established connection is not supported") + + if value is None: + self._password = None + else: + self._password = value.encode("utf-8") + + @property + def max_inflight_messages(self) -> int: + """ + Maximum number of messages with QoS > 0 that can be partway through the network flow at once + + This property may not be changed if the connection is already open. + """ + return self._max_inflight_messages + + @max_inflight_messages.setter + def max_inflight_messages(self, value: int) -> None: + if not self._connection_closed(): + # Not tested. Some doubt that everything is okay when max_inflight change between 0 + # and > 0 value because _update_inflight is skipped when _max_inflight_messages == 0 + raise RuntimeError("updating max_inflight_messages on established connection is not supported") + + if value < 0: + raise ValueError("Invalid inflight.") + + self._max_inflight_messages = value + + @property + def max_queued_messages(self) -> int: + """ + Maximum number of message in the outgoing message queue, 0 means unlimited + + This property may not be changed if the connection is already open. + """ + return self._max_queued_messages + + @max_queued_messages.setter + def max_queued_messages(self, value: int) -> None: + if not self._connection_closed(): + # Not tested. + raise RuntimeError("updating max_queued_messages on established connection is not supported") + + if value < 0: + raise ValueError("Invalid queue size.") + + self._max_queued_messages = value + + @property + def will_topic(self) -> str | None: + """ + The topic name a will message is sent to when disconnecting unexpectedly. None if a will shall not be sent. + + This property is read-only. Use `will_set()` to change its value. + """ + if self._will_topic is None: + return None + + return self._will_topic.decode("utf-8") + + @property + def will_payload(self) -> bytes | None: + """ + The payload for the will message that is sent when disconnecting unexpectedly. None if a will shall not be sent. + + This property is read-only. Use `will_set()` to change its value. + """ + return self._will_payload + + @property + def logger(self) -> logging.Logger | None: + return self._logger + + @logger.setter + def logger(self, value: logging.Logger | None) -> None: + self._logger = value + + def _sock_recv(self, bufsize: int) -> bytes: + if self._sock is None: + raise ConnectionError("self._sock is None") try: return self._sock.recv(bufsize) - except ssl.SSLWantReadError: - raise BlockingIOError - except ssl.SSLWantWriteError: + except ssl.SSLWantReadError as err: + raise BlockingIOError() from err + except ssl.SSLWantWriteError as err: self._call_socket_register_write() - raise BlockingIOError + raise BlockingIOError() from err + except AttributeError as err: + self._easy_log( + MQTT_LOG_DEBUG, "socket was None: %s", err) + raise ConnectionError() from err + + def _sock_send(self, buf: bytes) -> int: + if self._sock is None: + raise ConnectionError("self._sock is None") - def _sock_send(self, buf): try: return self._sock.send(buf) - except ssl.SSLWantReadError: - raise BlockingIOError - except ssl.SSLWantWriteError: + except ssl.SSLWantReadError as err: + raise BlockingIOError() from err + except ssl.SSLWantWriteError as err: self._call_socket_register_write() - raise BlockingIOError - except BlockingIOError: + raise BlockingIOError() from err + except BlockingIOError as err: self._call_socket_register_write() - raise BlockingIOError + raise BlockingIOError() from err - def _sock_close(self): + def _sock_close(self) -> None: """Close the connection to the server.""" if not self._sock: return @@ -670,8 +1134,8 @@ def _sock_close(self): # In case a callback fails, still close the socket to avoid leaking the file descriptor. sock.close() - def _reset_sockets(self, sockpair_only=False): - if sockpair_only == False: + def _reset_sockets(self, sockpair_only: bool = False) -> None: + if not sockpair_only: self._sock_close() if self._sockpairR: @@ -681,21 +1145,30 @@ def _reset_sockets(self, sockpair_only=False): self._sockpairW.close() self._sockpairW = None - def reinitialise(self, client_id="", clean_session=True, userdata=None): + def reinitialise( + self, + client_id: str = "", + clean_session: bool = True, + userdata: Any = None, + ) -> None: self._reset_sockets() - self.__init__(client_id, clean_session, userdata) + self.__init__(client_id, clean_session, userdata) # type: ignore[misc] - def ws_set_options(self, path="/mqtt", headers=None): + def ws_set_options( + self, + path: str = "/mqtt", + headers: WebSocketHeaders | None = None, + ) -> None: """ Set the path and headers for a websocket connection - path is a string starting with / which should be the endpoint of the - mqtt connection on the remote server + :param str path: a string starting with / which should be the endpoint of the + mqtt connection on the remote server - headers can be either a dict or a callable object. If it is a dict then - the extra items in the dict are added to the websocket headers. If it is - a callable, then the default websocket headers are passed into this - function and the result is used as the new headers. + :param headers: can be either a dict or a callable object. If it is a dict then + the extra items in the dict are added to the websocket headers. If it is + a callable, then the default websocket headers are passed into this + function and the result is used as the new headers. """ self._websocket_path = path @@ -706,24 +1179,21 @@ def ws_set_options(self, path="/mqtt", headers=None): raise ValueError( "'headers' option to ws_set_options has to be either a dictionary or callable") - def tls_set_context(self, context=None): + def tls_set_context( + self, + context: ssl.SSLContext | None = None, + ) -> None: """Configure network encryption and authentication context. Enables SSL/TLS support. - context : an ssl.SSLContext object. By default this is given by - `ssl.create_default_context()`, if available. + :param context: an ssl.SSLContext object. By default this is given by + ``ssl.create_default_context()``, if available. - Must be called before connect() or connect_async().""" + Must be called before `connect()`, `connect_async()` or `connect_srv()`.""" if self._ssl_context is not None: raise ValueError('SSL/TLS has already been configured.') - # Assume that have SSL support, or at least that context input behaves like ssl.SSLContext - # in current versions of Python - if context is None: - if hasattr(ssl, 'create_default_context'): - context = ssl.create_default_context() - else: - raise ValueError('SSL/TLS context must be specified') + context = ssl.create_default_context() self._ssl = True self._ssl_context = context @@ -732,46 +1202,59 @@ def tls_set_context(self, context=None): if hasattr(context, 'check_hostname'): self._tls_insecure = not context.check_hostname - def tls_set(self, ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tls_version=None, ciphers=None, keyfile_password=None): + def tls_set( + self, + ca_certs: str | None = None, + certfile: str | None = None, + keyfile: str | None = None, + cert_reqs: ssl.VerifyMode | None = None, + tls_version: int | None = None, + ciphers: str | None = None, + keyfile_password: str | None = None, + alpn_protocols: list[str] | None = None, + ) -> None: """Configure network encryption and authentication options. Enables SSL/TLS support. - ca_certs : a string path to the Certificate Authority certificate files - that are to be treated as trusted by this client. If this is the only - option given then the client will operate in a similar manner to a web - browser. That is to say it will require the broker to have a - certificate signed by the Certificate Authorities in ca_certs and will - communicate using TLS v1,2, but will not attempt any form of - authentication. This provides basic network encryption but may not be - sufficient depending on how the broker is configured. - By default, on Python 2.7.9+ or 3.4+, the default certification - authority of the system is used. On older Python version this parameter - is mandatory. - - certfile and keyfile are strings pointing to the PEM encoded client - certificate and private keys respectively. If these arguments are not - None then they will be used as client information for TLS based - authentication. Support for this feature is broker dependent. Note - that if either of these files in encrypted and needs a password to - decrypt it, then this can be passed using the keyfile_password - argument - you should take precautions to ensure that your password is - not hard coded into your program by loading the password from a file - for example. If you do not provide keyfile_password, the password will - be requested to be typed in at a terminal window. - - cert_reqs allows the certificate requirements that the client imposes - on the broker to be changed. By default this is ssl.CERT_REQUIRED, - which means that the broker must provide a certificate. See the ssl - pydoc for more information on this parameter. - - tls_version allows the version of the SSL/TLS protocol used to be - specified. By default TLS v1.2 is used. Previous versions are allowed - but not recommended due to possible security problems. - - ciphers is a string specifying which encryption ciphers are allowable - for this connection, or None to use the defaults. See the ssl pydoc for - more information. - - Must be called before connect() or connect_async().""" + :param str ca_certs: a string path to the Certificate Authority certificate files + that are to be treated as trusted by this client. If this is the only + option given then the client will operate in a similar manner to a web + browser. That is to say it will require the broker to have a + certificate signed by the Certificate Authorities in ca_certs and will + communicate using TLS v1,2, but will not attempt any form of + authentication. This provides basic network encryption but may not be + sufficient depending on how the broker is configured. + + By default, on Python 2.7.9+ or 3.4+, the default certification + authority of the system is used. On older Python version this parameter + is mandatory. + :param str certfile: PEM encoded client certificate filename. Used with + keyfile for client TLS based authentication. Support for this feature is + broker dependent. Note that if the files in encrypted and needs a password to + decrypt it, then this can be passed using the keyfile_password argument - you + should take precautions to ensure that your password is + not hard coded into your program by loading the password from a file + for example. If you do not provide keyfile_password, the password will + be requested to be typed in at a terminal window. + :param str keyfile: PEM encoded client private keys filename. Used with + certfile for client TLS based authentication. Support for this feature is + broker dependent. Note that if the files in encrypted and needs a password to + decrypt it, then this can be passed using the keyfile_password argument - you + should take precautions to ensure that your password is + not hard coded into your program by loading the password from a file + for example. If you do not provide keyfile_password, the password will + be requested to be typed in at a terminal window. + :param cert_reqs: the certificate requirements that the client imposes + on the broker to be changed. By default this is ssl.CERT_REQUIRED, + which means that the broker must provide a certificate. See the ssl + pydoc for more information on this parameter. + :param tls_version: the version of the SSL/TLS protocol used to be + specified. By default TLS v1.2 is used. Previous versions are allowed + but not recommended due to possible security problems. + :param str ciphers: encryption ciphers that are allowed + for this connection, or None to use the defaults. See the ssl pydoc for + more information. + + Must be called before `connect()`, `connect_async()` or `connect_srv()`.""" if ssl is None: raise ValueError('This platform has no SSL/TLS.') @@ -787,11 +1270,17 @@ def tls_set(self, ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tl if tls_version is None: tls_version = ssl.PROTOCOL_TLSv1_2 # If the python version supports it, use highest TLS version automatically - if hasattr(ssl, "PROTOCOL_TLS"): + if hasattr(ssl, "PROTOCOL_TLS_CLIENT"): + # This also enables CERT_REQUIRED and check_hostname by default. + tls_version = ssl.PROTOCOL_TLS_CLIENT + elif hasattr(ssl, "PROTOCOL_TLS"): tls_version = ssl.PROTOCOL_TLS context = ssl.SSLContext(tls_version) # Configure context + if ciphers is not None: + context.set_ciphers(ciphers) + if certfile is not None: context.load_cert_chain(certfile, keyfile, keyfile_password) @@ -805,8 +1294,10 @@ def tls_set(self, ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tl else: context.load_default_certs() - if ciphers is not None: - context.set_ciphers(ciphers) + if alpn_protocols is not None: + if not getattr(ssl, "HAS_ALPN", None): + raise ValueError("SSL library has no support for ALPN") + context.set_alpn_protocols(alpn_protocols) self.tls_set_context(context) @@ -818,7 +1309,7 @@ def tls_set(self, ca_certs=None, certfile=None, keyfile=None, cert_reqs=None, tl # But with ssl.CERT_NONE, we can not check_hostname self.tls_insecure_set(True) - def tls_insecure_set(self, value): + def tls_insecure_set(self, value: bool) -> None: """Configure verification of the server hostname in the server certificate. If value is set to true, it is impossible to guarantee that the host @@ -830,8 +1321,8 @@ def tls_insecure_set(self, value): Do not use this function in a real system. Setting value to true means there is no point using encryption. - Must be called before connect() and after either tls_set() or - tls_set_context().""" + Must be called before `connect()` and after either `tls_set()` or + `tls_set_context()`.""" if self._ssl_context is None: raise ValueError( @@ -845,7 +1336,7 @@ def tls_insecure_set(self, value): # If verify_mode is CERT_NONE then the host name will never be checked self._ssl_context.check_hostname = not value - def proxy_set(self, **proxy_args): + def proxy_set(self, **proxy_args: Any) -> None: """Configure proxying of MQTT connection. Enables support for SOCKS or HTTP proxies. @@ -853,16 +1344,23 @@ def proxy_set(self, **proxy_args): proxy_args parameters are below; see the PySocks docs for more info. (Required) - proxy_type: One of {socks.HTTP, socks.SOCKS4, or socks.SOCKS5} - proxy_addr: IP address or DNS name of proxy server + + :param proxy_type: One of {socks.HTTP, socks.SOCKS4, or socks.SOCKS5} + :param proxy_addr: IP address or DNS name of proxy server (Optional) - proxy_rdns: boolean indicating whether proxy lookup should be performed + + :param proxy_port: (int) port number of the proxy server. If not provided, + the PySocks package default value will be utilized, which differs by proxy_type. + :param proxy_rdns: boolean indicating whether proxy lookup should be performed remotely (True, default) or locally (False) - proxy_username: username for SOCKS5 proxy, or userid for SOCKS4 proxy - proxy_password: password for SOCKS5 proxy + :param proxy_username: username for SOCKS5 proxy, or userid for SOCKS4 proxy + :param proxy_password: password for SOCKS5 proxy + + Example:: - Must be called before connect() or connect_async().""" + mqttc.proxy_set(proxy_type=socks.HTTP, proxy_addr='1.2.3.4', proxy_port=4231) + """ if socks is None: raise ValueError("PySocks must be installed for proxy support.") elif not self._proxy_is_valid(proxy_args): @@ -870,35 +1368,58 @@ def proxy_set(self, **proxy_args): else: self._proxy = proxy_args - def enable_logger(self, logger=None): - """ Enables a logger to send log messages to """ + def enable_logger(self, logger: logging.Logger | None = None) -> None: + """ + Enables a logger to send log messages to + + :param logging.Logger logger: if specified, that ``logging.Logger`` object will be used, otherwise + one will be created automatically. + + See `disable_logger` to undo this action. + """ if logger is None: if self._logger is not None: # Do not replace existing logger return logger = logging.getLogger(__name__) - self._logger = logger + self.logger = logger - def disable_logger(self): + def disable_logger(self) -> None: + """ + Disable logging using standard python logging package. This has no effect on the `on_log` callback. + """ self._logger = None - def connect(self, host, port=1883, keepalive=60, bind_address="", bind_port=0, - clean_start=MQTT_CLEAN_START_FIRST_ONLY, properties=None): - """Connect to a remote broker. - - host is the hostname or IP address of the remote broker. - port is the network port of the server host to connect to. Defaults to - 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you - are using tls_set() the port may need providing. - keepalive: Maximum period in seconds between communications with the - broker. If no other messages are being exchanged, this controls the - rate at which the client will send ping messages to the broker. - clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY. - Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only, - respectively. MQTT session data (such as outstanding messages and subscriptions) - is cleared on successful connect when the clean_start flag is set. - properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the - MQTT connect packet. + def connect( + self, + host: str, + port: int = 1883, + keepalive: int = 60, + bind_address: str = "", + bind_port: int = 0, + clean_start: CleanStartOption = MQTT_CLEAN_START_FIRST_ONLY, + properties: Properties | None = None, + ) -> MQTTErrorCode: + """Connect to a remote broker. This is a blocking call that establishes + the underlying connection and transmits a CONNECT packet. + Note that the connection status will not be updated until a CONNACK is received and + processed (this requires a running network loop, see `loop_start`, `loop_forever`, `loop`...). + + :param str host: the hostname or IP address of the remote broker. + :param int port: the network port of the server host to connect to. Defaults to + 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you + are using `tls_set()` the port may need providing. + :param int keepalive: Maximum period in seconds between communications with the + broker. If no other messages are being exchanged, this controls the + rate at which the client will send ping messages to the broker. + :param bool clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY. + Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only, + respectively. MQTT session data (such as outstanding messages and subscriptions) + is cleared on successful connect when the clean_start flag is set. + For MQTT v3.1.1, the ``clean_session`` argument of `Client` should be used for similar + result. + :param Properties properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the + MQTT connect packet. """ if self._protocol == MQTTv5: @@ -906,97 +1427,110 @@ def connect(self, host, port=1883, keepalive=60, bind_address="", bind_port=0, else: if clean_start != MQTT_CLEAN_START_FIRST_ONLY: raise ValueError("Clean start only applies to MQTT V5") - if properties != None: + if properties: raise ValueError("Properties only apply to MQTT V5") self.connect_async(host, port, keepalive, bind_address, bind_port, clean_start, properties) return self.reconnect() - def connect_srv(self, domain=None, keepalive=60, bind_address="", - clean_start=MQTT_CLEAN_START_FIRST_ONLY, properties=None): + def connect_srv( + self, + domain: str | None = None, + keepalive: int = 60, + bind_address: str = "", + bind_port: int = 0, + clean_start: CleanStartOption = MQTT_CLEAN_START_FIRST_ONLY, + properties: Properties | None = None, + ) -> MQTTErrorCode: """Connect to a remote broker. - domain is the DNS domain to search for SRV records; if None, - try to determine local domain name. - keepalive, bind_address, clean_start and properties are as for connect() + :param str domain: the DNS domain to search for SRV records; if None, + try to determine local domain name. + :param keepalive, bind_address, clean_start and properties: see `connect()` """ if HAVE_DNS is False: raise ValueError( - 'No DNS resolver library found, try "pip install dnspython" or "pip3 install dnspython3".') + 'No DNS resolver library found, try "pip install dnspython".') if domain is None: domain = socket.getfqdn() domain = domain[domain.find('.') + 1:] try: - rr = '_mqtt._tcp.%s' % domain + rr = f'_mqtt._tcp.{domain}' if self._ssl: # IANA specifies secure-mqtt (not mqtts) for port 8883 - rr = '_secure-mqtt._tcp.%s' % domain + rr = f'_secure-mqtt._tcp.{domain}' answers = [] for answer in dns.resolver.query(rr, dns.rdatatype.SRV): addr = answer.target.to_text()[:-1] answers.append( (addr, answer.port, answer.priority, answer.weight)) - except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers): - raise ValueError("No answer/NXDOMAIN for SRV in %s" % (domain)) + except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers) as err: + raise ValueError(f"No answer/NXDOMAIN for SRV in {domain}") from err # FIXME: doesn't account for weight for answer in answers: host, port, prio, weight = answer try: - return self.connect(host, port, keepalive, bind_address, clean_start, properties) - except Exception: + return self.connect(host, port, keepalive, bind_address, bind_port, clean_start, properties) + except Exception: # noqa: S110 pass raise ValueError("No SRV hosts responded") - def connect_async(self, host, port=1883, keepalive=60, bind_address="", bind_port=0, - clean_start=MQTT_CLEAN_START_FIRST_ONLY, properties=None): + def connect_async( + self, + host: str, + port: int = 1883, + keepalive: int = 60, + bind_address: str = "", + bind_port: int = 0, + clean_start: CleanStartOption = MQTT_CLEAN_START_FIRST_ONLY, + properties: Properties | None = None, + ) -> None: """Connect to a remote broker asynchronously. This is a non-blocking - connect call that can be used with loop_start() to provide very quick + connect call that can be used with `loop_start()` to provide very quick start. - host is the hostname or IP address of the remote broker. - port is the network port of the server host to connect to. Defaults to - 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you - are using tls_set() the port may need providing. - keepalive: Maximum period in seconds between communications with the - broker. If no other messages are being exchanged, this controls the - rate at which the client will send ping messages to the broker. - clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY. - Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only, - respectively. MQTT session data (such as outstanding messages and subscriptions) - is cleared on successful connect when the clean_start flag is set. - properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the - MQTT connect packet. Use the Properties class. - """ - if host is None or len(host) == 0: - raise ValueError('Invalid host.') - if port <= 0: - raise ValueError('Invalid port number.') - if keepalive < 0: - raise ValueError('Keepalive must be >=0.') - if bind_address != "" and bind_address is not None: - if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2): - raise ValueError('bind_address requires Python 2.7 or 3.2.') + Any already established connection will be terminated immediately. + + :param str host: the hostname or IP address of the remote broker. + :param int port: the network port of the server host to connect to. Defaults to + 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you + are using `tls_set()` the port may need providing. + :param int keepalive: Maximum period in seconds between communications with the + broker. If no other messages are being exchanged, this controls the + rate at which the client will send ping messages to the broker. + :param bool clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY. + Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only, + respectively. MQTT session data (such as outstanding messages and subscriptions) + is cleared on successful connect when the clean_start flag is set. + For MQTT v3.1.1, the ``clean_session`` argument of `Client` should be used for similar + result. + :param Properties properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the + MQTT connect packet. + """ if bind_port < 0: raise ValueError('Invalid bind port number.') - self._host = host - self._port = port - self._keepalive = keepalive + # Switch to state NEW to allow update of host, port & co. + self._sock_close() + self._state = _ConnectionState.MQTT_CS_NEW + + self.host = host + self.port = port + self.keepalive = keepalive self._bind_address = bind_address self._bind_port = bind_port self._clean_start = clean_start self._connect_properties = properties - self._state = mqtt_cs_connect_async - + self._state = _ConnectionState.MQTT_CS_CONNECT_ASYNC - def reconnect_delay_set(self, min_delay=1, max_delay=120): + def reconnect_delay_set(self, min_delay: int = 1, max_delay: int = 120) -> None: """ Configure the exponential reconnect delay When connection is lost, wait initially min_delay seconds and @@ -1009,7 +1543,7 @@ def reconnect_delay_set(self, min_delay=1, max_delay=120): self._reconnect_max_delay = max_delay self._reconnect_delay = None - def reconnect(self): + def reconnect(self) -> MQTTErrorCode: """Reconnect the client after a disconnect. Can only be called after connect()/connect_async().""" if len(self._host) == 0: @@ -1025,88 +1559,69 @@ def reconnect(self): "remaining_length": 0, "packet": bytearray(b""), "to_process": 0, - "pos": 0} + "pos": 0, + } + + self._ping_t = 0.0 + self._state = _ConnectionState.MQTT_CS_CONNECTING - self._out_packet = collections.deque() + self._sock_close() + + # Mark all currently outgoing QoS = 0 packets as lost, + # or `wait_for_publish()` could hang forever + for pkt in self._out_packet: + if pkt["command"] & 0xF0 == PUBLISH and pkt["qos"] == 0 and pkt["info"] is not None: + pkt["info"].rc = MQTT_ERR_CONN_LOST + pkt["info"]._set_as_published() + + self._out_packet.clear() with self._msgtime_mutex: self._last_msg_in = time_func() self._last_msg_out = time_func() - self._ping_t = 0 - self._state = mqtt_cs_new - - self._sock_close() - # Put messages in progress in a valid state. self._messages_reconnect_reset() - sock = self._create_socket_connection() - - if self._ssl: - # SSL is only supported when SSLContext is available (implies Python >= 2.7.9 or >= 3.2) + with self._callback_mutex: + on_pre_connect = self.on_pre_connect - verify_host = not self._tls_insecure + if on_pre_connect: try: - # Try with server_hostname, even it's not supported in certain scenarios - sock = self._ssl_context.wrap_socket( - sock, - server_hostname=self._host, - do_handshake_on_connect=False, - ) - except ssl.CertificateError: - # CertificateError is derived from ValueError - raise - except ValueError: - # Python version requires SNI in order to handle server_hostname, but SNI is not available - sock = self._ssl_context.wrap_socket( - sock, - do_handshake_on_connect=False, - ) - else: - # If SSL context has already checked hostname, then don't need to do it again - if (hasattr(self._ssl_context, 'check_hostname') and - self._ssl_context.check_hostname): - verify_host = False - - sock.settimeout(self._keepalive) - sock.do_handshake() - - if verify_host: - ssl.match_hostname(sock.getpeercert(), self._host) + on_pre_connect(self, self._userdata) + except Exception as err: + self._easy_log( + MQTT_LOG_ERR, 'Caught exception in on_pre_connect: %s', err) + if not self.suppress_exceptions: + raise - if self._transport == "websockets": - sock.settimeout(self._keepalive) - sock = WebsocketWrapper(sock, self._host, self._port, self._ssl, - self._websocket_path, self._websocket_extra_headers) + self._sock = self._create_socket() - self._sock = sock - self._sock.setblocking(0) + self._sock.setblocking(False) # type: ignore[attr-defined] self._registered_write = False - self._call_socket_open() + self._call_socket_open(self._sock) return self._send_connect(self._keepalive) - def loop(self, timeout=1.0, max_packets=1): + def loop(self, timeout: float = 1.0) -> MQTTErrorCode: """Process network events. - It is strongly recommended that you use loop_start(), or - loop_forever(), or if you are using an external event loop using - loop_read(), loop_write(), and loop_misc(). Using loop() on it's own is + It is strongly recommended that you use `loop_start()`, or + `loop_forever()`, or if you are using an external event loop using + `loop_read()`, `loop_write()`, and `loop_misc()`. Using loop() on it's own is no longer recommended. This function must be called regularly to ensure communication with the broker is carried out. It calls select() on the network socket to wait for network events. If incoming data is present it will then be - processed. Outgoing commands, from e.g. publish(), are normally sent + processed. Outgoing commands, from e.g. `publish()`, are normally sent immediately that their function is called, but this is not always possible. loop() will also attempt to send any remaining outgoing messages, which also includes commands that are part of the flow for messages with QoS>0. - timeout: The time in seconds to wait for incoming/outgoing network + :param int timeout: The time in seconds to wait for incoming/outgoing network traffic before timing out and returning. - max_packets: Not currently used. Returns MQTT_ERR_SUCCESS on success. Returns >0 on error. @@ -1119,21 +1634,19 @@ def loop(self, timeout=1.0, max_packets=1): return self._loop(timeout) - def _loop(self, timeout=1.0): + def _loop(self, timeout: float = 1.0) -> MQTTErrorCode: if timeout < 0.0: raise ValueError('Invalid timeout.') - try: - packet = self._out_packet.popleft() - self._out_packet.appendleft(packet) + if self.want_write(): wlist = [self._sock] - except IndexError: + else: wlist = [] # used to check if there are any bytes left in the (SSL) socket pending_bytes = 0 if hasattr(self._sock, 'pending'): - pending_bytes = self._sock.pending() + pending_bytes = self._sock.pending() # type: ignore[union-attr] # if bytes are pending do not wait in select if pending_bytes > 0: @@ -1150,15 +1663,24 @@ def _loop(self, timeout=1.0): socklist = select.select(rlist, wlist, [], timeout) except TypeError: # Socket isn't correct type, in likelihood connection is lost - return MQTT_ERR_CONN_LOST + # ... or we called disconnect(). In that case the socket will + # be closed but some loop (like loop_forever) will continue to + # call _loop(). We still want to break that loop by returning an + # rc != MQTT_ERR_SUCCESS and we don't want state to change from + # mqtt_cs_disconnecting. + if self._state not in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): + self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST except ValueError: # Can occur if we just reconnected but rlist/wlist contain a -1 for # some reason. - return MQTT_ERR_CONN_LOST + if self._state not in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): + self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST except Exception: # Note that KeyboardInterrupt, etc. can still terminate since they # are not derived from Exception - return MQTT_ERR_UNKNOWN + return MQTTErrorCode.MQTT_ERR_UNKNOWN if self._sock in socklist[0] or pending_bytes > 0: rc = self.loop_read() @@ -1184,32 +1706,38 @@ def _loop(self, timeout=1.0): return self.loop_misc() - def publish(self, topic, payload=None, qos=0, retain=False, properties=None): + def publish( + self, + topic: str, + payload: PayloadType = None, + qos: int = 0, + retain: bool = False, + properties: Properties | None = None, + ) -> MQTTMessageInfo: """Publish a message on a topic. This causes a message to be sent to the broker and subsequently from the broker to any clients subscribing to matching topics. - topic: The topic that the message should be published on. - payload: The actual message to send. If not given, or set to None a - zero length message will be used. Passing an int or float will result - in the payload being converted to a string representing that number. If - you wish to send a true int/float, use struct.pack() to create the - payload you require. - qos: The quality of service level to use. - retain: If set to true, the message will be set as the "last known - good"/retained message for the topic. - properties: (MQTT v5.0 only) the MQTT v5.0 properties to be included. - Use the Properties class. - - Returns a MQTTMessageInfo class, which can be used to determine whether - the message has been delivered (using info.is_published()) or to block - waiting for the message to be delivered (info.wait_for_publish()). The + :param str topic: The topic that the message should be published on. + :param payload: The actual message to send. If not given, or set to None a + zero length message will be used. Passing an int or float will result + in the payload being converted to a string representing that number. If + you wish to send a true int/float, use struct.pack() to create the + payload you require. + :param int qos: The quality of service level to use. + :param bool retain: If set to true, the message will be set as the "last known + good"/retained message for the topic. + :param Properties properties: (MQTT v5.0 only) the MQTT v5.0 properties to be included. + + Returns a `MQTTMessageInfo` class, which can be used to determine whether + the message has been delivered (using `is_published()`) or to block + waiting for the message to be delivered (`wait_for_publish()`). The message ID and return code of the publish() call can be found at - info.mid and info.rc. + :py:attr:`info.mid ` and :py:attr:`info.rc `. - For backwards compatibility, the MQTTMessageInfo class is iterable so - the old construct of (rc, mid) = client.publish(...) is still valid. + For backwards compatibility, the `MQTTMessageInfo` class is iterable so + the old construct of ``(rc, mid) = client.publish(...)`` is still valid. rc is MQTT_ERR_SUCCESS to indicate success or MQTT_ERR_NO_CONN if the client is not currently connected. mid is the message ID for the @@ -1217,35 +1745,24 @@ def publish(self, topic, payload=None, qos=0, retain=False, properties=None): by checking against the mid argument in the on_publish() callback if it is defined. - A ValueError will be raised if topic is None, has zero length or is - invalid (contains a wildcard), except if the MQTT version used is v5.0. - For v5.0, a zero length topic can be used when a Topic Alias has been set. - - A ValueError will be raised if qos is not one of 0, 1 or 2, or if - the length of the payload is greater than 268435455 bytes.""" + :raises ValueError: if topic is None, has zero length or is + invalid (contains a wildcard), except if the MQTT version used is v5.0. + For v5.0, a zero length topic can be used when a Topic Alias has been set. + :raises ValueError: if qos is not one of 0, 1 or 2 + :raises ValueError: if the length of the payload is greater than 268435455 bytes. + """ if self._protocol != MQTTv5: if topic is None or len(topic) == 0: raise ValueError('Invalid topic.') - topic = topic.encode('utf-8') + topic_bytes = topic.encode('utf-8') - if self._topic_wildcard_len_check(topic) != MQTT_ERR_SUCCESS: - raise ValueError('Publish topic cannot contain wildcards.') + self._raise_for_invalid_topic(topic_bytes) if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') - if isinstance(payload, unicode): - local_payload = payload.encode('utf-8') - elif isinstance(payload, (bytes, bytearray)): - local_payload = payload - elif isinstance(payload, (int, float)): - local_payload = str(payload).encode('ascii') - elif payload is None: - local_payload = b'' - else: - raise TypeError( - 'payload must be a string, bytearray, int, float or None.') + local_payload = _encode_payload(payload) if len(local_payload) > 268435455: raise ValueError('Payload too large.') @@ -1255,11 +1772,11 @@ def publish(self, topic, payload=None, qos=0, retain=False, properties=None): if qos == 0: info = MQTTMessageInfo(local_mid) rc = self._send_publish( - local_mid, topic, local_payload, qos, retain, False, info, properties) + local_mid, topic_bytes, local_payload, qos, retain, False, info, properties) info.rc = rc return info else: - message = MQTTMessage(local_mid, topic) + message = MQTTMessage(local_mid, topic_bytes) message.timestamp = time_func() message.payload = local_payload message.qos = qos @@ -1269,11 +1786,11 @@ def publish(self, topic, payload=None, qos=0, retain=False, properties=None): with self._out_message_mutex: if self._max_queued_messages > 0 and len(self._out_messages) >= self._max_queued_messages: - message.info.rc = MQTT_ERR_QUEUE_SIZE + message.info.rc = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE return message.info if local_mid in self._out_messages: - message.info.rc = MQTT_ERR_QUEUE_SIZE + message.info.rc = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE return message.info self._out_messages[message.mid] = message @@ -1284,11 +1801,11 @@ def publish(self, topic, payload=None, qos=0, retain=False, properties=None): elif qos == 2: message.state = mqtt_ms_wait_for_pubrec - rc = self._send_publish(message.mid, topic, message.payload, message.qos, message.retain, + rc = self._send_publish(message.mid, topic_bytes, message.payload, message.qos, message.retain, message.dup, message.info, message.properties) # remove from inflight messages so it will be send after a connection is made - if rc is MQTT_ERR_NO_CONN: + if rc == MQTTErrorCode.MQTT_ERR_NO_CONN: self._inflight_messages -= 1 message.state = mqtt_ms_publish @@ -1296,32 +1813,35 @@ def publish(self, topic, payload=None, qos=0, retain=False, properties=None): return message.info else: message.state = mqtt_ms_queued - message.info.rc = MQTT_ERR_SUCCESS + message.info.rc = MQTTErrorCode.MQTT_ERR_SUCCESS return message.info - def username_pw_set(self, username, password=None): + def username_pw_set( + self, username: str | None, password: str | None = None + ) -> None: """Set a username and optionally a password for broker authentication. Must be called before connect() to have any effect. - Requires a broker that supports MQTT v3.1. + Requires a broker that supports MQTT v3.1 or more. - username: The username to authenticate with. Need have no relationship to the client id. Must be unicode + :param str username: The username to authenticate with. Need have no relationship to the client id. Must be str [MQTT-3.1.3-11]. Set to None to reset client back to not using username/password for broker authentication. - password: The password to authenticate with. Optional, set to None if not required. If it is unicode, then it + :param str password: The password to authenticate with. Optional, set to None if not required. If it is str, then it will be encoded as UTF-8. """ # [MQTT-3.1.3-11] User name must be UTF-8 encoded string self._username = None if username is None else username.encode('utf-8') - self._password = password - if isinstance(self._password, unicode): - self._password = self._password.encode('utf-8') + if isinstance(password, str): + self._password = password.encode('utf-8') + else: + self._password = password - def enable_bridge_mode(self): + def enable_bridge_mode(self) -> None: """Sets the client in a bridge mode instead of client mode. - Must be called before connect() to have any effect. + Must be called before `connect()` to have any effect. Requires brokers that support bridge mode. Under bridge mode, the broker will identify the client as a bridge and @@ -1334,30 +1854,50 @@ def enable_bridge_mode(self): """ self._client_mode = MQTT_BRIDGE - def is_connected(self): + def _connection_closed(self) -> bool: + """ + Return true if the connection is closed (and not trying to be opened). + """ + return ( + self._state == _ConnectionState.MQTT_CS_NEW + or (self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED) and self._sock is None)) + + def is_connected(self) -> bool: """Returns the current status of the connection True if connection exists False if connection is closed """ - return self._state == mqtt_cs_connected + return self._state == _ConnectionState.MQTT_CS_CONNECTED - def disconnect(self, reasoncode=None, properties=None): + def disconnect( + self, + reasoncode: ReasonCode | None = None, + properties: Properties | None = None, + ) -> MQTTErrorCode: """Disconnect a connected client from the broker. - reasoncode: (MQTT v5.0 only) a ReasonCodes instance setting the MQTT v5.0 - reasoncode to be sent with the disconnect. It is optional, the receiver - then assuming that 0 (success) is the value. - properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties - to be included. Optional - if not set, no properties are sent. - """ - self._state = mqtt_cs_disconnecting + :param ReasonCode reasoncode: (MQTT v5.0 only) a ReasonCode instance setting the MQTT v5.0 + reasoncode to be sent with the disconnect packet. It is optional, the receiver + then assuming that 0 (success) is the value. + :param Properties properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties + to be included. Optional - if not set, no properties are sent. + """ if self._sock is None: + self._state = _ConnectionState.MQTT_CS_DISCONNECTED return MQTT_ERR_NO_CONN + else: + self._state = _ConnectionState.MQTT_CS_DISCONNECTING return self._send_disconnect(reasoncode, properties) - def subscribe(self, topic, qos=0, options=None, properties=None): + def subscribe( + self, + topic: str | tuple[str, int] | tuple[str, SubscribeOptions] | list[tuple[str, int]] | list[tuple[str, SubscribeOptions]], + qos: int = 0, + options: SubscribeOptions | None = None, + properties: Properties | None = None, + ) -> tuple[MQTTErrorCode, int | None]: """Subscribe the client to one or more topics. This function may be called in three different ways (and a further three for MQTT v5.0): @@ -1366,40 +1906,40 @@ def subscribe(self, topic, qos=0, options=None, properties=None): ------------------------- e.g. subscribe("my/topic", 2) - topic: A string specifying the subscription topic to subscribe to. - qos: The desired quality of service level for the subscription. - Defaults to 0. - options and properties: Not used. + :topic: A string specifying the subscription topic to subscribe to. + :qos: The desired quality of service level for the subscription. + Defaults to 0. + :options and properties: Not used. Simple string and subscribe options (MQTT v5.0 only) ---------------------------------------------------- e.g. subscribe("my/topic", options=SubscribeOptions(qos=2)) - topic: A string specifying the subscription topic to subscribe to. - qos: Not used. - options: The MQTT v5.0 subscribe options. - properties: a Properties instance setting the MQTT v5.0 properties - to be included. Optional - if not set, no properties are sent. + :topic: A string specifying the subscription topic to subscribe to. + :qos: Not used. + :options: The MQTT v5.0 subscribe options. + :properties: a Properties instance setting the MQTT v5.0 properties + to be included. Optional - if not set, no properties are sent. String and integer tuple ------------------------ e.g. subscribe(("my/topic", 1)) - topic: A tuple of (topic, qos). Both topic and qos must be present in + :topic: A tuple of (topic, qos). Both topic and qos must be present in the tuple. - qos and options: Not used. - properties: Only used for MQTT v5.0. A Properties instance setting the - MQTT v5.0 properties. Optional - if not set, no properties are sent. + :qos and options: Not used. + :properties: Only used for MQTT v5.0. A Properties instance setting the + MQTT v5.0 properties. Optional - if not set, no properties are sent. String and subscribe options tuple (MQTT v5.0 only) --------------------------------------------------- e.g. subscribe(("my/topic", SubscribeOptions(qos=1))) - topic: A tuple of (topic, SubscribeOptions). Both topic and subscribe + :topic: A tuple of (topic, SubscribeOptions). Both topic and subscribe options must be present in the tuple. - qos and options: Not used. - properties: a Properties instance setting the MQTT v5.0 properties - to be included. Optional - if not set, no properties are sent. + :qos and options: Not used. + :properties: a Properties instance setting the MQTT v5.0 properties + to be included. Optional - if not set, no properties are sent. List of string and integer tuples --------------------------------- @@ -1409,9 +1949,9 @@ def subscribe(self, topic, qos=0, options=None, properties=None): command, which is more efficient than using multiple calls to subscribe(). - topic: A list of tuple of format (topic, qos). Both topic and qos must + :topic: A list of tuple of format (topic, qos). Both topic and qos must be present in all of the tuples. - qos, options and properties: Not used. + :qos, options and properties: Not used. List of string and subscribe option tuples (MQTT v5.0 only) ----------------------------------------------------------- @@ -1421,11 +1961,11 @@ def subscribe(self, topic, qos=0, options=None, properties=None): command, which is more efficient than using multiple calls to subscribe(). - topic: A list of tuple of format (topic, SubscribeOptions). Both topic and subscribe + :topic: A list of tuple of format (topic, SubscribeOptions). Both topic and subscribe options must be present in all of the tuples. - qos and options: Not used. - properties: a Properties instance setting the MQTT v5.0 properties - to be included. Optional - if not set, no properties are sent. + :qos and options: Not used. + :properties: a Properties instance setting the MQTT v5.0 properties + to be included. Optional - if not set, no properties are sent. The function returns a tuple (result, mid), where result is MQTT_ERR_SUCCESS to indicate success or (MQTT_ERR_NO_CONN, None) if the @@ -1441,14 +1981,14 @@ def subscribe(self, topic, qos=0, options=None, properties=None): if isinstance(topic, tuple): if self._protocol == MQTTv5: - topic, options = topic + topic, options = topic # type: ignore if not isinstance(options, SubscribeOptions): raise ValueError( 'Subscribe options must be instance of SubscribeOptions class.') else: - topic, qos = topic + topic, qos = topic # type: ignore - if isinstance(topic, basestring): + if isinstance(topic, (bytes, str)): if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') if self._protocol == MQTTv5: @@ -1465,8 +2005,10 @@ def subscribe(self, topic, qos=0, options=None, properties=None): else: if topic is None or len(topic) == 0: raise ValueError('Invalid topic.') - topic_qos_list = [(topic.encode('utf-8'), qos)] + topic_qos_list = [(topic.encode('utf-8'), qos)] # type: ignore elif isinstance(topic, list): + if len(topic) == 0: + raise ValueError('Empty topic list') topic_qos_list = [] if self._protocol == MQTTv5: for t, o in topic: @@ -1478,11 +2020,11 @@ def subscribe(self, topic, qos=0, options=None, properties=None): topic_qos_list.append((t.encode('utf-8'), o)) else: for t, q in topic: - if q < 0 or q > 2: + if isinstance(q, SubscribeOptions) or q < 0 or q > 2: raise ValueError('Invalid QoS level.') - if t is None or len(t) == 0 or not isinstance(t, basestring): + if t is None or len(t) == 0 or not isinstance(t, (bytes, str)): raise ValueError('Invalid topic.') - topic_qos_list.append((t.encode('utf-8'), q)) + topic_qos_list.append((t.encode('utf-8'), q)) # type: ignore if topic_qos_list is None: raise ValueError("No topic specified, or incorrect topic type.") @@ -1495,13 +2037,15 @@ def subscribe(self, topic, qos=0, options=None, properties=None): return self._send_subscribe(False, topic_qos_list, properties) - def unsubscribe(self, topic, properties=None): + def unsubscribe( + self, topic: str | list[str], properties: Properties | None = None + ) -> tuple[MQTTErrorCode, int | None]: """Unsubscribe the client from one or more topics. - topic: A single string, or list of strings that are the subscription - topics to unsubscribe from. - properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties - to be included. Optional - if not set, no properties are sent. + :param topic: A single string, or list of strings that are the subscription + topics to unsubscribe from. + :param properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties + to be included. Optional - if not set, no properties are sent. Returns a tuple (result, mid), where result is MQTT_ERR_SUCCESS to indicate success or (MQTT_ERR_NO_CONN, None) if the client is not @@ -1510,20 +2054,20 @@ def unsubscribe(self, topic, properties=None): used to track the unsubscribe request by checking against the mid argument in the on_unsubscribe() callback if it is defined. - Raises a ValueError if topic is None or has zero string length, or is - not a string or list. + :raises ValueError: if topic is None or has zero string length, or is + not a string or list. """ topic_list = None if topic is None: raise ValueError('Invalid topic.') - if isinstance(topic, basestring): + if isinstance(topic, (bytes, str)): if len(topic) == 0: raise ValueError('Invalid topic.') topic_list = [topic.encode('utf-8')] elif isinstance(topic, list): topic_list = [] for t in topic: - if len(t) == 0 or not isinstance(t, basestring): + if len(t) == 0 or not isinstance(t, (bytes, str)): raise ValueError('Invalid topic.') topic_list.append(t.encode('utf-8')) @@ -1531,20 +2075,20 @@ def unsubscribe(self, topic, properties=None): raise ValueError("No topic specified, or incorrect topic type.") if self._sock is None: - return (MQTT_ERR_NO_CONN, None) + return (MQTTErrorCode.MQTT_ERR_NO_CONN, None) return self._send_unsubscribe(False, topic_list, properties) - def loop_read(self, max_packets=1): - """Process read network events. Use in place of calling loop() if you + def loop_read(self, max_packets: int = 1) -> MQTTErrorCode: + """Process read network events. Use in place of calling `loop()` if you wish to handle your client reads as part of your own application. - Use socket() to obtain the client socket to call select() or equivalent + Use `socket()` to obtain the client socket to call select() or equivalent on. - Do not use if you are using the threaded interface loop_start().""" + Do not use if you are using `loop_start()` or `loop_forever()`.""" if self._sock is None: - return MQTT_ERR_NO_CONN + return MQTTErrorCode.MQTT_ERR_NO_CONN max_packets = len(self._out_messages) + len(self._in_messages) if max_packets < 1: @@ -1552,59 +2096,54 @@ def loop_read(self, max_packets=1): for _ in range(0, max_packets): if self._sock is None: - return MQTT_ERR_NO_CONN + return MQTTErrorCode.MQTT_ERR_NO_CONN rc = self._packet_read() if rc > 0: return self._loop_rc_handle(rc) - elif rc == MQTT_ERR_AGAIN: - return MQTT_ERR_SUCCESS - return MQTT_ERR_SUCCESS + elif rc == MQTTErrorCode.MQTT_ERR_AGAIN: + return MQTTErrorCode.MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def loop_write(self, max_packets=1): - """Process write network events. Use in place of calling loop() if you + def loop_write(self) -> MQTTErrorCode: + """Process write network events. Use in place of calling `loop()` if you wish to handle your client writes as part of your own application. - Use socket() to obtain the client socket to call select() or equivalent + Use `socket()` to obtain the client socket to call select() or equivalent on. - Use want_write() to determine if there is data waiting to be written. + Use `want_write()` to determine if there is data waiting to be written. - Do not use if you are using the threaded interface loop_start().""" + Do not use if you are using `loop_start()` or `loop_forever()`.""" if self._sock is None: - return MQTT_ERR_NO_CONN + return MQTTErrorCode.MQTT_ERR_NO_CONN try: rc = self._packet_write() - if rc == MQTT_ERR_AGAIN: - return MQTT_ERR_SUCCESS + if rc == MQTTErrorCode.MQTT_ERR_AGAIN: + return MQTTErrorCode.MQTT_ERR_SUCCESS elif rc > 0: return self._loop_rc_handle(rc) else: - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS finally: if self.want_write(): self._call_socket_register_write() else: self._call_socket_unregister_write() - def want_write(self): + def want_write(self) -> bool: """Call to determine if there is network data waiting to be written. - Useful if you are calling select() yourself rather than using loop(). + Useful if you are calling select() yourself rather than using `loop()`, `loop_start()` or `loop_forever()`. """ - try: - packet = self._out_packet.popleft() - self._out_packet.appendleft(packet) - return True - except IndexError: - return False + return len(self._out_packet) > 0 - def loop_misc(self): - """Process miscellaneous network events. Use in place of calling loop() if you + def loop_misc(self) -> MQTTErrorCode: + """Process miscellaneous network events. Use in place of calling `loop()` if you wish to call select() or equivalent on. - Do not use if you are using the threaded interface loop_start().""" + Do not use if you are using `loop_start()` or `loop_forever()`.""" if self._sock is None: - return MQTT_ERR_NO_CONN + return MQTTErrorCode.MQTT_ERR_NO_CONN now = time_func() self._check_keepalive() @@ -1614,61 +2153,72 @@ def loop_misc(self): # This hasn't happened in the keepalive time so we should disconnect. self._sock_close() - if self._state == mqtt_cs_disconnecting: - rc = MQTT_ERR_SUCCESS + if self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): + self._state = _ConnectionState.MQTT_CS_DISCONNECTED + rc = MQTTErrorCode.MQTT_ERR_SUCCESS else: - rc = MQTT_ERR_KEEPALIVE + self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST + rc = MQTTErrorCode.MQTT_ERR_KEEPALIVE - self._do_on_disconnect(rc) + self._do_on_disconnect( + packet_from_broker=False, + v1_rc=rc, + ) - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def max_inflight_messages_set(self, inflight): + def max_inflight_messages_set(self, inflight: int) -> None: """Set the maximum number of messages with QoS>0 that can be part way through their network flow at once. Defaults to 20.""" - if inflight < 0: - raise ValueError('Invalid inflight.') - self._max_inflight_messages = inflight + self.max_inflight_messages = inflight - def max_queued_messages_set(self, queue_size): + def max_queued_messages_set(self, queue_size: int) -> Client: """Set the maximum number of messages in the outgoing message queue. 0 means unlimited.""" - if queue_size < 0: - raise ValueError('Invalid queue size.') if not isinstance(queue_size, int): raise ValueError('Invalid type of queue size.') - self._max_queued_messages = queue_size + self.max_queued_messages = queue_size return self - def message_retry_set(self, retry): - """No longer used, remove in version 2.0""" - pass - - def user_data_set(self, userdata): + def user_data_set(self, userdata: Any) -> None: """Set the user data variable passed to callbacks. May be any data type.""" self._userdata = userdata - def will_set(self, topic, payload=None, qos=0, retain=False, properties=None): + def user_data_get(self) -> Any: + """Get the user data variable passed to callbacks. May be any data type.""" + return self._userdata + + def will_set( + self, + topic: str, + payload: PayloadType = None, + qos: int = 0, + retain: bool = False, + properties: Properties | None = None, + ) -> None: """Set a Will to be sent by the broker in case the client disconnects unexpectedly. This must be called before connect() to have any effect. - topic: The topic that the will message should be published on. - payload: The message to send as a will. If not given, or set to None a - zero length message will be used as the will. Passing an int or float - will result in the payload being converted to a string representing - that number. If you wish to send a true int/float, use struct.pack() to - create the payload you require. - qos: The quality of service level to use for the will. - retain: If set to true, the will message will be set as the "last known - good"/retained message for the topic. - properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties - to be included with the will message. Optional - if not set, no properties are sent. - - Raises a ValueError if qos is not 0, 1 or 2, or if topic is None or has - zero string length. + :param str topic: The topic that the will message should be published on. + :param payload: The message to send as a will. If not given, or set to None a + zero length message will be used as the will. Passing an int or float + will result in the payload being converted to a string representing + that number. If you wish to send a true int/float, use struct.pack() to + create the payload you require. + :param int qos: The quality of service level to use for the will. + :param bool retain: If set to true, the will message will be set as the "last known + good"/retained message for the topic. + :param Properties properties: (MQTT v5.0 only) the MQTT v5.0 properties + to be included with the will message. Optional - if not set, no properties are sent. + + :raises ValueError: if qos is not 0, 1 or 2, or if topic is None or has + zero string length. + + See `will_clear` to clear will. Note that will are NOT send if the client disconnect cleanly + for example by calling `disconnect()`. """ if topic is None or len(topic) == 0: raise ValueError('Invalid topic.') @@ -1676,30 +2226,19 @@ def will_set(self, topic, payload=None, qos=0, retain=False, properties=None): if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') - if properties != None and not isinstance(properties, Properties): + if properties and not isinstance(properties, Properties): raise ValueError( "The properties argument must be an instance of the Properties class.") - if isinstance(payload, unicode): - self._will_payload = payload.encode('utf-8') - elif isinstance(payload, (bytes, bytearray)): - self._will_payload = payload - elif isinstance(payload, (int, float)): - self._will_payload = str(payload).encode('ascii') - elif payload is None: - self._will_payload = b"" - else: - raise TypeError( - 'payload must be a string, bytearray, int, float or None.') - + self._will_payload = _encode_payload(payload) self._will = True self._will_topic = topic.encode('utf-8') self._will_qos = qos self._will_retain = retain self._will_properties = properties - def will_clear(self): - """ Removes a will that was previously configured with will_set(). + def will_clear(self) -> None: + """ Removes a will that was previously configured with `will_set()`. Must be called before connect() to have any effect.""" self._will = False @@ -1708,27 +2247,29 @@ def will_clear(self): self._will_qos = 0 self._will_retain = False - def socket(self): + def socket(self) -> SocketLike | None: """Return the socket or ssl object for this client.""" return self._sock - def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False): + def loop_forever( + self, + timeout: float = 1.0, + retry_first_connection: bool = False, + ) -> MQTTErrorCode: """This function calls the network loop functions for you in an infinite blocking loop. It is useful for the case where you only want to run the MQTT client loop in your program. loop_forever() will handle reconnecting for you if reconnect_on_failure is - true (this is the default behavior). If you call disconnect() in a callback + true (this is the default behavior). If you call `disconnect()` in a callback it will return. - - timeout: The time in seconds to wait for incoming/outgoing network + :param int timeout: The time in seconds to wait for incoming/outgoing network traffic before timing out and returning. - max_packets: Not currently used. - retry_first_connection: Should the first connection attempt be retried on failure. + :param bool retry_first_connection: Should the first connection attempt be retried on failure. This is independent of the reconnect_on_failure setting. - Raises OSError/WebsocketConnectionError on first connection failures unless retry_first_connection=True + :raises OSError: if the first connection fail unless retry_first_connection=True """ run = True @@ -1737,10 +2278,10 @@ def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False) if self._thread_terminate is True: break - if self._state == mqtt_cs_connect_async: + if self._state == _ConnectionState.MQTT_CS_CONNECT_ASYNC: try: self.reconnect() - except (OSError, WebsocketConnectionError): + except OSError: self._handle_on_connect_fail() if not retry_first_connection: raise @@ -1751,8 +2292,8 @@ def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False) break while run: - rc = MQTT_ERR_SUCCESS - while rc == MQTT_ERR_SUCCESS: + rc = MQTTErrorCode.MQTT_ERR_SUCCESS + while rc == MQTTErrorCode.MQTT_ERR_SUCCESS: rc = self._loop(timeout) # We don't need to worry about locking here, because we've # either called loop_forever() when in single threaded mode, or @@ -1761,11 +2302,15 @@ def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False) if (self._thread_terminate is True and len(self._out_packet) == 0 and len(self._out_messages) == 0): - rc = 1 + rc = MQTTErrorCode.MQTT_ERR_NOMEM run = False - def should_exit(): - return self._state == mqtt_cs_disconnecting or run is False or self._thread_terminate is True + def should_exit() -> bool: + return ( + self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED) or + run is False or # noqa: B023 (uses the run variable from the outer scope on purpose) + self._thread_terminate is True + ) if should_exit() or not self._reconnect_on_failure: run = False @@ -1777,99 +2322,152 @@ def should_exit(): else: try: self.reconnect() - except (OSError, WebsocketConnectionError): + except OSError: self._handle_on_connect_fail() self._easy_log( MQTT_LOG_DEBUG, "Connection failed, retrying") return rc - def loop_start(self): + def loop_start(self) -> MQTTErrorCode: """This is part of the threaded client interface. Call this once to start a new thread to process network traffic. This provides an - alternative to repeatedly calling loop() yourself. + alternative to repeatedly calling `loop()` yourself. + + Under the hood, this will call `loop_forever` in a thread, which means that + the thread will terminate if you call `disconnect()` """ if self._thread is not None: - return MQTT_ERR_INVAL + return MQTTErrorCode.MQTT_ERR_INVAL self._sockpairR, self._sockpairW = _socketpair_compat() self._thread_terminate = False - self._thread = threading.Thread(target=self._thread_main) + self._thread = threading.Thread(target=self._thread_main, name=f"paho-mqtt-client-{self._client_id.decode()}") self._thread.daemon = True self._thread.start() - def loop_stop(self, force=False): + return MQTTErrorCode.MQTT_ERR_SUCCESS + + def loop_stop(self) -> MQTTErrorCode: """This is part of the threaded client interface. Call this once to - stop the network thread previously created with loop_start(). This call + stop the network thread previously created with `loop_start()`. This call will block until the network thread finishes. - The force parameter is currently ignored. + This don't guarantee that publish packet are sent, use `wait_for_publish` or + `on_publish` to ensure `publish` are sent. """ if self._thread is None: - return MQTT_ERR_INVAL + return MQTTErrorCode.MQTT_ERR_INVAL self._thread_terminate = True if threading.current_thread() != self._thread: self._thread.join() - self._thread = None + + return MQTTErrorCode.MQTT_ERR_SUCCESS @property - def on_log(self): - """If implemented, called when the client has log information. - Defined to allow debugging.""" - return self._on_log + def callback_api_version(self) -> CallbackAPIVersion: + """ + Return the callback API version used for user-callback. See docstring for + each user-callback (`on_connect`, `on_publish`, ...) for details. - @on_log.setter - def on_log(self, func): - """ Define the logging callback implementation. + This property is read-only. + """ + return self._callback_api_version + + @property + def on_log(self) -> CallbackOnLog | None: + """The callback called when the client has log information. + Defined to allow debugging. + + Expected signature is:: - Expected signature is: log_callback(client, userdata, level, buf) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - level: gives the severity of the message and will be one of + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param int level: gives the severity of the message and will be one of MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING, MQTT_LOG_ERR, and MQTT_LOG_DEBUG. - buf: the message itself + :param str buf: the message itself - Decorator: @client.log_callback() (```client``` is the name of the + Decorator: @client.log_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_log + + @on_log.setter + def on_log(self, func: CallbackOnLog | None) -> None: self._on_log = func - def log_callback(self): - def decorator(func): + def log_callback(self) -> Callable[[CallbackOnLog], CallbackOnLog]: + def decorator(func: CallbackOnLog) -> CallbackOnLog: self.on_log = func return func return decorator @property - def on_connect(self): - """If implemented, called when the broker responds to our connection - request.""" - return self._on_connect + def on_pre_connect(self) -> CallbackOnPreConnect | None: + """The callback called immediately prior to the connection is made + request. - @on_connect.setter - def on_connect(self, func): - """ Define the connect callback implementation. - - Expected signature for MQTT v3.1 and v3.1.1 is: - connect_callback(client, userdata, flags, rc) - - and for MQTT v5.0: - connect_callback(client, userdata, flags, reasonCode, properties) - - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - flags: response flags sent by the broker - rc: the connection result - reasonCode: the MQTT v5.0 reason code: an instance of the ReasonCode class. - ReasonCode may be compared to integer. - properties: the MQTT v5.0 properties returned from the broker. An instance - of the Properties class. - For MQTT v3.1 and v3.1.1 properties is not provided but for compatibility - with MQTT v5.0, we recommend adding properties=None. + Expected signature (for all callback API version):: + + connect_callback(client, userdata) + + :parama Client client: the client instance for this callback + :parama userdata: the private user data as set in Client() or user_data_set() + + Decorator: @client.pre_connect_callback() (``client`` is the name of the + instance which this callback is being attached to) + + """ + return self._on_pre_connect + + @on_pre_connect.setter + def on_pre_connect(self, func: CallbackOnPreConnect | None) -> None: + with self._callback_mutex: + self._on_pre_connect = func + + def pre_connect_callback( + self, + ) -> Callable[[CallbackOnPreConnect], CallbackOnPreConnect]: + def decorator(func: CallbackOnPreConnect) -> CallbackOnPreConnect: + self.on_pre_connect = func + return func + return decorator + + @property + def on_connect(self) -> CallbackOnConnect | None: + """The callback called when the broker reponds to our connection request. + + Expected signature for callback API version 2:: + + connect_callback(client, userdata, connect_flags, reason_code, properties) + + Expected signature for callback API version 1 change with MQTT protocol version: + * For MQTT v3.1 and v3.1.1 it's:: + + connect_callback(client, userdata, flags, rc) + + * For MQTT v5.0 it's:: + + connect_callback(client, userdata, flags, reason_code, properties) + + + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param ConnectFlags connect_flags: the flags for this connection + :param ReasonCode reason_code: the connection reason code received from the broken. + In MQTT v5.0 it's the reason code defined by the standard. + In MQTT v3, we convert return code to a reason code, see + `convert_connack_rc_to_reason_code()`. + `ReasonCode` may be compared to integer. + :param Properties properties: the MQTT v5.0 properties received from the broker. + For MQTT v3.1 and v3.1.1 properties is not provided and an empty Properties + object is always used. + :param dict flags: response flags sent by the broker + :param int rc: the connection result, should have a value of `ConnackCode` flags is a dict that contains response flags from the broker: flags['session present'] - this flag is useful for clients that are @@ -1879,272 +2477,339 @@ def on_connect(self, func): session information for the client. If 1, the session still exists. The value of rc indicates success or not: - 0: Connection successful - 1: Connection refused - incorrect protocol version - 2: Connection refused - invalid client identifier - 3: Connection refused - server unavailable - 4: Connection refused - bad username or password - 5: Connection refused - not authorised - 6-255: Currently unused. - - Decorator: @client.connect_callback() (```client``` is the name of the + - 0: Connection successful + - 1: Connection refused - incorrect protocol version + - 2: Connection refused - invalid client identifier + - 3: Connection refused - server unavailable + - 4: Connection refused - bad username or password + - 5: Connection refused - not authorised + - 6-255: Currently unused. + + Decorator: @client.connect_callback() (``client`` is the name of the instance which this callback is being attached to) - """ + return self._on_connect + + @on_connect.setter + def on_connect(self, func: CallbackOnConnect | None) -> None: with self._callback_mutex: self._on_connect = func - def connect_callback(self): - def decorator(func): + def connect_callback( + self, + ) -> Callable[[CallbackOnConnect], CallbackOnConnect]: + def decorator(func: CallbackOnConnect) -> CallbackOnConnect: self.on_connect = func return func return decorator @property - def on_connect_fail(self): - """If implemented, called when the client failed to connect - to the broker.""" - return self._on_connect_fail + def on_connect_fail(self) -> CallbackOnConnectFail | None: + """The callback called when the client failed to connect + to the broker. - @on_connect_fail.setter - def on_connect_fail(self, func): - """ Define the connection failure callback implementation + Expected signature is (for all callback_api_version):: - Expected signature is: - on_connect_fail(client, userdata) + connect_fail_callback(client, userdata) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() + :param Client client: the client instance for this callback + :parama userdata: the private user data as set in Client() or user_data_set() - Decorator: @client.connect_fail_callback() (```client``` is the name of the + Decorator: @client.connect_fail_callback() (``client`` is the name of the instance which this callback is being attached to) - """ + return self._on_connect_fail + + @on_connect_fail.setter + def on_connect_fail(self, func: CallbackOnConnectFail | None) -> None: with self._callback_mutex: self._on_connect_fail = func - def connect_fail_callback(self): - def decorator(func): + def connect_fail_callback( + self, + ) -> Callable[[CallbackOnConnectFail], CallbackOnConnectFail]: + def decorator(func: CallbackOnConnectFail) -> CallbackOnConnectFail: self.on_connect_fail = func return func return decorator @property - def on_subscribe(self): - """If implemented, called when the broker responds to a subscribe - request.""" - return self._on_subscribe + def on_subscribe(self) -> CallbackOnSubscribe | None: + """The callback called when the broker responds to a subscribe + request. - @on_subscribe.setter - def on_subscribe(self, func): - """ Define the subscribe callback implementation. - - Expected signature for MQTT v3.1.1 and v3.1 is: - subscribe_callback(client, userdata, mid, granted_qos) - - and for MQTT v5.0: - subscribe_callback(client, userdata, mid, reasonCodes, properties) - - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - mid: matches the mid variable returned from the corresponding - subscribe() call. - granted_qos: list of integers that give the QoS level the broker has - granted for each of the different subscription requests. - reasonCodes: the MQTT v5.0 reason codes received from the broker for each - subscription. A list of ReasonCodes instances. - properties: the MQTT v5.0 properties received from the broker. A - list of Properties class instances. - - Decorator: @client.subscribe_callback() (```client``` is the name of the + Expected signature for callback API version 2:: + + subscribe_callback(client, userdata, mid, reason_code_list, properties) + + Expected signature for callback API version 1 change with MQTT protocol version: + * For MQTT v3.1 and v3.1.1 it's:: + + subscribe_callback(client, userdata, mid, granted_qos) + + * For MQTT v5.0 it's:: + + subscribe_callback(client, userdata, mid, reason_code_list, properties) + + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param int mid: matches the mid variable returned from the corresponding + subscribe() call. + :param list[ReasonCode] reason_code_list: reason codes received from the broker for each subscription. + In MQTT v5.0 it's the reason code defined by the standard. + In MQTT v3, we convert granted QoS to a reason code. + It's a list of ReasonCode instances. + :param Properties properties: the MQTT v5.0 properties received from the broker. + For MQTT v3.1 and v3.1.1 properties is not provided and an empty Properties + object is always used. + :param list[int] granted_qos: list of integers that give the QoS level the broker has + granted for each of the different subscription requests. + + Decorator: @client.subscribe_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_subscribe + + @on_subscribe.setter + def on_subscribe(self, func: CallbackOnSubscribe | None) -> None: with self._callback_mutex: self._on_subscribe = func - def subscribe_callback(self): - def decorator(func): + def subscribe_callback( + self, + ) -> Callable[[CallbackOnSubscribe], CallbackOnSubscribe]: + def decorator(func: CallbackOnSubscribe) -> CallbackOnSubscribe: self.on_subscribe = func return func return decorator @property - def on_message(self): - """If implemented, called when a message has been received on a topic + def on_message(self) -> CallbackOnMessage | None: + """The callback called when a message has been received on a topic that the client subscribes to. - This callback will be called for every message received. Use - message_callback_add() to define multiple callbacks that will be called - for specific topic filters.""" - return self._on_message - - @on_message.setter - def on_message(self, func): - """ Define the message received callback implementation. + This callback will be called for every message received unless a + `message_callback_add()` matched the message. - Expected signature is: - on_message_callback(client, userdata, message) + Expected signature is (for all callback API version): + message_callback(client, userdata, message) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - message: an instance of MQTTMessage. + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param MQTTMessage message: the received message. This is a class with members topic, payload, qos, retain. - Decorator: @client.message_callback() (```client``` is the name of the + Decorator: @client.message_callback() (``client`` is the name of the instance which this callback is being attached to) - """ + return self._on_message + + @on_message.setter + def on_message(self, func: CallbackOnMessage | None) -> None: with self._callback_mutex: self._on_message = func - def message_callback(self): - def decorator(func): + def message_callback( + self, + ) -> Callable[[CallbackOnMessage], CallbackOnMessage]: + def decorator(func: CallbackOnMessage) -> CallbackOnMessage: self.on_message = func return func return decorator @property - def on_publish(self): - """If implemented, called when a message that was to be sent using the - publish() call has completed transmission to the broker. + def on_publish(self) -> CallbackOnPublish | None: + """The callback called when a message that was to be sent using the + `publish()` call has completed transmission to the broker. For messages with QoS levels 1 and 2, this means that the appropriate handshakes have completed. For QoS 0, this simply means that the message has left the client. - This callback is important because even if the publish() call returns - success, it does not always mean that the message has been sent.""" - return self._on_publish + This callback is important because even if the `publish()` call returns + success, it does not always mean that the message has been sent. - @on_publish.setter - def on_publish(self, func): - """ Define the published message callback implementation. + See also `wait_for_publish` which could be simpler to use. + + Expected signature for callback API version 2:: - Expected signature is: - on_publish_callback(client, userdata, mid) + publish_callback(client, userdata, mid, reason_code, properties) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - mid: matches the mid variable returned from the corresponding - publish() call, to allow outgoing messages to be tracked. + Expected signature for callback API version 1:: - Decorator: @client.publish_callback() (```client``` is the name of the + publish_callback(client, userdata, mid) + + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param int mid: matches the mid variable returned from the corresponding + `publish()` call, to allow outgoing messages to be tracked. + :param ReasonCode reason_code: the connection reason code received from the broken. + In MQTT v5.0 it's the reason code defined by the standard. + In MQTT v3 it's always the reason code Success + :parama Properties properties: the MQTT v5.0 properties received from the broker. + For MQTT v3.1 and v3.1.1 properties is not provided and an empty Properties + object is always used. + + Note: for QoS = 0, the reason_code and the properties don't really exist, it's the client + library that generate them. It's always an empty properties and a success reason code. + Because the (MQTTv5) standard don't have reason code for PUBLISH packet, the library create them + at PUBACK packet, as if the message was sent with QoS = 1. + + Decorator: @client.publish_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_publish + + @on_publish.setter + def on_publish(self, func: CallbackOnPublish | None) -> None: with self._callback_mutex: self._on_publish = func - def publish_callback(self): - def decorator(func): + def publish_callback( + self, + ) -> Callable[[CallbackOnPublish], CallbackOnPublish]: + def decorator(func: CallbackOnPublish) -> CallbackOnPublish: self.on_publish = func return func return decorator @property - def on_unsubscribe(self): - """If implemented, called when the broker responds to an unsubscribe - request.""" - return self._on_unsubscribe + def on_unsubscribe(self) -> CallbackOnUnsubscribe | None: + """The callback called when the broker responds to an unsubscribe + request. - @on_unsubscribe.setter - def on_unsubscribe(self, func): - """ Define the unsubscribe callback implementation. + Expected signature for callback API version 2:: - Expected signature for MQTT v3.1.1 and v3.1 is: - unsubscribe_callback(client, userdata, mid) + unsubscribe_callback(client, userdata, mid, reason_code_list, properties) - and for MQTT v5.0: - unsubscribe_callback(client, userdata, mid, properties, reasonCodes) + Expected signature for callback API version 1 change with MQTT protocol version: + * For MQTT v3.1 and v3.1.1 it's:: - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - mid: matches the mid variable returned from the corresponding - unsubscribe() call. - properties: the MQTT v5.0 properties received from the broker. A - list of Properties class instances. - reasonCodes: the MQTT v5.0 reason codes received from the broker for each - unsubscribe topic. A list of ReasonCodes instances + unsubscribe_callback(client, userdata, mid) - Decorator: @client.unsubscribe_callback() (```client``` is the name of the + * For MQTT v5.0 it's:: + + unsubscribe_callback(client, userdata, mid, properties, v1_reason_codes) + + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param mid: matches the mid variable returned from the corresponding + unsubscribe() call. + :param list[ReasonCode] reason_code_list: reason codes received from the broker for each unsubscription. + In MQTT v5.0 it's the reason code defined by the standard. + In MQTT v3, there is not equivalent from broken and empty list + is always used. + :param Properties properties: the MQTT v5.0 properties received from the broker. + For MQTT v3.1 and v3.1.1 properties is not provided and an empty Properties + object is always used. + :param v1_reason_codes: the MQTT v5.0 reason codes received from the broker for each + unsubscribe topic. A list of ReasonCode instances OR a single + ReasonCode when we unsubscribe from a single topic. + + Decorator: @client.unsubscribe_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_unsubscribe + + @on_unsubscribe.setter + def on_unsubscribe(self, func: CallbackOnUnsubscribe | None) -> None: with self._callback_mutex: self._on_unsubscribe = func - def unsubscribe_callback(self): - def decorator(func): + def unsubscribe_callback( + self, + ) -> Callable[[CallbackOnUnsubscribe], CallbackOnUnsubscribe]: + def decorator(func: CallbackOnUnsubscribe) -> CallbackOnUnsubscribe: self.on_unsubscribe = func return func return decorator @property - def on_disconnect(self): - """If implemented, called when the client disconnects from the broker. - """ - return self._on_disconnect + def on_disconnect(self) -> CallbackOnDisconnect | None: + """The callback called when the client disconnects from the broker. - @on_disconnect.setter - def on_disconnect(self, func): - """ Define the disconnect callback implementation. + Expected signature for callback API version 2:: + + disconnect_callback(client, userdata, disconnect_flags, reason_code, properties) + + Expected signature for callback API version 1 change with MQTT protocol version: + * For MQTT v3.1 and v3.1.1 it's:: + + disconnect_callback(client, userdata, rc) - Expected signature for MQTT v3.1.1 and v3.1 is: - disconnect_callback(client, userdata, rc) + * For MQTT v5.0 it's:: - and for MQTT v5.0: - disconnect_callback(client, userdata, reasonCode, properties) + disconnect_callback(client, userdata, reason_code, properties) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - rc: the disconnection result - The rc parameter indicates the disconnection state. If - MQTT_ERR_SUCCESS (0), the callback was called in response to - a disconnect() call. If any other value the disconnection - was unexpected, such as might be caused by a network error. + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param DisconnectFlag disconnect_flags: the flags for this disconnection. + :param ReasonCode reason_code: the disconnection reason code possibly received from the broker (see disconnect_flags). + In MQTT v5.0 it's the reason code defined by the standard. + In MQTT v3 it's never received from the broker, we convert an MQTTErrorCode, + see `convert_disconnect_error_code_to_reason_code()`. + `ReasonCode` may be compared to integer. + :param Properties properties: the MQTT v5.0 properties received from the broker. + For MQTT v3.1 and v3.1.1 properties is not provided and an empty Properties + object is always used. + :param int rc: the disconnection result + The rc parameter indicates the disconnection state. If + MQTT_ERR_SUCCESS (0), the callback was called in response to + a disconnect() call. If any other value the disconnection + was unexpected, such as might be caused by a network error. - Decorator: @client.disconnect_callback() (```client``` is the name of the + Decorator: @client.disconnect_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_disconnect + + @on_disconnect.setter + def on_disconnect(self, func: CallbackOnDisconnect | None) -> None: with self._callback_mutex: self._on_disconnect = func - def disconnect_callback(self): - def decorator(func): + def disconnect_callback( + self, + ) -> Callable[[CallbackOnDisconnect], CallbackOnDisconnect]: + def decorator(func: CallbackOnDisconnect) -> CallbackOnDisconnect: self.on_disconnect = func return func return decorator @property - def on_socket_open(self): - """If implemented, called just after the socket was opend.""" - return self._on_socket_open - - @on_socket_open.setter - def on_socket_open(self, func): - """Define the socket_open callback implementation. + def on_socket_open(self) -> CallbackOnSocket | None: + """The callback called just after the socket was opend. This should be used to register the socket to an external event loop for reading. - Expected signature is: + Expected signature is (for all callback API version):: + socket_open_callback(client, userdata, socket) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - sock: the socket which was just opened. + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param SocketLike sock: the socket which was just opened. - Decorator: @client.socket_open_callback() (```client``` is the name of the + Decorator: @client.socket_open_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_socket_open + + @on_socket_open.setter + def on_socket_open(self, func: CallbackOnSocket | None) -> None: with self._callback_mutex: self._on_socket_open = func - def socket_open_callback(self): - def decorator(func): + def socket_open_callback( + self, + ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: + def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self.on_socket_open = func return func return decorator - def _call_socket_open(self): + def _call_socket_open(self, sock: SocketLike) -> None: """Call the socket_open callback with the just-opened socket""" with self._callback_mutex: on_socket_open = self.on_socket_open @@ -2152,7 +2817,7 @@ def _call_socket_open(self): if on_socket_open: with self._in_callback_mutex: try: - on_socket_open(self, self._userdata, self._sock) + on_socket_open(self, self._userdata, sock) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_socket_open: %s', err) @@ -2160,36 +2825,38 @@ def _call_socket_open(self): raise @property - def on_socket_close(self): - """If implemented, called just before the socket is closed.""" - return self._on_socket_close - - @on_socket_close.setter - def on_socket_close(self, func): - """Define the socket_close callback implementation. + def on_socket_close(self) -> CallbackOnSocket | None: + """The callback called just before the socket is closed. This should be used to unregister the socket from an external event loop for reading. - Expected signature is: + Expected signature is (for all callback API version):: + socket_close_callback(client, userdata, socket) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - sock: the socket which is about to be closed. + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param SocketLike sock: the socket which is about to be closed. - Decorator: @client.socket_close_callback() (```client``` is the name of the + Decorator: @client.socket_close_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_socket_close + + @on_socket_close.setter + def on_socket_close(self, func: CallbackOnSocket | None) -> None: with self._callback_mutex: self._on_socket_close = func - def socket_close_callback(self): - def decorator(func): + def socket_close_callback( + self, + ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: + def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self.on_socket_close = func return func return decorator - def _call_socket_close(self, sock): + def _call_socket_close(self, sock: SocketLike) -> None: """Call the socket_close callback with the about-to-be-closed socket""" with self._callback_mutex: on_socket_close = self.on_socket_close @@ -2205,36 +2872,38 @@ def _call_socket_close(self, sock): raise @property - def on_socket_register_write(self): - """If implemented, called when the socket needs writing but can't.""" - return self._on_socket_register_write - - @on_socket_register_write.setter - def on_socket_register_write(self, func): - """Define the socket_register_write callback implementation. + def on_socket_register_write(self) -> CallbackOnSocket | None: + """The callback called when the socket needs writing but can't. This should be used to register the socket with an external event loop for writing. - Expected signature is: + Expected signature is (for all callback API version):: + socket_register_write_callback(client, userdata, socket) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - sock: the socket which should be registered for writing + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param SocketLike sock: the socket which should be registered for writing - Decorator: @client.socket_register_write_callback() (```client``` is the name of the + Decorator: @client.socket_register_write_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_socket_register_write + + @on_socket_register_write.setter + def on_socket_register_write(self, func: CallbackOnSocket | None) -> None: with self._callback_mutex: self._on_socket_register_write = func - def socket_register_write_callback(self): - def decorator(func): + def socket_register_write_callback( + self, + ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: + def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self._on_socket_register_write = func return func return decorator - def _call_socket_register_write(self): + def _call_socket_register_write(self) -> None: """Call the socket_register_write callback with the unwritable socket""" if not self._sock or self._registered_write: return @@ -2253,36 +2922,46 @@ def _call_socket_register_write(self): raise @property - def on_socket_unregister_write(self): - """If implemented, called when the socket doesn't need writing anymore.""" - return self._on_socket_unregister_write - - @on_socket_unregister_write.setter - def on_socket_unregister_write(self, func): - """Define the socket_unregister_write callback implementation. + def on_socket_unregister_write( + self, + ) -> CallbackOnSocket | None: + """The callback called when the socket doesn't need writing anymore. This should be used to unregister the socket from an external event loop for writing. - Expected signature is: + Expected signature is (for all callback API version):: + socket_unregister_write_callback(client, userdata, socket) - client: the client instance for this callback - userdata: the private user data as set in Client() or userdata_set() - sock: the socket which should be unregistered for writing + :param Client client: the client instance for this callback + :param userdata: the private user data as set in Client() or user_data_set() + :param SocketLike sock: the socket which should be unregistered for writing - Decorator: @client.socket_unregister_write_callback() (```client``` is the name of the + Decorator: @client.socket_unregister_write_callback() (``client`` is the name of the instance which this callback is being attached to) """ + return self._on_socket_unregister_write + + @on_socket_unregister_write.setter + def on_socket_unregister_write( + self, func: CallbackOnSocket | None + ) -> None: with self._callback_mutex: self._on_socket_unregister_write = func - def socket_unregister_write_callback(self): - def decorator(func): + def socket_unregister_write_callback( + self, + ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: + def decorator( + func: CallbackOnSocket, + ) -> CallbackOnSocket: self._on_socket_unregister_write = func return func return decorator - def _call_socket_unregister_write(self, sock=None): + def _call_socket_unregister_write( + self, sock: SocketLike | None = None + ) -> None: """Call the socket_unregister_write callback with the writable socket""" sock = sock or self._sock if not sock or not self._registered_write: @@ -2301,32 +2980,46 @@ def _call_socket_unregister_write(self, sock=None): if not self.suppress_exceptions: raise - def message_callback_add(self, sub, callback): + def message_callback_add(self, sub: str, callback: CallbackOnMessage) -> None: """Register a message callback for a specific topic. Messages that match 'sub' will be passed to 'callback'. Any - non-matching messages will be passed to the default on_message + non-matching messages will be passed to the default `on_message` callback. Call multiple times with different 'sub' to define multiple topic specific callbacks. Topic specific callbacks may be removed with - message_callback_remove().""" + `message_callback_remove()`. + + See `on_message` for the expected signature of the callback. + + Decorator: @client.topic_callback(sub) (``client`` is the name of the + instance which this callback is being attached to) + + Example:: + + @client.topic_callback("mytopic/#") + def handle_mytopic(client, userdata, message): + ... + """ if callback is None or sub is None: raise ValueError("sub and callback must both be defined.") with self._callback_mutex: self._on_message_filtered[sub] = callback - def topic_callback(self, sub): - def decorator(func): + def topic_callback( + self, sub: str + ) -> Callable[[CallbackOnMessage], CallbackOnMessage]: + def decorator(func: CallbackOnMessage) -> CallbackOnMessage: self.message_callback_add(sub, func) return func return decorator - def message_callback_remove(self, sub): + def message_callback_remove(self, sub: str) -> None: """Remove a message callback previously registered with - message_callback_add().""" + `message_callback_add()`.""" if sub is None: raise ValueError("sub must defined.") @@ -2340,18 +3033,25 @@ def message_callback_remove(self, sub): # Private functions # ============================================================ - def _loop_rc_handle(self, rc, properties=None): + def _loop_rc_handle( + self, + rc: MQTTErrorCode, + ) -> MQTTErrorCode: if rc: self._sock_close() - if self._state == mqtt_cs_disconnecting: - rc = MQTT_ERR_SUCCESS + if self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): + self._state = _ConnectionState.MQTT_CS_DISCONNECTED + rc = MQTTErrorCode.MQTT_ERR_SUCCESS + + self._do_on_disconnect(packet_from_broker=False, v1_rc=rc) - self._do_on_disconnect(rc, properties) + if rc == MQTT_ERR_CONN_LOST: + self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST return rc - def _packet_read(self): + def _packet_read(self) -> MQTTErrorCode: # This gets called if pselect() indicates that there is network data # available - ie. at least one byte. What we do depends on what data we # already have. @@ -2369,16 +3069,19 @@ def _packet_read(self): try: command = self._sock_recv(1) except BlockingIOError: - return MQTT_ERR_AGAIN - except ConnectionError as err: + return MQTTErrorCode.MQTT_ERR_AGAIN + except TimeoutError as err: + self._easy_log( + MQTT_LOG_ERR, 'timeout on socket: %s', err) + return MQTTErrorCode.MQTT_ERR_CONN_LOST + except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(command) == 0: - return MQTT_ERR_CONN_LOST - command, = struct.unpack("!B", command) - self._in_packet['command'] = command + return MQTTErrorCode.MQTT_ERR_CONN_LOST + self._in_packet['command'] = command[0] if self._in_packet['have_remaining'] == 0: # Read remaining @@ -2388,26 +3091,26 @@ def _packet_read(self): try: byte = self._sock_recv(1) except BlockingIOError: - return MQTT_ERR_AGAIN - except ConnectionError as err: + return MQTTErrorCode.MQTT_ERR_AGAIN + except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(byte) == 0: - return MQTT_ERR_CONN_LOST - byte, = struct.unpack("!B", byte) - self._in_packet['remaining_count'].append(byte) + return MQTTErrorCode.MQTT_ERR_CONN_LOST + byte_value = byte[0] + self._in_packet['remaining_count'].append(byte_value) # Max 4 bytes length for remaining length as defined by protocol. # Anything more likely means a broken/malicious client. if len(self._in_packet['remaining_count']) > 4: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL self._in_packet['remaining_length'] += ( - byte & 127) * self._in_packet['remaining_mult'] + byte_value & 127) * self._in_packet['remaining_mult'] self._in_packet['remaining_mult'] = self._in_packet['remaining_mult'] * 128 - if (byte & 128) == 0: + if (byte_value & 128) == 0: break self._in_packet['have_remaining'] = 1 @@ -2418,21 +3121,21 @@ def _packet_read(self): try: data = self._sock_recv(self._in_packet['to_process']) except BlockingIOError: - return MQTT_ERR_AGAIN - except ConnectionError as err: + return MQTTErrorCode.MQTT_ERR_AGAIN + except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(data) == 0: - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST self._in_packet['to_process'] -= len(data) self._in_packet['packet'] += data count -= 1 if count == 0: with self._msgtime_mutex: self._last_msg_in = time_func() - return MQTT_ERR_AGAIN + return MQTTErrorCode.MQTT_ERR_AGAIN # All data for this packet is read. self._in_packet['pos'] = 0 @@ -2440,40 +3143,41 @@ def _packet_read(self): # Free data and reset values self._in_packet = { - 'command': 0, - 'have_remaining': 0, - 'remaining_count': [], - 'remaining_mult': 1, - 'remaining_length': 0, - 'packet': bytearray(b""), - 'to_process': 0, - 'pos': 0} + "command": 0, + "have_remaining": 0, + "remaining_count": [], + "remaining_mult": 1, + "remaining_length": 0, + "packet": bytearray(b""), + "to_process": 0, + "pos": 0, + } with self._msgtime_mutex: self._last_msg_in = time_func() return rc - def _packet_write(self): + def _packet_write(self) -> MQTTErrorCode: while True: try: packet = self._out_packet.popleft() except IndexError: - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS try: write_length = self._sock_send( packet['packet'][packet['pos']:]) except (AttributeError, ValueError): self._out_packet.appendleft(packet) - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS except BlockingIOError: self._out_packet.appendleft(packet) - return MQTT_ERR_AGAIN - except ConnectionError as err: + return MQTTErrorCode.MQTT_ERR_AGAIN + except OSError as err: self._out_packet.appendleft(packet) self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) - return MQTT_ERR_CONN_LOST + return MQTTErrorCode.MQTT_ERR_CONN_LOST if write_length > 0: packet['to_process'] -= write_length @@ -2487,23 +3191,49 @@ def _packet_write(self): if on_publish: with self._in_callback_mutex: try: - on_publish( - self, self._userdata, packet['mid']) + if self._callback_api_version == CallbackAPIVersion.VERSION1: + on_publish = cast(CallbackOnPublish_v1, on_publish) + + on_publish(self, self._userdata, packet["mid"]) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_publish = cast(CallbackOnPublish_v2, on_publish) + + on_publish( + self, + self._userdata, + packet["mid"], + ReasonCode(PacketTypes.PUBACK), + Properties(PacketTypes.PUBACK), + ) + else: + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err) if not self.suppress_exceptions: raise - packet['info']._set_as_published() + # TODO: Something is odd here. I don't see why packet["info"] can't be None. + # A packet could be produced by _handle_connack with qos=0 and no info + # (around line 3645). Ignore the mypy check for now but I feel there is a bug + # somewhere. + packet['info']._set_as_published() # type: ignore if (packet['command'] & 0xF0) == DISCONNECT: with self._msgtime_mutex: self._last_msg_out = time_func() - self._do_on_disconnect(MQTT_ERR_SUCCESS) + self._do_on_disconnect( + packet_from_broker=False, + v1_rc=MQTTErrorCode.MQTT_ERR_SUCCESS, + ) self._sock_close() - return MQTT_ERR_SUCCESS + # Only change to disconnected if the disconnection was wanted + # by the client (== state was disconnecting). If the broker disconnected + # use unilaterally don't change the state and client may reconnect. + if self._state == _ConnectionState.MQTT_CS_DISCONNECTING: + self._state = _ConnectionState.MQTT_CS_DISCONNECTED + return MQTTErrorCode.MQTT_ERR_SUCCESS else: # We haven't finished with this packet @@ -2514,23 +3244,23 @@ def _packet_write(self): with self._msgtime_mutex: self._last_msg_out = time_func() - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _easy_log(self, level, fmt, *args): + def _easy_log(self, level: LogLevel, fmt: str, *args: Any) -> None: if self.on_log is not None: buf = fmt % args try: self.on_log(self, self._userdata, level, buf) - except Exception: + except Exception: # noqa: S110 # Can't _easy_log this, as we'll recurse until we break pass # self._logger will pick this up, so we're fine if self._logger is not None: level_std = LOGGING_LEVEL[level] self._logger.log(level_std, fmt, *args) - def _check_keepalive(self): + def _check_keepalive(self) -> None: if self._keepalive == 0: - return MQTT_ERR_SUCCESS + return now = time_func() @@ -2539,12 +3269,15 @@ def _check_keepalive(self): last_msg_in = self._last_msg_in if self._sock is not None and (now - last_msg_out >= self._keepalive or now - last_msg_in >= self._keepalive): - if self._state == mqtt_cs_connected and self._ping_t == 0: + if self._state == _ConnectionState.MQTT_CS_CONNECTED and self._ping_t == 0: try: self._send_pingreq() except Exception: self._sock_close() - self._do_on_disconnect(MQTT_ERR_CONN_LOST) + self._do_on_disconnect( + packet_from_broker=False, + v1_rc=MQTTErrorCode.MQTT_ERR_CONN_LOST, + ) else: with self._msgtime_mutex: self._last_msg_out = now @@ -2552,14 +3285,18 @@ def _check_keepalive(self): else: self._sock_close() - if self._state == mqtt_cs_disconnecting: - rc = MQTT_ERR_SUCCESS + if self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): + self._state = _ConnectionState.MQTT_CS_DISCONNECTED + rc = MQTTErrorCode.MQTT_ERR_SUCCESS else: - rc = MQTT_ERR_KEEPALIVE + rc = MQTTErrorCode.MQTT_ERR_KEEPALIVE - self._do_on_disconnect(rc) + self._do_on_disconnect( + packet_from_broker=False, + v1_rc=rc, + ) - def _mid_generate(self): + def _mid_generate(self) -> int: with self._mid_generate_mutex: self._last_mid += 1 if self._last_mid == 65536: @@ -2567,44 +3304,47 @@ def _mid_generate(self): return self._last_mid @staticmethod - def _topic_wildcard_len_check(topic): - # Search for + or # in a topic. Return MQTT_ERR_INVAL if found. - # Also returns MQTT_ERR_INVAL if the topic string is too long. - # Returns MQTT_ERR_SUCCESS if everything is fine. - if b'+' in topic or b'#' in topic or len(topic) > 65535: - return MQTT_ERR_INVAL - else: - return MQTT_ERR_SUCCESS + def _raise_for_invalid_topic(topic: bytes) -> None: + """ Check if the topic is a topic without wildcard and valid length. + + Raise ValueError if the topic isn't valid. + """ + if b'+' in topic or b'#' in topic: + raise ValueError('Publish topic cannot contain wildcards.') + if len(topic) > 65535: + raise ValueError('Publish topic is too long.') @staticmethod - def _filter_wildcard_len_check(sub): + def _filter_wildcard_len_check(sub: bytes) -> MQTTErrorCode: if (len(sub) == 0 or len(sub) > 65535 or any(b'+' in p or b'#' in p for p in sub.split(b'/') if len(p) > 1) or b'#/' in sub): - return MQTT_ERR_INVAL + return MQTTErrorCode.MQTT_ERR_INVAL else: - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _send_pingreq(self): + def _send_pingreq(self) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PINGREQ") rc = self._send_simple_command(PINGREQ) - if rc == MQTT_ERR_SUCCESS: + if rc == MQTTErrorCode.MQTT_ERR_SUCCESS: self._ping_t = time_func() return rc - def _send_pingresp(self): + def _send_pingresp(self) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PINGRESP") return self._send_simple_command(PINGRESP) - def _send_puback(self, mid): + def _send_puback(self, mid: int) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PUBACK (Mid: %d)", mid) return self._send_command_with_mid(PUBACK, mid, False) - def _send_pubcomp(self, mid): + def _send_pubcomp(self, mid: int) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PUBCOMP (Mid: %d)", mid) return self._send_command_with_mid(PUBCOMP, mid, False) - def _pack_remaining_length(self, packet, remaining_length): + def _pack_remaining_length( + self, packet: bytearray, remaining_length: int + ) -> bytearray: remaining_bytes = [] while True: byte = remaining_length % 128 @@ -2619,19 +3359,30 @@ def _pack_remaining_length(self, packet, remaining_length): # FIXME - this doesn't deal with incorrectly large payloads return packet - def _pack_str16(self, packet, data): - if isinstance(data, unicode): - data = data.encode('utf-8') + def _pack_str16(self, packet: bytearray, data: bytes | str) -> None: + data = _force_bytes(data) packet.extend(struct.pack("!H", len(data))) packet.extend(data) - def _send_publish(self, mid, topic, payload=b'', qos=0, retain=False, dup=False, info=None, properties=None): + def _send_publish( + self, + mid: int, + topic: bytes, + payload: bytes|bytearray = b"", + qos: int = 0, + retain: bool = False, + dup: bool = False, + info: MQTTMessageInfo | None = None, + properties: Properties | None = None, + ) -> MQTTErrorCode: # we assume that topic and payload are already properly encoded - assert not isinstance(topic, unicode) and not isinstance( - payload, unicode) and payload is not None + if not isinstance(topic, bytes): + raise TypeError('topic must be bytes, not str') + if payload and not isinstance(payload, (bytes, bytearray)): + raise TypeError('payload must be bytes if set') if self._sock is None: - return MQTT_ERR_NO_CONN + return MQTTErrorCode.MQTT_ERR_NO_CONN command = PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain packet = bytearray() @@ -2692,15 +3443,15 @@ def _send_publish(self, mid, topic, payload=b'', qos=0, retain=False, dup=False, return self._packet_queue(PUBLISH, packet, mid, qos, info) - def _send_pubrec(self, mid): + def _send_pubrec(self, mid: int) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PUBREC (Mid: %d)", mid) return self._send_command_with_mid(PUBREC, mid, False) - def _send_pubrel(self, mid): + def _send_pubrel(self, mid: int) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PUBREL (Mid: %d)", mid) return self._send_command_with_mid(PUBREL | 2, mid, False) - def _send_command_with_mid(self, command, mid, dup): + def _send_command_with_mid(self, command: int, mid: int, dup: int) -> MQTTErrorCode: # For PUBACK, PUBCOMP, PUBREC, and PUBREL if dup: command |= 0x8 @@ -2709,14 +3460,14 @@ def _send_command_with_mid(self, command, mid, dup): packet = struct.pack('!BBH', command, remaining_length, mid) return self._packet_queue(command, packet, mid, 1) - def _send_simple_command(self, command): + def _send_simple_command(self, command: int) -> MQTTErrorCode: # For DISCONNECT, PINGREQ and PINGRESP remaining_length = 0 packet = struct.pack('!BB', command, remaining_length) return self._packet_queue(command, packet, 0, 0) - def _send_connect(self, keepalive): - proto_ver = self._protocol + def _send_connect(self, keepalive: int) -> MQTTErrorCode: + proto_ver = int(self._protocol) # hard-coded UTF-8 encoded string protocol = b"MQTT" if proto_ver >= MQTTv311 else b"MQIsdp" @@ -2725,7 +3476,7 @@ def _send_connect(self, keepalive): connect_flags = 0 if self._protocol == MQTTv5: - if self._clean_start == True: + if self._clean_start is True: connect_flags |= 0x02 elif self._clean_start == MQTT_CLEAN_START_FIRST_ONLY and self._mqttv5_first_connect: connect_flags |= 0x02 @@ -2768,8 +3519,10 @@ def _send_connect(self, keepalive): proto_ver |= 0x80 self._pack_remaining_length(packet, remaining_length) - packet.extend(struct.pack("!H" + str(len(protocol)) + "sBBH", len(protocol), protocol, proto_ver, connect_flags, - keepalive)) + packet.extend(struct.pack( + f"!H{len(protocol)}sBBH", + len(protocol), protocol, proto_ver, connect_flags, keepalive, + )) if self._protocol == MQTTv5: packet += packed_connect_properties @@ -2818,7 +3571,11 @@ def _send_connect(self, keepalive): ) return self._packet_queue(command, packet, 0, 0) - def _send_disconnect(self, reasoncode=None, properties=None): + def _send_disconnect( + self, + reasoncode: ReasonCode | None = None, + properties: Properties | None = None, + ) -> MQTTErrorCode: if self._protocol == MQTTv5: self._easy_log(MQTT_LOG_DEBUG, "Sending DISCONNECT reasonCode=%s properties=%s", reasoncode, @@ -2836,7 +3593,7 @@ def _send_disconnect(self, reasoncode=None, properties=None): if self._protocol == MQTTv5: if properties is not None or reasoncode is not None: if reasoncode is None: - reasoncode = ReasonCodes(DISCONNECT >> 4, identifier=0) + reasoncode = ReasonCode(DISCONNECT >> 4, identifier=0) remaining_length += 1 if properties is not None: packed_props = properties.pack() @@ -2845,14 +3602,19 @@ def _send_disconnect(self, reasoncode=None, properties=None): self._pack_remaining_length(packet, remaining_length) if self._protocol == MQTTv5: - if reasoncode != None: + if reasoncode is not None: packet += reasoncode.pack() - if properties != None: + if properties is not None: packet += packed_props return self._packet_queue(command, packet, 0, 0) - def _send_subscribe(self, dup, topics, properties=None): + def _send_subscribe( + self, + dup: int, + topics: Sequence[tuple[bytes, SubscribeOptions | int]], + properties: Properties | None = None, + ) -> tuple[MQTTErrorCode, int]: remaining_length = 2 if self._protocol == MQTTv5: if properties is None: @@ -2876,9 +3638,9 @@ def _send_subscribe(self, dup, topics, properties=None): for t, q in topics: self._pack_str16(packet, t) if self._protocol == MQTTv5: - packet += q.pack() + packet += q.pack() # type: ignore else: - packet.append(q) + packet.append(q) # type: ignore self._easy_log( MQTT_LOG_DEBUG, @@ -2889,7 +3651,12 @@ def _send_subscribe(self, dup, topics, properties=None): ) return (self._packet_queue(command, packet, local_mid, 1), local_mid) - def _send_unsubscribe(self, dup, topics, properties=None): + def _send_unsubscribe( + self, + dup: int, + topics: list[bytes], + properties: Properties | None = None, + ) -> tuple[MQTTErrorCode, int]: remaining_length = 2 if self._protocol == MQTTv5: if properties is None: @@ -2933,16 +3700,16 @@ def _send_unsubscribe(self, dup, topics, properties=None): ) return (self._packet_queue(command, packet, local_mid, 1), local_mid) - def _check_clean_session(self): + def _check_clean_session(self) -> bool: if self._protocol == MQTTv5: if self._clean_start == MQTT_CLEAN_START_FIRST_ONLY: return self._mqttv5_first_connect else: - return self._clean_start + return self._clean_start # type: ignore else: return self._clean_session - def _messages_reconnect_reset_out(self): + def _messages_reconnect_reset_out(self) -> None: with self._out_message_mutex: self._inflight_messages = 0 for m in self._out_messages.values(): @@ -2971,7 +3738,7 @@ def _messages_reconnect_reset_out(self): else: m.state = mqtt_ms_queued - def _messages_reconnect_reset_in(self): + def _messages_reconnect_reset_in(self) -> None: with self._in_message_mutex: if self._check_clean_session(): self._in_messages = collections.OrderedDict() @@ -2984,19 +3751,27 @@ def _messages_reconnect_reset_in(self): # Preserve current state pass - def _messages_reconnect_reset(self): + def _messages_reconnect_reset(self) -> None: self._messages_reconnect_reset_out() self._messages_reconnect_reset_in() - def _packet_queue(self, command, packet, mid, qos, info=None): - mpkt = { - 'command': command, - 'mid': mid, - 'qos': qos, - 'pos': 0, - 'to_process': len(packet), - 'packet': packet, - 'info': info} + def _packet_queue( + self, + command: int, + packet: bytes, + mid: int, + qos: int, + info: MQTTMessageInfo | None = None, + ) -> MQTTErrorCode: + mpkt: _OutPacket = { + "command": command, + "mid": mid, + "qos": qos, + "pos": 0, + "to_process": len(packet), + "packet": packet, + "info": info, + } self._out_packet.append(mpkt) @@ -3017,9 +3792,9 @@ def _packet_queue(self, command, packet, mid, qos, info=None): self._call_socket_register_write() - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _packet_handle(self): + def _packet_handle(self) -> MQTTErrorCode: cmd = self._in_packet['command'] & 0xF0 if cmd == PINGREQ: return self._handle_pingreq() @@ -3038,38 +3813,40 @@ def _packet_handle(self): elif cmd == CONNACK: return self._handle_connack() elif cmd == SUBACK: - return self._handle_suback() + self._handle_suback() + return MQTTErrorCode.MQTT_ERR_SUCCESS elif cmd == UNSUBACK: return self._handle_unsuback() elif cmd == DISCONNECT and self._protocol == MQTTv5: # only allowed in MQTT 5.0 - return self._handle_disconnect() + self._handle_disconnect() + return MQTTErrorCode.MQTT_ERR_SUCCESS else: # If we don't recognise the command, return an error straight away. self._easy_log(MQTT_LOG_ERR, "Error: Unrecognised command %s", cmd) - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL - def _handle_pingreq(self): + def _handle_pingreq(self) -> MQTTErrorCode: if self._in_packet['remaining_length'] != 0: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL self._easy_log(MQTT_LOG_DEBUG, "Received PINGREQ") return self._send_pingresp() - def _handle_pingresp(self): + def _handle_pingresp(self) -> MQTTErrorCode: if self._in_packet['remaining_length'] != 0: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL # No longer waiting for a PINGRESP. self._ping_t = 0 self._easy_log(MQTT_LOG_DEBUG, "Received PINGRESP") - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _handle_connack(self): + def _handle_connack(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL if self._protocol == MQTTv5: (flags, result) = struct.unpack( @@ -3077,14 +3854,16 @@ def _handle_connack(self): if result == 1: # This is probably a failure from a broker that doesn't support # MQTT v5. - reason = 132 # Unsupported protocol version + reason = ReasonCode(CONNACK >> 4, aName="Unsupported protocol version") properties = None else: - reason = ReasonCodes(CONNACK >> 4, identifier=result) + reason = ReasonCode(CONNACK >> 4, identifier=result) properties = Properties(CONNACK >> 4) properties.unpack(self._in_packet['packet'][2:]) else: (flags, result) = struct.unpack("!BB", self._in_packet['packet']) + reason = convert_connack_rc_to_reason_code(result) + properties = None if self._protocol == MQTTv311: if result == CONNACK_REFUSED_PROTOCOL_VERSION: if not self._reconnect_on_failure: @@ -3106,11 +3885,11 @@ def _handle_connack(self): "Received CONNACK (%s, %s), attempting to use non-empty CID", flags, result, ) - self._client_id = base62(uuid.uuid4().int, padding=22) + self._client_id = _base62(uuid.uuid4().int, padding=22).encode("utf8") return self.reconnect() if result == 0: - self._state = mqtt_cs_connected + self._state = _ConnectionState.MQTT_CS_CONNECTED self._reconnect_delay = None if self._protocol == MQTTv5: @@ -3131,12 +3910,36 @@ def _handle_connack(self): flags_dict['session present'] = flags & 0x01 with self._in_callback_mutex: try: - if self._protocol == MQTTv5: - on_connect(self, self._userdata, - flags_dict, reason, properties) - else: + if self._callback_api_version == CallbackAPIVersion.VERSION1: + if self._protocol == MQTTv5: + on_connect = cast(CallbackOnConnect_v1_mqtt5, on_connect) + + on_connect(self, self._userdata, + flags_dict, reason, properties) + else: + on_connect = cast(CallbackOnConnect_v1_mqtt3, on_connect) + + on_connect( + self, self._userdata, flags_dict, result) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_connect = cast(CallbackOnConnect_v2, on_connect) + + connect_flags = ConnectFlags( + session_present=flags_dict['session present'] > 0 + ) + + if properties is None: + properties = Properties(PacketTypes.CONNACK) + on_connect( - self, self._userdata, flags_dict, result) + self, + self._userdata, + connect_flags, + reason, + properties, + ) + else: + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_connect: %s', err) @@ -3144,7 +3947,7 @@ def _handle_connack(self): raise if result == 0: - rc = 0 + rc = MQTTErrorCode.MQTT_ERR_SUCCESS with self._out_message_mutex: for m in self._out_messages.values(): m.timestamp = time_func() @@ -3163,7 +3966,7 @@ def _handle_connack(self): m.dup, properties=m.properties ) - if rc != 0: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc elif m.qos == 1: if m.state == mqtt_ms_publish: @@ -3179,7 +3982,7 @@ def _handle_connack(self): m.dup, properties=m.properties ) - if rc != 0: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc elif m.qos == 2: if m.state == mqtt_ms_publish: @@ -3195,28 +3998,28 @@ def _handle_connack(self): m.dup, properties=m.properties ) - if rc != 0: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc elif m.state == mqtt_ms_resend_pubrel: self._inflight_messages += 1 m.state = mqtt_ms_wait_for_pubcomp with self._in_callback_mutex: # Don't call loop_write after _send_publish() rc = self._send_pubrel(m.mid) - if rc != 0: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc self.loop_write() # Process outgoing messages that have just been queued up return rc elif result > 0 and result < 6: - return MQTT_ERR_CONN_REFUSED + return MQTTErrorCode.MQTT_ERR_CONN_REFUSED else: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL - def _handle_disconnect(self): + def _handle_disconnect(self) -> None: packet_type = DISCONNECT >> 4 reasonCode = properties = None if self._in_packet['remaining_length'] > 2: - reasonCode = ReasonCodes(packet_type) + reasonCode = ReasonCode(packet_type) reasonCode.unpack(self._in_packet['packet']) if self._in_packet['remaining_length'] > 3: properties = Properties(packet_type) @@ -3227,26 +4030,28 @@ def _handle_disconnect(self): properties ) - self._loop_rc_handle(reasonCode, properties) - - return MQTT_ERR_SUCCESS + self._sock_close() + self._do_on_disconnect( + packet_from_broker=True, + v1_rc=MQTTErrorCode.MQTT_ERR_SUCCESS, # If reason is absent (remaining length < 1), it means normal disconnection + reason=reasonCode, + properties=properties, + ) - def _handle_suback(self): + def _handle_suback(self) -> None: self._easy_log(MQTT_LOG_DEBUG, "Received SUBACK") - pack_format = "!H" + str(len(self._in_packet['packet']) - 2) + 's' + pack_format = f"!H{len(self._in_packet['packet']) - 2}s" (mid, packet) = struct.unpack(pack_format, self._in_packet['packet']) if self._protocol == MQTTv5: properties = Properties(SUBACK >> 4) props, props_len = properties.unpack(packet) - reasoncodes = [] - for c in packet[props_len:]: - if sys.version_info[0] < 3: - c = ord(c) - reasoncodes.append(ReasonCodes(SUBACK >> 4, identifier=c)) + reasoncodes = [ReasonCode(SUBACK >> 4, identifier=c) for c in packet[props_len:]] else: - pack_format = "!" + "B" * len(packet) + pack_format = f"!{'B' * len(packet)}" granted_qos = struct.unpack(pack_format, packet) + reasoncodes = [ReasonCode(SUBACK >> 4, identifier=c) for c in granted_qos] + properties = Properties(SUBACK >> 4) with self._callback_mutex: on_subscribe = self.on_subscribe @@ -3254,36 +4059,49 @@ def _handle_suback(self): if on_subscribe: with self._in_callback_mutex: # Don't call loop_write after _send_publish() try: - if self._protocol == MQTTv5: + if self._callback_api_version == CallbackAPIVersion.VERSION1: + if self._protocol == MQTTv5: + on_subscribe = cast(CallbackOnSubscribe_v1_mqtt5, on_subscribe) + + on_subscribe( + self, self._userdata, mid, reasoncodes, properties) + else: + on_subscribe = cast(CallbackOnSubscribe_v1_mqtt3, on_subscribe) + + on_subscribe( + self, self._userdata, mid, granted_qos) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_subscribe = cast(CallbackOnSubscribe_v2, on_subscribe) + on_subscribe( - self, self._userdata, mid, reasoncodes, properties) + self, + self._userdata, + mid, + reasoncodes, + properties, + ) else: - on_subscribe( - self, self._userdata, mid, granted_qos) + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_subscribe: %s', err) if not self.suppress_exceptions: raise - return MQTT_ERR_SUCCESS - - def _handle_publish(self): - rc = 0 - + def _handle_publish(self) -> MQTTErrorCode: header = self._in_packet['command'] message = MQTTMessage() - message.dup = (header & 0x08) >> 3 + message.dup = ((header & 0x08) >> 3) != 0 message.qos = (header & 0x06) >> 1 - message.retain = (header & 0x01) + message.retain = (header & 0x01) != 0 - pack_format = "!H" + str(len(self._in_packet['packet']) - 2) + 's' + pack_format = f"!H{len(self._in_packet['packet']) - 2}s" (slen, packet) = struct.unpack(pack_format, self._in_packet['packet']) - pack_format = '!' + str(slen) + 's' + str(len(packet) - slen) + 's' + pack_format = f"!{slen}s{len(packet) - slen}s" (topic, packet) = struct.unpack(pack_format, packet) if self._protocol != MQTTv5 and len(topic) == 0: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL # Handle topics with invalid UTF-8 # This replaces an invalid topic with a message and the hex @@ -3292,12 +4110,12 @@ def _handle_publish(self): try: print_topic = topic.decode('utf-8') except UnicodeDecodeError: - print_topic = "TOPIC WITH INVALID UTF-8: " + str(topic) + print_topic = f"TOPIC WITH INVALID UTF-8: {topic!r}" message.topic = topic if message.qos > 0: - pack_format = "!H" + str(len(packet) - 2) + 's' + pack_format = f"!H{len(packet) - 2}s" (message.mid, packet) = struct.unpack(pack_format, packet) if self._protocol == MQTTv5: @@ -3325,27 +4143,63 @@ def _handle_publish(self): message.timestamp = time_func() if message.qos == 0: self._handle_on_message(message) - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS elif message.qos == 1: self._handle_on_message(message) - return self._send_puback(message.mid) + if self._manual_ack: + return MQTTErrorCode.MQTT_ERR_SUCCESS + else: + return self._send_puback(message.mid) elif message.qos == 2: + rc = self._send_pubrec(message.mid) + message.state = mqtt_ms_wait_for_pubrel with self._in_message_mutex: self._in_messages[message.mid] = message + return rc else: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL + + def ack(self, mid: int, qos: int) -> MQTTErrorCode: + """ + send an acknowledgement for a given message id (stored in :py:attr:`message.mid `). + only useful in QoS>=1 and ``manual_ack=True`` (option of `Client`) + """ + if self._manual_ack : + if qos == 1: + return self._send_puback(mid) + elif qos == 2: + return self._send_pubcomp(mid) + + return MQTTErrorCode.MQTT_ERR_SUCCESS + + def manual_ack_set(self, on: bool) -> None: + """ + The paho library normally acknowledges messages as soon as they are delivered to the caller. + If manual_ack is turned on, then the caller MUST manually acknowledge every message once + application processing is complete using `ack()` + """ + self._manual_ack = on - def _handle_pubrel(self): + + def _handle_pubrel(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL - mid, = struct.unpack("!H", self._in_packet['packet']) + mid, = struct.unpack("!H", self._in_packet['packet'][:2]) + if self._protocol == MQTTv5: + if self._in_packet['remaining_length'] > 2: + reasonCode = ReasonCode(PUBREL >> 4) + reasonCode.unpack(self._in_packet['packet'][2:]) + if self._in_packet['remaining_length'] > 3: + properties = Properties(PUBREL >> 4) + props, props_len = properties.unpack( + self._in_packet['packet'][3:]) self._easy_log(MQTT_LOG_DEBUG, "Received PUBREL (Mid: %d)", mid) with self._in_message_mutex: @@ -3358,18 +4212,21 @@ def _handle_pubrel(self): if self._max_inflight_messages > 0: with self._out_message_mutex: rc = self._update_inflight() - if rc != MQTT_ERR_SUCCESS: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc # FIXME: this should only be done if the message is known # If unknown it's a protocol error and we should close the connection. # But since we don't have (on disk) persistence for the session, it # is possible that we must known about this message. - # Choose to acknwoledge this messsage (and thus losing a message) but + # Choose to acknowledge this message (thus losing a message) but # avoid hanging. See #284. - return self._send_pubcomp(mid) + if self._manual_ack: + return MQTTErrorCode.MQTT_ERR_SUCCESS + else: + return self._send_pubcomp(mid) - def _update_inflight(self): + def _update_inflight(self) -> MQTTErrorCode: # Dont lock message_mutex here for m in self._out_messages.values(): if self._inflight_messages < self._max_inflight_messages: @@ -3388,23 +4245,23 @@ def _update_inflight(self): m.dup, properties=m.properties, ) - if rc != 0: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc else: - return MQTT_ERR_SUCCESS - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _handle_pubrec(self): + def _handle_pubrec(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL mid, = struct.unpack("!H", self._in_packet['packet'][:2]) if self._protocol == MQTTv5: if self._in_packet['remaining_length'] > 2: - reasonCode = ReasonCodes(PUBREC >> 4) + reasonCode = ReasonCode(PUBREC >> 4) reasonCode.unpack(self._in_packet['packet'][2:]) if self._in_packet['remaining_length'] > 3: properties = Properties(PUBREC >> 4) @@ -3419,27 +4276,27 @@ def _handle_pubrec(self): msg.timestamp = time_func() return self._send_pubrel(mid) - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _handle_unsuback(self): + def _handle_unsuback(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 4: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL mid, = struct.unpack("!H", self._in_packet['packet'][:2]) if self._protocol == MQTTv5: packet = self._in_packet['packet'][2:] properties = Properties(UNSUBACK >> 4) props, props_len = properties.unpack(packet) - reasoncodes = [] - for c in packet[props_len:]: - if sys.version_info[0] < 3: - c = ord(c) - reasoncodes.append(ReasonCodes(UNSUBACK >> 4, identifier=c)) - if len(reasoncodes) == 1: - reasoncodes = reasoncodes[0] + reasoncodes_list = [ + ReasonCode(UNSUBACK >> 4, identifier=c) + for c in packet[props_len:] + ] + else: + reasoncodes_list = [] + properties = Properties(UNSUBACK >> 4) self._easy_log(MQTT_LOG_DEBUG, "Received UNSUBACK (Mid: %d)", mid) with self._callback_mutex: @@ -3448,45 +4305,119 @@ def _handle_unsuback(self): if on_unsubscribe: with self._in_callback_mutex: try: - if self._protocol == MQTTv5: + if self._callback_api_version == CallbackAPIVersion.VERSION1: + if self._protocol == MQTTv5: + on_unsubscribe = cast(CallbackOnUnsubscribe_v1_mqtt5, on_unsubscribe) + + reasoncodes: ReasonCode | list[ReasonCode] = reasoncodes_list + if len(reasoncodes_list) == 1: + reasoncodes = reasoncodes_list[0] + + on_unsubscribe( + self, self._userdata, mid, properties, reasoncodes) + else: + on_unsubscribe = cast(CallbackOnUnsubscribe_v1_mqtt3, on_unsubscribe) + + on_unsubscribe(self, self._userdata, mid) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_unsubscribe = cast(CallbackOnUnsubscribe_v2, on_unsubscribe) + + if properties is None: + properties = Properties(PacketTypes.CONNACK) + on_unsubscribe( - self, self._userdata, mid, properties, reasoncodes) + self, + self._userdata, + mid, + reasoncodes_list, + properties, + ) else: - on_unsubscribe(self, self._userdata, mid) + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_unsubscribe: %s', err) if not self.suppress_exceptions: raise - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _do_on_disconnect(self, rc, properties=None): + def _do_on_disconnect( + self, + packet_from_broker: bool, + v1_rc: MQTTErrorCode, + reason: ReasonCode | None = None, + properties: Properties | None = None, + ) -> None: with self._callback_mutex: on_disconnect = self.on_disconnect if on_disconnect: with self._in_callback_mutex: try: - if self._protocol == MQTTv5: + if self._callback_api_version == CallbackAPIVersion.VERSION1: + if self._protocol == MQTTv5: + on_disconnect = cast(CallbackOnDisconnect_v1_mqtt5, on_disconnect) + + if packet_from_broker: + on_disconnect(self, self._userdata, reason, properties) + else: + on_disconnect(self, self._userdata, v1_rc, None) + else: + on_disconnect = cast(CallbackOnDisconnect_v1_mqtt3, on_disconnect) + + on_disconnect(self, self._userdata, v1_rc) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_disconnect = cast(CallbackOnDisconnect_v2, on_disconnect) + + disconnect_flags = DisconnectFlags( + is_disconnect_packet_from_server=packet_from_broker + ) + + if reason is None: + reason = convert_disconnect_error_code_to_reason_code(v1_rc) + + if properties is None: + properties = Properties(PacketTypes.DISCONNECT) + on_disconnect( - self, self._userdata, rc, properties) + self, + self._userdata, + disconnect_flags, + reason, + properties, + ) else: - on_disconnect(self, self._userdata, rc) + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err) if not self.suppress_exceptions: raise - def _do_on_publish(self, mid): + def _do_on_publish(self, mid: int, reason_code: ReasonCode, properties: Properties) -> MQTTErrorCode: with self._callback_mutex: on_publish = self.on_publish if on_publish: with self._in_callback_mutex: try: - on_publish(self, self._userdata, mid) + if self._callback_api_version == CallbackAPIVersion.VERSION1: + on_publish = cast(CallbackOnPublish_v1, on_publish) + + on_publish(self, self._userdata, mid) + elif self._callback_api_version == CallbackAPIVersion.VERSION2: + on_publish = cast(CallbackOnPublish_v2, on_publish) + + on_publish( + self, + self._userdata, + mid, + reason_code, + properties, + ) + else: + raise RuntimeError("Unsupported callback API version") except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err) @@ -3499,26 +4430,28 @@ def _do_on_publish(self, mid): self._inflight_messages -= 1 if self._max_inflight_messages > 0: rc = self._update_inflight() - if rc != MQTT_ERR_SUCCESS: + if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _handle_pubackcomp(self, cmd): + def _handle_pubackcomp( + self, cmd: Literal['PUBACK'] | Literal['PUBCOMP'] + ) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: - return MQTT_ERR_PROTOCOL + return MQTTErrorCode.MQTT_ERR_PROTOCOL - packet_type = PUBACK if cmd == "PUBACK" else PUBCOMP - packet_type = packet_type >> 4 + packet_type_enum = PUBACK if cmd == "PUBACK" else PUBCOMP + packet_type = packet_type_enum.value >> 4 mid, = struct.unpack("!H", self._in_packet['packet'][:2]) + reasonCode = ReasonCode(packet_type) + properties = Properties(packet_type) if self._protocol == MQTTv5: if self._in_packet['remaining_length'] > 2: - reasonCode = ReasonCodes(packet_type) reasonCode.unpack(self._in_packet['packet'][2:]) if self._in_packet['remaining_length'] > 3: - properties = Properties(packet_type) props, props_len = properties.unpack( self._in_packet['packet'][3:]) self._easy_log(MQTT_LOG_DEBUG, "Received %s (Mid: %d)", cmd, mid) @@ -3526,13 +4459,12 @@ def _handle_pubackcomp(self, cmd): with self._out_message_mutex: if mid in self._out_messages: # Only inform the client the message has been sent once. - rc = self._do_on_publish(mid) + rc = self._do_on_publish(mid, reasonCode, properties) return rc - return MQTT_ERR_SUCCESS + return MQTTErrorCode.MQTT_ERR_SUCCESS - def _handle_on_message(self, message): - matched = False + def _handle_on_message(self, message: MQTTMessage) -> None: try: topic = message.topic @@ -3542,8 +4474,7 @@ def _handle_on_message(self, message): on_message_callbacks = [] with self._callback_mutex: if topic is not None: - for callback in self._on_message_filtered.iter_match(message.topic): - on_message_callbacks.append(callback) + on_message_callbacks = list(self._on_message_filtered.iter_match(message.topic)) if len(on_message_callbacks) == 0: on_message = self.on_message @@ -3575,7 +4506,7 @@ def _handle_on_message(self, message): raise - def _handle_on_connect_fail(self): + def _handle_on_connect_fail(self) -> None: with self._callback_mutex: on_connect_fail = self.on_connect_fail @@ -3587,10 +4518,13 @@ def _handle_on_connect_fail(self): self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_connect_fail: %s', err) - def _thread_main(self): - self.loop_forever(retry_first_connection=True) + def _thread_main(self) -> None: + try: + self.loop_forever(retry_first_connection=True) + finally: + self._thread = None - def _reconnect_wait(self): + def _reconnect_wait(self) -> None: # See reconnect_delay_set for details now = time_func() with self._reconnect_delay_mutex: @@ -3605,7 +4539,7 @@ def _reconnect_wait(self): target_time = now + self._reconnect_delay remaining = target_time - now - while (self._state != mqtt_cs_disconnecting + while (self._state not in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED) and not self._thread_terminate and remaining > 0): @@ -3613,10 +4547,10 @@ def _reconnect_wait(self): remaining = target_time - time_func() @staticmethod - def _proxy_is_valid(p): - def check(t, a): + def _proxy_is_valid(p) -> bool: # type: ignore[no-untyped-def] + def check(t, a) -> bool: # type: ignore[no-untyped-def] return (socks is not None and - t in set([socks.HTTP, socks.SOCKS4, socks.SOCKS5]) and a) + t in {socks.HTTP, socks.SOCKS4, socks.SOCKS5} and a) if isinstance(p, dict): return check(p.get("proxy_type"), p.get("proxy_addr")) @@ -3625,7 +4559,7 @@ def check(t, a): else: return False - def _get_proxy(self): + def _get_proxy(self) -> dict[str, Any] | None: if socks is None: return None @@ -3636,11 +4570,11 @@ def _get_proxy(self): # Next, check for an mqtt_proxy environment variable as long as the host # we're trying to connect to isn't listed under the no_proxy environment # variable (matches built-in module urllib's behavior) - if not (hasattr(urllib_dot_request, "proxy_bypass") and - urllib_dot_request.proxy_bypass(self._host)): - env_proxies = urllib_dot_request.getproxies() + if not (hasattr(urllib.request, "proxy_bypass") and + urllib.request.proxy_bypass(self._host)): + env_proxies = urllib.request.getproxies() if "mqtt" in env_proxies: - parts = urllib_dot_parse.urlparse(env_proxies["mqtt"]) + parts = urllib.parse.urlparse(env_proxies["mqtt"]) if parts.scheme == "http": proxy = { "proxy_type": socks.HTTP, @@ -3668,24 +4602,83 @@ def _get_proxy(self): # None to indicate that the connection should be handled normally return None - def _create_socket_connection(self): + def _create_socket(self) -> SocketLike: + if self._transport == "unix": + sock = self._create_unix_socket_connection() + else: + sock = self._create_socket_connection() + + if self._ssl: + sock = self._ssl_wrap_socket(sock) + + if self._transport == "websockets": + sock.settimeout(self._keepalive) + return _WebsocketWrapper( + socket=sock, + host=self._host, + port=self._port, + is_ssl=self._ssl, + path=self._websocket_path, + extra_headers=self._websocket_extra_headers, + ) + + return sock + + def _create_unix_socket_connection(self) -> _socket.socket: + unix_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + unix_socket.connect(self._host) + return unix_socket + + def _create_socket_connection(self) -> _socket.socket: proxy = self._get_proxy() addr = (self._host, self._port) source = (self._bind_address, self._bind_port) - - if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2): - # Have to short-circuit here because of unsupported source_address - # param in earlier Python versions. - return socket.create_connection(addr, timeout=self._connect_timeout) - if proxy: return socks.create_connection(addr, timeout=self._connect_timeout, source_address=source, **proxy) else: return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source) + def _ssl_wrap_socket(self, tcp_sock: _socket.socket) -> ssl.SSLSocket: + if self._ssl_context is None: + raise ValueError( + "Impossible condition. _ssl_context should never be None if _ssl is True" + ) + + verify_host = not self._tls_insecure + try: + # Try with server_hostname, even it's not supported in certain scenarios + ssl_sock = self._ssl_context.wrap_socket( + tcp_sock, + server_hostname=self._host, + do_handshake_on_connect=False, + ) + except ssl.CertificateError: + # CertificateError is derived from ValueError + raise + except ValueError: + # Python version requires SNI in order to handle server_hostname, but SNI is not available + ssl_sock = self._ssl_context.wrap_socket( + tcp_sock, + do_handshake_on_connect=False, + ) + else: + # If SSL context has already checked hostname, then don't need to do it again + if getattr(self._ssl_context, 'check_hostname', False): # type: ignore + verify_host = False + + ssl_sock.settimeout(self._keepalive) + ssl_sock.do_handshake() + + if verify_host: + # TODO: this type error is a true error: + # error: Module has no attribute "match_hostname" [attr-defined] + # Python 3.12 no longer have this method. + ssl.match_hostname(ssl_sock.getpeercert(), self._host) # type: ignore -class WebsocketWrapper(object): + return ssl_sock + +class _WebsocketWrapper: OPCODE_CONTINUATION = 0x0 OPCODE_TEXT = 0x1 OPCODE_BINARY = 0x2 @@ -3693,8 +4686,15 @@ class WebsocketWrapper(object): OPCODE_PING = 0x9 OPCODE_PONG = 0xa - def __init__(self, socket, host, port, is_ssl, path, extra_headers): - + def __init__( + self, + socket: socket.socket | ssl.SSLSocket, + host: str, + port: int, + is_ssl: bool, + path: str, + extra_headers: WebSocketHeaders | None, + ): self.connected = False self._ssl = is_ssl @@ -3712,21 +4712,32 @@ def __init__(self, socket, host, port, is_ssl, path, extra_headers): self._do_handshake(extra_headers) - def __del__(self): - - self._sendbuffer = None - self._readbuffer = None + def __del__(self) -> None: + self._sendbuffer = bytearray() + self._readbuffer = bytearray() - def _do_handshake(self, extra_headers): + def _do_handshake(self, extra_headers: WebSocketHeaders | None) -> None: sec_websocket_key = uuid.uuid4().bytes sec_websocket_key = base64.b64encode(sec_websocket_key) + if self._ssl: + default_port = 443 + http_schema = "https" + else: + default_port = 80 + http_schema = "http" + + if default_port == self._port: + host_port = f"{self._host}" + else: + host_port = f"{self._host}:{self._port}" + websocket_headers = { - "Host": "{self._host:s}:{self._port:d}".format(self=self), + "Host": host_port, "Upgrade": "websocket", "Connection": "Upgrade", - "Origin": "https://{self._host:s}:{self._port:d}".format(self=self), + "Origin": f"{http_schema}://{host_port}", "Sec-WebSocket-Key": sec_websocket_key.decode("utf8"), "Sec-Websocket-Version": "13", "Sec-Websocket-Protocol": "mqtt", @@ -3740,9 +4751,8 @@ def _do_handshake(self, extra_headers): websocket_headers = extra_headers(websocket_headers) header = "\r\n".join([ - "GET {self._path} HTTP/1.1".format(self=self), - "\r\n".join("{}: {}".format(i, j) - for i, j in websocket_headers.items()), + f"GET {self._path} HTTP/1.1", + "\r\n".join(f"{i}: {j}" for i, j in websocket_headers.items()), "\r\n", ]).encode("utf8") @@ -3753,7 +4763,10 @@ def _do_handshake(self, extra_headers): while True: # read HTTP response header as lines - byte = self._socket.recv(1) + try: + byte = self._socket.recv(1) + except ConnectionResetError: + byte = b"" self._readbuffer.extend(byte) @@ -3772,13 +4785,14 @@ def _do_handshake(self, extra_headers): if b"sec-websocket-accept" in str(self._readbuffer).lower().encode('utf-8'): GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" - server_hash = self._readbuffer.decode( + server_hash_str = self._readbuffer.decode( 'utf-8').split(": ", 1)[1] - server_hash = server_hash.strip().encode('utf-8') + server_hash = server_hash_str.strip().encode('utf-8') - client_hash = sec_websocket_key.decode('utf-8') + GUID - client_hash = hashlib.sha1(client_hash.encode('utf-8')) - client_hash = base64.b64encode(client_hash.digest()) + client_hash_key = sec_websocket_key.decode('utf-8') + GUID + # Use of SHA-1 is OK here; it's according to the Websocket spec. + client_hash_digest = hashlib.sha1(client_hash_key.encode('utf-8')) # noqa: S324 + client_hash = base64.b64encode(client_hash_digest.digest()) if server_hash != client_hash: raise WebsocketConnectionError( @@ -3802,8 +4816,9 @@ def _do_handshake(self, extra_headers): self._readbuffer = bytearray() self.connected = True - def _create_frame(self, opcode, data, do_masking=1): - + def _create_frame( + self, opcode: int, data: bytearray, do_masking: int = 1 + ) -> bytearray: header = bytearray() length = len(data) @@ -3834,7 +4849,7 @@ def _create_frame(self, opcode, data, do_masking=1): return header + data - def _buffered_read(self, length): + def _buffered_read(self, length: int) -> bytearray: # try to recv and store needed bytes wanted_bytes = length - (len(self._readbuffer) - self._readbuffer_head) @@ -3853,14 +4868,14 @@ def _buffered_read(self, length): self._readbuffer_head += length return self._readbuffer[self._readbuffer_head - length:self._readbuffer_head] - def _recv_impl(self, length): + def _recv_impl(self, length: int) -> bytes: # try to decode websocket payload part from data try: self._readbuffer_head = 0 - result = None + result = b"" chunk_startindex = self._payload_head chunk_endindex = self._payload_head + length @@ -3899,7 +4914,7 @@ def _recv_impl(self, length): payload = self._buffered_read(readindex) # unmask only the needed part - if maskbit: + if mask_key is not None: for index in range(chunk_startindex, readindex): payload[index] ^= mask_key[index % 4] @@ -3913,20 +4928,20 @@ def _recv_impl(self, length): self._readbuffer = bytearray() self._payload_head = 0 - # respond to non-binary opcodes, their arrival is not guaranteed beacause of non-blocking sockets - if opcode == WebsocketWrapper.OPCODE_CONNCLOSE: + # respond to non-binary opcodes, their arrival is not guaranteed because of non-blocking sockets + if opcode == _WebsocketWrapper.OPCODE_CONNCLOSE: frame = self._create_frame( - WebsocketWrapper.OPCODE_CONNCLOSE, payload, 0) + _WebsocketWrapper.OPCODE_CONNCLOSE, payload, 0) self._socket.send(frame) - if opcode == WebsocketWrapper.OPCODE_PING: + if opcode == _WebsocketWrapper.OPCODE_PING: frame = self._create_frame( - WebsocketWrapper.OPCODE_PONG, payload, 0) + _WebsocketWrapper.OPCODE_PONG, payload, 0) self._socket.send(frame) # This isn't *proper* handling of continuation frames, but given # that we only support binary frames, it is *probably* good enough. - if (opcode == WebsocketWrapper.OPCODE_BINARY or opcode == WebsocketWrapper.OPCODE_CONTINUATION) \ + if (opcode == _WebsocketWrapper.OPCODE_BINARY or opcode == _WebsocketWrapper.OPCODE_CONTINUATION) \ and payload_length > 0: return result else: @@ -3936,13 +4951,13 @@ def _recv_impl(self, length): self.connected = False return b'' - def _send_impl(self, data): + def _send_impl(self, data: bytes) -> int: # if previous frame was sent successfully if len(self._sendbuffer) == 0: # create websocket frame frame = self._create_frame( - WebsocketWrapper.OPCODE_BINARY, bytearray(data)) + _WebsocketWrapper.OPCODE_BINARY, bytearray(data)) self._sendbuffer.extend(frame) self._requested_size = len(data) @@ -3958,32 +4973,32 @@ def _send_impl(self, data): # couldn't send whole data, request the same data again with 0 as sent length return 0 - def recv(self, length): + def recv(self, length: int) -> bytes: return self._recv_impl(length) - def read(self, length): + def read(self, length: int) -> bytes: return self._recv_impl(length) - def send(self, data): + def send(self, data: bytes) -> int: return self._send_impl(data) - def write(self, data): + def write(self, data: bytes) -> int: return self._send_impl(data) - def close(self): + def close(self) -> None: self._socket.close() - def fileno(self): + def fileno(self) -> int: return self._socket.fileno() - def pending(self): + def pending(self) -> int: # Fix for bug #131: a SSL socket may still have data available # for reading without select() being aware of it. if self._ssl: - return self._socket.pending() + return self._socket.pending() # type: ignore[union-attr] else: # normal socket rely only on select() return 0 - def setblocking(self, flag): + def setblocking(self, flag: bool) -> None: self._socket.setblocking(flag) diff --git a/src/paho/mqtt/enums.py b/src/paho/mqtt/enums.py new file mode 100644 index 00000000..5428769f --- /dev/null +++ b/src/paho/mqtt/enums.py @@ -0,0 +1,113 @@ +import enum + + +class MQTTErrorCode(enum.IntEnum): + MQTT_ERR_AGAIN = -1 + MQTT_ERR_SUCCESS = 0 + MQTT_ERR_NOMEM = 1 + MQTT_ERR_PROTOCOL = 2 + MQTT_ERR_INVAL = 3 + MQTT_ERR_NO_CONN = 4 + MQTT_ERR_CONN_REFUSED = 5 + MQTT_ERR_NOT_FOUND = 6 + MQTT_ERR_CONN_LOST = 7 + MQTT_ERR_TLS = 8 + MQTT_ERR_PAYLOAD_SIZE = 9 + MQTT_ERR_NOT_SUPPORTED = 10 + MQTT_ERR_AUTH = 11 + MQTT_ERR_ACL_DENIED = 12 + MQTT_ERR_UNKNOWN = 13 + MQTT_ERR_ERRNO = 14 + MQTT_ERR_QUEUE_SIZE = 15 + MQTT_ERR_KEEPALIVE = 16 + + +class MQTTProtocolVersion(enum.IntEnum): + MQTTv31 = 3 + MQTTv311 = 4 + MQTTv5 = 5 + + +class CallbackAPIVersion(enum.Enum): + """Defined the arguments passed to all user-callback. + + See each callbacks for details: `on_connect`, `on_connect_fail`, `on_disconnect`, `on_message`, `on_publish`, + `on_subscribe`, `on_unsubscribe`, `on_log`, `on_socket_open`, `on_socket_close`, + `on_socket_register_write`, `on_socket_unregister_write` + """ + VERSION1 = 1 + """The version used with paho-mqtt 1.x before introducing CallbackAPIVersion. + + This version had different arguments depending if MQTTv5 or MQTTv3 was used. `Properties` & `ReasonCode` were missing + on some callback (apply only to MQTTv5). + + This version is deprecated and will be removed in version 3.0. + """ + VERSION2 = 2 + """ This version fix some of the shortcoming of previous version. + + Callback have the same signature if using MQTTv5 or MQTTv3. `ReasonCode` are used in MQTTv3. + """ + + +class MessageType(enum.IntEnum): + CONNECT = 0x10 + CONNACK = 0x20 + PUBLISH = 0x30 + PUBACK = 0x40 + PUBREC = 0x50 + PUBREL = 0x60 + PUBCOMP = 0x70 + SUBSCRIBE = 0x80 + SUBACK = 0x90 + UNSUBSCRIBE = 0xA0 + UNSUBACK = 0xB0 + PINGREQ = 0xC0 + PINGRESP = 0xD0 + DISCONNECT = 0xE0 + AUTH = 0xF0 + + +class LogLevel(enum.IntEnum): + MQTT_LOG_INFO = 0x01 + MQTT_LOG_NOTICE = 0x02 + MQTT_LOG_WARNING = 0x04 + MQTT_LOG_ERR = 0x08 + MQTT_LOG_DEBUG = 0x10 + + +class ConnackCode(enum.IntEnum): + CONNACK_ACCEPTED = 0 + CONNACK_REFUSED_PROTOCOL_VERSION = 1 + CONNACK_REFUSED_IDENTIFIER_REJECTED = 2 + CONNACK_REFUSED_SERVER_UNAVAILABLE = 3 + CONNACK_REFUSED_BAD_USERNAME_PASSWORD = 4 + CONNACK_REFUSED_NOT_AUTHORIZED = 5 + + +class _ConnectionState(enum.Enum): + MQTT_CS_NEW = enum.auto() + MQTT_CS_CONNECT_ASYNC = enum.auto() + MQTT_CS_CONNECTING = enum.auto() + MQTT_CS_CONNECTED = enum.auto() + MQTT_CS_CONNECTION_LOST = enum.auto() + MQTT_CS_DISCONNECTING = enum.auto() + MQTT_CS_DISCONNECTED = enum.auto() + + +class MessageState(enum.IntEnum): + MQTT_MS_INVALID = 0 + MQTT_MS_PUBLISH = 1 + MQTT_MS_WAIT_FOR_PUBACK = 2 + MQTT_MS_WAIT_FOR_PUBREC = 3 + MQTT_MS_RESEND_PUBREL = 4 + MQTT_MS_WAIT_FOR_PUBREL = 5 + MQTT_MS_RESEND_PUBCOMP = 6 + MQTT_MS_WAIT_FOR_PUBCOMP = 7 + MQTT_MS_SEND_PUBREC = 8 + MQTT_MS_QUEUED = 9 + + +class PahoClientMode(enum.IntEnum): + MQTT_CLIENT = 0 + MQTT_BRIDGE = 1 diff --git a/src/paho/mqtt/matcher.py b/src/paho/mqtt/matcher.py index 01ce295c..b73c13ac 100644 --- a/src/paho/mqtt/matcher.py +++ b/src/paho/mqtt/matcher.py @@ -1,4 +1,4 @@ -class MQTTMatcher(object): +class MQTTMatcher: """Intended to manage topic filters including wildcards. Internally, MQTTMatcher use a prefix tree (trie) to store @@ -6,7 +6,7 @@ class MQTTMatcher(object): method to iterate efficiently over all filters that match some topic name.""" - class Node(object): + class Node: __slots__ = '_children', '_content' def __init__(self): @@ -33,8 +33,8 @@ def __getitem__(self, key): if node._content is None: raise KeyError(key) return node._content - except KeyError: - raise KeyError(key) + except KeyError as ke: + raise KeyError(key) from ke def __delitem__(self, key): """Delete the value associated with some topic filter :key""" @@ -46,8 +46,8 @@ def __delitem__(self, key): lst.append((parent, k, node)) # TODO node._content = None - except KeyError: - raise KeyError(key) + except KeyError as ke: + raise KeyError(key) from ke else: # cleanup for parent, k, node in reversed(lst): if node._children or node._content is not None: diff --git a/src/paho/mqtt/packettypes.py b/src/paho/mqtt/packettypes.py index 2fd6a1b5..d2051490 100644 --- a/src/paho/mqtt/packettypes.py +++ b/src/paho/mqtt/packettypes.py @@ -7,7 +7,7 @@ and Eclipse Distribution License v1.0 which accompany this distribution. The Eclipse Public License is available at - http://www.eclipse.org/legal/epl-v10.html + http://www.eclipse.org/legal/epl-v20.html and the Eclipse Distribution License is available at http://www.eclipse.org/org/documents/edl-v10.php. @@ -37,7 +37,7 @@ class PacketTypes: # Dummy packet type for properties use - will delay only applies to will WILLMESSAGE = 99 - Names = [ "reserved", \ + Names = ( "reserved", \ "Connect", "Connack", "Publish", "Puback", "Pubrec", "Pubrel", \ "Pubcomp", "Subscribe", "Suback", "Unsubscribe", "Unsuback", \ - "Pingreq", "Pingresp", "Disconnect", "Auth"] + "Pingreq", "Pingresp", "Disconnect", "Auth") diff --git a/src/paho/mqtt/properties.py b/src/paho/mqtt/properties.py index dbcf543e..f307b865 100644 --- a/src/paho/mqtt/properties.py +++ b/src/paho/mqtt/properties.py @@ -1,23 +1,20 @@ -""" -******************************************************************* - Copyright (c) 2017, 2019 IBM Corp. - - All rights reserved. This program and the accompanying materials - are made available under the terms of the Eclipse Public License v2.0 - and Eclipse Distribution License v1.0 which accompany this distribution. - - The Eclipse Public License is available at - http://www.eclipse.org/legal/epl-v10.html - and the Eclipse Distribution License is available at - http://www.eclipse.org/org/documents/edl-v10.php. - - Contributors: - Ian Craggs - initial implementation and/or documentation -******************************************************************* -""" +# ******************************************************************* +# Copyright (c) 2017, 2019 IBM Corp. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# and Eclipse Distribution License v1.0 which accompany this distribution. +# +# The Eclipse Public License is available at +# http://www.eclipse.org/legal/epl-v20.html +# and the Eclipse Distribution License is available at +# http://www.eclipse.org/org/documents/edl-v10.php. +# +# Contributors: +# Ian Craggs - initial implementation and/or documentation +# ******************************************************************* import struct -import sys from .packettypes import PacketTypes @@ -52,10 +49,8 @@ def readInt32(buf): def writeUTF(data): # data could be a string, or bytes. If string, encode into bytes with utf-8 - if sys.version_info[0] < 3: - data = bytearray(data, 'utf-8') - else: - data = data if type(data) == type(b"") else bytes(data, "utf-8") + if not isinstance(data, bytes): + data = bytes(data, "utf-8") return writeInt16(len(data)) + data @@ -100,19 +95,17 @@ class VariableByteIntegers: # Variable Byte Integer def encode(x): """ Convert an integer 0 <= x <= 268435455 into multi-byte format. - Returns the buffer convered from the integer. + Returns the buffer converted from the integer. """ - assert 0 <= x <= 268435455 + if not 0 <= x <= 268435455: + raise ValueError(f"Value {x!r} must be in range 0-268435455") buffer = b'' while 1: digit = x % 128 x //= 128 if x > 0: digit |= 0x80 - if sys.version_info[0] >= 3: - buffer += bytes([digit]) - else: - buffer += bytes(chr(digit)) + buffer += bytes([digit]) if x == 0: break return buffer @@ -139,21 +132,21 @@ def decode(buffer): return (value, bytes) -class Properties(object): +class Properties: """MQTT v5.0 properties class. See Properties.names for a list of accepted property names along with their numeric values. See Properties.properties for the data type of each property. - Example of use: + Example of use:: publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.UserProperty = ("a", "2") publish_properties.UserProperty = ("c", "3") First the object is created with packet type as argument, no properties will be present at - this point. Then properties are added as attributes, the name of which is the string property + this point. Then properties are added as attributes, the name of which is the string property name without the spaces. """ @@ -264,37 +257,33 @@ def __setattr__(self, name, value): # the name could have spaces in, or not. Remove spaces before assignment if name not in [aname.replace(' ', '') for aname in self.names.keys()]: raise MQTTException( - "Property name must be one of "+str(self.names.keys())) + f"Property name must be one of {self.names.keys()}") # check that this attribute applies to the packet type if self.packetType not in self.properties[self.getIdentFromName(name)][1]: - raise MQTTException("Property %s does not apply to packet type %s" - % (name, PacketTypes.Names[self.packetType])) + raise MQTTException(f"Property {name} does not apply to packet type {PacketTypes.Names[self.packetType]}") # Check for forbidden values - if type(value) != type([]): + if not isinstance(value, list): if name in ["ReceiveMaximum", "TopicAlias"] \ and (value < 1 or value > 65535): - raise MQTTException( - "%s property value must be in the range 1-65535" % (name)) + raise MQTTException(f"{name} property value must be in the range 1-65535") elif name in ["TopicAliasMaximum"] \ and (value < 0 or value > 65535): - raise MQTTException( - "%s property value must be in the range 0-65535" % (name)) + raise MQTTException(f"{name} property value must be in the range 0-65535") elif name in ["MaximumPacketSize", "SubscriptionIdentifier"] \ and (value < 1 or value > 268435455): - raise MQTTException( - "%s property value must be in the range 1-268435455" % (name)) + raise MQTTException(f"{name} property value must be in the range 1-268435455") elif name in ["RequestResponseInformation", "RequestProblemInformation", "PayloadFormatIndicator"] \ and (value != 0 and value != 1): raise MQTTException( - "%s property value must be 0 or 1" % (name)) + f"{name} property value must be 0 or 1") if self.allowsMultiple(name): - if type(value) != type([]): + if not isinstance(value, list): value = [value] if hasattr(self, name): value = object.__getattribute__(self, name) + value @@ -308,8 +297,7 @@ def __str__(self): if hasattr(self, compressedName): if not first: buffer += ", " - buffer += compressedName + " : " + \ - str(getattr(self, compressedName)) + buffer += f"{compressedName} : {getattr(self, compressedName)}" first = False buffer += "]" return buffer @@ -345,10 +333,7 @@ def writeProperty(self, identifier, type, value): buffer = b"" buffer += VariableByteIntegers.encode(identifier) # identifier if type == self.types.index("Byte"): # value - if sys.version_info[0] < 3: - buffer += chr(value) - else: - buffer += bytes([value]) + buffer += bytes([value]) elif type == self.types.index("Two Byte Integer"): buffer += writeInt16(value) elif type == self.types.index("Four Byte Integer"): @@ -412,8 +397,6 @@ def getNameFromIdent(self, identifier): return rc def unpack(self, buffer): - if sys.version_info[0] < 3: - buffer = bytearray(buffer) self.clear() # deserialize properties into attributes from buffer received from network propslen, VBIlen = VariableByteIntegers.decode(buffer) @@ -433,6 +416,6 @@ def unpack(self, buffer): compressedName = propname.replace(' ', '') if not self.allowsMultiple(compressedName) and hasattr(self, compressedName): raise MQTTException( - "Property '%s' must not exist more than once" % property) + f"Property '{property}' must not exist more than once") setattr(self, propname, value) return self, propslen + VBIlen diff --git a/src/paho/mqtt/publish.py b/src/paho/mqtt/publish.py index 6d1589a7..333c190a 100644 --- a/src/paho/mqtt/publish.py +++ b/src/paho/mqtt/publish.py @@ -5,7 +5,7 @@ # and Eclipse Distribution License v1.0 which accompany this distribution. # # The Eclipse Public License is available at -# http://www.eclipse.org/legal/epl-v10.html +# http://www.eclipse.org/legal/epl-v20.html # and the Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # @@ -18,20 +18,58 @@ situation where you have a single/multiple messages you want to publish to a broker, then disconnect and nothing else is required. """ -from __future__ import absolute_import +from __future__ import annotations import collections +from collections.abc import Iterable +from typing import TYPE_CHECKING, Any, List, Tuple, Union -try: - from collections.abc import Iterable -except ImportError: - from collections import Iterable +from paho.mqtt.enums import CallbackAPIVersion, MQTTProtocolVersion +from paho.mqtt.properties import Properties +from paho.mqtt.reasoncodes import ReasonCode from .. import mqtt from . import client as paho +if TYPE_CHECKING: + try: + from typing import NotRequired, Required, TypedDict # type: ignore + except ImportError: + from typing_extensions import NotRequired, Required, TypedDict -def _do_publish(client): + try: + from typing import Literal + except ImportError: + from typing_extensions import Literal # type: ignore + + + + class AuthParameter(TypedDict, total=False): + username: Required[str] + password: NotRequired[str] + + + class TLSParameter(TypedDict, total=False): + ca_certs: Required[str] + certfile: NotRequired[str] + keyfile: NotRequired[str] + tls_version: NotRequired[int] + ciphers: NotRequired[str] + insecure: NotRequired[bool] + + + class MessageDict(TypedDict, total=False): + topic: Required[str] + payload: NotRequired[paho.PayloadType] + qos: NotRequired[int] + retain: NotRequired[bool] + + MessageTuple = Tuple[str, paho.PayloadType, int, bool] + + MessagesList = List[Union[MessageDict, MessageTuple]] + + +def _do_publish(client: paho.Client): """Internal function""" message = client._userdata.popleft() @@ -44,21 +82,18 @@ def _do_publish(client): raise TypeError('message must be a dict, tuple, or list') -def _on_connect(client, userdata, flags, rc): - """Internal callback""" - #pylint: disable=invalid-name, unused-argument - - if rc == 0: +def _on_connect(client: paho.Client, userdata: MessagesList, flags, reason_code, properties): + """Internal v5 callback""" + if reason_code == 0: if len(userdata) > 0: _do_publish(client) else: - raise mqtt.MQTTException(paho.connack_string(rc)) + raise mqtt.MQTTException(paho.connack_string(reason_code)) -def _on_connect_v5(client, userdata, flags, rc, properties): - """Internal v5 callback""" - _on_connect(client, userdata, flags, rc) -def _on_publish(client, userdata, mid): +def _on_publish( + client: paho.Client, userdata: collections.deque[MessagesList], mid: int, reason_codes: ReasonCode, properties: Properties, +) -> None: """Internal callback""" #pylint: disable=unused-argument @@ -68,16 +103,26 @@ def _on_publish(client, userdata, mid): _do_publish(client) -def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, - will=None, auth=None, tls=None, protocol=paho.MQTTv311, - transport="tcp", proxy_args=None): +def multiple( + msgs: MessagesList, + hostname: str = "localhost", + port: int = 1883, + client_id: str = "", + keepalive: int = 60, + will: MessageDict | None = None, + auth: AuthParameter | None = None, + tls: TLSParameter | None = None, + protocol: MQTTProtocolVersion = paho.MQTTv311, + transport: Literal["tcp", "websockets"] = "tcp", + proxy_args: Any | None = None, +) -> None: """Publish multiple messages to a broker, then disconnect cleanly. This function creates an MQTT client, connects to a broker and publishes a list of messages. Once the messages have been delivered, it disconnects cleanly from the broker. - msgs : a list of messages to publish. Each message is either a dict or a + :param msgs: a list of messages to publish. Each message is either a dict or a tuple. If a dict, only the topic must be present. Default values will be @@ -94,30 +139,30 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, If a tuple, then it must be of the form: ("", "", qos, retain) - hostname : a string containing the address of the broker to connect to. + :param str hostname: the address of the broker to connect to. Defaults to localhost. - port : the port to connect to the broker on. Defaults to 1883. + :param int port: the port to connect to the broker on. Defaults to 1883. - client_id : the MQTT client id to use. If "" or None, the Paho library will + :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. - keepalive : the keepalive timeout value for the client. Defaults to 60 + :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. - will : a dict containing will parameters for the client: will = {'topic': + :param will: a dict containing will parameters for the client: will = {'topic': "", 'payload':", 'qos':, 'retain':}. Topic is required, all other parameters are optional and will default to None, 0 and False respectively. Defaults to None, which indicates no will should be used. - auth : a dict containing authentication parameters for the client: + :param auth: a dict containing authentication parameters for the client: auth = {'username':"", 'password':""} Username is required, password is optional and will default to None if not provided. Defaults to None, which indicates no authentication is to be used. - tls : a dict containing TLS configuration parameters for the client: + :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} @@ -128,23 +173,28 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. - transport : set to "tcp" to use the default setting of transport which is + :param str transport: set to "tcp" to use the default setting of transport which is raw TCP. Set to "websockets" to use WebSockets as the transport. - proxy_args: a dictionary that will be given to the client. + + :param proxy_args: a dictionary that will be given to the client. """ if not isinstance(msgs, Iterable): raise TypeError('msgs must be an iterable') - - - client = paho.Client(client_id=client_id, userdata=collections.deque(msgs), - protocol=protocol, transport=transport) - + if len(msgs) == 0: + raise ValueError('msgs is empty') + + client = paho.Client( + CallbackAPIVersion.VERSION2, + client_id=client_id, + userdata=collections.deque(msgs), + protocol=protocol, + transport=transport, + ) + + client.enable_logger() client.on_publish = _on_publish - if protocol == mqtt.client.MQTTv5: - client.on_connect = _on_connect_v5 - else: - client.on_connect = _on_connect + client.on_connect = _on_connect # type: ignore if proxy_args is not None: client.proxy_set(**proxy_args) @@ -164,7 +214,8 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, if tls is not None: if isinstance(tls, dict): insecure = tls.pop('insecure', False) - client.tls_set(**tls) + # mypy don't get that tls no longer contains the key insecure + client.tls_set(**tls) # type: ignore[misc] if insecure: # Must be set *after* the `client.tls_set()` call since it sets # up the SSL context that `client.tls_insecure_set` alters. @@ -177,49 +228,62 @@ def multiple(msgs, hostname="localhost", port=1883, client_id="", keepalive=60, client.loop_forever() -def single(topic, payload=None, qos=0, retain=False, hostname="localhost", - port=1883, client_id="", keepalive=60, will=None, auth=None, - tls=None, protocol=paho.MQTTv311, transport="tcp", proxy_args=None): +def single( + topic: str, + payload: paho.PayloadType = None, + qos: int = 0, + retain: bool = False, + hostname: str = "localhost", + port: int = 1883, + client_id: str = "", + keepalive: int = 60, + will: MessageDict | None = None, + auth: AuthParameter | None = None, + tls: TLSParameter | None = None, + protocol: MQTTProtocolVersion = paho.MQTTv311, + transport: Literal["tcp", "websockets"] = "tcp", + proxy_args: Any | None = None, +) -> None: """Publish a single message to a broker, then disconnect cleanly. This function creates an MQTT client, connects to a broker and publishes a single message. Once the message has been delivered, it disconnects cleanly from the broker. - topic : the only required argument must be the topic string to which the + :param str topic: the only required argument must be the topic string to which the payload will be published. - payload : the payload to be published. If "" or None, a zero length payload + :param payload: the payload to be published. If "" or None, a zero length payload will be published. - qos : the qos to use when publishing, default to 0. + :param int qos: the qos to use when publishing, default to 0. - retain : set the message to be retained (True) or not (False). + :param bool retain: set the message to be retained (True) or not (False). - hostname : a string containing the address of the broker to connect to. + :param str hostname: the address of the broker to connect to. Defaults to localhost. - port : the port to connect to the broker on. Defaults to 1883. + :param int port: the port to connect to the broker on. Defaults to 1883. - client_id : the MQTT client id to use. If "" or None, the Paho library will + :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. - keepalive : the keepalive timeout value for the client. Defaults to 60 + :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. - will : a dict containing will parameters for the client: will = {'topic': + :param will: a dict containing will parameters for the client: will = {'topic': "", 'payload':", 'qos':, 'retain':}. Topic is required, all other parameters are optional and will default to None, 0 and False respectively. Defaults to None, which indicates no will should be used. - auth : a dict containing authentication parameters for the client: - auth = {'username':"", 'password':""} + :param auth: a dict containing authentication parameters for the client: Username is required, password is optional and will default to None + auth = {'username':"", 'password':""} if not provided. Defaults to None, which indicates no authentication is to be used. - tls : a dict containing TLS configuration parameters for the client: + :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} @@ -230,12 +294,13 @@ def single(topic, payload=None, qos=0, retain=False, hostname="localhost", Alternatively, tls input can be an SSLContext object, which will be processed using the tls_set_context method. - transport : set to "tcp" to use the default setting of transport which is + :param transport: set to "tcp" to use the default setting of transport which is raw TCP. Set to "websockets" to use WebSockets as the transport. - proxy_args: a dictionary that will be given to the client. + + :param proxy_args: a dictionary that will be given to the client. """ - msg = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain} + msg: MessageDict = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain} multiple([msg], hostname, port, client_id, keepalive, will, auth, tls, protocol, transport, proxy_args) diff --git a/src/paho/mqtt/py.typed b/src/paho/mqtt/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/src/paho/mqtt/reasoncodes.py b/src/paho/mqtt/reasoncodes.py index c42e5ba9..243ac96f 100644 --- a/src/paho/mqtt/reasoncodes.py +++ b/src/paho/mqtt/reasoncodes.py @@ -1,35 +1,36 @@ -""" -******************************************************************* - Copyright (c) 2017, 2019 IBM Corp. - - All rights reserved. This program and the accompanying materials - are made available under the terms of the Eclipse Public License v2.0 - and Eclipse Distribution License v1.0 which accompany this distribution. - - The Eclipse Public License is available at - http://www.eclipse.org/legal/epl-v10.html - and the Eclipse Distribution License is available at - http://www.eclipse.org/org/documents/edl-v10.php. - - Contributors: - Ian Craggs - initial implementation and/or documentation -******************************************************************* -""" - -import sys +# ******************************************************************* +# Copyright (c) 2017, 2019 IBM Corp. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v2.0 +# and Eclipse Distribution License v1.0 which accompany this distribution. +# +# The Eclipse Public License is available at +# http://www.eclipse.org/legal/epl-v20.html +# and the Eclipse Distribution License is available at +# http://www.eclipse.org/org/documents/edl-v10.php. +# +# Contributors: +# Ian Craggs - initial implementation and/or documentation +# ******************************************************************* + +import functools +import warnings +from typing import Any from .packettypes import PacketTypes -class ReasonCodes: +@functools.total_ordering +class ReasonCode: """MQTT version 5.0 reason codes class. - See ReasonCodes.names for a list of possible numeric values along with their + See ReasonCode.names for a list of possible numeric values along with their names and the packets to which they apply. """ - def __init__(self, packetType, aName="Success", identifier=-1): + def __init__(self, packetType: int, aName: str ="Success", identifier: int =-1): """ packetType: the type of the packet, such as PacketTypes.CONNECT that this reason code will be used with. Some reason codes have different @@ -135,10 +136,12 @@ def __getName__(self, packetType, identifier): Used when displaying the reason code. """ - assert identifier in self.names.keys(), identifier + if identifier not in self.names: + raise KeyError(identifier) names = self.names[identifier] namelist = [name for name in names.keys() if packetType in names[name]] - assert len(namelist) == 1 + if len(namelist) != 1: + raise ValueError(f"Expected exactly one name, found {namelist!r}") return namelist[0] def getId(self, name): @@ -148,22 +151,17 @@ def getId(self, name): Used when setting the reason code for a packetType check that only valid codes for the packet are set. """ - identifier = None for code in self.names.keys(): if name in self.names[code].keys(): if self.packetType in self.names[code][name]: - identifier = code - break - assert identifier is not None, name - return identifier + return code + raise KeyError(f"Reason code name not found: {name}") def set(self, name): self.value = self.getId(name) def unpack(self, buffer): c = buffer[0] - if sys.version_info[0] < 3: - c = ord(c) name = self.__getName__(self.packetType, c) self.value = self.getId(name) return 1 @@ -177,11 +175,26 @@ def __eq__(self, other): if isinstance(other, int): return self.value == other if isinstance(other, str): - return self.value == str(self) - if isinstance(other, ReasonCodes): + return other == str(self) + if isinstance(other, ReasonCode): return self.value == other.value return False + def __lt__(self, other): + if isinstance(other, int): + return self.value < other + if isinstance(other, ReasonCode): + return self.value < other.value + return NotImplemented + + def __repr__(self): + try: + packet_name = PacketTypes.Names[self.packetType] + except IndexError: + packet_name = "Unknown" + + return f"ReasonCode({packet_name}, {self.getName()!r})" + def __str__(self): return self.getName() @@ -190,3 +203,21 @@ def json(self): def pack(self): return bytearray([self.value]) + + @property + def is_failure(self) -> bool: + return self.value >= 0x80 + + +class _CompatibilityIsInstance(type): + def __instancecheck__(self, other: Any) -> bool: + return isinstance(other, ReasonCode) + + +class ReasonCodes(ReasonCode, metaclass=_CompatibilityIsInstance): + def __init__(self, *args, **kwargs): + warnings.warn("ReasonCodes is deprecated, use ReasonCode (singular) instead", + category=DeprecationWarning, + stacklevel=2, + ) + super().__init__(*args, **kwargs) diff --git a/src/paho/mqtt/subscribe.py b/src/paho/mqtt/subscribe.py index 643df9c1..b6c80f44 100644 --- a/src/paho/mqtt/subscribe.py +++ b/src/paho/mqtt/subscribe.py @@ -5,7 +5,7 @@ # and Eclipse Distribution License v1.0 which accompany this distribution. # # The Eclipse Public License is available at -# http://www.eclipse.org/legal/epl-v10.html +# http://www.eclipse.org/legal/epl-v20.html # and the Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # @@ -18,16 +18,15 @@ returns one or messages matching a set of topics, and callback() which allows you to pass a callback for processing of messages. """ -from __future__ import absolute_import from .. import mqtt from . import client as paho -def _on_connect_v5(client, userdata, flags, rc, properties): +def _on_connect(client, userdata, flags, reason_code, properties): """Internal callback""" - if rc != 0: - raise mqtt.MQTTException(paho.connack_string(rc)) + if reason_code != 0: + raise mqtt.MQTTException(paho.connack_string(reason_code)) if isinstance(userdata['topics'], list): for topic in userdata['topics']: @@ -35,10 +34,6 @@ def _on_connect_v5(client, userdata, flags, rc, properties): else: client.subscribe(userdata['topics'], userdata['qos']) -def _on_connect(client, userdata, flags, rc): - """Internal v5 callback""" - _on_connect_v5(client, userdata, flags, rc, None) - def _on_message_callback(client, userdata, message): """Internal callback""" @@ -77,40 +72,41 @@ def callback(callback, topics, qos=0, userdata=None, hostname="localhost", to a list of topics. Incoming messages are processed by the user provided callback. This is a blocking function and will never return. - callback : function of the form "on_message(client, userdata, message)" for + :param callback: function with the same signature as `on_message` for processing the messages received. - topics : either a string containing a single topic to subscribe to, or a + :param topics: either a string containing a single topic to subscribe to, or a list of topics to subscribe to. - qos : the qos to use when subscribing. This is applied to all topics. + :param int qos: the qos to use when subscribing. This is applied to all topics. - userdata : passed to the callback + :param userdata: passed to the callback - hostname : a string containing the address of the broker to connect to. + :param str hostname: the address of the broker to connect to. Defaults to localhost. - port : the port to connect to the broker on. Defaults to 1883. + :param int port: the port to connect to the broker on. Defaults to 1883. - client_id : the MQTT client id to use. If "" or None, the Paho library will + :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. - keepalive : the keepalive timeout value for the client. Defaults to 60 + :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. - will : a dict containing will parameters for the client: will = {'topic': + :param will: a dict containing will parameters for the client: will = {'topic': "", 'payload':", 'qos':, 'retain':}. Topic is required, all other parameters are optional and will default to None, 0 and False respectively. + Defaults to None, which indicates no will should be used. - auth : a dict containing authentication parameters for the client: + :param auth: a dict containing authentication parameters for the client: auth = {'username':"", 'password':""} Username is required, password is optional and will default to None if not provided. Defaults to None, which indicates no authentication is to be used. - tls : a dict containing TLS configuration parameters for the client: + :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} @@ -121,17 +117,17 @@ def callback(callback, topics, qos=0, userdata=None, hostname="localhost", processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. - transport : set to "tcp" to use the default setting of transport which is + :param str transport: set to "tcp" to use the default setting of transport which is raw TCP. Set to "websockets" to use WebSockets as the transport. - clean_session : a boolean that determines the client type. If True, + :param clean_session: a boolean that determines the client type. If True, the broker will remove all information about this client when it disconnects. If False, the client is a persistent client and subscription information and queued messages will be retained when the client disconnects. Defaults to True. - proxy_args: a dictionary that will be given to the client. + :param proxy_args: a dictionary that will be given to the client. """ if qos < 0 or qos > 2: @@ -143,14 +139,18 @@ def callback(callback, topics, qos=0, userdata=None, hostname="localhost", 'qos':qos, 'userdata':userdata} - client = paho.Client(client_id=client_id, userdata=callback_userdata, - protocol=protocol, transport=transport, - clean_session=clean_session) + client = paho.Client( + paho.CallbackAPIVersion.VERSION2, + client_id=client_id, + userdata=callback_userdata, + protocol=protocol, + transport=transport, + clean_session=clean_session, + ) + client.enable_logger() + client.on_message = _on_message_callback - if protocol == mqtt.client.MQTTv5: - client.on_connect = _on_connect_v5 - else: - client.on_connect = _on_connect + client.on_connect = _on_connect if proxy_args is not None: client.proxy_set(**proxy_args) @@ -193,45 +193,45 @@ def simple(topics, qos=0, msg_count=1, retained=True, hostname="localhost", to a list of topics. Once "msg_count" messages have been received, it disconnects cleanly from the broker and returns the messages. - topics : either a string containing a single topic to subscribe to, or a + :param topics: either a string containing a single topic to subscribe to, or a list of topics to subscribe to. - qos : the qos to use when subscribing. This is applied to all topics. + :param int qos: the qos to use when subscribing. This is applied to all topics. - msg_count : the number of messages to retrieve from the broker. + :param int msg_count: the number of messages to retrieve from the broker. if msg_count == 1 then a single MQTTMessage will be returned. if msg_count > 1 then a list of MQTTMessages will be returned. - retained : If set to True, retained messages will be processed the same as + :param bool retained: If set to True, retained messages will be processed the same as non-retained messages. If set to False, retained messages will be ignored. This means that with retained=False and msg_count=1, the function will return the first message received that does not have the retained flag set. - hostname : a string containing the address of the broker to connect to. + :param str hostname: the address of the broker to connect to. Defaults to localhost. - port : the port to connect to the broker on. Defaults to 1883. + :param int port: the port to connect to the broker on. Defaults to 1883. - client_id : the MQTT client id to use. If "" or None, the Paho library will + :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. - keepalive : the keepalive timeout value for the client. Defaults to 60 + :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. - will : a dict containing will parameters for the client: will = {'topic': + :param will: a dict containing will parameters for the client: will = {'topic': "", 'payload':", 'qos':, 'retain':}. Topic is required, all other parameters are optional and will default to None, 0 and False respectively. Defaults to None, which indicates no will should be used. - auth : a dict containing authentication parameters for the client: + :param auth: a dict containing authentication parameters for the client: auth = {'username':"", 'password':""} Username is required, password is optional and will default to None if not provided. Defaults to None, which indicates no authentication is to be used. - tls : a dict containing TLS configuration parameters for the client: + :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} @@ -242,17 +242,20 @@ def simple(topics, qos=0, msg_count=1, retained=True, hostname="localhost", processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. - transport : set to "tcp" to use the default setting of transport which is + :param protocol: the MQTT protocol version to use. Defaults to MQTTv311. + + :param transport: set to "tcp" to use the default setting of transport which is raw TCP. Set to "websockets" to use WebSockets as the transport. - clean_session : a boolean that determines the client type. If True, + :param clean_session: a boolean that determines the client type. If True, the broker will remove all information about this client when it disconnects. If False, the client is a persistent client and subscription information and queued messages will be retained when the client disconnects. - Defaults to True. + Defaults to True. If protocol is MQTTv50, clean_session + is ignored. - proxy_args: a dictionary that will be given to the client. + :param proxy_args: a dictionary that will be given to the client. """ if msg_count < 1: @@ -265,6 +268,10 @@ def simple(topics, qos=0, msg_count=1, retained=True, hostname="localhost", else: messages = [] + # Ignore clean_session if protocol is MQTTv50, otherwise Client will raise + if protocol == paho.MQTTv5: + clean_session = None + userdata = {'retained':retained, 'msg_count':msg_count, 'messages':messages} callback(_on_message_simple, topics, qos, userdata, hostname, port, diff --git a/src/paho/mqtt/subscribeoptions.py b/src/paho/mqtt/subscribeoptions.py index 5b4f0733..7e0605de 100644 --- a/src/paho/mqtt/subscribeoptions.py +++ b/src/paho/mqtt/subscribeoptions.py @@ -7,7 +7,7 @@ and Eclipse Distribution License v1.0 which accompany this distribution. The Eclipse Public License is available at - http://www.eclipse.org/legal/epl-v10.html + http://www.eclipse.org/legal/epl-v20.html and the Eclipse Distribution License is available at http://www.eclipse.org/org/documents/edl-v10.php. @@ -16,14 +16,13 @@ ******************************************************************* """ -import sys class MQTTException(Exception): pass -class SubscribeOptions(object): +class SubscribeOptions: """The MQTT v5.0 subscribe options class. The options are: @@ -42,7 +41,13 @@ class SubscribeOptions(object): RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range( 0, 3) - def __init__(self, qos=0, noLocal=False, retainAsPublished=False, retainHandling=RETAIN_SEND_ON_SUBSCRIBE): + def __init__( + self, + qos: int = 0, + noLocal: bool = False, + retainAsPublished: bool = False, + retainHandling: int = RETAIN_SEND_ON_SUBSCRIBE, + ): """ qos: 0, 1 or 2. 0 is the default. noLocal: True or False. False is the default and corresponds to MQTT v3.1.1 behavior. @@ -56,29 +61,27 @@ def __init__(self, qos=0, noLocal=False, retainAsPublished=False, retainHandling self.noLocal = noLocal # bit 2 self.retainAsPublished = retainAsPublished # bit 3 self.retainHandling = retainHandling # bits 4 and 5: 0, 1 or 2 - assert self.QoS in [0, 1, 2] - assert self.retainHandling in [ - 0, 1, 2], "Retain handling should be 0, 1 or 2" + if self.retainHandling not in (0, 1, 2): + raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}") + if self.QoS not in (0, 1, 2): + raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}") def __setattr__(self, name, value): if name not in self.names: raise MQTTException( - name + " Attribute name must be one of "+str(self.names)) + f"{name} Attribute name must be one of {self.names}") object.__setattr__(self, name, value) def pack(self): - assert self.QoS in [0, 1, 2] - assert self.retainHandling in [ - 0, 1, 2], "Retain handling should be 0, 1 or 2" + if self.retainHandling not in (0, 1, 2): + raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}") + if self.QoS not in (0, 1, 2): + raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}") noLocal = 1 if self.noLocal else 0 retainAsPublished = 1 if self.retainAsPublished else 0 data = [(self.retainHandling << 4) | (retainAsPublished << 3) | (noLocal << 2) | self.QoS] - if sys.version_info[0] >= 3: - buffer = bytes(data) - else: - buffer = bytearray(data) - return buffer + return bytes(data) def unpack(self, buffer): b0 = buffer[0] @@ -86,10 +89,10 @@ def unpack(self, buffer): self.retainAsPublished = True if ((b0 >> 3) & 0x01) == 1 else False self.noLocal = True if ((b0 >> 2) & 0x01) == 1 else False self.QoS = (b0 & 0x03) - assert self.retainHandling in [ - 0, 1, 2], "Retain handling should be 0, 1 or 2, not %d" % self.retainHandling - assert self.QoS in [ - 0, 1, 2], "QoS should be 0, 1 or 2, not %d" % self.QoS + if self.retainHandling not in (0, 1, 2): + raise AssertionError(f"Retain handling should be 0, 1 or 2, not {self.retainHandling}") + if self.QoS not in (0, 1, 2): + raise AssertionError(f"QoS should be 0, 1 or 2, not {self.QoS}") return 1 def __repr__(self): diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 49f9ec9b..00000000 --- a/test/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -.PHONY: all test clean - -all : - -test : - $(MAKE) -C lib test - -clean : - $(MAKE) -C lib clean diff --git a/test/lib/01-no-clean-session.py b/test/lib/01-no-clean-session.py deleted file mode 100755 index b2312335..00000000 --- a/test/lib/01-no-clean-session.py +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with clean session not set. - -# The client should connect to port 1888 with keepalive=60, clean session not -# set, and client id 01-no-clean-session. - -import context -import paho_test - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("01-no-clean-session", clean_session=False, keepalive=keepalive) - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/test/lib/01-reconnect-on-failure.py b/test/lib/01-reconnect-on-failure.py deleted file mode 100755 index d1fb9a72..00000000 --- a/test/lib/01-reconnect-on-failure.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python3 - -# Test the reconnect_on_failure = False mode - -import context -import paho_test - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("01-reconnect-on-failure", keepalive=keepalive) -connack_packet_ok = paho_test.gen_connack(rc=0) -connack_packet_failure = paho_test.gen_connack(rc=1) # CONNACK_REFUSED_PROTOCOL_VERSION - -publish_packet = paho_test.gen_publish( - u"reconnect/test", qos=0, payload="message") - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - conn.send(connack_packet_ok) - - # Connection is a success, so we expect a publish - paho_test.expect_packet(conn, "publish", publish_packet) - conn.close() - # Expect the client to quit here due to socket being closed - client.wait(1) - if client.returncode == 42: - # Repeat the test, but with a bad connack code - client = context.start_client() - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - conn.send(connack_packet_failure) - # Expect the client to quit here due to socket being closed - client.wait(1) - if client.returncode == 42: - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/test/lib/01-unpwd-empty-password-set.py b/test/lib/01-unpwd-empty-password-set.py deleted file mode 100755 index 856a92e1..00000000 --- a/test/lib/01-unpwd-empty-password-set.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with a username and password. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# client id 01-unpwd-set, username set to uname and password set to empty string - -import context -import paho_test - -rc = 1 -keepalive = 60 -username = "uname" -password = "" -connect_packet = paho_test.gen_connect( - "01-unpwd-set", keepalive=keepalive, username=username, password=password) - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/01-unpwd-empty-set.py b/test/lib/01-unpwd-empty-set.py deleted file mode 100755 index 25523958..00000000 --- a/test/lib/01-unpwd-empty-set.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with a username and password. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# client id 01-unpwd-set, username and password set to empty string. - -import context -import paho_test - -rc = 1 -keepalive = 60 -username = "" -password = "" -connect_packet = paho_test.gen_connect( - "01-unpwd-set", keepalive=keepalive, username=username, password='') - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/01-unpwd-set.py b/test/lib/01-unpwd-set.py deleted file mode 100755 index 46e09074..00000000 --- a/test/lib/01-unpwd-set.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with a username and password. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# client id 01-unpwd-set, username set to uname and password set to ;'[08gn=# - -import context -import paho_test - -rc = 1 -keepalive = 60 -username = "uname" -password = ";'[08gn=#" -connect_packet = paho_test.gen_connect( - "01-unpwd-set", keepalive=keepalive, username=username, password=password) - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/01-unpwd-unicode-set.py b/test/lib/01-unpwd-unicode-set.py deleted file mode 100755 index 487b29eb..00000000 --- a/test/lib/01-unpwd-unicode-set.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with a unicode username and password. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# client id 01-unpwd-unicode-set, username and password from corresponding variables - -from __future__ import unicode_literals - -import context -import paho_test - -rc = 1 -keepalive = 60 -username = "\u00fas\u00e9rn\u00e1m\u00e9-h\u00e9ll\u00f3" -password = "h\u00e9ll\u00f3" -connect_packet = paho_test.gen_connect( - "01-unpwd-unicode-set", keepalive=keepalive, username=username, password=password) - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/01-will-unpwd-set.py b/test/lib/01-will-unpwd-set.py deleted file mode 100755 index 67af1cbb..00000000 --- a/test/lib/01-will-unpwd-set.py +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -# Test whether a client produces a correct connect with a will, username and password. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# client id 01-will-unpwd-set , will topic set to "will-topic", will payload -# set to "will message", will qos=2, will retain not set, username set to -# "oibvvwqw" and password set to "#'^2hg9a&nm38*us". - -import context -import paho_test - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("01-will-unpwd-set", - keepalive=keepalive, username="oibvvwqw", password="#'^2hg9a&nm38*us", - will_topic="will-topic", will_qos=2, will_payload="will message") - -sock = paho_test.create_server_socket() - -client = context.start_client() - -try: - (conn, address) = sock.accept() - conn.settimeout(10) - - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/test/lib/08-ssl-bad-cacert.py b/test/lib/08-ssl-bad-cacert.py deleted file mode 100755 index 19e100cd..00000000 --- a/test/lib/08-ssl-bad-cacert.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python3 - -import context - -context.check_ssl() - -rc = 1 - -client = context.start_client() - -client.wait() - -rc = client.returncode - -exit(rc) diff --git a/test/lib/08-ssl-fake-cacert.py b/test/lib/08-ssl-fake-cacert.py deleted file mode 100755 index fb466049..00000000 --- a/test/lib/08-ssl-fake-cacert.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python3 - -import time - -import context -import paho_test -from paho_test import ssl - -context.check_ssl() - -ssock = paho_test.create_server_socket_ssl(cert_reqs=ssl.CERT_REQUIRED) - -client = context.start_client() - -try: - (conn, address) = ssock.accept() - - conn.close() -except ssl.SSLError: - # Expected error due to ca certs not matching. - pass -finally: - time.sleep(1.0) - client.terminate() - client.wait() - ssock.close() - -if client.returncode == 0: - exit(0) -else: - exit(1) diff --git a/test/lib/Makefile b/test/lib/Makefile deleted file mode 100644 index 1e41b367..00000000 --- a/test/lib/Makefile +++ /dev/null @@ -1,37 +0,0 @@ -.PHONY: all test -.NOTPARALLEL: - -PYTHON?=python3 - -all : - -test : - $(PYTHON) ./01-asyncio.py python/01-asyncio.test - $(PYTHON) ./01-decorators.py python/01-decorators.test - $(PYTHON) ./01-keepalive-pingreq.py python/01-keepalive-pingreq.test - $(PYTHON) ./01-no-clean-session.py python/01-no-clean-session.test - $(PYTHON) ./01-reconnect-on-failure.py python/01-reconnect-on-failure.test - $(PYTHON) ./01-unpwd-empty-password-set.py python/01-unpwd-empty-password-set.test - $(PYTHON) ./01-unpwd-empty-set.py python/01-unpwd-empty-set.test - $(PYTHON) ./01-unpwd-set.py python/01-unpwd-set.test - $(PYTHON) ./01-unpwd-unicode-set.py python/01-unpwd-unicode-set.test - $(PYTHON) ./01-will-set.py python/01-will-set.test - $(PYTHON) ./01-will-unpwd-set.py python/01-will-unpwd-set.test - $(PYTHON) ./01-zero-length-clientid.py python/01-zero-length-clientid.test - $(PYTHON) ./02-subscribe-qos0.py python/02-subscribe-qos0.test - $(PYTHON) ./02-subscribe-qos1.py python/02-subscribe-qos1.test - $(PYTHON) ./02-subscribe-qos2.py python/02-subscribe-qos2.test - $(PYTHON) ./02-unsubscribe.py python/02-unsubscribe.test - $(PYTHON) ./03-publish-b2c-qos1.py python/03-publish-b2c-qos1.test - $(PYTHON) ./03-publish-c2b-qos1-disconnect.py python/03-publish-c2b-qos1-disconnect.test - $(PYTHON) ./03-publish-c2b-qos2-disconnect.py python/03-publish-c2b-qos2-disconnect.test - $(PYTHON) ./03-publish-helper-qos0.py python/03-publish-helper-qos0.test - $(PYTHON) ./03-publish-helper-qos0-v5.py python/03-publish-helper-qos0-v5.test - $(PYTHON) ./03-publish-helper-qos1-disconnect.py python/03-publish-helper-qos1-disconnect.test - $(PYTHON) ./03-publish-qos0-no-payload.py python/03-publish-qos0-no-payload.test - $(PYTHON) ./03-publish-qos0.py python/03-publish-qos0.test - $(PYTHON) ./04-retain-qos0.py python/04-retain-qos0.test - $(PYTHON) ./08-ssl-bad-cacert.py python/08-ssl-bad-cacert.test - $(PYTHON) ./08-ssl-connect-cert-auth-pw.py python/08-ssl-connect-cert-auth-pw.test - $(PYTHON) ./08-ssl-connect-cert-auth.py python/08-ssl-connect-cert-auth.test - $(PYTHON) ./08-ssl-connect-no-auth.py python/08-ssl-connect-no-auth.test diff --git a/test/lib/context.py b/test/lib/context.py deleted file mode 100644 index 8cfc07cf..00000000 --- a/test/lib/context.py +++ /dev/null @@ -1,54 +0,0 @@ -# -*- coding: utf-8 -*- - -import os -import subprocess -import sys - -try: - import ssl -except ImportError: - ssl = None - -# Ensure can import paho_test package -try: - import paho_test - -except ImportError: - # This part is only required when paho_test module is not on Python path - # From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder - import inspect - - cmd_subfolder = os.path.realpath( - os.path.abspath( - os.path.join( - os.path.split( - inspect.getfile(inspect.currentframe()) - )[0], - "..", - ) - ) - ) - if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) - - import paho_test - -env = dict(os.environ) -pp = env.get('PYTHONPATH', '') -env['PYTHONPATH'] = '../../src' + os.pathsep + pp - - -def start_client(): - args = [sys.executable, ] + sys.argv[1:] - client = subprocess.Popen(args, env=env) - return client - - -def check_ssl(): - if ssl is None: - print("WARNING: SSL not available in current environment") - exit(0) - - if not hasattr(ssl, 'SSLContext'): - print("WARNING: SSL without SSLContext is not supported") - exit(0) diff --git a/test/lib/python/01-asyncio.test b/test/lib/python/01-asyncio.test deleted file mode 100755 index 43368a73..00000000 --- a/test/lib/python/01-asyncio.test +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env python3 - -import asyncio -import socket -import uuid - -import context # Ensures paho is in PYTHONPATH - -import paho.mqtt.client as mqtt - -client_id = 'asyncio-test' - -class AsyncioHelper: - def __init__(self, loop, client): - self.loop = loop - self.client = client - self.client.on_socket_open = self.on_socket_open - self.client.on_socket_close = self.on_socket_close - self.client.on_socket_register_write = self.on_socket_register_write - self.client.on_socket_unregister_write = self.on_socket_unregister_write - - def on_socket_open(self, client, userdata, sock): - def cb(): - client.loop_read() - - self.loop.add_reader(sock, cb) - self.misc = self.loop.create_task(self.misc_loop()) - - def on_socket_close(self, client, userdata, sock): - self.loop.remove_reader(sock) - self.misc.cancel() - - def on_socket_register_write(self, client, userdata, sock): - def cb(): - client.loop_write() - - self.loop.add_writer(sock, cb) - - def on_socket_unregister_write(self, client, userdata, sock): - self.loop.remove_writer(sock) - - async def misc_loop(self): - while self.client.loop_misc() == mqtt.MQTT_ERR_SUCCESS: - try: - await asyncio.sleep(1) - except asyncio.CancelledError: - break - - -class AsyncMqttExample: - def __init__(self, loop): - self.loop = loop - self.payload = "" - self.complete = False - - def on_connect(self, client, obj, flags, rc): - client.subscribe("sub-test", 1) - - def on_subscribe(self, client, obj, mid, granted_qos): - client.unsubscribe("unsub-test") - - def on_unsubscribe(self, client, obj, mid): - self.payload = "message" - - def on_message(self, client, obj, msg): - client.publish("asyncio", qos=1, payload=self.payload) - - def on_publish(self, client, obj, mid): - client.disconnect() - - def on_disconnect(self, client, userdata, rc): - self.disconnected.set_result(rc) - - async def main(self): - global rc - self.disconnected = self.loop.create_future() - - self.client = mqtt.Client(client_id=client_id) - self.client.on_connect = self.on_connect - self.client.on_message = self.on_message - self.client.on_publish = self.on_publish - self.client.on_subscribe = self.on_subscribe - self.client.on_unsubscribe = self.on_unsubscribe - self.client.on_disconnect = self.on_disconnect - - aioh = AsyncioHelper(self.loop, self.client) - - self.client.connect('localhost', 1888, 60) - self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) - - await self.disconnected - rc = 0 - -rc = 1 -loop = asyncio.get_event_loop() -loop.run_until_complete(AsyncMqttExample(loop).main()) -loop.close() -exit(rc) diff --git a/test/lib/python/01-keepalive-pingreq.test b/test/lib/python/01-keepalive-pingreq.test deleted file mode 100755 index 632866ae..00000000 --- a/test/lib/python/01-keepalive-pingreq.test +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - -run = -1 -mqttc = mqtt.Client("01-keepalive-pingreq") -mqttc.on_connect = on_connect - -mqttc.connect("localhost", 1888, 4) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-no-clean-session.test b/test/lib/python/01-no-clean-session.test deleted file mode 100755 index 8f05024c..00000000 --- a/test/lib/python/01-no-clean-session.test +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-no-clean-session", False) - -run = -1 -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-reconnect-on-failure.test b/test/lib/python/01-reconnect-on-failure.test deleted file mode 100755 index 695249fb..00000000 --- a/test/lib/python/01-reconnect-on-failure.test +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - mqttc.publish("reconnect/test", "message") - -mqttc = mqtt.Client("01-reconnect-on-failure", reconnect_on_failure=False) -mqttc.on_connect = on_connect - -mqttc.connect("localhost", 1888) -mqttc.loop_forever() - -exit(42) diff --git a/test/lib/python/01-unpwd-empty-password-set.test b/test/lib/python/01-unpwd-empty-password-set.test deleted file mode 100755 index f3ac7f55..00000000 --- a/test/lib/python/01-unpwd-empty-password-set.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-unpwd-set") - -run = -1 -mqttc.username_pw_set("uname", "") -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-unpwd-empty-set.test b/test/lib/python/01-unpwd-empty-set.test deleted file mode 100755 index 01638626..00000000 --- a/test/lib/python/01-unpwd-empty-set.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-unpwd-set") - -run = -1 -mqttc.username_pw_set("", "") -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-unpwd-set.test b/test/lib/python/01-unpwd-set.test deleted file mode 100755 index d74b0348..00000000 --- a/test/lib/python/01-unpwd-set.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-unpwd-set") - -run = -1 -mqttc.username_pw_set("uname", ";'[08gn=#") -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-unpwd-unicode-set.test b/test/lib/python/01-unpwd-unicode-set.test deleted file mode 100755 index fc733d24..00000000 --- a/test/lib/python/01-unpwd-unicode-set.test +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env python3 - -from __future__ import unicode_literals - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-unpwd-unicode-set") - -run = -1 -username = "\u00fas\u00e9rn\u00e1m\u00e9-h\u00e9ll\u00f3" -password = "h\u00e9ll\u00f3" -mqttc.username_pw_set(username, password) -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-will-set.test b/test/lib/python/01-will-set.test deleted file mode 100755 index e1d4c43b..00000000 --- a/test/lib/python/01-will-set.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-will-set") - -run = -1 -mqttc.will_set("topic/on/unexpected/disconnect", "will message", 1, True) -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-will-unpwd-set.test b/test/lib/python/01-will-unpwd-set.test deleted file mode 100755 index ad1199f0..00000000 --- a/test/lib/python/01-will-unpwd-set.test +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python3 - -import paho.mqtt.client as mqtt - -mqttc = mqtt.Client("01-will-unpwd-set") - -run = -1 -mqttc.username_pw_set("oibvvwqw", "#'^2hg9a&nm38*us") -mqttc.will_set("will-topic", "will message", 2, False) -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/01-zero-length-clientid.test b/test/lib/python/01-zero-length-clientid.test deleted file mode 100755 index 24691ea4..00000000 --- a/test/lib/python/01-zero-length-clientid.test +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.disconnect() - -def on_disconnect(mqttc, obj, rc): - mqttc.loop() - obj = rc - - -run = -1 -mqttc = mqtt.Client("", run, protocol=mqtt.MQTTv311) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/02-subscribe-qos0.test b/test/lib/python/02-subscribe-qos0.test deleted file mode 100755 index 602cf5c3..00000000 --- a/test/lib/python/02-subscribe-qos0.test +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.subscribe("qos0/test", 0) - -def on_disconnect(mqttc, obj, rc): - obj = rc - -def on_subscribe(mqttc, obj, mid, granted_qos): - mqttc.disconnect() - -run = -1 -mqttc = mqtt.Client("subscribe-qos0-test", run) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_subscribe = on_subscribe - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/02-subscribe-qos1.test b/test/lib/python/02-subscribe-qos1.test deleted file mode 100755 index 90667758..00000000 --- a/test/lib/python/02-subscribe-qos1.test +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.subscribe("qos1/test", 1) - -def on_disconnect(mqttc, obj, rc): - obj = rc - -def on_subscribe(mqttc, obj, mid, granted_qos): - mqttc.disconnect() - -run = -1 -mqttc = mqtt.Client("subscribe-qos1-test", run) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_subscribe = on_subscribe - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/02-subscribe-qos2.test b/test/lib/python/02-subscribe-qos2.test deleted file mode 100755 index d756ac7e..00000000 --- a/test/lib/python/02-subscribe-qos2.test +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.subscribe("qos2/test", 2) - -def on_disconnect(mqttc, obj, rc): - obj = rc - -def on_subscribe(mqttc, obj, mid, granted_qos): - mqttc.disconnect() - -run = -1 -mqttc = mqtt.Client("subscribe-qos2-test", run) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_subscribe = on_subscribe - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/02-unsubscribe.test b/test/lib/python/02-unsubscribe.test deleted file mode 100755 index 5ca6f77c..00000000 --- a/test/lib/python/02-unsubscribe.test +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.unsubscribe("unsubscribe/test") - -def on_disconnect(mqttc, obj, rc): - obj = rc - -def on_unsubscribe(mqttc, obj, mid): - mqttc.disconnect() - -run = -1 -mqttc = mqtt.Client("unsubscribe-test", run) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_unsubscribe = on_unsubscribe - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/03-publish-b2c-qos1.test b/test/lib/python/03-publish-b2c-qos1.test deleted file mode 100755 index d127d385..00000000 --- a/test/lib/python/03-publish-b2c-qos1.test +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info[0] < 3: - expected_payload = "message" -else: - expected_payload = b"message" - - -def on_message(mqttc, obj, msg): - if msg.mid != 123: - print("Invalid mid: ("+str(msg.mid)+")") - exit(1) - if msg.topic != "pub/qos1/receive": - print("Invalid topic: ("+str(msg.topic)+")") - exit(1) - if msg.payload != expected_payload: - print("Invalid payload: ("+str(msg.payload)+")") - exit(1) - if msg.qos != 1: - print("Invalid qos: ("+str(msg.qos)+")") - exit(1) - if msg.retain != False: - print("Invalid retain: ("+str(msg.retain)+")") - exit(1) - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - print("Connect failed ("+str(rc)+")") - exit(rc) - -mqttc = mqtt.Client("publish-qos1-test") -mqttc.on_connect = on_connect -mqttc.on_message = on_message - -mqttc.connect("localhost", 1888) -rc = 0 -while rc == 0: - rc = mqttc.loop() -exit(0) diff --git a/test/lib/python/03-publish-b2c-qos2.test b/test/lib/python/03-publish-b2c-qos2.test deleted file mode 100755 index 7593c514..00000000 --- a/test/lib/python/03-publish-b2c-qos2.test +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info[0] < 3: - expected_payload = "message" -else: - expected_payload = b"message" - - -def on_message(mqttc, obj, msg): - global run - if msg.mid != 13423: - print("Invalid mid ("+str(msg.mid)+")") - exit(1) - if msg.topic != "pub/qos2/receive": - print("Invalid topic ("+str(msg.topic)+")") - exit(1) - if msg.payload != expected_payload: - print("Invalid payload ("+str(msg.payload)+")") - exit(1) - if msg.qos != 2: - print("Invalid qos ("+str(msg.qos)+")") - exit(1) - if msg.retain != False: - print("Invalid retain ("+str(msg.retain)+")") - exit(1) - - run = 0 - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - -run = -1 -mqttc = mqtt.Client("publish-qos2-test", run) -mqttc.on_connect = on_connect -mqttc.on_message = on_message - -mqttc.connect("localhost", 1888) -rc = 0 -while run == -1 and rc == 0: - rc = mqttc.loop(0.3) - -exit(run) diff --git a/test/lib/python/03-publish-c2b-qos1-disconnect.test b/test/lib/python/03-publish-c2b-qos1-disconnect.test deleted file mode 100755 index 89af39f5..00000000 --- a/test/lib/python/03-publish-c2b-qos1-disconnect.test +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -sent_mid = -1 - -def on_connect(mqttc, obj, flags, rc): - global sent_mid - if rc != 0: - exit(rc) - else: - if sent_mid == -1: - res = mqttc.publish("pub/qos1/test", "message", 1) - sent_mid = res[1] - -def on_disconnect(mqttc, obj, rc): - if rc == mqtt.MQTT_ERR_SUCCESS: - run = 0 - else: - mqttc.reconnect() - -def on_publish(mqttc, obj, mid): - global sent_mid - if mid == sent_mid: - mqttc.disconnect() - else: - exit(1) - -mqttc = mqtt.Client("publish-qos1-test", clean_session=False) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_publish = on_publish - -mqttc.connect("localhost", 1888) -rc = 0 -while True: - rc = mqttc.loop() diff --git a/test/lib/python/03-publish-c2b-qos2-disconnect.test b/test/lib/python/03-publish-c2b-qos2-disconnect.test deleted file mode 100755 index 69aee789..00000000 --- a/test/lib/python/03-publish-c2b-qos2-disconnect.test +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -first_connection = 1 - -def on_connect(mqttc, obj, flags, rc): - global first_connection - if rc != 0: - exit(rc) - else: - if first_connection == 1: - mqttc.publish("pub/qos2/test", "message", 2) - first_connection = 0 - -def on_disconnect(mqttc, obj, rc): - if rc == 0: - run = 0 - else: - mqttc.reconnect() - -def on_publish(mqttc, obj, mid): - mqttc.disconnect() - -mqttc = mqtt.Client("publish-qos2-test", clean_session=False) -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect -mqttc.on_publish = on_publish - -mqttc.connect("localhost", 1888) -rc = 0 -while True: - rc = mqttc.loop() diff --git a/test/lib/python/03-publish-helper-qos0-v5.test b/test/lib/python/03-publish-helper-qos0-v5.test deleted file mode 100755 index 50a8db5c..00000000 --- a/test/lib/python/03-publish-helper-qos0-v5.test +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python3 - - -import paho.mqtt.client -import paho.mqtt.publish - -paho.mqtt.publish.single( - "pub/qos0/test", - "message", - qos=0, - hostname="localhost", - port=1888, - client_id="publish-helper-qos0-test", - protocol=paho.mqtt.client.MQTTv5 -) diff --git a/test/lib/python/03-publish-helper-qos0.test b/test/lib/python/03-publish-helper-qos0.test deleted file mode 100755 index 52b8085c..00000000 --- a/test/lib/python/03-publish-helper-qos0.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - - -import paho.mqtt.publish - -paho.mqtt.publish.single( - "pub/qos0/test", - "message", - qos=0, - hostname="localhost", - port=1888, - client_id="publish-helper-qos0-test", -) diff --git a/test/lib/python/03-publish-helper-qos1-disconnect.test b/test/lib/python/03-publish-helper-qos1-disconnect.test deleted file mode 100755 index a989b640..00000000 --- a/test/lib/python/03-publish-helper-qos1-disconnect.test +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 - - -import paho.mqtt.publish - -paho.mqtt.publish.single( - "pub/qos1/test", - "message", - qos=1, - hostname="localhost", - port=1888, - client_id="publish-helper-qos1-disconnect-test", -) diff --git a/test/lib/python/03-publish-qos0-no-payload.test b/test/lib/python/03-publish-qos0-no-payload.test deleted file mode 100755 index 19ebbd8d..00000000 --- a/test/lib/python/03-publish-qos0-no-payload.test +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -sent_mid = -1 - -def on_connect(mqttc, obj, flags, rc): - global sent_mid - if rc != 0: - exit(rc) - else: - (res, sent_mid) = mqttc.publish("pub/qos0/no-payload/test") - -def on_publish(mqttc, obj, mid): - global sent_mid, run - if sent_mid == mid: - mqttc.disconnect() - run = 0 - -run = -1 -mqttc = mqtt.Client("publish-qos0-test-np", run) -mqttc.on_connect = on_connect -mqttc.on_publish = on_publish - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/03-publish-qos0.test b/test/lib/python/03-publish-qos0.test deleted file mode 100755 index 68016749..00000000 --- a/test/lib/python/03-publish-qos0.test +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -sent_mid = -1 - -def on_connect(mqttc, obj, flags, rc): - global sent_mid - if rc != 0: - exit(rc) - else: - res = mqttc.publish("pub/qos0/test", "message") - sent_mid = res[1] - -def on_publish(mqttc, obj, mid): - global sent_mid, run - if sent_mid == mid: - mqttc.disconnect() - -run = -1 -mqttc = mqtt.Client("publish-qos0-test", run) -mqttc.on_connect = on_connect -mqttc.on_publish = on_publish - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/04-retain-qos0.test b/test/lib/python/04-retain-qos0.test deleted file mode 100755 index 2d51dad9..00000000 --- a/test/lib/python/04-retain-qos0.test +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.publish("retain/qos0/test", "retained message", 0, True) - -run = -1 -mqttc = mqtt.Client("retain-qos0-test", run) -mqttc.on_connect = on_connect - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/08-ssl-bad-cacert.test b/test/lib/python/08-ssl-bad-cacert.test deleted file mode 100755 index ed0712c8..00000000 --- a/test/lib/python/08-ssl-bad-cacert.test +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info < (2, 7, 9): - print("WARNING: SSL/TLS not supported on Python 2.6") - exit(0) - -rc = 1 -mqttc = mqtt.Client("08-ssl-bad-cacert") -try: - mqttc.tls_set("this/file/doesnt/exist") -except IOError as err: - rc = 0 - -exit(rc) diff --git a/test/lib/python/08-ssl-connect-cert-auth-pw.test b/test/lib/python/08-ssl-connect-cert-auth-pw.test deleted file mode 100755 index 8011590a..00000000 --- a/test/lib/python/08-ssl-connect-cert-auth-pw.test +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info < (2, 7, 9): - print("WARNING: SSL/TLS not supported on Python 2.6") - exit(0) - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.disconnect() - -def on_disconnect(mqttc, obj, rc): - obj = rc - - -run = -1 -mqttc = mqtt.Client("08-ssl-connect-crt-auth-pw", run) -mqttc.tls_set("../ssl/all-ca.crt", "../ssl/client-pw.crt", "../ssl/client-pw.key", keyfile_password="password") -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/08-ssl-connect-cert-auth.test b/test/lib/python/08-ssl-connect-cert-auth.test deleted file mode 100755 index f12e014e..00000000 --- a/test/lib/python/08-ssl-connect-cert-auth.test +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info < (2, 7, 9): - print("WARNING: SSL/TLS not supported on Python 2.6") - exit(0) - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.disconnect() - -def on_disconnect(mqttc, obj, rc): - obj = rc - - -run = -1 -mqttc = mqtt.Client("08-ssl-connect-crt-auth", run) -mqttc.tls_set("../ssl/all-ca.crt", "../ssl/client.crt", "../ssl/client.key") -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/08-ssl-connect-no-auth.test b/test/lib/python/08-ssl-connect-no-auth.test deleted file mode 100755 index 6b90533d..00000000 --- a/test/lib/python/08-ssl-connect-no-auth.test +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info < (2, 7, 9): - print("WARNING: SSL/TLS not supported on Python 2.6") - exit(0) - - -def on_connect(mqttc, obj, flags, rc): - if rc != 0: - exit(rc) - else: - mqttc.disconnect() - -def on_disconnect(mqttc, obj, rc): - obj = rc - - -run = -1 -mqttc = mqtt.Client("08-ssl-connect-no-auth", run) -mqttc.tls_set("../ssl/all-ca.crt") -mqttc.on_connect = on_connect -mqttc.on_disconnect = on_disconnect - -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() - -exit(run) diff --git a/test/lib/python/08-ssl-fake-cacert.test b/test/lib/python/08-ssl-fake-cacert.test deleted file mode 100755 index 178461c3..00000000 --- a/test/lib/python/08-ssl-fake-cacert.test +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python3 - -import os -import socket -import ssl -import subprocess -import sys -import time -from struct import * - -import paho.mqtt.client as mqtt - -if sys.version_info < (2, 7, 9): - print("WARNING: SSL/TLS not supported on Python 2.6") - exit(0) - - -def on_connect(mqttc, obj, flags, rc): - exit(1) - -mqttc = mqtt.Client("08-ssl-fake-cacert") -mqttc.tls_set("../ssl/test-fake-root-ca.crt", "../ssl/client.crt", "../ssl/client.key") -mqttc.on_connect = on_connect - -try: - mqttc.connect("localhost", 1888) -except ssl.SSLError as msg: - if msg.errno == 1 and "certificate verify failed" in msg.strerror: - exit(0) - else: - exit(1) -else: - exit(1) diff --git a/test/mqtt5_opts.py b/test/mqtt5_opts.py deleted file mode 100644 index 55675a73..00000000 --- a/test/mqtt5_opts.py +++ /dev/null @@ -1,5 +0,0 @@ -MQTT_SUB_OPT_NO_LOCAL = 0x04 -MQTT_SUB_OPT_RETAIN_AS_PUBLISHED = 0x08 -MQTT_SUB_OPT_SEND_RETAIN_ALWAYS = 0x00 -MQTT_SUB_OPT_SEND_RETAIN_NEW = 0x10 -MQTT_SUB_OPT_SEND_RETAIN_NEVER = 0x20 diff --git a/test/mqtt5_rc.py b/test/mqtt5_rc.py deleted file mode 100644 index 3987e720..00000000 --- a/test/mqtt5_rc.py +++ /dev/null @@ -1,46 +0,0 @@ -MQTT_RC_SUCCESS = 0 -MQTT_RC_NORMAL_DISCONNECTION = 0 -MQTT_RC_GRANTED_QOS0 = 0 -MQTT_RC_GRANTED_QOS1 = 1 -MQTT_RC_GRANTED_QOS2 = 2 -MQTT_RC_DISCONNECT_WITH_WILL_MSG = 4 -MQTT_RC_NO_MATCHING_SUBSCRIBERS = 16 -MQTT_RC_NO_SUBSCRIPTION_EXISTED = 17 -MQTT_RC_CONTINUE_AUTHENTICATION = 24 -MQTT_RC_REAUTHENTICATE = 25 - -MQTT_RC_UNSPECIFIED = 128 -MQTT_RC_MALFORMED_PACKET = 129 -MQTT_RC_PROTOCOL_ERROR = 130 -MQTT_RC_IMPLEMENTATION_SPECIFIC = 131 -MQTT_RC_UNSUPPORTED_PROTOCOL_VERSION = 132 -MQTT_RC_CLIENTID_NOT_VALID = 133 -MQTT_RC_BAD_USERNAME_OR_PASSWORD = 134 -MQTT_RC_NOT_AUTHORIZED = 135 -MQTT_RC_SERVER_UNAVAILABLE = 136 -MQTT_RC_SERVER_BUSY = 137 -MQTT_RC_BANNED = 138 -MQTT_RC_SERVER_SHUTTING_DOWN = 139 -MQTT_RC_BAD_AUTHENTICATION_METHOD = 140 -MQTT_RC_KEEP_ALIVE_TIMEOUT = 141 -MQTT_RC_SESSION_TAKEN_OVER = 142 -MQTT_RC_TOPIC_FILTER_INVALID = 143 -MQTT_RC_TOPIC_NAME_INVALID = 144 -MQTT_RC_PACKET_ID_IN_USE = 145 -MQTT_RC_PACKET_ID_NOT_FOUND = 146 -MQTT_RC_RECEIVE_MAXIMUM_EXCEEDED = 147 -MQTT_RC_TOPIC_ALIAS_INVALID = 148 -MQTT_RC_PACKET_TOO_LARGE = 149 -MQTT_RC_MESSAGE_RATE_TOO_HIGH = 150 -MQTT_RC_QUOTA_EXCEEDED = 151 -MQTT_RC_ADMINISTRATIVE_ACTION = 152 -MQTT_RC_PAYLOAD_FORMAT_INVALID = 153 -MQTT_RC_RETAIN_NOT_SUPPORTED = 154 -MQTT_RC_QOS_NOT_SUPPORTED = 155 -MQTT_RC_USE_ANOTHER_SERVER = 156 -MQTT_RC_SERVER_MOVED = 157 -MQTT_RC_SHARED_SUBS_NOT_SUPPORTED = 158 -MQTT_RC_CONNECTION_RATE_EXCEEDED = 159 -MQTT_RC_MAXIMUM_CONNECT_TIME = 160 -MQTT_RC_SUBSCRIPTION_IDS_NOT_SUPPORTED = 161 -MQTT_RC_WILDCARD_SUBS_NOT_SUPPORTED = 162 diff --git a/test/paho_test.py b/test/paho_test.py deleted file mode 100644 index 65c753d7..00000000 --- a/test/paho_test.py +++ /dev/null @@ -1,735 +0,0 @@ -import binascii -import errno -import os -import socket -import struct -import subprocess -import sys -import time - -try: - import ssl -except ImportError: - ssl = None - -import atexit - -import __main__ -import mqtt5_props - -vg_index = 1 -vg_logfiles = [] - - -class TestError(Exception): - def __init__(self, message="Mismatched packets"): - self.message = message - -def create_server_socket(): - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - sock.settimeout(10) - sock.bind(('', 1888)) - sock.listen(5) - return sock - - -def create_server_socket_ssl(*args, **kwargs): - if ssl is None: - raise RuntimeError - - ssl_version = ssl.PROTOCOL_TLSv1 - if hasattr(ssl, "PROTOCOL_TLS"): - ssl_version = ssl.PROTOCOL_TLS - - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - ssock = ssl.wrap_socket( - sock, ca_certs="../ssl/all-ca.crt", - keyfile="../ssl/server.key", certfile="../ssl/server.crt", - server_side=True, ssl_version=ssl_version, **kwargs) - ssock.settimeout(10) - ssock.bind(('', 1888)) - ssock.listen(5) - return ssock - - -def expect_packet(sock, name, expected): - if len(expected) > 0: - rlen = len(expected) - else: - rlen = 1 - - packet_recvd = b"" - try: - while len(packet_recvd) < rlen: - data = sock.recv(rlen-len(packet_recvd)) - if len(data) == 0: - break - packet_recvd += data - except socket.timeout: - pass - - if packet_matches(name, packet_recvd, expected): - return True - else: - raise TestError - - -def packet_matches(name, recvd, expected): - if recvd != expected: - print("FAIL: Received incorrect " + name + ".") - try: - print("Received: " + to_string(recvd)) - except struct.error: - print("Received (not decoded): 0x" + - binascii.b2a_hex(recvd).decode('utf8')) - try: - print("Expected: " + to_string(expected)) - except struct.error: - print("Expected (not decoded): 0x" + - binascii.b2a_hex(expected).decode('utf8')) - - return False - else: - return True - - -def receive_unordered(sock, recv1_packet, recv2_packet, error_string): - expected1 = recv1_packet + recv2_packet - expected2 = recv2_packet + recv1_packet - recvd = b'' - while len(recvd) < len(expected1): - r = sock.recv(1) - if len(r) == 0: - raise ValueError(error_string) - recvd += r - - if recvd == expected1 or recvd == expected2: - return - else: - packet_matches(error_string, recvd, expected2) - raise ValueError(error_string) - - -def do_send_receive(sock, send_packet, receive_packet, error_string="send receive error"): - size = len(send_packet) - total_sent = 0 - while total_sent < size: - sent = sock.send(send_packet[total_sent:]) - if sent == 0: - raise RuntimeError("socket connection broken") - total_sent += sent - - if expect_packet(sock, error_string, receive_packet): - return sock - else: - sock.close() - raise ValueError - - -# Useful for mocking a client receiving (with ack) a qos1 publish -def do_receive_send(sock, receive_packet, send_packet, error_string="receive send error"): - if expect_packet(sock, error_string, receive_packet): - size = len(send_packet) - total_sent = 0 - while total_sent < size: - sent = sock.send(send_packet[total_sent:]) - if sent == 0: - raise RuntimeError("socket connection broken") - total_sent += sent - return sock - else: - sock.close() - raise ValueError - - -def do_client_connect(connect_packet, connack_packet, hostname="localhost", port=1888, timeout=10, connack_error="connack"): - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(timeout) - sock.connect((hostname, port)) - - return do_send_receive(sock, connect_packet, connack_packet, connack_error) - - -def remaining_length(packet): - l = min(5, len(packet)) - all_bytes = struct.unpack("!" + "B" * l, packet[:l]) - mult = 1 - rl = 0 - for i in range(1, l - 1): - byte = all_bytes[i] - - rl += (byte & 127) * mult - mult *= 128 - if byte & 128 == 0: - packet = packet[i + 1:] - break - - return (packet, rl) - - -def to_hex_string(packet): - if len(packet) == 0: - return "" - - s = "" - while len(packet) > 0: - packet0 = struct.unpack("!B", packet[0]) - s = s+hex(packet0[0]) + " " - packet = packet[1:] - - return s - - -def to_string(packet): - if len(packet) == 0: - return "" - - packet0 = struct.unpack("!B%ds" % (len(packet)-1), bytes(packet)) - packet0 = packet0[0] - cmd = packet0 & 0xF0 - if cmd == 0x00: - # Reserved - return "0x00" - elif cmd == 0x10: - # CONNECT - (packet, rl) = remaining_length(packet) - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 'sBBH' + str(len(packet) - slen - 4) + 's' - (protocol, proto_ver, flags, keepalive, packet) = struct.unpack(pack_format, packet) - s = "CONNECT, proto=" + str(protocol) + str(proto_ver) + ", keepalive=" + str(keepalive) - if flags & 2: - s = s + ", clean-session" - else: - s = s + ", durable" - - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' - (client_id, packet) = struct.unpack(pack_format, packet) - s = s + ", id=" + str(client_id) - - if flags & 4: - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' - (will_topic, packet) = struct.unpack(pack_format, packet) - s = s + ", will-topic=" + str(will_topic) - - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' - (will_message, packet) = struct.unpack(pack_format, packet) - s = s + ", will-message=" + will_message - - s = s + ", will-qos=" + str((flags & 24) >> 3) - s = s + ", will-retain=" + str((flags & 32) >> 5) - - if flags & 128: - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' - (username, packet) = struct.unpack(pack_format, packet) - s = s + ", username=" + str(username) - - if flags & 64: - pack_format = "!H" + str(len(packet) - 2) + 's' - (slen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' - (password, packet) = struct.unpack(pack_format, packet) - s = s + ", password=" + str(password) - - if flags & 1: - s = s + ", reserved=1" - - return s - elif cmd == 0x20: - # CONNACK - if len(packet) == 4: - (cmd, rl, resv, rc) = struct.unpack('!BBBB', packet) - return "CONNACK, rl="+str(rl)+", res="+str(resv)+", rc="+str(rc) - elif len(packet) == 5: - (cmd, rl, flags, reason_code, proplen) = struct.unpack('!BBBBB', packet) - return "CONNACK, rl="+str(rl)+", flags="+str(flags)+", rc="+str(reason_code)+", proplen="+str(proplen) - else: - return "CONNACK, (not decoded)" - - elif cmd == 0x30: - # PUBLISH - dup = (packet0 & 0x08) >> 3 - qos = (packet0 & 0x06) >> 1 - retain = (packet0 & 0x01) - (packet, rl) = remaining_length(packet) - pack_format = "!H" + str(len(packet) - 2) + 's' - (tlen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(tlen) + 's' + str(len(packet) - tlen) + 's' - (topic, packet) = struct.unpack(pack_format, packet) - s = "PUBLISH, rl=" + str(rl) + ", topic=" + str(topic) + ", qos=" + str(qos) + ", retain=" + str(retain) + ", dup=" + str(dup) - if qos > 0: - pack_format = "!H" + str(len(packet) - 2) + 's' - (mid, packet) = struct.unpack(pack_format, packet) - s = s + ", mid=" + str(mid) - - s = s + ", payload=" + str(packet) - return s - elif cmd == 0x40: - # PUBACK - if len(packet) == 5: - (cmd, rl, mid, reason_code) = struct.unpack('!BBHB', packet) - return "PUBACK, rl="+str(rl)+", mid="+str(mid)+", reason_code="+str(reason_code) - else: - (cmd, rl, mid) = struct.unpack('!BBH', packet) - return "PUBACK, rl="+str(rl)+", mid="+str(mid) - elif cmd == 0x50: - # PUBREC - if len(packet) == 5: - (cmd, rl, mid, reason_code) = struct.unpack('!BBHB', packet) - return "PUBREC, rl="+str(rl)+", mid="+str(mid)+", reason_code="+str(reason_code) - else: - (cmd, rl, mid) = struct.unpack('!BBH', packet) - return "PUBREC, rl="+str(rl)+", mid="+str(mid) - elif cmd == 0x60: - # PUBREL - dup = (packet0 & 0x08) >> 3 - (cmd, rl, mid) = struct.unpack('!BBH', packet) - return "PUBREL, rl=" + str(rl) + ", mid=" + str(mid) + ", dup=" + str(dup) - elif cmd == 0x70: - # PUBCOMP - (cmd, rl, mid) = struct.unpack('!BBH', packet) - return "PUBCOMP, rl=" + str(rl) + ", mid=" + str(mid) - elif cmd == 0x80: - # SUBSCRIBE - (packet, rl) = remaining_length(packet) - pack_format = "!H" + str(len(packet) - 2) + 's' - (mid, packet) = struct.unpack(pack_format, packet) - s = "SUBSCRIBE, rl=" + str(rl) + ", mid=" + str(mid) - topic_index = 0 - while len(packet) > 0: - pack_format = "!H" + str(len(packet) - 2) + 's' - (tlen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(tlen) + 'sB' + str(len(packet) - tlen - 1) + 's' - (topic, qos, packet) = struct.unpack(pack_format, packet) - s = s + ", topic" + str(topic_index) + "=" + str(topic) + "," + str(qos) - return s - elif cmd == 0x90: - # SUBACK - (packet, rl) = remaining_length(packet) - pack_format = "!H" + str(len(packet) - 2) + 's' - (mid, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + "B" * len(packet) - granted_qos = struct.unpack(pack_format, packet) - - s = "SUBACK, rl=" + str(rl) + ", mid=" + str(mid) + ", granted_qos=" + str(granted_qos[0]) - for i in range(1, len(granted_qos) - 1): - s = s + ", " + str(granted_qos[i]) - return s - elif cmd == 0xA0: - # UNSUBSCRIBE - (packet, rl) = remaining_length(packet) - pack_format = "!H" + str(len(packet) - 2) + 's' - (mid, packet) = struct.unpack(pack_format, packet) - s = "UNSUBSCRIBE, rl=" + str(rl) + ", mid=" + str(mid) - topic_index = 0 - while len(packet) > 0: - pack_format = "!H" + str(len(packet) - 2) + 's' - (tlen, packet) = struct.unpack(pack_format, packet) - pack_format = "!" + str(tlen) + 's' + str(len(packet) - tlen) + 's' - (topic, packet) = struct.unpack(pack_format, packet) - s = s + ", topic" + str(topic_index) + "=" + str(topic) - return s - elif cmd == 0xB0: - # UNSUBACK - (cmd, rl, mid) = struct.unpack('!BBH', packet) - return "UNSUBACK, rl=" + str(rl) + ", mid=" + str(mid) - elif cmd == 0xC0: - # PINGREQ - (cmd, rl) = struct.unpack('!BB', packet) - return "PINGREQ, rl=" + str(rl) - elif cmd == 0xD0: - # PINGRESP - (cmd, rl) = struct.unpack('!BB', packet) - return "PINGRESP, rl=" + str(rl) - elif cmd == 0xE0: - # DISCONNECT - if len(packet) == 3: - (cmd, rl, reason_code) = struct.unpack('!BBB', packet) - return "DISCONNECT, rl="+str(rl)+", reason_code="+str(reason_code) - else: - (cmd, rl) = struct.unpack('!BB', packet) - return "DISCONNECT, rl="+str(rl) - elif cmd == 0xF0: - # AUTH - (cmd, rl) = struct.unpack('!BB', packet) - return "AUTH, rl="+str(rl) - - -def read_varint(sock, rl): - varint = 0 - multiplier = 1 - while True: - byte = sock.recv(1) - byte, = struct.unpack("!B", byte) - varint += (byte & 127)*multiplier - multiplier *= 128 - rl -= 1 - if byte & 128 == 0x00: - return (varint, rl) - - -def mqtt_read_string(sock, rl): - slen = sock.recv(2) - slen, = struct.unpack("!H", slen) - payload = sock.recv(slen) - payload, = struct.unpack("!%ds" % (slen), payload) - rl -= (2 + slen) - return (payload, rl) - - -def read_publish(sock, proto_ver=4): - cmd, = struct.unpack("!B", sock.recv(1)) - if cmd & 0xF0 != 0x30: - raise ValueError - - qos = (cmd & 0x06) >> 1 - rl, t = read_varint(sock, 0) - topic, rl = mqtt_read_string(sock, rl) - - if qos > 0: - sock.recv(2) - rl -= 1 - - if proto_ver == 5: - proplen, rl = read_varint(sock, rl) - sock.recv(proplen) - rl -= proplen - - payload = sock.recv(rl).decode('utf-8') - return payload - - -def gen_connect(client_id, clean_session=True, keepalive=60, username=None, password=None, will_topic=None, will_qos=0, will_retain=False, will_payload=b"", proto_ver=4, connect_reserved=False, properties=b"", will_properties=b"", session_expiry=-1): - if (proto_ver&0x7F) == 3 or proto_ver == 0: - remaining_length = 12 - elif (proto_ver&0x7F) == 4 or proto_ver == 5: - remaining_length = 10 - else: - raise ValueError - - if client_id is not None: - client_id = client_id.encode("utf-8") - remaining_length = remaining_length + 2+len(client_id) - else: - remaining_length = remaining_length + 2 - - connect_flags = 0 - - if connect_reserved: - connect_flags = connect_flags | 0x01 - - if clean_session: - connect_flags = connect_flags | 0x02 - - if proto_ver == 5: - if properties == b"": - properties += mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_RECEIVE_MAXIMUM, 20) - - if session_expiry != -1: - properties += mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, session_expiry) - - properties = mqtt5_props.prop_finalise(properties) - remaining_length += len(properties) - - if will_topic is not None: - will_topic = will_topic.encode('utf-8') - remaining_length = remaining_length + 2 + len(will_topic) + 2 + len(will_payload) - connect_flags = connect_flags | 0x04 | ((will_qos & 0x03) << 3) - if will_retain: - connect_flags = connect_flags | 32 - if proto_ver == 5: - will_properties = mqtt5_props.prop_finalise(will_properties) - remaining_length += len(will_properties) - - if username is not None: - username = username.encode('utf-8') - remaining_length = remaining_length + 2 + len(username) - connect_flags = connect_flags | 0x80 - if password is not None: - password = password.encode('utf-8') - connect_flags = connect_flags | 0x40 - remaining_length = remaining_length + 2 + len(password) - - rl = pack_remaining_length(remaining_length) - packet = struct.pack("!B" + str(len(rl)) + "s", 0x10, rl) - if (proto_ver&0x7F) == 3 or proto_ver == 0: - packet = packet + struct.pack("!H6sBBH", len(b"MQIsdp"), b"MQIsdp", proto_ver, connect_flags, keepalive) - elif (proto_ver&0x7F) == 4 or proto_ver == 5: - packet = packet + struct.pack("!H4sBBH", len(b"MQTT"), b"MQTT", proto_ver, connect_flags, keepalive) - - if proto_ver == 5: - packet += properties - - if client_id is not None: - packet = packet + struct.pack("!H" + str(len(client_id)) + "s", len(client_id), bytes(client_id)) - else: - packet = packet + struct.pack("!H", 0) - - if will_topic is not None: - packet += will_properties - packet = packet + struct.pack("!H" + str(len(will_topic)) + "s", len(will_topic), will_topic) - if len(will_payload) > 0: - packet = packet + struct.pack("!H" + str(len(will_payload)) + "s", len(will_payload), will_payload.encode('utf8')) - else: - packet = packet + struct.pack("!H", 0) - - if username is not None: - packet = packet + struct.pack("!H" + str(len(username)) + "s", len(username), username) - if password is not None: - packet = packet + struct.pack("!H" + str(len(password)) + "s", len(password), password) - return packet - -def gen_connack(flags=0, rc=0, proto_ver=4, properties=b"", property_helper=True): - if proto_ver == 5: - if property_helper == True: - if properties is not None: - properties = mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_TOPIC_ALIAS_MAXIMUM, 10) \ - + properties + mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_RECEIVE_MAXIMUM, 20) - else: - properties = b"" - properties = mqtt5_props.prop_finalise(properties) - - packet = struct.pack('!BBBB', 32, 2+len(properties), flags, rc) + properties - else: - packet = struct.pack('!BBBB', 32, 2, flags, rc); - - return packet - -def gen_publish(topic, qos, payload=None, retain=False, dup=False, mid=0, proto_ver=4, properties=b""): - if isinstance(topic, str): - topic = topic.encode("utf-8") - rl = 2+len(topic) - pack_format = "H"+str(len(topic))+"s" - if qos > 0: - rl = rl + 2 - pack_format = pack_format + "H" - - if proto_ver == 5: - properties = mqtt5_props.prop_finalise(properties) - rl += len(properties) - # This will break if len(properties) > 127 - pack_format = pack_format + "%ds"%(len(properties)) - - if payload is not None: - payload = payload.encode("utf-8") - rl = rl + len(payload) - pack_format = pack_format + str(len(payload)) + "s" - else: - payload = b"" - pack_format = pack_format + "0s" - - rlpacked = pack_remaining_length(rl) - cmd = 48 | (qos << 1) - if retain: - cmd = cmd + 1 - if dup: - cmd = cmd + 8 - - if proto_ver == 5: - if qos > 0: - return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, mid, properties, payload) - else: - return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, properties, payload) - else: - if qos > 0: - return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, mid, payload) - else: - return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, payload) - -def _gen_command_with_mid(cmd, mid, proto_ver=4, reason_code=-1, properties=None): - if proto_ver == 5 and (reason_code != -1 or properties is not None): - if reason_code == -1: - reason_code = 0 - - if properties is None: - return struct.pack('!BBHB', cmd, 3, mid, reason_code) - elif properties == "": - return struct.pack('!BBHBB', cmd, 4, mid, reason_code, 0) - else: - properties = mqtt5_props.prop_finalise(properties) - pack_format = "!BBHB"+str(len(properties))+"s" - return struct.pack(pack_format, cmd, 2+1+len(properties), mid, reason_code, properties) - else: - return struct.pack('!BBH', cmd, 2, mid) - -def gen_puback(mid, proto_ver=4, reason_code=-1, properties=None): - return _gen_command_with_mid(64, mid, proto_ver, reason_code, properties) - -def gen_pubrec(mid, proto_ver=4, reason_code=-1, properties=None): - return _gen_command_with_mid(80, mid, proto_ver, reason_code, properties) - -def gen_pubrel(mid, dup=False, proto_ver=4, reason_code=-1, properties=None): - if dup: - cmd = 96+8+2 - else: - cmd = 96+2 - return _gen_command_with_mid(cmd, mid, proto_ver, reason_code, properties) - -def gen_pubcomp(mid, proto_ver=4, reason_code=-1, properties=None): - return _gen_command_with_mid(112, mid, proto_ver, reason_code, properties) - - -def gen_subscribe(mid, topic, qos, cmd=130, proto_ver=4, properties=b""): - topic = topic.encode("utf-8") - packet = struct.pack("!B", cmd) - if proto_ver == 5: - if properties == b"": - packet += pack_remaining_length(2+1+2+len(topic)+1) - pack_format = "!HBH"+str(len(topic))+"sB" - return packet + struct.pack(pack_format, mid, 0, len(topic), topic, qos) - else: - properties = mqtt5_props.prop_finalise(properties) - packet += pack_remaining_length(2+1+2+len(topic)+len(properties)) - pack_format = "!H"+str(len(properties))+"s"+"H"+str(len(topic))+"sB" - return packet + struct.pack(pack_format, mid, properties, len(topic), topic, qos) - else: - packet += pack_remaining_length(2+2+len(topic)+1) - pack_format = "!HH"+str(len(topic))+"sB" - return packet + struct.pack(pack_format, mid, len(topic), topic, qos) - - -def gen_suback(mid, qos, proto_ver=4): - if proto_ver == 5: - return struct.pack('!BBHBB', 144, 2+1+1, mid, 0, qos) - else: - return struct.pack('!BBHB', 144, 2+1, mid, qos) - -def gen_unsubscribe(mid, topic, cmd=162, proto_ver=4, properties=b""): - topic = topic.encode("utf-8") - if proto_ver == 5: - if properties == b"": - pack_format = "!BBHBH"+str(len(topic))+"s" - return struct.pack(pack_format, cmd, 2+2+len(topic)+1, mid, 0, len(topic), topic) - else: - properties = mqtt5_props.prop_finalise(properties) - packet = struct.pack("!B", cmd) - l = 2+2+len(topic)+1+len(properties) - packet += pack_remaining_length(l) - pack_format = "!HB"+str(len(properties))+"sH"+str(len(topic))+"s" - packet += struct.pack(pack_format, mid, len(properties), properties, len(topic), topic) - return packet - else: - pack_format = "!BBHH"+str(len(topic))+"s" - return struct.pack(pack_format, cmd, 2+2+len(topic), mid, len(topic), topic) - -def gen_unsubscribe_multiple(mid, topics, proto_ver=4): - packet = b"" - remaining_length = 0 - for t in topics: - t = t.encode("utf-8") - remaining_length += 2+len(t) - packet += struct.pack("!H"+str(len(t))+"s", len(t), t) - - if proto_ver == 5: - remaining_length += 2+1 - - return struct.pack("!BBHB", 162, remaining_length, mid, 0) + packet - else: - remaining_length += 2 - - return struct.pack("!BBH", 162, remaining_length, mid) + packet - -def gen_unsuback(mid, reason_code=0, proto_ver=4): - if proto_ver == 5: - if isinstance(reason_code, list): - reason_code_count = len(reason_code) - p = struct.pack('!BBHB', 176, 3+reason_code_count, mid, 0) - for r in reason_code: - p += struct.pack('B', r) - return p - else: - return struct.pack('!BBHBB', 176, 4, mid, 0, reason_code) - else: - return struct.pack('!BBH', 176, 2, mid) - -def gen_pingreq(): - return struct.pack('!BB', 192, 0) - -def gen_pingresp(): - return struct.pack('!BB', 208, 0) - - -def _gen_short(cmd, reason_code=-1, proto_ver=5, properties=None): - if proto_ver == 5 and (reason_code != -1 or properties is not None): - if reason_code == -1: - reason_code = 0 - - if properties is None: - return struct.pack('!BBB', cmd, 1, reason_code) - elif properties == "": - return struct.pack('!BBBB', cmd, 2, reason_code, 0) - else: - properties = mqtt5_props.prop_finalise(properties) - return struct.pack("!BBB", cmd, 1+len(properties), reason_code) + properties - else: - return struct.pack('!BB', cmd, 0) - -def gen_disconnect(reason_code=-1, proto_ver=4, properties=None): - return _gen_short(0xE0, reason_code, proto_ver, properties) - -def gen_auth(reason_code=-1, properties=None): - return _gen_short(0xF0, reason_code, 5, properties) - - -def pack_remaining_length(remaining_length): - s = b"" - while True: - byte = remaining_length % 128 - remaining_length = remaining_length // 128 - # If there are more digits to encode, set the top bit of this digit - if remaining_length > 0: - byte = byte | 0x80 - - s = s + struct.pack("!B", byte) - if remaining_length == 0: - return s - - -def get_port(count=1): - if count == 1: - if len(sys.argv) == 2: - return int(sys.argv[1]) - else: - return 1888 - else: - if len(sys.argv) == 1+count: - p = () - for i in range(0, count): - p = p + (int(sys.argv[1+i]),) - return p - else: - return tuple(range(1888, 1888+count)) - - -def get_lib_port(): - if len(sys.argv) == 3: - return int(sys.argv[2]) - else: - return 1888 - - -def do_ping(sock, error_string="pingresp"): - do_send_receive(sock, gen_pingreq(), gen_pingresp(), error_string) - - -@atexit.register -def test_cleanup(): - global vg_logfiles - - if os.environ.get('MOSQ_USE_VALGRIND') is not None: - for f in vg_logfiles: - try: - if os.stat(f).st_size == 0: - os.remove(f) - except OSError: - pass diff --git a/test/ssl/demoCA/serial b/test/ssl/demoCA/serial deleted file mode 100644 index 8a0f05e1..00000000 --- a/test/ssl/demoCA/serial +++ /dev/null @@ -1 +0,0 @@ -01 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/consts.py b/tests/consts.py new file mode 100644 index 00000000..c9e7eb42 --- /dev/null +++ b/tests/consts.py @@ -0,0 +1,5 @@ +import pathlib + +tests_path = pathlib.Path(__file__).parent +lib_path = tests_path.parent +ssl_path = tests_path / "ssl" diff --git a/tests/debug_helpers.py b/tests/debug_helpers.py new file mode 100644 index 00000000..54b96368 --- /dev/null +++ b/tests/debug_helpers.py @@ -0,0 +1,223 @@ +import binascii +import struct +from typing import Tuple + + +def dump_packet(prefix: str, data: bytes) -> None: + try: + data = to_string(data) + print(prefix, ": ", data, sep="") + except struct.error: + data = binascii.b2a_hex(data).decode('utf8') + print(prefix, " (not decoded): 0x", data, sep="") + + +def remaining_length(packet: bytes) -> Tuple[bytes, int]: + l = min(5, len(packet)) # noqa: E741 + all_bytes = struct.unpack("!" + "B" * l, packet[:l]) + mult = 1 + rl = 0 + for i in range(1, l - 1): + byte = all_bytes[i] + + rl += (byte & 127) * mult + mult *= 128 + if byte & 128 == 0: + packet = packet[i + 1:] + break + + return (packet, rl) + + +def to_hex_string(packet: bytes) -> str: + if not packet: + return "" + + s = "" + while len(packet) > 0: + packet0 = struct.unpack("!B", packet[0]) + s = s+hex(packet0[0]) + " " + packet = packet[1:] + + return s + + +def to_string(packet: bytes) -> str: + if not packet: + return "" + + packet0 = struct.unpack("!B%ds" % (len(packet)-1), bytes(packet)) + packet0 = packet0[0] + cmd = packet0 & 0xF0 + if cmd == 0x00: + # Reserved + return "0x00" + elif cmd == 0x10: + # CONNECT + (packet, rl) = remaining_length(packet) + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 'sBBH' + str(len(packet) - slen - 4) + 's' + (protocol, proto_ver, flags, keepalive, packet) = struct.unpack(pack_format, packet) + kind = ("clean-session" if flags & 2 else "durable") + s = f"CONNECT, proto={protocol}{proto_ver}, keepalive={keepalive}, {kind}" + + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' + (client_id, packet) = struct.unpack(pack_format, packet) + s = s + ", id=" + str(client_id) + + if flags & 4: + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' + (will_topic, packet) = struct.unpack(pack_format, packet) + s = s + ", will-topic=" + str(will_topic) + + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' + (will_message, packet) = struct.unpack(pack_format, packet) + s = s + ", will-message=" + will_message + + s = s + ", will-qos=" + str((flags & 24) >> 3) + s = s + ", will-retain=" + str((flags & 32) >> 5) + + if flags & 128: + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' + (username, packet) = struct.unpack(pack_format, packet) + s = s + ", username=" + str(username) + + if flags & 64: + pack_format = "!H" + str(len(packet) - 2) + 's' + (slen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(slen) + 's' + str(len(packet) - slen) + 's' + (password, packet) = struct.unpack(pack_format, packet) + s = s + ", password=" + str(password) + + if flags & 1: + s = s + ", reserved=1" + + return s + elif cmd == 0x20: + # CONNACK + if len(packet) == 4: + (cmd, rl, resv, rc) = struct.unpack('!BBBB', packet) + return "CONNACK, rl="+str(rl)+", res="+str(resv)+", rc="+str(rc) + elif len(packet) == 5: + (cmd, rl, flags, reason_code, proplen) = struct.unpack('!BBBBB', packet) + return "CONNACK, rl="+str(rl)+", flags="+str(flags)+", rc="+str(reason_code)+", proplen="+str(proplen) + else: + return "CONNACK, (not decoded)" + + elif cmd == 0x30: + # PUBLISH + dup = (packet0 & 0x08) >> 3 + qos = (packet0 & 0x06) >> 1 + retain = (packet0 & 0x01) + (packet, rl) = remaining_length(packet) + pack_format = "!H" + str(len(packet) - 2) + 's' + (tlen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(tlen) + 's' + str(len(packet) - tlen) + 's' + (topic, packet) = struct.unpack(pack_format, packet) + s = "PUBLISH, rl=" + str(rl) + ", topic=" + str(topic) + ", qos=" + str(qos) + ", retain=" + str(retain) + ", dup=" + str(dup) + if qos > 0: + pack_format = "!H" + str(len(packet) - 2) + 's' + (mid, packet) = struct.unpack(pack_format, packet) + s = s + ", mid=" + str(mid) + + s = s + ", payload=" + str(packet) + return s + elif cmd == 0x40: + # PUBACK + if len(packet) == 5: + (cmd, rl, mid, reason_code) = struct.unpack('!BBHB', packet) + return "PUBACK, rl="+str(rl)+", mid="+str(mid)+", reason_code="+str(reason_code) + else: + (cmd, rl, mid) = struct.unpack('!BBH', packet) + return "PUBACK, rl="+str(rl)+", mid="+str(mid) + elif cmd == 0x50: + # PUBREC + if len(packet) == 5: + (cmd, rl, mid, reason_code) = struct.unpack('!BBHB', packet) + return "PUBREC, rl="+str(rl)+", mid="+str(mid)+", reason_code="+str(reason_code) + else: + (cmd, rl, mid) = struct.unpack('!BBH', packet) + return "PUBREC, rl="+str(rl)+", mid="+str(mid) + elif cmd == 0x60: + # PUBREL + dup = (packet0 & 0x08) >> 3 + (cmd, rl, mid) = struct.unpack('!BBH', packet) + return "PUBREL, rl=" + str(rl) + ", mid=" + str(mid) + ", dup=" + str(dup) + elif cmd == 0x70: + # PUBCOMP + (cmd, rl, mid) = struct.unpack('!BBH', packet) + return "PUBCOMP, rl=" + str(rl) + ", mid=" + str(mid) + elif cmd == 0x80: + # SUBSCRIBE + (packet, rl) = remaining_length(packet) + pack_format = "!H" + str(len(packet) - 2) + 's' + (mid, packet) = struct.unpack(pack_format, packet) + s = "SUBSCRIBE, rl=" + str(rl) + ", mid=" + str(mid) + topic_index = 0 + while len(packet) > 0: + pack_format = "!H" + str(len(packet) - 2) + 's' + (tlen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(tlen) + 'sB' + str(len(packet) - tlen - 1) + 's' + (topic, qos, packet) = struct.unpack(pack_format, packet) + s = s + ", topic" + str(topic_index) + "=" + str(topic) + "," + str(qos) + return s + elif cmd == 0x90: + # SUBACK + (packet, rl) = remaining_length(packet) + pack_format = "!H" + str(len(packet) - 2) + 's' + (mid, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + "B" * len(packet) + granted_qos = struct.unpack(pack_format, packet) + + s = "SUBACK, rl=" + str(rl) + ", mid=" + str(mid) + ", granted_qos=" + str(granted_qos[0]) + for i in range(1, len(granted_qos) - 1): + s = s + ", " + str(granted_qos[i]) + return s + elif cmd == 0xA0: + # UNSUBSCRIBE + (packet, rl) = remaining_length(packet) + pack_format = "!H" + str(len(packet) - 2) + 's' + (mid, packet) = struct.unpack(pack_format, packet) + s = "UNSUBSCRIBE, rl=" + str(rl) + ", mid=" + str(mid) + topic_index = 0 + while len(packet) > 0: + pack_format = "!H" + str(len(packet) - 2) + 's' + (tlen, packet) = struct.unpack(pack_format, packet) + pack_format = "!" + str(tlen) + 's' + str(len(packet) - tlen) + 's' + (topic, packet) = struct.unpack(pack_format, packet) + s = s + ", topic" + str(topic_index) + "=" + str(topic) + return s + elif cmd == 0xB0: + # UNSUBACK + (cmd, rl, mid) = struct.unpack('!BBH', packet) + return "UNSUBACK, rl=" + str(rl) + ", mid=" + str(mid) + elif cmd == 0xC0: + # PINGREQ + (cmd, rl) = struct.unpack('!BB', packet) + return "PINGREQ, rl=" + str(rl) + elif cmd == 0xD0: + # PINGRESP + (cmd, rl) = struct.unpack('!BB', packet) + return "PINGRESP, rl=" + str(rl) + elif cmd == 0xE0: + # DISCONNECT + if len(packet) == 3: + (cmd, rl, reason_code) = struct.unpack('!BBB', packet) + return "DISCONNECT, rl="+str(rl)+", reason_code="+str(reason_code) + else: + (cmd, rl) = struct.unpack('!BB', packet) + return "DISCONNECT, rl="+str(rl) + elif cmd == 0xF0: + # AUTH + (cmd, rl) = struct.unpack('!BB', packet) + return "AUTH, rl="+str(rl) + raise ValueError(f"Unknown packet type {cmd}") diff --git a/tests/lib/__init__.py b/tests/lib/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/lib/clients/01-asyncio.py b/tests/lib/clients/01-asyncio.py new file mode 100644 index 00000000..a8ba9280 --- /dev/null +++ b/tests/lib/clients/01-asyncio.py @@ -0,0 +1,89 @@ +import asyncio +import socket + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port + +client_id = 'asyncio-test' + + +class AsyncioHelper: + def __init__(self, loop, client): + self.loop = loop + self.client = client + self.client.on_socket_open = self.on_socket_open + self.client.on_socket_close = self.on_socket_close + self.client.on_socket_register_write = self.on_socket_register_write + self.client.on_socket_unregister_write = self.on_socket_unregister_write + + def on_socket_open(self, client, userdata, sock): + def cb(): + client.loop_read() + + self.loop.add_reader(sock, cb) + self.misc = self.loop.create_task(self.misc_loop()) + + def on_socket_close(self, client, userdata, sock): + self.loop.remove_reader(sock) + self.misc.cancel() + + def on_socket_register_write(self, client, userdata, sock): + def cb(): + client.loop_write() + + self.loop.add_writer(sock, cb) + + def on_socket_unregister_write(self, client, userdata, sock): + self.loop.remove_writer(sock) + + async def misc_loop(self): + while self.client.loop_misc() == mqtt.MQTT_ERR_SUCCESS: + try: + await asyncio.sleep(1) + except asyncio.CancelledError: + break + + +async def main(): + loop = asyncio.get_event_loop() + payload = "" + + def on_connect(client, obj, flags, rc): + client.subscribe("sub-test", 1) + + def on_subscribe(client, obj, mid, granted_qos): + client.unsubscribe("unsub-test") + + def on_unsubscribe(client, obj, mid): + nonlocal payload + payload = "message" + + def on_message(client, obj, msg): + client.publish("asyncio", qos=1, payload=payload) + + def on_publish(client, obj, mid): + client.disconnect() + + def on_disconnect(client, userdata, rc): + disconnected.set_result(rc) + + disconnected = loop.create_future() + + client = mqtt.Client(callback_api_version=mqtt.CallbackAPIVersion.VERSION1, client_id=client_id) + client.on_connect = on_connect + client.on_message = on_message + client.on_publish = on_publish + client.on_subscribe = on_subscribe + client.on_unsubscribe = on_unsubscribe + client.on_disconnect = on_disconnect + + _aioh = AsyncioHelper(loop, client) + + client.connect('localhost', get_test_server_port(), 60) + client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) + + await disconnected + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/test/lib/python/01-decorators.test b/tests/lib/clients/01-decorators.py old mode 100755 new mode 100644 similarity index 66% rename from test/lib/python/01-decorators.test rename to tests/lib/clients/01-decorators.py index bff109bf..55e6d485 --- a/test/lib/python/01-decorators.test +++ b/tests/lib/clients/01-decorators.py @@ -1,46 +1,42 @@ -#!/usr/bin/env python3 - -import os -import socket -import subprocess -import sys -import time -from struct import * - import paho.mqtt.client as mqtt -run = -1 -mqttc = mqtt.Client("decorators-test", run) +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "decorators-test", clean_session=True) payload = b"" + @mqttc.connect_callback() def on_connect(mqttc, obj, flags, rc): mqttc.subscribe("sub-test", 1) + @mqttc.subscribe_callback() def on_subscribe(mqttc, obj, mid, granted_qos): mqttc.unsubscribe("unsub-test") + @mqttc.unsubscribe_callback() def on_unsubscribe(mqttc, obj, mid): global payload payload = "message" + @mqttc.message_callback() def on_message(mqttc, obj, msg): global payload mqttc.publish("decorators", qos=1, payload=payload) + @mqttc.publish_callback() def on_publish(mqttc, obj, mid): mqttc.disconnect() + @mqttc.disconnect_callback() def on_disconnect(mqttc, obj, rc): - obj = rc + pass # TODO: should probably test that this gets called -mqttc.connect("localhost", 1888) -while run == -1: - mqttc.loop() -exit(run) +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-keepalive-pingreq.py b/tests/lib/clients/01-keepalive-pingreq.py new file mode 100644 index 00000000..ffa6383b --- /dev/null +++ b/tests/lib/clients/01-keepalive-pingreq.py @@ -0,0 +1,14 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-keepalive-pingreq") +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port(), keepalive=4) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-no-clean-session.py b/tests/lib/clients/01-no-clean-session.py new file mode 100644 index 00000000..62966130 --- /dev/null +++ b/tests/lib/clients/01-no-clean-session.py @@ -0,0 +1,8 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-no-clean-session", clean_session=False) + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-reconnect-on-failure.py b/tests/lib/clients/01-reconnect-on-failure.py new file mode 100644 index 00000000..907a2f18 --- /dev/null +++ b/tests/lib/clients/01-reconnect-on-failure.py @@ -0,0 +1,16 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + mqttc.publish("reconnect/test", "message") + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-reconnect-on-failure", reconnect_on_failure=False) +mqttc.on_connect = on_connect + +with wait_for_keyboard_interrupt(): + mqttc.connect("localhost", get_test_server_port()) + mqttc.loop_forever() + exit(42) # this is expected by the test case diff --git a/tests/lib/clients/01-unpwd-empty-password-set.py b/tests/lib/clients/01-unpwd-empty-password-set.py new file mode 100644 index 00000000..40a1a434 --- /dev/null +++ b/tests/lib/clients/01-unpwd-empty-password-set.py @@ -0,0 +1,9 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-unpwd-set") + +mqttc.username_pw_set("uname", "") +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-unpwd-empty-set.py b/tests/lib/clients/01-unpwd-empty-set.py new file mode 100644 index 00000000..b65d6797 --- /dev/null +++ b/tests/lib/clients/01-unpwd-empty-set.py @@ -0,0 +1,9 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-unpwd-set") + +mqttc.username_pw_set("", "") +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-unpwd-set.py b/tests/lib/clients/01-unpwd-set.py new file mode 100644 index 00000000..763297f5 --- /dev/null +++ b/tests/lib/clients/01-unpwd-set.py @@ -0,0 +1,9 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-unpwd-set") + +mqttc.username_pw_set("uname", ";'[08gn=#") +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-unpwd-unicode-set.py b/tests/lib/clients/01-unpwd-unicode-set.py new file mode 100644 index 00000000..58445417 --- /dev/null +++ b/tests/lib/clients/01-unpwd-unicode-set.py @@ -0,0 +1,12 @@ + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-unpwd-unicode-set") + +username = "\u00fas\u00e9rn\u00e1m\u00e9-h\u00e9ll\u00f3" +password = "h\u00e9ll\u00f3" +mqttc.username_pw_set(username, password) +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-will-set.py b/tests/lib/clients/01-will-set.py new file mode 100644 index 00000000..c3310a49 --- /dev/null +++ b/tests/lib/clients/01-will-set.py @@ -0,0 +1,9 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-will-set") + +mqttc.will_set("topic/on/unexpected/disconnect", "will message", 1, True) +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-will-unpwd-set.py b/tests/lib/clients/01-will-unpwd-set.py new file mode 100644 index 00000000..31bb976c --- /dev/null +++ b/tests/lib/clients/01-will-unpwd-set.py @@ -0,0 +1,10 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "01-will-unpwd-set") + +mqttc.username_pw_set("oibvvwqw", "#'^2hg9a&nm38*us") +mqttc.will_set("will-topic", "will message", 2, False) +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/01-zero-length-clientid.py b/tests/lib/clients/01-zero-length-clientid.py new file mode 100644 index 00000000..992efedc --- /dev/null +++ b/tests/lib/clients/01-zero-length-clientid.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.disconnect() + + +def on_disconnect(mqttc, obj, rc): + mqttc.loop() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "", clean_session=True, protocol=mqtt.MQTTv311) +mqttc.on_connect = on_connect +mqttc.on_disconnect = on_disconnect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/02-subscribe-qos0.py b/tests/lib/clients/02-subscribe-qos0.py new file mode 100644 index 00000000..2444ffb0 --- /dev/null +++ b/tests/lib/clients/02-subscribe-qos0.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.subscribe("qos0/test", 0) + + +def on_subscribe(mqttc, obj, mid, granted_qos): + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "subscribe-qos0-test", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_subscribe = on_subscribe + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/02-subscribe-qos1.py b/tests/lib/clients/02-subscribe-qos1.py new file mode 100644 index 00000000..2079de6e --- /dev/null +++ b/tests/lib/clients/02-subscribe-qos1.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.subscribe("qos1/test", 1) + + +def on_subscribe(mqttc, obj, mid, granted_qos): + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "subscribe-qos1-test", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_subscribe = on_subscribe + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/02-subscribe-qos2.py b/tests/lib/clients/02-subscribe-qos2.py new file mode 100644 index 00000000..4776a769 --- /dev/null +++ b/tests/lib/clients/02-subscribe-qos2.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.subscribe("qos2/test", 2) + + +def on_subscribe(mqttc, obj, mid, granted_qos): + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "subscribe-qos2-test", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_subscribe = on_subscribe + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/02-unsubscribe.py b/tests/lib/clients/02-unsubscribe.py new file mode 100644 index 00000000..de36bfb6 --- /dev/null +++ b/tests/lib/clients/02-unsubscribe.py @@ -0,0 +1,20 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.unsubscribe("unsubscribe/test") + + +def on_unsubscribe(mqttc, obj, mid): + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "unsubscribe-test", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_unsubscribe = on_unsubscribe + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-b2c-qos1.py b/tests/lib/clients/03-publish-b2c-qos1.py new file mode 100644 index 00000000..71ac9508 --- /dev/null +++ b/tests/lib/clients/03-publish-b2c-qos1.py @@ -0,0 +1,25 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +expected_payload = b"message" + + +def on_message(mqttc, obj, msg): + assert msg.mid == 123, f"Invalid mid: ({msg.mid})" + assert msg.topic == "pub/qos1/receive", f"Invalid topic: ({msg.topic})" + assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})" + assert msg.qos == 1, f"Invalid qos: ({msg.qos})" + assert not msg.retain, f"Invalid retain: ({msg.retain})" + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos1-test") +mqttc.on_connect = on_connect +mqttc.on_message = on_message + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-b2c-qos2.py b/tests/lib/clients/03-publish-b2c-qos2.py new file mode 100644 index 00000000..e3b14926 --- /dev/null +++ b/tests/lib/clients/03-publish-b2c-qos2.py @@ -0,0 +1,28 @@ +import logging + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +expected_payload = b"message" + + +def on_message(mqttc, obj, msg): + assert msg.mid == 13423, f"Invalid mid: ({msg.mid})" + assert msg.topic == "pub/qos2/receive", f"Invalid topic: ({msg.topic})" + assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})" + assert msg.qos == 2, f"Invalid qos: ({msg.qos})" + assert not msg.retain, f"Invalid retain: ({msg.retain})" + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + +logging.basicConfig(level=logging.DEBUG) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos2-test", clean_session=True) +mqttc.enable_logger() +mqttc.on_connect = on_connect +mqttc.on_message = on_message + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-c2b-qos1-disconnect.py b/tests/lib/clients/03-publish-c2b-qos1-disconnect.py new file mode 100644 index 00000000..52fa5167 --- /dev/null +++ b/tests/lib/clients/03-publish-c2b-qos1-disconnect.py @@ -0,0 +1,33 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +sent_mid = -1 + + +def on_connect(mqttc, obj, flags, rc): + global sent_mid + assert rc == 0, f"Connect failed ({rc})" + if sent_mid == -1: + res = mqttc.publish("pub/qos1/test", "message", 1) + sent_mid = res[1] + + +def on_disconnect(mqttc, obj, rc): + if rc != mqtt.MQTT_ERR_SUCCESS: + mqttc.reconnect() + + +def on_publish(mqttc, obj, mid): + global sent_mid + assert mid == sent_mid, f"Invalid mid: ({mid})" + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos1-test", clean_session=False) +mqttc.on_connect = on_connect +mqttc.on_disconnect = on_disconnect +mqttc.on_publish = on_publish + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-c2b-qos2-disconnect.py b/tests/lib/clients/03-publish-c2b-qos2-disconnect.py new file mode 100644 index 00000000..fe7c2ae2 --- /dev/null +++ b/tests/lib/clients/03-publish-c2b-qos2-disconnect.py @@ -0,0 +1,31 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +first_connection = 1 + + +def on_connect(mqttc, obj, flags, rc): + global first_connection + assert rc == 0, f"Connect failed ({rc})" + if first_connection == 1: + mqttc.publish("pub/qos2/test", "message", 2) + first_connection = 0 + + +def on_disconnect(mqttc, obj, rc): + if rc != 0: + mqttc.reconnect() + + +def on_publish(mqttc, obj, mid): + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos2-test", clean_session=False) +mqttc.on_connect = on_connect +mqttc.on_disconnect = on_disconnect +mqttc.on_publish = on_publish + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-fill-inflight.py b/tests/lib/clients/03-publish-fill-inflight.py new file mode 100644 index 00000000..0a7eb857 --- /dev/null +++ b/tests/lib/clients/03-publish-fill-inflight.py @@ -0,0 +1,39 @@ +import logging + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def expected_payload(i: int) -> bytes: + return f"message{i}".encode() + + +def on_message(mqttc, obj, msg): + assert msg.mid == 123, f"Invalid mid: ({msg.mid})" + assert msg.topic == "pub/qos1/receive", f"Invalid topic: ({msg.topic})" + assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})" + assert msg.qos == 1, f"Invalid qos: ({msg.qos})" + assert msg.retain is not False, f"Invalid retain: ({msg.retain})" + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + for i in range(12): + mqttc.publish("topic", expected_payload(i), qos=1) + +def on_disconnect(mqttc, rc, properties): + logging.info("disconnected") + mqttc.reconnect() + +logging.basicConfig(level=logging.DEBUG) +logging.info(str(mqtt)) +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos1-test") +mqttc.max_inflight_messages_set(10) +mqttc.on_connect = on_connect +mqttc.on_disconnect = on_disconnect +mqttc.on_message = on_message +mqttc.enable_logger() + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-helper-qos0-v5.py b/tests/lib/clients/03-publish-helper-qos0-v5.py new file mode 100644 index 00000000..1b59113d --- /dev/null +++ b/tests/lib/clients/03-publish-helper-qos0-v5.py @@ -0,0 +1,15 @@ +import paho.mqtt.client +import paho.mqtt.publish + +from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt + +with wait_for_keyboard_interrupt(): + paho.mqtt.publish.single( + "pub/qos0/test", + "message", + qos=0, + hostname="localhost", + port=get_test_server_port(), + client_id="publish-helper-qos0-test", + protocol=paho.mqtt.client.MQTTv5, + ) diff --git a/tests/lib/clients/03-publish-helper-qos0.py b/tests/lib/clients/03-publish-helper-qos0.py new file mode 100644 index 00000000..a7b696eb --- /dev/null +++ b/tests/lib/clients/03-publish-helper-qos0.py @@ -0,0 +1,13 @@ +import paho.mqtt.publish + +from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt + +with wait_for_keyboard_interrupt(): + paho.mqtt.publish.single( + "pub/qos0/test", + "message", + qos=0, + hostname="localhost", + port=get_test_server_port(), + client_id="publish-helper-qos0-test", + ) diff --git a/tests/lib/clients/03-publish-helper-qos1-disconnect.py b/tests/lib/clients/03-publish-helper-qos1-disconnect.py new file mode 100644 index 00000000..b07166f6 --- /dev/null +++ b/tests/lib/clients/03-publish-helper-qos1-disconnect.py @@ -0,0 +1,13 @@ +import paho.mqtt.publish + +from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt + +with wait_for_keyboard_interrupt(): + paho.mqtt.publish.single( + "pub/qos1/test", + "message", + qos=1, + hostname="localhost", + port=get_test_server_port(), + client_id="publish-helper-qos1-disconnect-test", + ) diff --git a/tests/lib/clients/03-publish-qos0-no-payload.py b/tests/lib/clients/03-publish-qos0-no-payload.py new file mode 100644 index 00000000..7ac8e351 --- /dev/null +++ b/tests/lib/clients/03-publish-qos0-no-payload.py @@ -0,0 +1,24 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +sent_mid = -1 + + +def on_connect(mqttc, obj, flags, rc): + global sent_mid + assert rc == 0, f"Connect failed ({rc})" + (res, sent_mid) = mqttc.publish("pub/qos0/no-payload/test") + + +def on_publish(mqttc, obj, mid): + if sent_mid == mid: + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos0-test-np", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_publish = on_publish + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/03-publish-qos0.py b/tests/lib/clients/03-publish-qos0.py new file mode 100644 index 00000000..dc6ac745 --- /dev/null +++ b/tests/lib/clients/03-publish-qos0.py @@ -0,0 +1,26 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + +sent_mid = -1 + + +def on_connect(mqttc, obj, flags, rc): + global sent_mid + assert rc == 0, f"Connect failed ({rc})" + res = mqttc.publish("pub/qos0/test", "message") + sent_mid = res[1] + + +def on_publish(mqttc, obj, mid): + global sent_mid, run + if sent_mid == mid: + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "publish-qos0-test", clean_session=True) +mqttc.on_connect = on_connect +mqttc.on_publish = on_publish + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/04-retain-qos0.py b/tests/lib/clients/04-retain-qos0.py new file mode 100644 index 00000000..bdfa660d --- /dev/null +++ b/tests/lib/clients/04-retain-qos0.py @@ -0,0 +1,15 @@ +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.publish("retain/qos0/test", "retained message", 0, True) + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "retain-qos0-test", clean_session=True) +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/08-ssl-connect-alpn.py b/tests/lib/clients/08-ssl-connect-alpn.py new file mode 100755 index 00000000..f830e506 --- /dev/null +++ b/tests/lib/clients/08-ssl-connect-alpn.py @@ -0,0 +1,23 @@ +import os + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-connect-alpn", clean_session=True) +mqttc.tls_set( + os.path.join(os.environ["PAHO_SSL_PATH"], "all-ca.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.key"), + alpn_protocols=["paho-test-protocol"], +) +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/08-ssl-connect-cert-auth-pw.py b/tests/lib/clients/08-ssl-connect-cert-auth-pw.py new file mode 100644 index 00000000..4681dc42 --- /dev/null +++ b/tests/lib/clients/08-ssl-connect-cert-auth-pw.py @@ -0,0 +1,23 @@ +import os + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-connect-crt-auth-pw") +mqttc.tls_set( + os.path.join(os.environ["PAHO_SSL_PATH"], "all-ca.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client-pw.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client-pw.key"), + keyfile_password="password", +) +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/08-ssl-connect-cert-auth.py b/tests/lib/clients/08-ssl-connect-cert-auth.py new file mode 100644 index 00000000..a34409a3 --- /dev/null +++ b/tests/lib/clients/08-ssl-connect-cert-auth.py @@ -0,0 +1,22 @@ +import os + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-connect-crt-auth") +mqttc.tls_set( + os.path.join(os.environ["PAHO_SSL_PATH"], "all-ca.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.key"), +) +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/08-ssl-connect-no-auth.py b/tests/lib/clients/08-ssl-connect-no-auth.py new file mode 100644 index 00000000..9d86f288 --- /dev/null +++ b/tests/lib/clients/08-ssl-connect-no-auth.py @@ -0,0 +1,18 @@ +import os + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, loop_until_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + assert rc == 0, f"Connect failed ({rc})" + mqttc.disconnect() + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-connect-no-auth") +mqttc.tls_set(os.path.join(os.environ["PAHO_SSL_PATH"], "all-ca.crt")) +mqttc.on_connect = on_connect + +mqttc.connect("localhost", get_test_server_port()) +loop_until_keyboard_interrupt(mqttc) diff --git a/tests/lib/clients/08-ssl-fake-cacert.py b/tests/lib/clients/08-ssl-fake-cacert.py new file mode 100644 index 00000000..ffa53645 --- /dev/null +++ b/tests/lib/clients/08-ssl-fake-cacert.py @@ -0,0 +1,27 @@ +import os +import ssl + +import paho.mqtt.client as mqtt + +from tests.paho_test import get_test_server_port, wait_for_keyboard_interrupt + + +def on_connect(mqttc, obj, flags, rc): + raise RuntimeError("Connection should have failed!") + + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-fake-cacert") +mqttc.tls_set( + os.path.join(os.environ["PAHO_SSL_PATH"], "test-fake-root-ca.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.crt"), + os.path.join(os.environ["PAHO_SSL_PATH"], "client.key"), +) +mqttc.on_connect = on_connect + +with wait_for_keyboard_interrupt(): + try: + mqttc.connect("localhost", get_test_server_port()) + except ssl.SSLError as msg: + assert msg.errno == 1 and "certificate verify failed" in msg.strerror + else: + raise Exception("Expected SSLError") diff --git a/tests/lib/conftest.py b/tests/lib/conftest.py new file mode 100644 index 00000000..30ab6fdb --- /dev/null +++ b/tests/lib/conftest.py @@ -0,0 +1,80 @@ +import os +import signal +import subprocess +import sys + +import pytest + +from tests.consts import ssl_path, tests_path +from tests.paho_test import create_server_socket, create_server_socket_ssl, ssl + +clients_path = tests_path / "lib" / "clients" + + +def _yield_server(monkeypatch, sockport): + sock, port = sockport + monkeypatch.setenv("PAHO_SERVER_PORT", str(port)) + try: + yield sock + finally: + sock.close() + + +@pytest.fixture() +def server_socket(monkeypatch): + yield from _yield_server(monkeypatch, create_server_socket()) + + +@pytest.fixture() +def ssl_server_socket(monkeypatch): + if ssl is None: + pytest.skip("no ssl module") + yield from _yield_server(monkeypatch, create_server_socket_ssl()) + + +@pytest.fixture() +def alpn_ssl_server_socket(monkeypatch): + if ssl is None: + pytest.skip("no ssl module") + if not getattr(ssl, "HAS_ALPN", False): + pytest.skip("ALPN not supported in this version of Python") + yield from _yield_server(monkeypatch, create_server_socket_ssl(alpn_protocols=["paho-test-protocol"])) + + +def stop_process(proc: subprocess.Popen) -> None: + if sys.platform == "win32": + proc.send_signal(signal.CTRL_C_EVENT) + else: + proc.send_signal(signal.SIGINT) + try: + proc.wait(5) + except subprocess.TimeoutExpired: + proc.terminate() + + +@pytest.fixture() +def start_client(request: pytest.FixtureRequest): + def starter(name: str, expected_returncode: int = 0) -> None: + client_path = clients_path / name + if not client_path.exists(): + raise FileNotFoundError(client_path) + env = dict( + os.environ, + PAHO_SSL_PATH=str(ssl_path), + PYTHONPATH=f"{tests_path}{os.pathsep}{os.environ.get('PYTHONPATH', '')}", + ) + assert 'PAHO_SERVER_PORT' in env, "PAHO_SERVER_PORT must be set in the environment when starting a client" + proc = subprocess.Popen([ # noqa: S603 + sys.executable, + str(client_path), + ], env=env) + + def fin(): + stop_process(proc) + if proc.returncode != expected_returncode: + raise RuntimeError(f"Client {name} exited with code {proc.returncode}, expected {expected_returncode}") + + request.addfinalizer(fin) + return proc + + return starter diff --git a/test/lib/01-asyncio.py b/tests/lib/test_01_asyncio.py old mode 100755 new mode 100644 similarity index 59% rename from test/lib/01-asyncio.py rename to tests/lib/test_01_asyncio.py index f10a0b39..14967375 --- a/test/lib/01-asyncio.py +++ b/tests/lib/test_01_asyncio.py @@ -1,34 +1,29 @@ -#!/usr/bin/env python3 - # Test whether asyncio works -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("asyncio-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("asyncio-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) -subscribe_packet = paho_test.gen_subscribe(mid=1, topic=u"sub-test", qos=1) +subscribe_packet = paho_test.gen_subscribe(mid=1, topic="sub-test", qos=1) suback_packet = paho_test.gen_suback(mid=1, qos=1) -unsubscribe_packet = paho_test.gen_unsubscribe(mid=2, topic=u"unsub-test") +unsubscribe_packet = paho_test.gen_unsubscribe(mid=2, topic="unsub-test") unsuback_packet = paho_test.gen_unsuback(mid=2) -publish_packet = paho_test.gen_publish(u"b2c", qos=0, payload="msg") +publish_packet = paho_test.gen_publish("b2c", qos=0, payload="msg") -publish_packet_in = paho_test.gen_publish(u"asyncio", qos=1, mid=3, payload="message") +publish_packet_in = paho_test.gen_publish("asyncio", qos=1, mid=3, payload="message") puback_packet_in = paho_test.gen_puback(mid=3) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_01_asyncio(server_socket, start_client): + proc = start_client("01-asyncio.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -45,12 +40,6 @@ conn.send(puback_packet_in) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) + assert proc.wait() == 0 diff --git a/test/lib/01-decorators.py b/tests/lib/test_01_decorators.py old mode 100755 new mode 100644 similarity index 59% rename from test/lib/01-decorators.py rename to tests/lib/test_01_decorators.py index 0360a538..7ae66ef2 --- a/test/lib/01-decorators.py +++ b/tests/lib/test_01_decorators.py @@ -1,34 +1,29 @@ -#!/usr/bin/env python3 - # Test whether callback decorators work -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("decorators-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("decorators-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) -subscribe_packet = paho_test.gen_subscribe(mid=1, topic=u"sub-test", qos=1) +subscribe_packet = paho_test.gen_subscribe(mid=1, topic="sub-test", qos=1) suback_packet = paho_test.gen_suback(mid=1, qos=1) -unsubscribe_packet = paho_test.gen_unsubscribe(mid=2, topic=u"unsub-test") +unsubscribe_packet = paho_test.gen_unsubscribe(mid=2, topic="unsub-test") unsuback_packet = paho_test.gen_unsuback(mid=2) -publish_packet = paho_test.gen_publish(u"b2c", qos=0, payload="msg") +publish_packet = paho_test.gen_publish("b2c", qos=0, payload="msg") -publish_packet_in = paho_test.gen_publish(u"decorators", qos=1, mid=3, payload="message") +publish_packet_in = paho_test.gen_publish("decorators", qos=1, mid=3, payload="message") puback_packet_in = paho_test.gen_puback(mid=3) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_01_decorators(server_socket, start_client): + start_client("01-decorators.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -45,12 +40,5 @@ conn.send(puback_packet_in) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/01-keepalive-pingreq.py b/tests/lib/test_01_keepalive_pingreq.py old mode 100755 new mode 100644 similarity index 61% rename from test/lib/01-keepalive-pingreq.py rename to tests/lib/test_01_keepalive_pingreq.py index 0fb149c9..4c327db3 --- a/test/lib/01-keepalive-pingreq.py +++ b/tests/lib/test_01_keepalive_pingreq.py @@ -1,32 +1,26 @@ -#!/usr/bin/env python3 - # Test whether a client sends a pingreq after the keepalive time -# The client should connect to port 1888 with keepalive=4, clean session set, +# The client should connect with keepalive=4, clean session set, # and client id 01-keepalive-pingreq # The client should send a PINGREQ message after the appropriate amount of time # (4 seconds after no traffic). import time -import context -import paho_test +import tests.paho_test as paho_test -rc = 1 -keepalive = 4 -connect_packet = paho_test.gen_connect("01-keepalive-pingreq", keepalive=keepalive) +connect_packet = paho_test.gen_connect("01-keepalive-pingreq", keepalive=4) connack_packet = paho_test.gen_connack(rc=0) pingreq_packet = paho_test.gen_pingreq() pingresp_packet = paho_test.gen_pingresp() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_01_keepalive_pingreq(server_socket, start_client): + start_client("01-keepalive-pingreq.py") -try: - (conn, address) = sock.accept() - conn.settimeout(keepalive+10) + (conn, address) = server_socket.accept() + conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) @@ -36,13 +30,3 @@ conn.send(pingresp_packet) paho_test.expect_packet(conn, "pingreq", pingreq_packet) - rc = 0 - - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/tests/lib/test_01_no_clean_session.py b/tests/lib/test_01_no_clean_session.py new file mode 100644 index 00000000..7f00e544 --- /dev/null +++ b/tests/lib/test_01_no_clean_session.py @@ -0,0 +1,20 @@ +# Test whether a client produces a correct connect with clean session not set. + +# The client should connect with keepalive=60, clean session not +# set, and client id 01-no-clean-session. + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("01-no-clean-session", clean_session=False, keepalive=60) + + +def test_01_no_clean_session(server_socket, start_client): + start_client("01-no-clean-session.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/tests/lib/test_01_reconnect_on_failure.py b/tests/lib/test_01_reconnect_on_failure.py new file mode 100644 index 00000000..8deb6539 --- /dev/null +++ b/tests/lib/test_01_reconnect_on_failure.py @@ -0,0 +1,31 @@ +# Test the reconnect_on_failure = False mode +import pytest + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("01-reconnect-on-failure", keepalive=60) +connack_packet_ok = paho_test.gen_connack(rc=0) +connack_packet_failure = paho_test.gen_connack(rc=1) # CONNACK_REFUSED_PROTOCOL_VERSION + +publish_packet = paho_test.gen_publish( + "reconnect/test", qos=0, payload="message") + + +@pytest.mark.parametrize("ok_code", [False, True]) +def test_01_reconnect_on_failure(server_socket, start_client, ok_code): + client = start_client("01-reconnect-on-failure.py", expected_returncode=42) + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + if ok_code: + conn.send(connack_packet_ok) + # Connection is a success, so we expect a publish + paho_test.expect_packet(conn, "publish", publish_packet) + else: + conn.send(connack_packet_failure) + conn.close() + # Expect the client to quit here due to socket being closed + client.wait(1) + assert client.returncode == 42 diff --git a/tests/lib/test_01_unpwd_empty_password_set.py b/tests/lib/test_01_unpwd_empty_password_set.py new file mode 100644 index 00000000..225d72bc --- /dev/null +++ b/tests/lib/test_01_unpwd_empty_password_set.py @@ -0,0 +1,21 @@ +# Test whether a client produces a correct connect with a username and password. + +# The client should connect with keepalive=60, clean session set, +# client id 01-unpwd-set, username set to uname and password set to empty string + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect( + "01-unpwd-set", keepalive=60, username="uname", password="") + + +def test_01_unpwd_empty_password_set(server_socket, start_client): + start_client("01-unpwd-empty-password-set.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/tests/lib/test_01_unpwd_empty_set.py b/tests/lib/test_01_unpwd_empty_set.py new file mode 100644 index 00000000..8c51c22a --- /dev/null +++ b/tests/lib/test_01_unpwd_empty_set.py @@ -0,0 +1,21 @@ +# Test whether a client produces a correct connect with a username and password. + +# The client should connect with keepalive=60, clean session set, +# client id 01-unpwd-set, username and password set to empty string. + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect( + "01-unpwd-set", keepalive=60, username="", password='') + + +def test_01_unpwd_empty_set(server_socket, start_client): + start_client("01-unpwd-empty-set.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/tests/lib/test_01_unpwd_set.py b/tests/lib/test_01_unpwd_set.py new file mode 100644 index 00000000..38834ab9 --- /dev/null +++ b/tests/lib/test_01_unpwd_set.py @@ -0,0 +1,21 @@ +# Test whether a client produces a correct connect with a username and password. + +# The client should connect with keepalive=60, clean session set, +# client id 01-unpwd-set, username set to uname and password set to ;'[08gn=# + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect( + "01-unpwd-set", keepalive=60, username="uname", password=";'[08gn=#") + + +def test_01_unpwd_set(server_socket, start_client): + start_client("01-unpwd-set.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/tests/lib/test_01_unpwd_unicode_set.py b/tests/lib/test_01_unpwd_unicode_set.py new file mode 100644 index 00000000..64e49af1 --- /dev/null +++ b/tests/lib/test_01_unpwd_unicode_set.py @@ -0,0 +1,25 @@ +# Test whether a client produces a correct connect with a unicode username and password. + +# The client should connect with keepalive=60, clean session set, +# client id 01-unpwd-unicode-set, username and password from corresponding variables + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect( + "01-unpwd-unicode-set", + keepalive=60, + username="\u00fas\u00e9rn\u00e1m\u00e9-h\u00e9ll\u00f3", + password="h\u00e9ll\u00f3", +) + + +def test_01_unpwd_unicode_set(server_socket, start_client): + start_client("01-unpwd-unicode-set.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/test/lib/01-will-set.py b/tests/lib/test_01_will_set.py old mode 100755 new mode 100644 similarity index 50% rename from test/lib/01-will-set.py rename to tests/lib/test_01_will_set.py index 49ad0af2..55b55a25 --- a/test/lib/01-will-set.py +++ b/tests/lib/test_01_will_set.py @@ -1,37 +1,21 @@ -#!/usr/bin/env python3 - # Test whether a client produces a correct connect with a will. # Will QoS=1, will retain=1. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # client id 01-will-set will topic set to topic/on/unexpected/disconnect , will # payload set to "will message", will qos set to 1 and will retain set. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "01-will-set", keepalive=keepalive, will_topic="topic/on/unexpected/disconnect", + "01-will-set", keepalive=60, will_topic="topic/on/unexpected/disconnect", will_qos=1, will_retain=True, will_payload="will message") -sock = paho_test.create_server_socket() -client = context.start_client() - -try: - (conn, address) = sock.accept() +def test_01_will_set(server_socket, start_client): + start_client("01-will-set.py") + (conn, address) = server_socket.accept() conn.settimeout(10) - paho_test.expect_packet(conn, "connect", connect_packet) - rc = 0 - conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/tests/lib/test_01_will_unpwd_set.py b/tests/lib/test_01_will_unpwd_set.py new file mode 100644 index 00000000..95c0517f --- /dev/null +++ b/tests/lib/test_01_will_unpwd_set.py @@ -0,0 +1,26 @@ +# Test whether a client produces a correct connect with a will, username and password. + +# The client should connect with keepalive=60, clean session set, +# client id 01-will-unpwd-set , will topic set to "will-topic", will payload +# set to "will message", will qos=2, will retain not set, username set to +# "oibvvwqw" and password set to "#'^2hg9a&nm38*us". + + +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect( + "01-will-unpwd-set", + keepalive=60, username="oibvvwqw", password="#'^2hg9a&nm38*us", + will_topic="will-topic", will_qos=2, will_payload="will message", +) + + +def test_01_will_unpwd_set(server_socket, start_client): + start_client("01-will-unpwd-set.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + + conn.close() diff --git a/test/lib/01-zero-length-clientid.py b/tests/lib/test_01_zero_length_clientid.py old mode 100755 new mode 100644 similarity index 51% rename from test/lib/01-zero-length-clientid.py rename to tests/lib/test_01_zero_length_clientid.py index 587ed60f..f9934083 --- a/test/lib/01-zero-length-clientid.py +++ b/tests/lib/test_01_zero_length_clientid.py @@ -1,36 +1,23 @@ -#!/usr/bin/env python3 - # Test whether a client connects correctly with a zero length clientid. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("", keepalive=keepalive, proto_ver=4) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("", keepalive=60, proto_ver=4) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_01_zero_length_clientid(server_socket, start_client): + start_client("01-zero-length-clientid.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) - diff --git a/test/lib/02-subscribe-qos0.py b/tests/lib/test_02_subscribe_qos0.py old mode 100755 new mode 100644 similarity index 75% rename from test/lib/02-subscribe-qos0.py rename to tests/lib/test_02_subscribe_qos0.py index c97783d3..d1ff422e --- a/test/lib/02-subscribe-qos0.py +++ b/tests/lib/test_02_subscribe_qos0.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct SUBSCRIBE to a topic with QoS 0. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id subscribe-qos0-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE @@ -12,12 +10,10 @@ # SUBACK message with the accepted QoS set to 0. On receiving the SUBACK # message, the client should send a DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("subscribe-qos0-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("subscribe-qos0-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() @@ -26,12 +22,11 @@ subscribe_packet = paho_test.gen_subscribe(mid, "qos0/test", 0) suback_packet = paho_test.gen_suback(mid, 0) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_02_subscribe_qos0(server_socket, start_client): + start_client("02-subscribe-qos0.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,12 +36,5 @@ conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/02-subscribe-qos1.py b/tests/lib/test_02_subscribe_qos1.py old mode 100755 new mode 100644 similarity index 75% rename from test/lib/02-subscribe-qos1.py rename to tests/lib/test_02_subscribe_qos1.py index 1f98dd98..1e96d491 --- a/test/lib/02-subscribe-qos1.py +++ b/tests/lib/test_02_subscribe_qos1.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct SUBSCRIBE to a topic with QoS 1. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id subscribe-qos1-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE @@ -12,12 +10,10 @@ # SUBACK message with the accepted QoS set to 1. On receiving the SUBACK # message, the client should send a DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("subscribe-qos1-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("subscribe-qos1-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() @@ -26,12 +22,11 @@ subscribe_packet = paho_test.gen_subscribe(mid, "qos1/test", 1) suback_packet = paho_test.gen_suback(mid, 1) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_02_subscribe_qos1(server_socket, start_client): + start_client("02-subscribe-qos1.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,12 +36,5 @@ conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/02-subscribe-qos2.py b/tests/lib/test_02_subscribe_qos2.py old mode 100755 new mode 100644 similarity index 75% rename from test/lib/02-subscribe-qos2.py rename to tests/lib/test_02_subscribe_qos2.py index 38e74390..5d04aff5 --- a/test/lib/02-subscribe-qos2.py +++ b/tests/lib/test_02_subscribe_qos2.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct SUBSCRIBE to a topic with QoS 2. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id subscribe-qos2-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE @@ -12,12 +10,10 @@ # SUBACK message with the accepted QoS set to 2. On receiving the SUBACK # message, the client should send a DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("subscribe-qos2-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("subscribe-qos2-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() @@ -26,12 +22,11 @@ subscribe_packet = paho_test.gen_subscribe(mid, "qos2/test", 2) suback_packet = paho_test.gen_suback(mid, 2) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_02_subscribe_qos2(server_socket, start_client): + start_client("02-subscribe-qos2.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,12 +36,5 @@ conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/02-unsubscribe.py b/tests/lib/test_02_unsubscribe.py old mode 100755 new mode 100644 similarity index 68% rename from test/lib/02-unsubscribe.py rename to tests/lib/test_02_unsubscribe.py index 92ecda94..92346b68 --- a/test/lib/02-unsubscribe.py +++ b/tests/lib/test_02_unsubscribe.py @@ -1,13 +1,9 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct UNSUBSCRIBE packet. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("unsubscribe-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("unsubscribe-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() @@ -16,12 +12,11 @@ unsubscribe_packet = paho_test.gen_unsubscribe(mid, "unsubscribe/test") unsuback_packet = paho_test.gen_unsuback(mid) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_02_unsubscribe(server_socket, start_client): + start_client("02-unsubscribe.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -31,12 +26,5 @@ conn.send(unsuback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-b2c-qos1.py b/tests/lib/test_03_publish_b2c_qos1.py old mode 100755 new mode 100644 similarity index 61% rename from test/lib/03-publish-b2c-qos1.py rename to tests/lib/test_03_publish_b2c_qos1.py index 8b70a4c8..32578ddd --- a/test/lib/03-publish-b2c-qos1.py +++ b/tests/lib/test_03_publish_b2c_qos1.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client responds correctly to a PUBLISH with QoS 1. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id publish-qos1-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK the client should verify that rc==0. @@ -10,30 +8,23 @@ # "pub/qos1/receive", payload of "message", QoS=1 and mid=123. The client # should handle this as per the spec by sending a PUBACK message. # The client should then exit with return code==0. +import tests.paho_test as paho_test -import time - -import context -import paho_test - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("publish-qos1-test", keepalive=keepalive) +connect_packet = paho_test.gen_connect("publish-qos1-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() mid = 123 publish_packet = paho_test.gen_publish( - u"pub/qos1/receive", qos=1, mid=mid, payload="message") + "pub/qos1/receive", qos=1, mid=mid, payload="message") puback_packet = paho_test.gen_puback(mid) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_b2c_qos1(server_socket, start_client): + start_client("03-publish-b2c-qos1.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,19 +32,5 @@ conn.send(publish_packet) paho_test.expect_packet(conn, "puback", puback_packet) - rc = 0 conn.close() -finally: - for i in range(0, 5): - if client.returncode != None: - break - time.sleep(0.1) - - client.terminate() - client.wait() - sock.close() - if client.returncode != 0: - exit(1) - -exit(rc) diff --git a/tests/lib/test_03_publish_b2c_qos2.py b/tests/lib/test_03_publish_b2c_qos2.py new file mode 100644 index 00000000..781938f9 --- /dev/null +++ b/tests/lib/test_03_publish_b2c_qos2.py @@ -0,0 +1,41 @@ +# Test whether a client responds correctly to a PUBLISH with QoS 1. + +# The client should connect with keepalive=60, clean session set, +# and client id publish-qos1-test +# The test will send a CONNACK message to the client with rc=0. Upon receiving +# the CONNACK the client should verify that rc==0. +# The test will send the client a PUBLISH message with topic +# "pub/qos1/receive", payload of "message", QoS=1 and mid=123. The client +# should handle this as per the spec by sending a PUBACK message. +# The client should then exit with return code==0. +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("publish-qos2-test", keepalive=60) +connack_packet = paho_test.gen_connack(rc=0) + +disconnect_packet = paho_test.gen_disconnect() + +mid = 13423 +publish_packet = paho_test.gen_publish( + "pub/qos2/receive", qos=2, mid=mid, payload="message") +pubrec_packet = paho_test.gen_pubrec(mid=mid) +pubrel_packet = paho_test.gen_pubrel(mid=mid) +pubcomp_packet = paho_test.gen_pubcomp(mid) + + +def test_03_publish_b2c_qos2(server_socket, start_client): + start_client("03-publish-b2c-qos2.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + conn.send(connack_packet) + conn.send(publish_packet) + + paho_test.expect_packet(conn, "pubrec", pubrec_packet) + conn.send(pubrel_packet) + + paho_test.expect_packet(conn, "pubcomp", pubcomp_packet) + + conn.close() diff --git a/test/lib/03-publish-c2b-qos1-disconnect.py b/tests/lib/test_03_publish_c2b_qos1_disconnect.py old mode 100755 new mode 100644 similarity index 64% rename from test/lib/03-publish-c2b-qos1-disconnect.py rename to tests/lib/test_03_publish_c2b_qos1_disconnect.py index 555c0b39..10daca6a --- a/test/lib/03-publish-c2b-qos1-disconnect.py +++ b/tests/lib/test_03_publish_c2b_qos1_disconnect.py @@ -1,14 +1,10 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 1, then responds correctly to a disconnect. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "publish-qos1-test", keepalive=keepalive, clean_session=False, + "publish-qos1-test", keepalive=60, clean_session=False, ) connack_packet = paho_test.gen_connack(rc=0) @@ -16,17 +12,16 @@ mid = 1 publish_packet = paho_test.gen_publish( - u"pub/qos1/test", qos=1, mid=mid, payload="message") + "pub/qos1/test", qos=1, mid=mid, payload="message") publish_packet_dup = paho_test.gen_publish( - u"pub/qos1/test", qos=1, mid=mid, payload="message", dup=True) + "pub/qos1/test", qos=1, mid=mid, payload="message", dup=True) puback_packet = paho_test.gen_puback(mid) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_c2b_qos1_disconnect(server_socket, start_client): + start_client("03-publish-c2b-qos1-disconnect.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) @@ -36,7 +31,7 @@ # Disconnect client. It should reconnect. conn.close() - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) @@ -46,12 +41,5 @@ conn.send(puback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-c2b-qos2-disconnect.py b/tests/lib/test_03_publish_c2b_qos2_disconnect.py old mode 100755 new mode 100644 similarity index 71% rename from test/lib/03-publish-c2b-qos2-disconnect.py rename to tests/lib/test_03_publish_c2b_qos2_disconnect.py index 543d5f0c..15b1d496 --- a/test/lib/03-publish-c2b-qos2-disconnect.py +++ b/tests/lib/test_03_publish_c2b_qos2_disconnect.py @@ -1,14 +1,10 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 2 and responds to a disconnect. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "publish-qos2-test", keepalive=keepalive, clean_session=False, + "publish-qos2-test", keepalive=60, clean_session=False, ) connack_packet = paho_test.gen_connack(rc=0) @@ -16,19 +12,18 @@ mid = 1 publish_packet = paho_test.gen_publish( - u"pub/qos2/test", qos=2, mid=mid, payload="message") + "pub/qos2/test", qos=2, mid=mid, payload="message") publish_dup_packet = paho_test.gen_publish( - u"pub/qos2/test", qos=2, mid=mid, payload="message", dup=True) + "pub/qos2/test", qos=2, mid=mid, payload="message", dup=True) pubrec_packet = paho_test.gen_pubrec(mid) pubrel_packet = paho_test.gen_pubrel(mid) pubcomp_packet = paho_test.gen_pubcomp(mid) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_c2b_qos2_disconnect(server_socket, start_client): + start_client("03-publish-c2b-qos2-disconnect.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(5) paho_test.expect_packet(conn, "connect", connect_packet) @@ -38,7 +33,7 @@ # Disconnect client. It should reconnect. conn.close() - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) @@ -51,7 +46,7 @@ # Disconnect client. It should reconnect. conn.close() - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(15) # Complete connection and message flow. @@ -62,12 +57,5 @@ conn.send(pubcomp_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/tests/lib/test_03_publish_fill_inflight.py b/tests/lib/test_03_publish_fill_inflight.py new file mode 100644 index 00000000..697c896a --- /dev/null +++ b/tests/lib/test_03_publish_fill_inflight.py @@ -0,0 +1,90 @@ +# Test whether a client responds to max-inflight and reconnect when max-inflight is reached + +# The client should connect with keepalive=60, clean session set, +# and client id publish-fill-inflight +# The test will send a CONNACK message to the client with rc=0. Upon receiving +# the CONNACK the client should verify that rc==0. +# Then client should send 10 PUBLISH with QoS == 1. On client side 12 message will be +# submitted, so 2 will be queued. +# The test will wait 0.5 seconds after received the 10 PUBLISH. After this wait, it will +# disconnect the client. +# The client should re-connect and re-sent the first 10 messages. +# The test will PUBACK one message, it should receive another PUBLISH. +# The test will wait 0.5 seconds and expect no PUBLISH. +# The test will then PUBACK all message. +# The client should disconnect once everything is acked. + +import pytest + +import tests.paho_test as paho_test + + +def expected_payload(i: int) -> bytes: + return f"message{i}" + +connect_packet = paho_test.gen_connect("publish-qos1-test", keepalive=60) +connack_packet = paho_test.gen_connack(rc=0) + +disconnect_packet = paho_test.gen_disconnect() + +first_connection_publishs = [ + paho_test.gen_publish( + "topic", qos=1, mid=i+1, payload=expected_payload(i), + ) + for i in range(10) +] +second_connection_publishs = [ + paho_test.gen_publish( + # I'm not sure we should have the mid+13. + # Currently on reconnection client will do two wrong thing: + # * it sent more than max_inflight packet + # * it re-send message both with mid = old_mid + 12 AND with mid = old_mid & dup=1 + "topic", qos=1, mid=i+13, payload=expected_payload(i), + ) + for i in range(12) +] +second_connection_pubacks = [ + paho_test.gen_puback(i+13) + for i in range(12) +] + +@pytest.mark.xfail +def test_03_publish_fill_inflight(server_socket, start_client): + start_client("03-publish-fill-inflight.py") + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + conn.send(connack_packet) + + for packet in first_connection_publishs: + paho_test.expect_packet(conn, "publish", packet) + + paho_test.expect_no_packet(conn, 0.5) + + conn.close() + + (conn, address) = server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + conn.send(connack_packet) + + for packet in second_connection_publishs[:10]: + paho_test.expect_packet(conn, "publish", packet) + + paho_test.expect_no_packet(conn, 0.2) + + conn.send(second_connection_pubacks[0]) + paho_test.expect_packet(conn, "publish", second_connection_publishs[10]) + + paho_test.expect_no_packet(conn, 0.5) + + for packet in second_connection_pubacks[1:11]: + conn.send(packet) + + paho_test.expect_packet(conn, "publish", second_connection_publishs[11]) + + paho_test.expect_no_packet(conn, 0.5) + diff --git a/test/lib/03-publish-helper-qos0.py b/tests/lib/test_03_publish_helper_qos0.py old mode 100755 new mode 100644 similarity index 67% rename from test/lib/03-publish-helper-qos0.py rename to tests/lib/test_03_publish_helper_qos0.py index 5bd1af67..b1c57d90 --- a/test/lib/03-publish-helper-qos0.py +++ b/tests/lib/test_03_publish_helper_qos0.py @@ -1,9 +1,7 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 0. # Use paho.mqtt.publish helper for that. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id publish-helper-qos0-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a PUBLISH message @@ -12,28 +10,25 @@ # After sending the PUBLISH message, the client should send a # DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "publish-helper-qos0-test", keepalive=keepalive, + "publish-helper-qos0-test", keepalive=60, ) connack_packet = paho_test.gen_connack(rc=0) publish_packet = paho_test.gen_publish( - u"pub/qos0/test", qos=0, payload="message" + "pub/qos0/test", qos=0, payload="message" ) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_helper_qos0(server_socket, start_client): + start_client("03-publish-helper-qos0.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,12 +36,5 @@ paho_test.expect_packet(conn, "publish", publish_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-helper-qos0-v5.py b/tests/lib/test_03_publish_helper_qos0_v5.py old mode 100755 new mode 100644 similarity index 66% rename from test/lib/03-publish-helper-qos0-v5.py rename to tests/lib/test_03_publish_helper_qos0_v5.py index 0bea5371..ab950778 --- a/test/lib/03-publish-helper-qos0-v5.py +++ b/tests/lib/test_03_publish_helper_qos0_v5.py @@ -1,9 +1,7 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 0. # Use paho.mqtt.publish helper for that. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id publish-helper-qos0-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a PUBLISH message @@ -12,28 +10,25 @@ # After sending the PUBLISH message, the client should send a # DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "publish-helper-qos0-test", keepalive=keepalive, proto_ver=5, properties=None + "publish-helper-qos0-test", keepalive=60, proto_ver=5, properties=None ) connack_packet = paho_test.gen_connack(rc=0, proto_ver=5) publish_packet = paho_test.gen_publish( - u"pub/qos0/test", qos=0, payload="message", proto_ver=5 + "pub/qos0/test", qos=0, payload="message", proto_ver=5 ) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_helper_qos0_v5(server_socket, start_client): + start_client("03-publish-helper-qos0-v5.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,12 +36,5 @@ paho_test.expect_packet(conn, "publish", publish_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-helper-qos1-disconnect.py b/tests/lib/test_03_publish_helper_qos1_disconnect.py old mode 100755 new mode 100644 similarity index 66% rename from test/lib/03-publish-helper-qos1-disconnect.py rename to tests/lib/test_03_publish_helper_qos1_disconnect.py index b490a2de..f73462c3 --- a/test/lib/03-publish-helper-qos1-disconnect.py +++ b/tests/lib/test_03_publish_helper_qos1_disconnect.py @@ -1,37 +1,32 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 1, # then responds correctly to a disconnect. # Use paho.mqtt.publish helper for that. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + connect_packet = paho_test.gen_connect( - "publish-helper-qos1-disconnect-test", keepalive=keepalive, + "publish-helper-qos1-disconnect-test", keepalive=60, ) connack_packet = paho_test.gen_connack(rc=0) mid = 1 publish_packet = paho_test.gen_publish( - u"pub/qos1/test", qos=1, mid=mid, payload="message" + "pub/qos1/test", qos=1, mid=mid, payload="message" ) publish_packet_dup = paho_test.gen_publish( - u"pub/qos1/test", qos=1, mid=mid, payload="message", + "pub/qos1/test", qos=1, mid=mid, payload="message", dup=True, ) puback_packet = paho_test.gen_puback(mid) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_helper_qos1_disconnect(server_socket, start_client): + start_client("03-publish-helper-qos1-disconnect.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -41,7 +36,7 @@ # Disconnect client. It should reconnect. conn.close() - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) @@ -51,12 +46,5 @@ conn.send(puback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-qos0.py b/tests/lib/test_03_publish_qos0.py old mode 100755 new mode 100644 similarity index 65% rename from test/lib/03-publish-qos0.py rename to tests/lib/test_03_publish_qos0.py index ca47d05f..82f312e6 --- a/test/lib/03-publish-qos0.py +++ b/tests/lib/test_03_publish_qos0.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 0. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id publish-qos0-test # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a PUBLISH message @@ -10,24 +8,20 @@ # client should exit with an error. # After sending the PUBLISH message, the client should send a DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("publish-qos0-test", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("publish-qos0-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) -publish_packet = paho_test.gen_publish(u"pub/qos0/test", qos=0, payload="message") +publish_packet = paho_test.gen_publish("pub/qos0/test", qos=0, payload="message") disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() - -try: - (conn, address) = sock.accept() +def test_03_publish_qos0(server_socket, start_client): + start_client("03-publish-qos0.py") + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -35,12 +29,5 @@ paho_test.expect_packet(conn, "publish", publish_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/03-publish-qos0-no-payload.py b/tests/lib/test_03_publish_qos0_no_payload.py old mode 100755 new mode 100644 similarity index 66% rename from test/lib/03-publish-qos0-no-payload.py rename to tests/lib/test_03_publish_qos0_no_payload.py index 569afb36..69b9aefb --- a/test/lib/03-publish-qos0-no-payload.py +++ b/tests/lib/test_03_publish_qos0_no_payload.py @@ -1,8 +1,6 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct PUBLISH to a topic with QoS 0 and no payload. -# The client should connect to port 1888 with keepalive=60, clean session set, +# The client should connect with keepalive=60, clean session set, # and client id publish-qos0-test-np # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a PUBLISH message @@ -10,24 +8,21 @@ # rc!=0, the client should exit with an error. # After sending the PUBLISH message, the client should send a DISCONNECT message. -import context -import paho_test -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("publish-qos0-test-np", keepalive=keepalive) +import tests.paho_test as paho_test + +connect_packet = paho_test.gen_connect("publish-qos0-test-np", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) -publish_packet = paho_test.gen_publish(u"pub/qos0/no-payload/test", qos=0) +publish_packet = paho_test.gen_publish("pub/qos0/no-payload/test", qos=0) disconnect_packet = paho_test.gen_disconnect() -sock = paho_test.create_server_socket() -client = context.start_client() +def test_03_publish_qos0_no_payload(server_socket, start_client): + start_client("03-publish-qos0-no-payload.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) @@ -35,12 +30,5 @@ paho_test.expect_packet(conn, "publish", publish_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/test/lib/04-retain-qos0.py b/tests/lib/test_04_retain_qos0.py old mode 100755 new mode 100644 similarity index 54% rename from test/lib/04-retain-qos0.py rename to tests/lib/test_04_retain_qos0.py index 99a62efe..dee6b099 --- a/test/lib/04-retain-qos0.py +++ b/tests/lib/test_04_retain_qos0.py @@ -1,37 +1,25 @@ -#!/usr/bin/env python3 - # Test whether a client sends a correct retained PUBLISH to a topic with QoS 0. -import context -import paho_test -rc = 1 -keepalive = 60 +import tests.paho_test as paho_test + mid = 16 -connect_packet = paho_test.gen_connect("retain-qos0-test", keepalive=keepalive) +connect_packet = paho_test.gen_connect("retain-qos0-test", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) publish_packet = paho_test.gen_publish( - u"retain/qos0/test", qos=0, payload="retained message", retain=True) + "retain/qos0/test", qos=0, payload="retained message", retain=True) -sock = paho_test.create_server_socket() -client = context.start_client() +def test_04_retain_qos0(server_socket, start_client): + start_client("04-retain-qos0.py") -try: - (conn, address) = sock.accept() + (conn, address) = server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "publish", publish_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - sock.close() - -exit(rc) diff --git a/tests/lib/test_08_ssl_bad_cacert.py b/tests/lib/test_08_ssl_bad_cacert.py new file mode 100644 index 00000000..0031a2fd --- /dev/null +++ b/tests/lib/test_08_ssl_bad_cacert.py @@ -0,0 +1,8 @@ +import paho.mqtt.client as mqtt +import pytest + + +def test_08_ssl_bad_cacert(): + with pytest.raises(IOError): + mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1, "08-ssl-bad-cacert") + mqttc.tls_set("this/file/doesnt/exist") diff --git a/tests/lib/test_08_ssl_connect_alpn.py b/tests/lib/test_08_ssl_connect_alpn.py new file mode 100755 index 00000000..af1ecc4f --- /dev/null +++ b/tests/lib/test_08_ssl_connect_alpn.py @@ -0,0 +1,38 @@ +# Test whether a client produces a correct connect and subsequent disconnect when using SSL. +# Client must provide a certificate. +# +# The client should connect with keepalive=60, clean session set, +# and client id 08-ssl-connect-alpn +# It should use the CA certificate ssl/all-ca.crt for verifying the server. +# The test will send a CONNACK message to the client with rc=0. Upon receiving +# the CONNACK and verifying that rc=0, the client should send a DISCONNECT +# message. If rc!=0, the client should exit with an error. +# +# Additionally, the secure socket must have been negotiated with the "paho-test-protocol" + + +from tests import paho_test +from tests.paho_test import ssl + + +def test_08_ssl_connect_alpn(alpn_ssl_server_socket, start_client): + connect_packet = paho_test.gen_connect("08-ssl-connect-alpn", keepalive=60) + connack_packet = paho_test.gen_connack(rc=0) + disconnect_packet = paho_test.gen_disconnect() + + start_client("08-ssl-connect-alpn.py") + + (conn, address) = alpn_ssl_server_socket.accept() + conn.settimeout(10) + + paho_test.expect_packet(conn, "connect", connect_packet) + conn.send(connack_packet) + + paho_test.expect_packet(conn, "disconnect", disconnect_packet) + + if ssl.HAS_ALPN: + negotiated_protocol = conn.selected_alpn_protocol() + if negotiated_protocol != "paho-test-protocol": + raise Exception(f"Unexpected protocol '{negotiated_protocol}'") + + conn.close() diff --git a/test/lib/08-ssl-connect-cert-auth.py b/tests/lib/test_08_ssl_connect_cert_auth.py old mode 100755 new mode 100644 similarity index 63% rename from test/lib/08-ssl-connect-cert-auth.py rename to tests/lib/test_08_ssl_connect_cert_auth.py index fbe3b334..630773a0 --- a/test/lib/08-ssl-connect-cert-auth.py +++ b/tests/lib/test_08_ssl_connect_cert_auth.py @@ -1,45 +1,29 @@ -#!/usr/bin/env python3 - # Test whether a client produces a correct connect and subsequent disconnect when using SSL. # Client must provide a certificate. - -# The client should connect to port 1888 with keepalive=60, clean session set, +# +# The client should connect with keepalive=60, clean session set, # and client id 08-ssl-connect-crt-auth # It should use the CA certificate ssl/all-ca.crt for verifying the server. # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a DISCONNECT # message. If rc!=0, the client should exit with an error. -import context -import paho_test -from paho_test import ssl +import tests.paho_test as paho_test -context.check_ssl() - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("08-ssl-connect-crt-auth", keepalive=keepalive) +connect_packet = paho_test.gen_connect("08-ssl-connect-crt-auth", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() -ssock = paho_test.create_server_socket_ssl(cert_reqs=ssl.CERT_REQUIRED) -client = context.start_client() +def test_08_ssl_connect_crt_auth(ssl_server_socket, start_client): + start_client("08-ssl-connect-cert-auth.py") -try: - (conn, address) = ssock.accept() + (conn, address) = ssl_server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - ssock.close() - -exit(rc) diff --git a/test/lib/08-ssl-connect-cert-auth-pw.py b/tests/lib/test_08_ssl_connect_cert_auth_pw.py old mode 100755 new mode 100644 similarity index 65% rename from test/lib/08-ssl-connect-cert-auth-pw.py rename to tests/lib/test_08_ssl_connect_cert_auth_pw.py index 94d56555..333d6f75 --- a/test/lib/08-ssl-connect-cert-auth-pw.py +++ b/tests/lib/test_08_ssl_connect_cert_auth_pw.py @@ -1,45 +1,29 @@ -#!/usr/bin/env python3 - # Test whether a client produces a correct connect and subsequent disconnect when using SSL. # Client must provide a certificate - the private key is encrypted with a password. - -# The client should connect to port 1888 with keepalive=60, clean session set, +# +# The client should connect with keepalive=60, clean session set, # and client id 08-ssl-connect-crt-auth # It should use the CA certificate ssl/all-ca.crt for verifying the server. # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a DISCONNECT # message. If rc!=0, the client should exit with an error. -import context -import paho_test -from paho_test import ssl +import tests.paho_test as paho_test -context.check_ssl() - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("08-ssl-connect-crt-auth-pw", keepalive=keepalive) +connect_packet = paho_test.gen_connect("08-ssl-connect-crt-auth-pw", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() -ssock = paho_test.create_server_socket_ssl(cert_reqs=ssl.CERT_REQUIRED) -client = context.start_client() +def test_08_ssl_connect_crt_auth_pw(ssl_server_socket, start_client): + start_client("08-ssl-connect-cert-auth-pw.py") -try: - (conn, address) = ssock.accept() + (conn, address) = ssl_server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - ssock.close() - -exit(rc) diff --git a/test/lib/08-ssl-connect-no-auth.py b/tests/lib/test_08_ssl_connect_no_auth.py old mode 100755 new mode 100644 similarity index 62% rename from test/lib/08-ssl-connect-no-auth.py rename to tests/lib/test_08_ssl_connect_no_auth.py index df3322d8..d284658e --- a/test/lib/08-ssl-connect-no-auth.py +++ b/tests/lib/test_08_ssl_connect_no_auth.py @@ -1,43 +1,26 @@ -#!/usr/bin/env python3 - # Test whether a client produces a correct connect and subsequent disconnect when using SSL. - -# The client should connect to port 1888 with keepalive=60, clean session set, -# and client id 08-ssl-connect-no-auth +# +# The client should connect with keepalive=60, clean session set,# and client id 08-ssl-connect-no-auth # It should use the CA certificate ssl/all-ca.crt for verifying the server. # The test will send a CONNACK message to the client with rc=0. Upon receiving # the CONNACK and verifying that rc=0, the client should send a DISCONNECT # message. If rc!=0, the client should exit with an error. +import tests.paho_test as paho_test -import context -import paho_test - -context.check_ssl() - -rc = 1 -keepalive = 60 -connect_packet = paho_test.gen_connect("08-ssl-connect-no-auth", keepalive=keepalive) +connect_packet = paho_test.gen_connect("08-ssl-connect-no-auth", keepalive=60) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() -ssock = paho_test.create_server_socket_ssl() -client = context.start_client() +def test_08_ssl_connect_no_auth(ssl_server_socket, start_client): + start_client("08-ssl-connect-no-auth.py") -try: - (conn, address) = ssock.accept() + (conn, address) = ssl_server_socket.accept() conn.settimeout(10) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) - rc = 0 conn.close() -finally: - client.terminate() - client.wait() - ssock.close() - -exit(rc) diff --git a/tests/lib/test_08_ssl_fake_cacert.py b/tests/lib/test_08_ssl_fake_cacert.py new file mode 100644 index 00000000..09b0e3c2 --- /dev/null +++ b/tests/lib/test_08_ssl_fake_cacert.py @@ -0,0 +1,10 @@ +import pytest + +from tests.paho_test import ssl + + +def test_08_ssl_fake_cacert(ssl_server_socket, start_client): + start_client("08-ssl-fake-cacert.py") + with pytest.raises(ssl.SSLError): + (conn, address) = ssl_server_socket.accept() + conn.close() diff --git a/test/mqtt5_props.py b/tests/mqtt5_props.py similarity index 89% rename from test/mqtt5_props.py rename to tests/mqtt5_props.py index 4d612150..f9be0d66 100644 --- a/test/mqtt5_props.py +++ b/tests/mqtt5_props.py @@ -42,18 +42,18 @@ def gen_uint32_prop(identifier, word): def gen_string_prop(identifier, s): s = s.encode("utf-8") - prop = struct.pack('!BH%ds'%(len(s)), identifier, len(s), s) + prop = struct.pack(f'!BH{len(s)}s', identifier, len(s), s) return prop def gen_string_pair_prop(identifier, s1, s2): s1 = s1.encode("utf-8") s2 = s2.encode("utf-8") - prop = struct.pack('!BH%dsH%ds'%(len(s1), len(s2)), identifier, len(s1), s1, len(s2), s2) + prop = struct.pack(f'!BH{len(s1)}sH{len(s2)}s', identifier, len(s1), s1, len(s2), s2) return prop def gen_varint_prop(identifier, val): v = pack_varint(val) - return struct.pack("!B"+str(len(v))+"s", identifier, v) + return struct.pack(f"!B{len(v)}s", identifier, v) def pack_varint(varint): s = b"" diff --git a/tests/paho_test.py b/tests/paho_test.py new file mode 100644 index 00000000..40e950a4 --- /dev/null +++ b/tests/paho_test.py @@ -0,0 +1,444 @@ +import contextlib +import os +import socket +import struct +import time + +from tests.consts import ssl_path +from tests.debug_helpers import dump_packet + +try: + import ssl +except ImportError: + ssl = None + +from tests import mqtt5_props + + +def bind_to_any_free_port(sock) -> int: + """ + Bind a socket to an available port on localhost, + and return the port number. + """ + sock.bind(('localhost', 0)) + return sock.getsockname()[1] + + +def create_server_socket(): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.settimeout(10) + port = bind_to_any_free_port(sock) + sock.listen(5) + return (sock, port) + + +def create_server_socket_ssl(*, verify_mode=None, alpn_protocols=None): + assert ssl, "SSL not available" + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + context.load_verify_locations(str(ssl_path / "all-ca.crt")) + context.load_cert_chain( + str(ssl_path / "server.crt"), + str(ssl_path / "server.key"), + ) + if verify_mode: + context.verify_mode = verify_mode + + if alpn_protocols is not None: + context.set_alpn_protocols(alpn_protocols) + + ssock = context.wrap_socket(sock, server_side=True) + ssock.settimeout(10) + port = bind_to_any_free_port(ssock) + ssock.listen(5) + return (ssock, port) + + +def expect_packet(sock, name, expected): + rlen = len(expected) if len(expected) > 0 else 1 + + packet_recvd = b"" + try: + while len(packet_recvd) < rlen: + data = sock.recv(rlen-len(packet_recvd)) + if len(data) == 0: + break + packet_recvd += data + except socket.timeout: # pragma: no cover + pass + + assert packet_matches(name, packet_recvd, expected) + return True + + +def expect_no_packet(sock, delay=1): + """ expect that nothing is received within given delay + """ + try: + previous_timeout = sock.gettimeout() + sock.settimeout(delay) + data = sock.recv(1024) + except socket.timeout: + data = None + finally: + sock.settimeout(previous_timeout) + + if data is not None: + dump_packet("Received unexpected", data) + + assert data is None, "shouldn't receive any data" + + +def packet_matches(name, recvd, expected): + if recvd != expected: # pragma: no cover + print(f"FAIL: Received incorrect {name}.") + dump_packet("Received", recvd) + dump_packet("Expected", expected) + return False + else: + return True + + +def gen_connect( + client_id, + clean_session=True, + keepalive=60, + username=None, + password=None, + will_topic=None, + will_qos=0, + will_retain=False, + will_payload=b"", + proto_ver=4, + connect_reserved=False, + properties=b"", + will_properties=b"", + session_expiry=-1, +): + if (proto_ver&0x7F) == 3 or proto_ver == 0: + remaining_length = 12 + elif (proto_ver&0x7F) == 4 or proto_ver == 5: + remaining_length = 10 + else: + raise ValueError + + if client_id is not None: + client_id = client_id.encode("utf-8") + remaining_length = remaining_length + 2+len(client_id) + else: + remaining_length = remaining_length + 2 + + connect_flags = 0 + + if connect_reserved: + connect_flags = connect_flags | 0x01 + + if clean_session: + connect_flags = connect_flags | 0x02 + + if proto_ver == 5: + if properties == b"": + properties += mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_RECEIVE_MAXIMUM, 20) + + if session_expiry != -1: + properties += mqtt5_props.gen_uint32_prop(mqtt5_props.PROP_SESSION_EXPIRY_INTERVAL, session_expiry) + + properties = mqtt5_props.prop_finalise(properties) + remaining_length += len(properties) + + if will_topic is not None: + will_topic = will_topic.encode('utf-8') + remaining_length = remaining_length + 2 + len(will_topic) + 2 + len(will_payload) + connect_flags = connect_flags | 0x04 | ((will_qos & 0x03) << 3) + if will_retain: + connect_flags = connect_flags | 32 + if proto_ver == 5: + will_properties = mqtt5_props.prop_finalise(will_properties) + remaining_length += len(will_properties) + + if username is not None: + username = username.encode('utf-8') + remaining_length = remaining_length + 2 + len(username) + connect_flags = connect_flags | 0x80 + if password is not None: + password = password.encode('utf-8') + connect_flags = connect_flags | 0x40 + remaining_length = remaining_length + 2 + len(password) + + rl = pack_remaining_length(remaining_length) + packet = struct.pack("!B" + str(len(rl)) + "s", 0x10, rl) + if (proto_ver&0x7F) == 3 or proto_ver == 0: + packet = packet + struct.pack("!H6sBBH", len(b"MQIsdp"), b"MQIsdp", proto_ver, connect_flags, keepalive) + elif (proto_ver&0x7F) == 4 or proto_ver == 5: + packet = packet + struct.pack("!H4sBBH", len(b"MQTT"), b"MQTT", proto_ver, connect_flags, keepalive) + + if proto_ver == 5: + packet += properties + + if client_id is not None: + packet = packet + struct.pack("!H" + str(len(client_id)) + "s", len(client_id), bytes(client_id)) + else: + packet = packet + struct.pack("!H", 0) + + if will_topic is not None: + packet += will_properties + packet = packet + struct.pack("!H" + str(len(will_topic)) + "s", len(will_topic), will_topic) + if len(will_payload) > 0: + packet = packet + struct.pack("!H" + str(len(will_payload)) + "s", len(will_payload), will_payload.encode('utf8')) + else: + packet = packet + struct.pack("!H", 0) + + if username is not None: + packet = packet + struct.pack("!H" + str(len(username)) + "s", len(username), username) + if password is not None: + packet = packet + struct.pack("!H" + str(len(password)) + "s", len(password), password) + return packet + +def gen_connack(flags=0, rc=0, proto_ver=4, properties=b"", property_helper=True): + if proto_ver == 5: + if property_helper: + if properties is not None: + properties = mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_TOPIC_ALIAS_MAXIMUM, 10) \ + + properties + mqtt5_props.gen_uint16_prop(mqtt5_props.PROP_RECEIVE_MAXIMUM, 20) + else: + properties = b"" + properties = mqtt5_props.prop_finalise(properties) + + packet = struct.pack('!BBBB', 32, 2+len(properties), flags, rc) + properties + else: + packet = struct.pack('!BBBB', 32, 2, flags, rc) + + return packet + +def gen_publish(topic, qos, payload=None, retain=False, dup=False, mid=0, proto_ver=4, properties=b""): + if isinstance(topic, str): + topic = topic.encode("utf-8") + rl = 2+len(topic) + pack_format = "H"+str(len(topic))+"s" + if qos > 0: + rl = rl + 2 + pack_format = pack_format + "H" + + if proto_ver == 5: + properties = mqtt5_props.prop_finalise(properties) + rl += len(properties) + # This will break if len(properties) > 127 + pack_format = pack_format + "%ds"%(len(properties)) + + if payload is not None: + if isinstance(payload, str): + payload = payload.encode("utf-8") + rl = rl + len(payload) + pack_format = pack_format + str(len(payload)) + "s" + else: + payload = b"" + pack_format = pack_format + "0s" + + rlpacked = pack_remaining_length(rl) + cmd = 48 | (qos << 1) + if retain: + cmd = cmd + 1 + if dup: + cmd = cmd + 8 + + if proto_ver == 5: + if qos > 0: + return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, mid, properties, payload) + else: + return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, properties, payload) + else: + if qos > 0: + return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, mid, payload) + else: + return struct.pack("!B" + str(len(rlpacked))+"s" + pack_format, cmd, rlpacked, len(topic), topic, payload) + +def _gen_command_with_mid(cmd, mid, proto_ver=4, reason_code=-1, properties=None): + if proto_ver == 5 and (reason_code != -1 or properties is not None): + if reason_code == -1: + reason_code = 0 + + if properties is None: + return struct.pack('!BBHB', cmd, 3, mid, reason_code) + elif properties == "": + return struct.pack('!BBHBB', cmd, 4, mid, reason_code, 0) + else: + properties = mqtt5_props.prop_finalise(properties) + pack_format = "!BBHB"+str(len(properties))+"s" + return struct.pack(pack_format, cmd, 2+1+len(properties), mid, reason_code, properties) + else: + return struct.pack('!BBH', cmd, 2, mid) + +def gen_puback(mid, proto_ver=4, reason_code=-1, properties=None): + return _gen_command_with_mid(64, mid, proto_ver, reason_code, properties) + +def gen_pubrec(mid, proto_ver=4, reason_code=-1, properties=None): + return _gen_command_with_mid(80, mid, proto_ver, reason_code, properties) + +def gen_pubrel(mid, dup=False, proto_ver=4, reason_code=-1, properties=None): + if dup: + cmd = 96+8+2 + else: + cmd = 96+2 + return _gen_command_with_mid(cmd, mid, proto_ver, reason_code, properties) + +def gen_pubcomp(mid, proto_ver=4, reason_code=-1, properties=None): + return _gen_command_with_mid(112, mid, proto_ver, reason_code, properties) + + +def gen_subscribe(mid, topic, qos, cmd=130, proto_ver=4, properties=b""): + topic = topic.encode("utf-8") + packet = struct.pack("!B", cmd) + if proto_ver == 5: + if properties == b"": + packet += pack_remaining_length(2+1+2+len(topic)+1) + pack_format = "!HBH"+str(len(topic))+"sB" + return packet + struct.pack(pack_format, mid, 0, len(topic), topic, qos) + else: + properties = mqtt5_props.prop_finalise(properties) + packet += pack_remaining_length(2+1+2+len(topic)+len(properties)) + pack_format = "!H"+str(len(properties))+"s"+"H"+str(len(topic))+"sB" + return packet + struct.pack(pack_format, mid, properties, len(topic), topic, qos) + else: + packet += pack_remaining_length(2+2+len(topic)+1) + pack_format = "!HH"+str(len(topic))+"sB" + return packet + struct.pack(pack_format, mid, len(topic), topic, qos) + + +def gen_suback(mid, qos, proto_ver=4): + if proto_ver == 5: + return struct.pack('!BBHBB', 144, 2+1+1, mid, 0, qos) + else: + return struct.pack('!BBHB', 144, 2+1, mid, qos) + +def gen_unsubscribe(mid, topic, cmd=162, proto_ver=4, properties=b""): + topic = topic.encode("utf-8") + if proto_ver == 5: + if properties == b"": + pack_format = "!BBHBH"+str(len(topic))+"s" + return struct.pack(pack_format, cmd, 2+2+len(topic)+1, mid, 0, len(topic), topic) + else: + properties = mqtt5_props.prop_finalise(properties) + packet = struct.pack("!B", cmd) + l = 2+2+len(topic)+1+len(properties) # noqa: E741 + packet += pack_remaining_length(l) + pack_format = "!HB"+str(len(properties))+"sH"+str(len(topic))+"s" + packet += struct.pack(pack_format, mid, len(properties), properties, len(topic), topic) + return packet + else: + pack_format = "!BBHH"+str(len(topic))+"s" + return struct.pack(pack_format, cmd, 2+2+len(topic), mid, len(topic), topic) + +def gen_unsubscribe_multiple(mid, topics, proto_ver=4): + packet = b"" + remaining_length = 0 + for t in topics: + t = t.encode("utf-8") + remaining_length += 2+len(t) + packet += struct.pack("!H"+str(len(t))+"s", len(t), t) + + if proto_ver == 5: + remaining_length += 2+1 + + return struct.pack("!BBHB", 162, remaining_length, mid, 0) + packet + else: + remaining_length += 2 + + return struct.pack("!BBH", 162, remaining_length, mid) + packet + +def gen_unsuback(mid, reason_code=0, proto_ver=4): + if proto_ver == 5: + if isinstance(reason_code, list): + reason_code_count = len(reason_code) + p = struct.pack('!BBHB', 176, 3+reason_code_count, mid, 0) + for r in reason_code: + p += struct.pack('B', r) + return p + else: + return struct.pack('!BBHBB', 176, 4, mid, 0, reason_code) + else: + return struct.pack('!BBH', 176, 2, mid) + +def gen_pingreq(): + return struct.pack('!BB', 192, 0) + +def gen_pingresp(): + return struct.pack('!BB', 208, 0) + + +def _gen_short(cmd, reason_code=-1, proto_ver=5, properties=None): + if proto_ver == 5 and (reason_code != -1 or properties is not None): + if reason_code == -1: + reason_code = 0 + + if properties is None: + return struct.pack('!BBB', cmd, 1, reason_code) + elif properties == "": + return struct.pack('!BBBB', cmd, 2, reason_code, 0) + else: + properties = mqtt5_props.prop_finalise(properties) + return struct.pack("!BBB", cmd, 1+len(properties), reason_code) + properties + else: + return struct.pack('!BB', cmd, 0) + +def gen_disconnect(reason_code=-1, proto_ver=4, properties=None): + return _gen_short(0xE0, reason_code, proto_ver, properties) + +def gen_auth(reason_code=-1, properties=None): + return _gen_short(0xF0, reason_code, 5, properties) + + +def pack_remaining_length(remaining_length): + s = b"" + while True: + byte = remaining_length % 128 + remaining_length = remaining_length // 128 + # If there are more digits to encode, set the top bit of this digit + if remaining_length > 0: + byte = byte | 0x80 + + s = s + struct.pack("!B", byte) + if remaining_length == 0: + return s + + +def loop_until_keyboard_interrupt(mqttc): + """ + Call loop() in a loop until KeyboardInterrupt is received. + + This is used by the test clients in `lib/clients`; + the client spawner will send a SIGINT to the client process + when it wants the client to stop, so we should catch that + and stop the client gracefully. + """ + try: + while True: + mqttc.loop() + except KeyboardInterrupt: + pass + + +@contextlib.contextmanager +def wait_for_keyboard_interrupt(): + """ + Run the code in the context manager, then wait for a KeyboardInterrupt. + + This is used by the test clients in `lib/clients`; + the client spawner will send a SIGINT to the client process + when it wants the client to stop, so we should catch that + and stop the client gracefully. + """ + yield # If we get a KeyboardInterrupt during the block, it's too soon! + try: + while True: + time.sleep(0.1) + except KeyboardInterrupt: + pass + + +def get_test_server_port() -> int: + """ + Get the port number for the test server. + """ + return int(os.environ['PAHO_SERVER_PORT']) diff --git a/test/ssl/all-ca.crt b/tests/ssl/all-ca.crt similarity index 100% rename from test/ssl/all-ca.crt rename to tests/ssl/all-ca.crt diff --git a/test/ssl/client-expired.crt b/tests/ssl/client-expired.crt similarity index 100% rename from test/ssl/client-expired.crt rename to tests/ssl/client-expired.crt diff --git a/test/ssl/client-pw.crt b/tests/ssl/client-pw.crt similarity index 100% rename from test/ssl/client-pw.crt rename to tests/ssl/client-pw.crt diff --git a/test/ssl/client-pw.key b/tests/ssl/client-pw.key similarity index 100% rename from test/ssl/client-pw.key rename to tests/ssl/client-pw.key diff --git a/test/ssl/client-revoked.crt b/tests/ssl/client-revoked.crt similarity index 100% rename from test/ssl/client-revoked.crt rename to tests/ssl/client-revoked.crt diff --git a/test/ssl/client-revoked.key b/tests/ssl/client-revoked.key similarity index 100% rename from test/ssl/client-revoked.key rename to tests/ssl/client-revoked.key diff --git a/test/ssl/client.crt b/tests/ssl/client.crt similarity index 100% rename from test/ssl/client.crt rename to tests/ssl/client.crt diff --git a/test/ssl/client.key b/tests/ssl/client.key similarity index 100% rename from test/ssl/client.key rename to tests/ssl/client.key diff --git a/test/ssl/crl.pem b/tests/ssl/crl.pem similarity index 100% rename from test/ssl/crl.pem rename to tests/ssl/crl.pem diff --git a/test/ssl/gen.sh b/tests/ssl/gen.sh similarity index 100% rename from test/ssl/gen.sh rename to tests/ssl/gen.sh diff --git a/test/ssl/openssl.cnf b/tests/ssl/openssl.cnf similarity index 98% rename from test/ssl/openssl.cnf rename to tests/ssl/openssl.cnf index 5de4ed40..75342a43 100644 --- a/test/ssl/openssl.cnf +++ b/tests/ssl/openssl.cnf @@ -44,7 +44,7 @@ certs = $dir/certs # Where the issued certs are kept crl_dir = $dir/crl # Where the issued crl are kept database = $dir/index.txt # database index file. #unique_subject = no # Set to 'no' to allow creation of - # several ctificates with same subject. + # several certificates with same subject. new_certs_dir = $dir/newcerts # default place for new certs. certificate = test-signing-ca.crt # The CA certificate @@ -55,7 +55,7 @@ crl = $dir/crl.pem # The current CRL private_key = test-signing-ca.key # The private key RANDFILE = $dir/.rand # private random number file -x509_extensions = usr_cert # The extentions to add to the cert +x509_extensions = usr_cert # The extensions to add to the cert # Comment out the following two lines for the "traditional" # (and highly broken) format. @@ -162,7 +162,7 @@ default_bits = 2048 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes -x509_extensions = v3_ca # The extentions to add to the self signed cert +x509_extensions = v3_ca # The extensions to add to the self signed cert # Passwords for private keys if not present they will be prompted for # input_password = secret diff --git a/test/ssl/server-expired.crt b/tests/ssl/server-expired.crt similarity index 100% rename from test/ssl/server-expired.crt rename to tests/ssl/server-expired.crt diff --git a/test/ssl/server.crt b/tests/ssl/server.crt similarity index 100% rename from test/ssl/server.crt rename to tests/ssl/server.crt diff --git a/test/ssl/server.key b/tests/ssl/server.key similarity index 100% rename from test/ssl/server.key rename to tests/ssl/server.key diff --git a/test/ssl/test-alt-ca.crt b/tests/ssl/test-alt-ca.crt similarity index 100% rename from test/ssl/test-alt-ca.crt rename to tests/ssl/test-alt-ca.crt diff --git a/test/ssl/test-alt-ca.key b/tests/ssl/test-alt-ca.key similarity index 100% rename from test/ssl/test-alt-ca.key rename to tests/ssl/test-alt-ca.key diff --git a/test/ssl/test-bad-root-ca.crt b/tests/ssl/test-bad-root-ca.crt similarity index 100% rename from test/ssl/test-bad-root-ca.crt rename to tests/ssl/test-bad-root-ca.crt diff --git a/test/ssl/test-bad-root-ca.key b/tests/ssl/test-bad-root-ca.key similarity index 100% rename from test/ssl/test-bad-root-ca.key rename to tests/ssl/test-bad-root-ca.key diff --git a/test/ssl/test-ca.srl b/tests/ssl/test-ca.srl similarity index 100% rename from test/ssl/test-ca.srl rename to tests/ssl/test-ca.srl diff --git a/test/ssl/test-fake-root-ca.crt b/tests/ssl/test-fake-root-ca.crt similarity index 100% rename from test/ssl/test-fake-root-ca.crt rename to tests/ssl/test-fake-root-ca.crt diff --git a/test/ssl/test-fake-root-ca.key b/tests/ssl/test-fake-root-ca.key similarity index 100% rename from test/ssl/test-fake-root-ca.key rename to tests/ssl/test-fake-root-ca.key diff --git a/test/ssl/test-root-ca.crt b/tests/ssl/test-root-ca.crt similarity index 100% rename from test/ssl/test-root-ca.crt rename to tests/ssl/test-root-ca.crt diff --git a/test/ssl/test-root-ca.key b/tests/ssl/test-root-ca.key similarity index 100% rename from test/ssl/test-root-ca.key rename to tests/ssl/test-root-ca.key diff --git a/test/ssl/test-signing-ca.crt b/tests/ssl/test-signing-ca.crt similarity index 100% rename from test/ssl/test-signing-ca.crt rename to tests/ssl/test-signing-ca.crt diff --git a/test/ssl/test-signing-ca.key b/tests/ssl/test-signing-ca.key similarity index 100% rename from test/ssl/test-signing-ca.key rename to tests/ssl/test-signing-ca.key diff --git a/tests/test_client.py b/tests/test_client.py index 688c948c..09e46066 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,48 +1,46 @@ -import inspect -import os -import sys +import threading import time import unicodedata -import pytest - import paho.mqtt.client as client +import pytest +from paho.mqtt.enums import CallbackAPIVersion, MQTTErrorCode, MQTTProtocolVersion +from paho.mqtt.packettypes import PacketTypes +from paho.mqtt.properties import Properties +from paho.mqtt.reasoncodes import ReasonCode -# From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder -cmd_subfolder = os.path.realpath( - os.path.abspath( - os.path.join( - os.path.split( - inspect.getfile(inspect.currentframe()))[0], - '..', 'test'))) -if cmd_subfolder not in sys.path: - sys.path.insert(0, cmd_subfolder) -import paho_test +import tests.paho_test as paho_test # Import test fixture -from testsupport.broker import fake_broker +from tests.testsupport.broker import FakeBroker, fake_broker # noqa: F401 -@pytest.mark.parametrize("proto_ver", [ - (client.MQTTv31), - (client.MQTTv311), +@pytest.mark.parametrize("proto_ver,callback_version", [ + (MQTTProtocolVersion.MQTTv31, CallbackAPIVersion.VERSION1), + (MQTTProtocolVersion.MQTTv31, CallbackAPIVersion.VERSION2), + (MQTTProtocolVersion.MQTTv311, CallbackAPIVersion.VERSION1), + (MQTTProtocolVersion.MQTTv311, CallbackAPIVersion.VERSION2), ]) -class Test_connect(object): +class Test_connect: """ Tests on connect/disconnect behaviour of the client """ - def test_01_con_discon_success(self, proto_ver, fake_broker): + def test_01_con_discon_success(self, proto_ver, callback_version, fake_broker): mqttc = client.Client( - "01-con-discon-success", protocol=proto_ver) - - def on_connect(mqttc, obj, flags, rc): - assert rc == 0 + callback_version, + "01-con-discon-success", + protocol=proto_ver, + transport=fake_broker.transport, + ) + + def on_connect(mqttc, obj, flags, rc_or_reason_code, properties_or_none=None): + assert rc_or_reason_code == 0 mqttc.disconnect() mqttc.on_connect = on_connect - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -71,16 +69,22 @@ def on_connect(mqttc, obj, flags, rc): packet_in = fake_broker.receive_packet(1) assert not packet_in # Check connection is closed - def test_01_con_failure_rc(self, proto_ver, fake_broker): + def test_01_con_failure_rc(self, proto_ver, callback_version, fake_broker): mqttc = client.Client( - "01-con-failure-rc", protocol=proto_ver) + callback_version, "01-con-failure-rc", + protocol=proto_ver, transport=fake_broker.transport) - def on_connect(mqttc, obj, flags, rc): - assert rc == 1 + def on_connect(mqttc, obj, flags, rc_or_reason_code, properties_or_none=None): + assert rc_or_reason_code > 0 + assert rc_or_reason_code != 0 + if callback_version == CallbackAPIVersion.VERSION1: + assert rc_or_reason_code == 1 + else: + assert rc_or_reason_code == ReasonCode(PacketTypes.CONNACK, "Unsupported protocol version") mqttc.on_connect = on_connect - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -104,20 +108,394 @@ def on_connect(mqttc, obj, flags, rc): finally: mqttc.loop_stop() + def test_connection_properties(self, proto_ver, callback_version, fake_broker): + mqttc = client.Client( + CallbackAPIVersion.VERSION2, "client-id", + protocol=proto_ver, transport=fake_broker.transport) + mqttc.enable_logger() + + is_connected = threading.Event() + is_disconnected = threading.Event() + + def on_connect(mqttc, obj, flags, rc, properties): + assert rc == 0 + is_connected.set() + + def on_disconnect(*args): + import logging + logging.info("disco") + is_disconnected.set() + + mqttc.on_connect = on_connect + mqttc.on_disconnect = on_disconnect + + mqttc.host = "localhost" + mqttc.connect_timeout = 7 + mqttc.port = fake_broker.port + mqttc.keepalive = 7 + mqttc.max_inflight_messages = 7 + mqttc.max_queued_messages = 7 + mqttc.transport = fake_broker.transport + mqttc.username = "username" + mqttc.password = "password" + + mqttc.reconnect() + + # As soon as connection try to be established, no longer accept updates + with pytest.raises(RuntimeError): + mqttc.host = "localhost" + + mqttc.loop_start() + + try: + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "client-id", + keepalive=7, + username="username", + password="password", + proto_ver=proto_ver, + ) + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == connect_packet + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + is_connected.wait() + + # Check that all connections related properties can't be updated + with pytest.raises(RuntimeError): + mqttc.host = "localhost" + + with pytest.raises(RuntimeError): + mqttc.connect_timeout = 7 + + with pytest.raises(RuntimeError): + mqttc.port = fake_broker.port + + with pytest.raises(RuntimeError): + mqttc.keepalive = 7 + + with pytest.raises(RuntimeError): + mqttc.max_inflight_messages = 7 + + with pytest.raises(RuntimeError): + mqttc.max_queued_messages = 7 + + with pytest.raises(RuntimeError): + mqttc.transport = fake_broker.transport + + with pytest.raises(RuntimeError): + mqttc.username = "username" + + with pytest.raises(RuntimeError): + mqttc.password = "password" + + # close the connection, but from broker + fake_broker.finish() + + is_disconnected.wait() + assert not mqttc.is_connected() + + # still not allowed to update, because client try to reconnect in background + with pytest.raises(RuntimeError): + mqttc.host = "localhost" + + mqttc.disconnect() + + # Now it's allowed, connection is closing AND not trying to reconnect + mqttc.host = "localhost" + + finally: + mqttc.loop_stop() + + +class Test_connect_v5: + """ + Tests on connect/disconnect behaviour of the client with MQTTv5 + """ + + def test_01_broker_no_support(self, fake_broker): + mqttc = client.Client( + CallbackAPIVersion.VERSION2, "01-broker-no-support", + protocol=MQTTProtocolVersion.MQTTv5, transport=fake_broker.transport) + + def on_connect(mqttc, obj, flags, reason, properties): + assert reason == 132 + assert reason == ReasonCode(client.CONNACK >> 4, aName="Unsupported protocol version") + mqttc.disconnect() + + mqttc.on_connect = on_connect + + mqttc.connect_async("localhost", fake_broker.port) + mqttc.loop_start() + + try: + fake_broker.start() + + # Can't test the connect_packet, we can't yet generate MQTTv5 packet. + # connect_packet = paho_test.gen_connect( + # "01-con-discon-success", keepalive=60, + # proto_ver=client.MQTTv311) + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + # assert packet_in == connect_packet + + # The reply packet is a MQTTv3 connack. But that the propose of this test, + # ensure client convert it to a reason code 132 "Unsupported protocol version" + connack_packet = paho_test.gen_connack(rc=1) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed + + finally: + mqttc.loop_stop() + + +class TestConnectionLost: + def test_with_loop_start(self, fake_broker: FakeBroker): + mqttc = client.Client( + CallbackAPIVersion.VERSION1, + "test_with_loop_start", + protocol=MQTTProtocolVersion.MQTTv311, + reconnect_on_failure=False, + transport=fake_broker.transport + ) + + on_connect_reached = threading.Event() + on_disconnect_reached = threading.Event() + + + def on_connect(mqttc, obj, flags, rc): + assert rc == 0 + on_connect_reached.set() + + def on_disconnect(*args): + on_disconnect_reached.set() + + mqttc.on_connect = on_connect + mqttc.on_disconnect = on_disconnect + + mqttc.connect_async("localhost", fake_broker.port) + mqttc.loop_start() + + try: + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "test_with_loop_start", keepalive=60, + proto_ver=MQTTProtocolVersion.MQTTv311) + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == connect_packet + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + assert on_connect_reached.wait(1) + assert mqttc.is_connected() + + fake_broker.finish() + + assert on_disconnect_reached.wait(1) + assert not mqttc.is_connected() + + finally: + mqttc.loop_stop() + + def test_with_loop(self, fake_broker: FakeBroker): + mqttc = client.Client( + CallbackAPIVersion.VERSION1, + "test_with_loop", + clean_session=True, + transport=fake_broker.transport, + ) + + on_connect_reached = threading.Event() + on_disconnect_reached = threading.Event() + + + def on_connect(mqttc, obj, flags, rc): + assert rc == 0 + on_connect_reached.set() + + def on_disconnect(*args): + on_disconnect_reached.set() + + mqttc.on_connect = on_connect + mqttc.on_disconnect = on_disconnect + + mqttc.connect("localhost", fake_broker.port) + + fake_broker.start() + + # not yet connected, packet are not yet processed by loop() + assert not mqttc.is_connected() + + # connect packet is sent during connect() call + connect_packet = paho_test.gen_connect( + "test_with_loop", keepalive=60, + proto_ver=MQTTProtocolVersion.MQTTv311) + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == connect_packet + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + # call loop() to process the connack packet + assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_SUCCESS + + assert on_connect_reached.wait(1) + assert mqttc.is_connected() + + fake_broker.finish() + + # call loop() to detect the connection lost + assert mqttc.loop(timeout=1) == MQTTErrorCode.MQTT_ERR_CONN_LOST + + assert on_disconnect_reached.wait(1) + assert not mqttc.is_connected() + + +class TestPublish: + def test_publish_before_connect(self, fake_broker: FakeBroker) -> None: + mqttc = client.Client( + CallbackAPIVersion.VERSION1, + "test_publish_before_connect", + transport=fake_broker.transport, + ) + + def on_connect(mqttc, obj, flags, rc): + assert rc == 0 + + mqttc.on_connect = on_connect + + mqttc.loop_start() + mqttc.connect("localhost", fake_broker.port) + mqttc.enable_logger() + + try: + mi = mqttc.publish("test", "testing") + + fake_broker.start() + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed + # re-call fake_broker.start() to take the 2nd connection done by client + # ... this is probably a bug, when using loop_start/loop_forever + # and doing a connect() before, the TCP connection is opened twice. + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "test_publish_before_connect", keepalive=60, + proto_ver=client.MQTTv311) + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == connect_packet + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + with pytest.raises(RuntimeError): + mi.wait_for_publish(1) + + mqttc.disconnect() + + disconnect_packet = paho_test.gen_disconnect() + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == disconnect_packet + + finally: + mqttc.loop_stop() + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed + + @pytest.mark.parametrize("user_payload,sent_payload", [ + ("string", b"string"), + (b"byte", b"byte"), + (bytearray(b"bytearray"), b"bytearray"), + (42, b"42"), + (4.2, b"4.2"), + (None, b""), + ]) + def test_publish_various_payload(self, user_payload: client.PayloadType, sent_payload: bytes, fake_broker: FakeBroker) -> None: + mqttc = client.Client( + CallbackAPIVersion.VERSION2, + "test_publish_various_payload", + transport=fake_broker.transport, + ) + + mqttc.connect("localhost", fake_broker.port) + mqttc.loop_start() + mqttc.enable_logger() + + try: + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "test_publish_various_payload", keepalive=60, + proto_ver=client.MQTTv311) + fake_broker.expect_packet("connect", connect_packet) + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + mqttc.publish("test", user_payload) + + publish_packet = paho_test.gen_publish( + b"test", payload=sent_payload, qos=0 + ) + fake_broker.expect_packet("publish", publish_packet) + + mqttc.disconnect() + + disconnect_packet = paho_test.gen_disconnect() + packet_in = fake_broker.receive_packet(1000) + assert packet_in # Check connection was not closed + assert packet_in == disconnect_packet + + finally: + mqttc.loop_stop() + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed -class TestPublishBroker2Client(object): - def test_invalid_utf8_topic(self, fake_broker): - mqttc = client.Client("client-id") +@pytest.mark.parametrize("callback_version", [ + (CallbackAPIVersion.VERSION1), + (CallbackAPIVersion.VERSION2), +]) +class TestPublishBroker2Client: + def test_invalid_utf8_topic(self, callback_version, fake_broker): + mqttc = client.Client(callback_version, "client-id", transport=fake_broker.transport) def on_message(client, userdata, msg): with pytest.raises(UnicodeDecodeError): - msg.topic + assert msg.topic client.disconnect() mqttc.on_message = on_message - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -149,8 +527,8 @@ def on_message(client, userdata, msg): packet_in = fake_broker.receive_packet(1) assert not packet_in # Check connection is closed - def test_valid_utf8_topic_recv(self, fake_broker): - mqttc = client.Client("client-id") + def test_valid_utf8_topic_recv(self, callback_version, fake_broker): + mqttc = client.Client(callback_version, "client-id", transport=fake_broker.transport) # It should be non-ascii multi-bytes character topic = unicodedata.lookup('SNOWMAN') @@ -161,7 +539,7 @@ def on_message(client, userdata, msg): mqttc.on_message = on_message - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -195,13 +573,13 @@ def on_message(client, userdata, msg): packet_in = fake_broker.receive_packet(1) assert not packet_in # Check connection is closed - def test_valid_utf8_topic_publish(self, fake_broker): - mqttc = client.Client("client-id") + def test_valid_utf8_topic_publish(self, callback_version, fake_broker): + mqttc = client.Client(callback_version, "client-id", transport=fake_broker.transport) # It should be non-ascii multi-bytes character topic = unicodedata.lookup('SNOWMAN') - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -241,8 +619,8 @@ def test_valid_utf8_topic_publish(self, fake_broker): packet_in = fake_broker.receive_packet(1) assert not packet_in # Check connection is closed - def test_message_callback(self, fake_broker): - mqttc = client.Client("client-id") + def test_message_callback(self, callback_version, fake_broker): + mqttc = client.Client(callback_version, "client-id", transport=fake_broker.transport) userdata = { 'on_message': 0, 'callback1': 0, @@ -266,7 +644,7 @@ def callback2(client, userdata, msg): mqttc.message_callback_add('topic/callback/1', callback1) mqttc.message_callback_add('topic/callback/+', callback2) - mqttc.connect_async("localhost", 1888) + mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: @@ -329,3 +707,315 @@ def callback2(client, userdata, msg): assert userdata['on_message'] == 1 assert userdata['callback1'] == 1 assert userdata['callback2'] == 2 + + +class TestCompatibility: + """ + Some tests for backward compatibility + """ + + def test_change_error_code_to_enum(self): + """Make sure code don't break after MQTTErrorCode enum introduction""" + rc_ok = client.MQTTErrorCode.MQTT_ERR_SUCCESS + rc_again = client.MQTTErrorCode.MQTT_ERR_AGAIN + rc_err = client.MQTTErrorCode.MQTT_ERR_NOMEM + + # Access using old name still works + assert rc_ok == client.MQTT_ERR_SUCCESS + + # User might compare to 0 to check for success + assert rc_ok == 0 + assert not rc_err == 0 + assert not rc_again == 0 + assert not rc_ok != 0 + assert rc_err != 0 + assert rc_again != 0 + + # User might compare to specific code + assert rc_again == -1 + assert rc_err == 1 + + # User might just use "if rc:" + assert not rc_ok + assert rc_err + assert rc_again + + # User might do inequality with 0 (like "if rc > 0") + assert not (rc_ok > 0) + assert rc_err > 0 + assert rc_again < 0 + + # This might probably not be done: User might use rc as number in + # operation + assert rc_ok + 1 == 1 + + def test_migration_callback_version(self): + with pytest.raises(ValueError, match="see docs/migrations.rst"): + _ = client.Client("client-id") + + def test_callback_v1_mqtt3(self, fake_broker): + callback_called = [] + with pytest.deprecated_call(): + mqttc = client.Client( + CallbackAPIVersion.VERSION1, + "client-id", + userdata=callback_called, + transport=fake_broker.transport, + ) + + def on_connect(cl, userdata, flags, rc): + assert isinstance(cl, client.Client) + assert isinstance(flags, dict) + assert isinstance(flags["session present"], int) + assert isinstance(rc, int) + userdata.append("on_connect") + cl.subscribe([("topic", 0)]) + + def on_subscribe(cl, userdata, mid, granted_qos): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + assert isinstance(granted_qos, tuple) + assert isinstance(granted_qos[0], int) + userdata.append("on_subscribe") + cl.publish("topic", "payload", 2) + + def on_publish(cl, userdata, mid): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + userdata.append("on_publish") + + def on_message(cl, userdata, message): + assert isinstance(cl, client.Client) + assert isinstance(message, client.MQTTMessage) + userdata.append("on_message") + cl.unsubscribe("topic") + + def on_unsubscribe(cl, userdata, mid): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + userdata.append("on_unsubscribe") + cl.disconnect() + + def on_disconnect(cl, userdata, rc): + assert isinstance(cl, client.Client) + assert isinstance(rc, int) + userdata.append("on_disconnect") + + mqttc.on_connect = on_connect + mqttc.on_subscribe = on_subscribe + mqttc.on_publish = on_publish + mqttc.on_message = on_message + mqttc.on_unsubscribe = on_unsubscribe + mqttc.on_disconnect = on_disconnect + + mqttc.enable_logger() + mqttc.connect_async("localhost", fake_broker.port) + mqttc.loop_start() + + try: + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "client-id", keepalive=60) + fake_broker.expect_packet("connect", connect_packet) + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + subscribe_packet = paho_test.gen_subscribe(1, "topic", 0) + fake_broker.expect_packet("subscribe", subscribe_packet) + + suback_packet = paho_test.gen_suback(1, 0) + count = fake_broker.send_packet(suback_packet) + assert count # Check connection was not closed + assert count == len(suback_packet) + + publish_packet = paho_test.gen_publish("topic", 2, "payload", mid=2) + fake_broker.expect_packet("publish", publish_packet) + + pubrec_packet = paho_test.gen_pubrec(mid=2) + count = fake_broker.send_packet(pubrec_packet) + assert count # Check connection was not closed + assert count == len(pubrec_packet) + + pubrel_packet = paho_test.gen_pubrel(mid=2) + fake_broker.expect_packet("pubrel", pubrel_packet) + + pubcomp_packet = paho_test.gen_pubcomp(mid=2) + count = fake_broker.send_packet(pubcomp_packet) + assert count # Check connection was not closed + assert count == len(pubcomp_packet) + + publish_from_broker_packet = paho_test.gen_publish("topic", qos=0, payload="payload", mid=99) + count = fake_broker.send_packet(publish_from_broker_packet) + assert count # Check connection was not closed + assert count == len(publish_from_broker_packet) + + unsubscribe_packet = paho_test.gen_unsubscribe(mid=3, topic="topic") + fake_broker.expect_packet("unsubscribe", unsubscribe_packet) + + suback_packet = paho_test.gen_unsuback(mid=3) + count = fake_broker.send_packet(suback_packet) + assert count # Check connection was not closed + assert count == len(suback_packet) + + disconnect_packet = paho_test.gen_disconnect() + fake_broker.expect_packet("disconnect", disconnect_packet) + + assert callback_called == [ + "on_connect", + "on_subscribe", + "on_publish", + "on_message", + "on_unsubscribe", + "on_disconnect", + ] + + finally: + mqttc.disconnect() + mqttc.loop_stop() + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed + + def test_callback_v2_mqtt3(self, fake_broker): + callback_called = [] + mqttc = client.Client( + CallbackAPIVersion.VERSION2, + "client-id", + userdata=callback_called, + transport=fake_broker.transport, + ) + + def on_connect(cl, userdata, flags, reason, properties): + assert isinstance(cl, client.Client) + assert isinstance(flags, client.ConnectFlags) + assert isinstance(reason, ReasonCode) + assert isinstance(properties, Properties) + assert reason == 0 + assert properties.isEmpty() + userdata.append("on_connect") + cl.subscribe([("topic", 0)]) + + def on_subscribe(cl, userdata, mid, reason_code_list, properties): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + assert isinstance(reason_code_list, list) + assert isinstance(reason_code_list[0], ReasonCode) + assert isinstance(properties, Properties) + assert properties.isEmpty() + userdata.append("on_subscribe") + cl.publish("topic", "payload", 2) + + def on_publish(cl, userdata, mid, reason_code, properties): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + assert isinstance(reason_code, ReasonCode) + assert isinstance(properties, Properties) + assert properties.isEmpty() + userdata.append("on_publish") + + def on_message(cl, userdata, message): + assert isinstance(cl, client.Client) + assert isinstance(message, client.MQTTMessage) + userdata.append("on_message") + cl.unsubscribe("topic") + + def on_unsubscribe(cl, userdata, mid, reason_code_list, properties): + assert isinstance(cl, client.Client) + assert isinstance(mid, int) + assert isinstance(reason_code_list, list) + assert len(reason_code_list) == 0 + assert isinstance(properties, Properties) + assert properties.isEmpty() + userdata.append("on_unsubscribe") + cl.disconnect() + + def on_disconnect(cl, userdata, flags, reason_code, properties): + assert isinstance(cl, client.Client) + assert isinstance(flags, client.DisconnectFlags) + assert isinstance(reason_code, ReasonCode) + assert isinstance(properties, Properties) + assert properties.isEmpty() + userdata.append("on_disconnect") + + mqttc.on_connect = on_connect + mqttc.on_subscribe = on_subscribe + mqttc.on_publish = on_publish + mqttc.on_message = on_message + mqttc.on_unsubscribe = on_unsubscribe + mqttc.on_disconnect = on_disconnect + + mqttc.enable_logger() + mqttc.connect_async("localhost", fake_broker.port) + mqttc.loop_start() + + try: + fake_broker.start() + + connect_packet = paho_test.gen_connect( + "client-id", keepalive=60) + fake_broker.expect_packet("connect", connect_packet) + + connack_packet = paho_test.gen_connack(rc=0) + count = fake_broker.send_packet(connack_packet) + assert count # Check connection was not closed + assert count == len(connack_packet) + + subscribe_packet = paho_test.gen_subscribe(1, "topic", 0) + fake_broker.expect_packet("subscribe", subscribe_packet) + + suback_packet = paho_test.gen_suback(1, 0) + count = fake_broker.send_packet(suback_packet) + assert count # Check connection was not closed + assert count == len(suback_packet) + + publish_packet = paho_test.gen_publish("topic", 2, "payload", mid=2) + fake_broker.expect_packet("publish", publish_packet) + + pubrec_packet = paho_test.gen_pubrec(mid=2) + count = fake_broker.send_packet(pubrec_packet) + assert count # Check connection was not closed + assert count == len(pubrec_packet) + + pubrel_packet = paho_test.gen_pubrel(mid=2) + fake_broker.expect_packet("pubrel", pubrel_packet) + + pubcomp_packet = paho_test.gen_pubcomp(mid=2) + count = fake_broker.send_packet(pubcomp_packet) + assert count # Check connection was not closed + assert count == len(pubcomp_packet) + + publish_from_broker_packet = paho_test.gen_publish("topic", qos=0, payload="payload", mid=99) + count = fake_broker.send_packet(publish_from_broker_packet) + assert count # Check connection was not closed + assert count == len(publish_from_broker_packet) + + unsubscribe_packet = paho_test.gen_unsubscribe(mid=3, topic="topic") + fake_broker.expect_packet("unsubscribe", unsubscribe_packet) + + suback_packet = paho_test.gen_unsuback(mid=3) + count = fake_broker.send_packet(suback_packet) + assert count # Check connection was not closed + assert count == len(suback_packet) + + disconnect_packet = paho_test.gen_disconnect() + fake_broker.expect_packet("disconnect", disconnect_packet) + + assert callback_called == [ + "on_connect", + "on_subscribe", + "on_publish", + "on_message", + "on_unsubscribe", + "on_disconnect", + ] + + finally: + mqttc.disconnect() + mqttc.loop_stop() + + packet_in = fake_broker.receive_packet(1) + assert not packet_in # Check connection is closed diff --git a/tests/test_matcher.py b/tests/test_matcher.py index e7124d45..e2dc02a4 100644 --- a/tests/test_matcher.py +++ b/tests/test_matcher.py @@ -1,9 +1,8 @@ -import pytest - import paho.mqtt.client as client +import pytest -class Test_client_function(object): +class Test_client_function: """ Tests on topic_matches_sub function in the client module """ diff --git a/tests/test_mqttv5.py b/tests/test_mqttv5.py index bdc56644..83a1fdd8 100644 --- a/tests/test_mqttv5.py +++ b/tests/test_mqttv5.py @@ -7,7 +7,7 @@ and Eclipse Distribution License v1.0 which accompany this distribution. The Eclipse Public License is available at - http://www.eclipse.org/legal/epl-v10.html + http://www.eclipse.org/legal/epl-v20.html and the Eclipse Distribution License is available at http://www.eclipse.org/org/documents/edl-v10.php. @@ -16,91 +16,87 @@ ******************************************************************* """ -import getopt import logging +import queue import sys import threading import time -import traceback import unittest +import unittest.mock import paho.mqtt import paho.mqtt.client +from paho.mqtt.enums import CallbackAPIVersion from paho.mqtt.packettypes import PacketTypes from paho.mqtt.properties import Properties -from paho.mqtt.reasoncodes import ReasonCodes from paho.mqtt.subscribeoptions import SubscribeOptions +DEFAULT_TIMEOUT = 5 +# timeout for something that should not happen but we wait to +# give it time to happen if it does due to a bug. +WAIT_NON_EVENT_TIMEOUT = 1 class Callbacks: def __init__(self): - self.messages = [] - self.publisheds = [] - self.subscribeds = [] - self.unsubscribeds = [] - self.disconnecteds = [] - self.connecteds = [] - self.conn_failures = [] + self.messages = queue.Queue() + self.publisheds = queue.Queue() + self.subscribeds = queue.Queue() + self.unsubscribeds = queue.Queue() + self.disconnecteds = queue.Queue() + self.connecteds = queue.Queue() + self.conn_failures = queue.Queue() def __str__(self): - return str(self.messages) + str(self.messagedicts) + str(self.publisheds) + \ - str(self.subscribeds) + \ - str(self.unsubscribeds) + str(self.disconnects) + return str(self.messages.queue) + str(self.messagedicts.queue) + str(self.publisheds.queue) + \ + str(self.subscribeds.queue) + \ + str(self.unsubscribeds.queue) + str(self.disconnects.queue) def clear(self): self.__init__() def on_connect(self, client, userdata, flags, reasonCode, properties): - self.connecteds.append({"userdata": userdata, "flags": flags, - "reasonCode": reasonCode, "properties": properties}) + self.connecteds.put({"userdata": userdata, "flags": flags, + "reasonCode": reasonCode, "properties": properties}) def on_connect_fail(self, client, userdata): - self.conn_failures.append({"userdata": userdata}) - - def wait(self, alist, timeout=2): - interval = 0.2 - total = 0 - while len(alist) == 0 and total < timeout: - time.sleep(interval) - total += interval - return alist.pop(0) # if len(alist) > 0 else None + self.conn_failures.put({"userdata": userdata}) def wait_connect_fail(self): - return self.wait(self.conn_failures, timeout=10) + return self.conn_failures.get(timeout=10) def wait_connected(self): - return self.wait(self.connecteds) + return self.connecteds.get(timeout=2) def on_disconnect(self, client, userdata, reasonCode, properties=None): - self.disconnecteds.append( + self.disconnecteds.put( {"reasonCode": reasonCode, "properties": properties}) def wait_disconnected(self): - return self.wait(self.disconnecteds) + return self.disconnecteds.get(timeout=2) def on_message(self, client, userdata, message): - self.messages.append({"userdata": userdata, "message": message}) + self.messages.put({"userdata": userdata, "message": message}) def published(self, client, userdata, msgid): - self.publisheds.append(msgid) + self.publisheds.put(msgid) def wait_published(self): - return self.wait(self.publisheds) + return self.publisheds.get(timeout=2) def on_subscribe(self, client, userdata, mid, reasonCodes, properties): - self.subscribeds.append({"mid": mid, "userdata": userdata, - "properties": properties, "reasonCodes": reasonCodes}) + self.subscribeds.put({"mid": mid, "userdata": userdata, + "properties": properties, "reasonCodes": reasonCodes}) def wait_subscribed(self): - return self.wait(self.subscribeds) + return self.subscribeds.get(timeout=2) def unsubscribed(self, client, userdata, mid, properties, reasonCodes): - self.unsubscribeds.append({"mid": mid, "userdata": userdata, - "properties": properties, "reasonCodes": reasonCodes}) + self.unsubscribeds.put({"mid": mid, "userdata": userdata, + "properties": properties, "reasonCodes": reasonCodes}) def wait_unsubscribed(self): - return self.wait(self.unsubscribeds) + return self.unsubscribeds.get(timeout=2) def on_log(self, client, userdata, level, buf): print(buf) @@ -115,24 +111,57 @@ def register(self, client): client.on_connect_fail = self.on_connect_fail client.on_log = self.on_log + def get_messages(self, count: int, timeout: float = DEFAULT_TIMEOUT): + result = [] + deadline = time.time() + timeout + while len(result) < count: + get_timeout = deadline - time.time() + if get_timeout <= 0: + result.append(self.messages.get_nowait()) + else: + result.append(self.messages.get(timeout=get_timeout)) + + return result + + def get_at_most_messages(self, count: int, timeout: float = DEFAULT_TIMEOUT): + result = [] + deadline = time.time() + timeout + try: + while len(result) < count: + get_timeout = deadline - time.time() + if get_timeout <= 0: + result.append(self.messages.get_nowait()) + else: + result.append(self.messages.get(timeout=get_timeout)) + except queue.Empty: + pass + + return result + def cleanRetained(port): callback = Callbacks() - curclient = paho.mqtt.client.Client("clean retained".encode("utf-8"), - protocol=paho.mqtt.client.MQTTv5) - curclient.loop_start() + curclient = paho.mqtt.client.Client( + CallbackAPIVersion.VERSION1, + b"clean retained", + protocol=paho.mqtt.client.MQTTv5, + ) callback.register(curclient) curclient.connect(host="localhost", port=port) - response = callback.wait_connected() + curclient.loop_start() + callback.wait_connected() curclient.subscribe("#", options=SubscribeOptions(qos=0)) - response = callback.wait_subscribed() # wait for retained messages to arrive - time.sleep(1) - for message in callback.messages: - logging.info("deleting retained message for topic", message["message"]) - curclient.publish(message["message"].topic, b"", 0, retain=True) + callback.wait_subscribed() # wait for retained messages to arrive + try: + while True: + message = callback.messages.get(timeout=WAIT_NON_EVENT_TIMEOUT) + if message["message"].payload != b"": + logging.info("deleting retained message for topic", message["message"]) + curclient.publish(message["message"].topic, b"", 0, retain=True) + except queue.Empty: + pass curclient.disconnect() curclient.loop_stop() - time.sleep(.1) def cleanup(port): @@ -140,15 +169,18 @@ def cleanup(port): print("clean up starting") clientids = ("aclient", "bclient") + def _on_connect(client, *args): + client.disconnect() + for clientid in clientids: - curclient = paho.mqtt.client.Client(clientid.encode( - "utf-8"), protocol=paho.mqtt.client.MQTTv5) - curclient.loop_start() + curclient = paho.mqtt.client.Client( + CallbackAPIVersion.VERSION1, + clientid.encode("utf-8"), + protocol=paho.mqtt.client.MQTTv5, + ) + curclient.on_connect = _on_connect curclient.connect(host="localhost", port=port, clean_start=True) - time.sleep(.1) - curclient.disconnect() - time.sleep(.1) - curclient.loop_stop() + curclient.loop_forever() # clean retained messages cleanRetained(port) @@ -164,21 +196,31 @@ def setUpClass(cls): sys.path.append("paho.mqtt.testing/interoperability/") try: import mqtt.brokers - except ImportError: - raise unittest.SkipTest("paho.mqtt.testing not present.") - - cls._test_broker = threading.Thread( - target=mqtt.brokers.run, - kwargs={ - "config": ["listener 0"], - }, - ) - cls._test_broker.daemon = True - cls._test_broker.start() - # Wait a bit for TCP server to bind to an address - time.sleep(0.5) - # Hack to find the port used by the test broker... - cls._test_broker_port = mqtt.brokers.listeners.TCPListeners.server.socket.getsockname()[1] + except ImportError as ie: + raise unittest.SkipTest("paho.mqtt.testing not present.") from ie + + # Hack: we need to patch `signal.signal()` because `mqtt.brokers.run()` + # calls it to set up a signal handler; however, that won't work + # from a thread... + with unittest.mock.patch("signal.signal", unittest.mock.MagicMock()): + cls._test_broker = threading.Thread( + target=mqtt.brokers.run, + kwargs={ + "config": ["listener 0"], + }, + ) + cls._test_broker.daemon = True + cls._test_broker.start() + # Wait a bit for TCP server to bind to an address + for _ in range(20): + time.sleep(0.1) + if mqtt.brokers.listeners.TCPListeners.server is not None: + port = mqtt.brokers.listeners.TCPListeners.server.socket.getsockname()[1] + if port != 0: + cls._test_broker_port = port + break + else: + raise ValueError("can't find the test broker port") setData() cleanup(cls._test_broker_port) @@ -187,12 +229,10 @@ def setUpClass(cls): #aclient = mqtt_client.Client(b"\xEF\xBB\xBF" + "myclientid".encode("utf-8")) #aclient = mqtt_client.Client("myclientid".encode("utf-8")) - aclient = paho.mqtt.client.Client("aclient".encode( - "utf-8"), protocol=paho.mqtt.client.MQTTv5) + aclient = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, b"aclient", protocol=paho.mqtt.client.MQTTv5) callback.register(aclient) - bclient = paho.mqtt.client.Client("bclient".encode( - "utf-8"), protocol=paho.mqtt.client.MQTTv5) + bclient = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, b"bclient", protocol=paho.mqtt.client.MQTTv5) callback2.register(bclient) @classmethod @@ -202,31 +242,33 @@ def tearDownClass(cls): mqtt.brokers.listeners.TCPListeners.server.shutdown() cls._test_broker.join(5) - def waitfor(self, queue, depth, limit): - total = 0 - while len(queue) < depth and total < limit: - interval = .5 - total += interval - time.sleep(interval) - def test_basic(self): + import datetime + print(datetime.datetime.now(), "start") aclient.connect(host="localhost", port=self._test_broker_port) aclient.loop_start() + print(datetime.datetime.now(), "loop_start") response = callback.wait_connected() + print(datetime.datetime.now(), "connected") self.assertEqual(response["reasonCode"].getName(), "Success") aclient.subscribe(topics[0], options=SubscribeOptions(qos=2)) response = callback.wait_subscribed() + print(datetime.datetime.now(), "wait_subscribed") self.assertEqual(response["reasonCodes"][0].getName(), "Granted QoS 2") aclient.publish(topics[0], b"qos 0") aclient.publish(topics[0], b"qos 1", 1) aclient.publish(topics[0], b"qos 2", 2) - i = 0 - while len(callback.messages) < 3 and i < 10: - time.sleep(.2) - i += 1 - self.assertEqual(len(callback.messages), 3) + + msgs = callback.get_messages(3) + print(datetime.datetime.now(), "publish get") + got_payload = { + x["message"].payload + for x in msgs + } + + self.assertEqual(got_payload, {b"qos 0", b"qos 1", b"qos 2"}) aclient.disconnect() callback.clear() @@ -244,10 +286,6 @@ def test_connect_fail(self): fclient.loop_stop() def test_retained_message(self): - qos0topic = "fromb/qos 0" - qos1topic = "fromb/qos 1" - qos2topic = "fromb/qos2" - wildcardtopic = "fromb/+" publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.UserProperty = ("a", "2") @@ -265,26 +303,27 @@ def test_retained_message(self): aclient.publish(topics[3], b"qos 2", 2, retain=True, properties=publish_properties) # wait until those messages are published - time.sleep(1) + time.sleep(WAIT_NON_EVENT_TIMEOUT) aclient.subscribe(wildtopics[5], options=SubscribeOptions(qos=2)) response = callback.wait_subscribed() self.assertEqual(response["reasonCodes"][0].getName(), "Granted QoS 2") + msgs = callback.get_messages(3) - time.sleep(1) aclient.disconnect() aclient.loop_stop() - self.assertEqual(len(callback.messages), 3) - userprops = callback.messages[0]["message"].properties.UserProperty + self.assertTrue(callback.messages.empty()) + + userprops = msgs[0]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - userprops = callback.messages[1]["message"].properties.UserProperty + userprops = msgs[1]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - userprops = callback.messages[2]["message"].properties.UserProperty + userprops = msgs[2]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - qoss = [callback.messages[i]["message"].qos for i in range(3)] + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) cleanRetained(self._test_broker_port) @@ -293,7 +332,7 @@ def test_will_message(self): # will messages and keep alive callback.clear() callback2.clear() - self.assertEqual(len(callback2.messages), 0, callback2.messages) + self.assertTrue(callback2.messages.empty(), callback2.messages.queue) will_properties = Properties(PacketTypes.WILLMESSAGE) will_properties.WillDelayInterval = 0 # this is the default anyway @@ -315,12 +354,11 @@ def test_will_message(self): # keep alive timeout ought to be triggered so the will message is received aclient.loop_stop() # so that pings aren't sent - self.waitfor(callback2.messages, 1, 10) + msg = callback2.messages.get(timeout=10) bclient.disconnect() bclient.loop_stop() - # should have the will message - self.assertEqual(len(callback2.messages), 1, callback2.messages) - props = callback2.messages[0]["message"].properties + + props = msg["message"].properties self.assertEqual(props.UserProperty, [("a", "2"), ("c", "3")]) def test_zero_length_clientid(self): @@ -328,7 +366,7 @@ def test_zero_length_clientid(self): callback0 = Callbacks() - client0 = paho.mqtt.client.Client(protocol=paho.mqtt.client.MQTTv5) + client0 = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, protocol=paho.mqtt.client.MQTTv5) callback0.register(client0) client0.loop_start() # should not be rejected @@ -340,7 +378,7 @@ def test_zero_length_clientid(self): client0.disconnect() client0.loop_stop() - client0 = paho.mqtt.client.Client(protocol=paho.mqtt.client.MQTTv5) + client0 = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, protocol=paho.mqtt.client.MQTTv5) callback0.register(client0) client0.loop_start() client0.connect(host="localhost", port=self._test_broker_port) # should work @@ -353,7 +391,8 @@ def test_zero_length_clientid(self): # when we supply a client id, we should not get one assigned client0 = paho.mqtt.client.Client( - "client0", protocol=paho.mqtt.client.MQTTv5) + CallbackAPIVersion.VERSION1, "client0", protocol=paho.mqtt.client.MQTTv5, + ) callback0.register(client0) client0.loop_start() client0.connect(host="localhost", port=self._test_broker_port) # should work @@ -368,55 +407,64 @@ def test_offline_message_queueing(self): # message queueing for offline clients cleanRetained(self._test_broker_port) ocallback = Callbacks() - clientid = "offline message queueing".encode("utf-8") + clientid = b"offline message queueing" oclient = paho.mqtt.client.Client( - clientid, protocol=paho.mqtt.client.MQTTv5) + CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, + ) ocallback.register(oclient) connect_properties = Properties(PacketTypes.CONNECT) connect_properties.SessionExpiryInterval = 99999 oclient.loop_start() oclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) - response = ocallback.wait_connected() + ocallback.wait_connected() oclient.subscribe(wildtopics[5], qos=2) - response = ocallback.wait_subscribed() + ocallback.wait_subscribed() oclient.disconnect() oclient.loop_stop() bclient.loop_start() bclient.connect(host="localhost", port=self._test_broker_port) - response = callback2.wait_connected() - bclient.publish(topics[1], b"qos 0", 0) - bclient.publish(topics[2], b"qos 1", 1) - bclient.publish(topics[3], b"qos 2", 2) - time.sleep(2) + callback2.wait_connected() + msg1 = bclient.publish(topics[1], b"qos 0", 0) + msg2 = bclient.publish(topics[2], b"qos 1", 1) + msg3 = bclient.publish(topics[3], b"qos 2", 2) + + msg1.wait_for_publish() + msg2.wait_for_publish() + msg3.wait_for_publish() + bclient.disconnect() bclient.loop_stop() oclient = paho.mqtt.client.Client( - clientid, protocol=paho.mqtt.client.MQTTv5) + CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, + ) ocallback.register(oclient) oclient.loop_start() oclient.connect(host="localhost", port=self._test_broker_port, clean_start=False) - response = ocallback.wait_connected() - time.sleep(2) + ocallback.wait_connected() + + msgs = ocallback.get_at_most_messages(3) + oclient.disconnect() oclient.loop_stop() - self.assertTrue(len(ocallback.messages) in [ - 2, 3], len(ocallback.messages)) + self.assertTrue(len(msgs) in [ + 2, 3], ocallback.messages.qsize()) logging.info("This server %s queueing QoS 0 messages for offline clients" % - ("is" if len(ocallback.messages) == 3 else "is not")) + ("is" if len(msgs) == 3 else "is not")) def test_overlapping_subscriptions(self): # overlapping subscriptions. When there is more than one matching subscription for the same client for a topic, # the server may send back one message with the highest QoS of any matching subscription, or one message for # each subscription with a matching QoS. ocallback = Callbacks() - clientid = "overlapping subscriptions".encode("utf-8") + clientid = b"overlapping subscriptions" oclient = paho.mqtt.client.Client( - clientid, protocol=paho.mqtt.client.MQTTv5) + CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, + ) ocallback.register(oclient) oclient.loop_start() @@ -427,31 +475,32 @@ def test_overlapping_subscriptions(self): ocallback.wait_subscribed() oclient.publish(topics[3], b"overlapping topic filters", 2) ocallback.wait_published() - time.sleep(1) - self.assertTrue(len(ocallback.messages) in [1, 2], ocallback.messages) - if len(ocallback.messages) == 1: + + msgs = ocallback.get_at_most_messages(2) + if len(msgs) == 1: logging.info( "This server is publishing one message for all matching overlapping subscriptions, not one for each.") self.assertEqual( - ocallback.messages[0]["message"].qos, 2, ocallback.messages[0]["message"].qos) + msgs[0]["message"].qos, 2, msgs[0]["message"].qos) else: logging.info( "This server is publishing one message per each matching overlapping subscription.") - self.assertTrue((ocallback.messages[0]["message"].qos == 2 and ocallback.messages[1]["message"].qos == 1) or - (ocallback.messages[0]["message"].qos == 1 and ocallback.messages[1]["message"].qos == 2), callback.messages) + self.assertTrue((msgs[0]["message"].qos == 2 and msgs[1]["message"].qos == 1) or + (msgs[0]["message"].qos == 1 and msgs[1]["message"].qos == 2), msgs) oclient.disconnect() oclient.loop_stop() ocallback.clear() def test_subscribe_failure(self): - # Subscribe failure. A new feature of MQTT 3.1.1 is the ability to send back negative reponses to subscribe + # Subscribe failure. A new feature of MQTT 3.1.1 is the ability to send back negative responses to subscribe # requests. One way of doing this is to subscribe to a topic which is not allowed to be subscribed to. logging.info("Subscribe failure test starting") ocallback = Callbacks() - clientid = "subscribe failure".encode("utf-8") + clientid = b"subscribe failure" oclient = paho.mqtt.client.Client( - clientid, protocol=paho.mqtt.client.MQTTv5) + CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, + ) ocallback.register(oclient) oclient.loop_start() oclient.connect(host="localhost", port=self._test_broker_port) @@ -460,7 +509,7 @@ def test_subscribe_failure(self): response = ocallback.wait_subscribed() self.assertEqual(response["reasonCodes"][0].getName(), "Unspecified error", - "return code should be 0x80 %s" % response["reasonCodes"][0].getName()) + f"return code should be 0x80 {response['reasonCodes'][0].getName()}") oclient.disconnect() oclient.loop_stop() @@ -479,7 +528,7 @@ def test_unsubscribe(self): # Unsubscribe from one topic bclient.unsubscribe(topics[0]) callback2.wait_unsubscribed() - callback2.clear() # if there were any retained messsages + callback2.clear() # if there were any retained messages aclient.connect(host="localhost", port=self._test_broker_port) aclient.loop_start() @@ -487,18 +536,22 @@ def test_unsubscribe(self): aclient.publish(topics[0], b"topic 0 - unsubscribed", 1, retain=False) aclient.publish(topics[1], b"topic 1", 1, retain=False) aclient.publish(topics[2], b"topic 2", 1, retain=False) - time.sleep(2) + + msgs = callback2.get_messages(2) bclient.disconnect() bclient.loop_stop() aclient.disconnect() aclient.loop_stop() - self.assertEqual(len(callback2.messages), 2, callback2.messages) + self.assertEqual(len(msgs), 2) def new_client(self, clientid): callback = Callbacks() - client = paho.mqtt.client.Client(clientid.encode( - "utf-8"), protocol=paho.mqtt.client.MQTTv5) + client = paho.mqtt.client.Client( + CallbackAPIVersion.VERSION1, + clientid.encode("utf-8"), + protocol=paho.mqtt.client.MQTTv5, + ) callback.register(client) client.loop_start() return client, callback @@ -633,24 +686,23 @@ def test_user_properties(self): properties=publish_properties) uclient.publish(topics[0], b"", 2, retain=False, properties=publish_properties) - count = 0 - while len(ucallback.messages) < 3 and count < 50: - time.sleep(.1) - count += 1 + + msgs = ucallback.get_messages(3) + uclient.disconnect() ucallback.wait_disconnected() uclient.loop_stop() - self.assertEqual(len(ucallback.messages), 3, ucallback.messages) - userprops = ucallback.messages[0]["message"].properties.UserProperty + self.assertTrue(ucallback.messages.empty(), ucallback.messages.queue) + userprops = msgs[0]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - userprops = ucallback.messages[1]["message"].properties.UserProperty + userprops = msgs[1]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - userprops = ucallback.messages[2]["message"].properties.UserProperty + userprops = msgs[2]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) - qoss = [ucallback.messages[i]["message"].qos for i in range(3)] + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) def test_payload_format(self): @@ -658,10 +710,10 @@ def test_payload_format(self): pclient, pcallback = self.new_client(clientid) pclient.loop_start() pclient.connect_async(host="localhost", port=self._test_broker_port) - response = pcallback.wait_connected() + pcallback.wait_connected() pclient.subscribe(topics[0], qos=2) - response = pcallback.wait_subscribed() + pcallback.wait_subscribed() publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.PayloadFormatIndicator = 1 publish_properties.ContentType = "My name" @@ -675,28 +727,26 @@ def test_payload_format(self): topics[0], b"qos 2", 2, retain=False, properties=publish_properties) info.wait_for_publish() - count = 0 - while len(pcallback.messages) < 3 and count < 50: - time.sleep(.1) - count += 1 + msgs = pcallback.get_messages(3) + pclient.disconnect() pcallback.wait_disconnected() pclient.loop_stop() - self.assertEqual(len(pcallback.messages), 3, pcallback.messages) - props = pcallback.messages[0]["message"].properties + self.assertTrue(pcallback.messages.empty(), pcallback.messages.queue) + props = msgs[0]["message"].properties self.assertEqual(props.ContentType, "My name", props.ContentType) self.assertEqual(props.PayloadFormatIndicator, 1, props.PayloadFormatIndicator) - props = pcallback.messages[1]["message"].properties + props = msgs[1]["message"].properties self.assertEqual(props.ContentType, "My name", props.ContentType) self.assertEqual(props.PayloadFormatIndicator, 1, props.PayloadFormatIndicator) - props = pcallback.messages[2]["message"].properties + props = msgs[2]["message"].properties self.assertEqual(props.ContentType, "My name", props.ContentType) self.assertEqual(props.PayloadFormatIndicator, 1, props.PayloadFormatIndicator) - qoss = [pcallback.messages[i]["message"].qos for i in range(3)] + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) def test_message_expiry(self): @@ -705,19 +755,19 @@ def test_message_expiry(self): connect_properties = Properties(PacketTypes.CONNECT) connect_properties.SessionExpiryInterval = 99999 - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.loop_start() lbclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) - response = lbcallback.wait_connected() + lbcallback.wait_connected() lbclient.subscribe(topics[0], qos=2) - response = lbcallback.wait_subscribed() + lbcallback.wait_subscribed() disconnect_properties = Properties(PacketTypes.DISCONNECT) disconnect_properties.SessionExpiryInterval = 999999999 lbclient.disconnect(properties=disconnect_properties) lbcallback.wait_disconnected() lbclient.loop_stop() - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.loop_start() laclient.connect(host="localhost", port=self._test_broker_port) publish_properties = Properties(PacketTypes.PUBLISH) @@ -735,17 +785,18 @@ def test_message_expiry(self): 2, retain=False, properties=publish_properties) time.sleep(3) - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.loop_start() lbclient.connect(host="localhost", port=self._test_broker_port, clean_start=False) lbcallback.wait_connected() - self.waitfor(lbcallback.messages, 1, 3) - time.sleep(1) - self.assertEqual(len(lbcallback.messages), 2, lbcallback.messages) - self.assertTrue(lbcallback.messages[0]["message"].properties.MessageExpiryInterval < 6, - lbcallback.messages[0]["message"].properties.MessageExpiryInterval) - self.assertTrue(lbcallback.messages[1]["message"].properties.MessageExpiryInterval < 6, - lbcallback.messages[1]["message"].properties.MessageExpiryInterval) + + msgs = lbcallback.get_messages(2) + + self.assertTrue(lbcallback.messages.empty(), lbcallback.messages.queue) + self.assertTrue(msgs[0]["message"].properties.MessageExpiryInterval < 6, + msgs[0]["message"].properties.MessageExpiryInterval) + self.assertTrue(msgs[1]["message"].properties.MessageExpiryInterval < 6, + msgs[1]["message"].properties.MessageExpiryInterval) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() @@ -758,7 +809,7 @@ def test_subscribe_options(self): # noLocal clientid = 'subscribe options - noLocal' - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.loop_start() @@ -766,7 +817,7 @@ def test_subscribe_options(self): topics[0], options=SubscribeOptions(qos=2, noLocal=True)) lacallback.wait_subscribed() - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() @@ -775,11 +826,16 @@ def test_subscribe_options(self): lbcallback.wait_subscribed() laclient.publish(topics[0], b"noLocal test", 1, retain=False) - self.waitfor(lbcallback.messages, 1, 3) - time.sleep(1) - self.assertEqual(lacallback.messages, [], lacallback.messages) - self.assertEqual(len(lbcallback.messages), 1, lbcallback.messages) + lbcallback.messages.get(timeout=DEFAULT_TIMEOUT) + try: + lacallback.messages.get(timeout=WAIT_NON_EVENT_TIMEOUT) + raise ValueError("unexpected message received") + except queue.Empty: + pass + + self.assertTrue(lacallback.messages.empty(), lacallback.messages.queue) + self.assertTrue(lbcallback.messages.empty(), lbcallback.messages.queue) laclient.disconnect() lacallback.wait_disconnected() lbclient.disconnect() @@ -789,31 +845,29 @@ def test_subscribe_options(self): # retainAsPublished clientid = 'subscribe options - retain as published' - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.subscribe(topics[0], options=SubscribeOptions( qos=2, retainAsPublished=True)) lacallback.wait_subscribed() - self.waitfor(lacallback.subscribeds, 1, 3) laclient.publish( topics[0], b"retain as published false", 1, retain=False) laclient.publish( topics[0], b"retain as published true", 1, retain=True) - self.waitfor(lacallback.messages, 2, 3) - time.sleep(1) + msgs = lacallback.get_messages(2) - self.assertEqual(len(lacallback.messages), 2, lacallback.messages) + self.assertTrue(lacallback.messages.empty(), lacallback.messages.queue) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() - self.assertEqual(lacallback.messages[0]["message"].retain, False) - self.assertEqual(lacallback.messages[1]["message"].retain, True) + self.assertEqual(msgs[0]["message"].retain, False) + self.assertEqual(msgs[1]["message"].retain, True) # retainHandling clientid = 'subscribe options - retain handling' - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.publish(topics[1], b"qos 0", 0, retain=True) @@ -825,70 +879,72 @@ def test_subscribe_options(self): laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=1)) lacallback.wait_subscribed() - self.assertEqual(len(lacallback.messages), 3) - qoss = [lacallback.messages[i]["message"].qos for i in range(3)] + + msgs = lacallback.get_messages(3) + + self.assertTrue(lacallback.messages.empty()) + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) lacallback.clear() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=1)) lacallback.wait_subscribed() time.sleep(1) - self.assertEqual(len(lacallback.messages), 0) + self.assertTrue(lacallback.messages.empty()) # remove that subscription properties = Properties(PacketTypes.UNSUBSCRIBE) properties.UserProperty = ("a", "2") properties.UserProperty = ("c", "3") laclient.unsubscribe(wildtopics[5], properties) - response = lacallback.wait_unsubscribed() + lacallback.wait_unsubscribed() # check that we really did remove that subscription laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=1)) lacallback.wait_subscribed() - self.assertEqual(len(lacallback.messages), 3) - qoss = [lacallback.messages[i]["message"].qos for i in range(3)] + msgs = lacallback.get_messages(3) + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) lacallback.clear() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=1)) lacallback.wait_subscribed() - time.sleep(1) - self.assertEqual(len(lacallback.messages), 0) + time.sleep(WAIT_NON_EVENT_TIMEOUT) + self.assertTrue(lacallback.messages.empty()) # remove that subscription properties = Properties(PacketTypes.UNSUBSCRIBE) properties.UserProperty = ("a", "2") properties.UserProperty = ("c", "3") laclient.unsubscribe(wildtopics[5], properties) - response = lacallback.wait_unsubscribed() + lacallback.wait_unsubscribed() lacallback.clear() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=2)) lacallback.wait_subscribed() - self.assertEqual(len(lacallback.messages), 0) + self.assertTrue(lacallback.messages.empty()) laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=2)) lacallback.wait_subscribed() - self.assertEqual(len(lacallback.messages), 0) + self.assertTrue(lacallback.messages.empty()) # remove that subscription laclient.unsubscribe(wildtopics[5]) - response = lacallback.wait_unsubscribed() + lacallback.wait_unsubscribed() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=0)) lacallback.wait_subscribed() - self.assertEqual(len(lacallback.messages), 3) - qoss = [lacallback.messages[i]["message"].qos for i in range(3)] + msgs = lacallback.get_messages(3) + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) lacallback.clear() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=0)) - time.sleep(1) - self.assertEqual(len(lacallback.messages), 3) - qoss = [lacallback.messages[i]["message"].qos for i in range(3)] + msgs = lacallback.get_messages(3) + qoss = [x["message"].qos for x in msgs] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) laclient.disconnect() lacallback.wait_disconnected() @@ -899,7 +955,7 @@ def test_subscribe_options(self): def test_subscription_identifiers(self): clientid = 'subscription identifiers' - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.loop_start() @@ -909,7 +965,7 @@ def test_subscription_identifiers(self): laclient.subscribe(topics[0], qos=2, properties=sub_properties) lacallback.wait_subscribed() - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() @@ -920,23 +976,23 @@ def test_subscription_identifiers(self): sub_properties.clear() sub_properties.SubscriptionIdentifier = 3 - lbclient.subscribe(topics[0]+"/#", qos=2, properties=sub_properties) + lbclient.subscribe(f"{topics[0]}/#", qos=2, properties=sub_properties) lbclient.publish(topics[0], b"sub identifier test", 1, retain=False) - self.waitfor(lacallback.messages, 1, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) - self.assertEqual(lacallback.messages[0]["message"].properties.SubscriptionIdentifier[0], - 456789, lacallback.messages[0]["message"].properties.SubscriptionIdentifier) + msg = lacallback.messages.get(timeout=DEFAULT_TIMEOUT) + self.assertTrue(lacallback.messages.empty(), lacallback.messages.queue) + self.assertEqual(msg["message"].properties.SubscriptionIdentifier[0], + 456789, msg["message"].properties.SubscriptionIdentifier) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() - self.waitfor(lbcallback.messages, 1, 3) - self.assertEqual(len(lbcallback.messages), 1, lbcallback.messages) - expected_subsids = set([2, 3]) + msg = lbcallback.messages.get(timeout=DEFAULT_TIMEOUT) + self.assertTrue(lbcallback.messages.empty(), lbcallback.messages.queue) + expected_subsids = {2, 3} received_subsids = set( - lbcallback.messages[0]["message"].properties.SubscriptionIdentifier) + msg["message"].properties.SubscriptionIdentifier) self.assertEqual(received_subsids, expected_subsids, received_subsids) lbclient.disconnect() lbcallback.wait_disconnected() @@ -945,12 +1001,12 @@ def test_subscription_identifiers(self): def test_request_response(self): clientid = 'request response' - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.loop_start() - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() @@ -971,19 +1027,17 @@ def test_request_response(self): properties=publish_properties) # client b is the responder - self.waitfor(lbcallback.messages, 1, 3) - self.assertEqual(len(lbcallback.messages), 1, lbcallback.messages) - self.assertEqual(lbcallback.messages[0]["message"].properties.ResponseTopic, topics[0], - lbcallback.messages[0]["message"].properties) - self.assertEqual(lbcallback.messages[0]["message"].properties.CorrelationData, b"334", - lbcallback.messages[0]["message"].properties) + msg = lbcallback.messages.get(timeout=DEFAULT_TIMEOUT) + self.assertEqual(msg["message"].properties.ResponseTopic, topics[0], + msg["message"].properties) + self.assertEqual(msg["message"].properties.CorrelationData, b"334", + msg["message"].properties) - lbclient.publish(lbcallback.messages[0]["message"].properties.ResponseTopic, b"response", 1, - properties=lbcallback.messages[0]["message"].properties) + lbclient.publish(msg["message"].properties.ResponseTopic, b"response", 1, + properties=msg["message"].properties) # client a gets the response - self.waitfor(lacallback.messages, 1, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) + lacallback.messages.get(timeout=DEFAULT_TIMEOUT) laclient.disconnect() lacallback.wait_disconnected() @@ -995,25 +1049,10 @@ def test_request_response(self): def test_client_topic_alias(self): clientid = 'client topic alias' - # no server side topic aliases allowed - laclient, lacallback = self.new_client(clientid+" a") - laclient.connect(host="localhost", port=self._test_broker_port) - connack = lacallback.wait_connected() - laclient.loop_start() - - publish_properties = Properties(PacketTypes.PUBLISH) - publish_properties.TopicAlias = 0 # topic alias 0 not allowed - laclient.publish(topics[0], "topic alias 0", 1, - properties=publish_properties) - - # should get back a disconnect with Topic alias invalid - lacallback.wait_disconnected() - laclient.loop_stop() - connect_properties = Properties(PacketTypes.CONNECT) connect_properties.TopicAliasMaximum = 0 # server topic aliases not allowed connect_properties.SessionExpiryInterval = 99999 - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) connack = lacallback.wait_connected() clientTopicAliasMaximum = 0 @@ -1033,26 +1072,23 @@ def test_client_topic_alias(self): publish_properties.TopicAlias = 1 laclient.publish(topics[0], b"topic alias 1", 1, properties=publish_properties) - self.waitfor(lacallback.messages, 1, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) + lacallback.messages.get(timeout=DEFAULT_TIMEOUT) laclient.publish("", b"topic alias 2", 1, properties=publish_properties) - self.waitfor(lacallback.messages, 2, 3) - self.assertEqual(len(lacallback.messages), 2, lacallback.messages) + lacallback.messages.get(timeout=DEFAULT_TIMEOUT) laclient.disconnect() # should get rid of the topic aliases but not subscriptions lacallback.wait_disconnected() laclient.loop_stop() # check aliases have been deleted - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) laclient.publish(topics[0], b"topic alias 3", 1) - self.waitfor(lacallback.messages, 1, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) + lacallback.messages.get(timeout=DEFAULT_TIMEOUT) publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.TopicAlias = 1 @@ -1070,110 +1106,96 @@ def test_server_topic_alias(self): connect_properties = Properties(PacketTypes.CONNECT) connect_properties.TopicAliasMaximum = serverTopicAliasMaximum - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) - connack = lacallback.wait_connected() + lacallback.wait_connected() laclient.loop_start() - clientTopicAliasMaximum = 0 - if hasattr(connack["properties"], "TopicAliasMaximum"): - clientTopicAliasMaximum = connack["properties"].TopicAliasMaximum laclient.subscribe(topics[0], qos=2) lacallback.wait_subscribed() for qos in range(3): laclient.publish(topics[0], b"topic alias 1", qos) - self.waitfor(lacallback.messages, 3, 3) - self.assertEqual(len(lacallback.messages), 3, lacallback.messages) + msgs = lacallback.get_messages(3) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() # first message should set the topic alias self.assertTrue(hasattr( - lacallback.messages[0]["message"].properties, "TopicAlias"), lacallback.messages[0]["message"].properties) - topicalias = lacallback.messages[0]["message"].properties.TopicAlias + msgs[0]["message"].properties, "TopicAlias"), msgs[0]["message"].properties) + topicalias = msgs[0]["message"].properties.TopicAlias self.assertTrue(topicalias > 0) - self.assertEqual(lacallback.messages[0]["message"].topic, topics[0]) + self.assertEqual(msgs[0]["message"].topic, topics[0]) self.assertEqual( - lacallback.messages[1]["message"].properties.TopicAlias, topicalias) - self.assertEqual(lacallback.messages[1]["message"].topic, "") + msgs[1]["message"].properties.TopicAlias, topicalias) + self.assertEqual(msgs[1]["message"].topic, "") self.assertEqual( - lacallback.messages[2]["message"].properties.TopicAlias, topicalias) - self.assertEqual(lacallback.messages[2]["message"].topic, "") + msgs[2]["message"].properties.TopicAlias, topicalias) + self.assertEqual(msgs[2]["message"].topic, "") serverTopicAliasMaximum = 0 # no server topic alias allowed connect_properties = Properties(PacketTypes.CONNECT) # connect_properties.TopicAliasMaximum = serverTopicAliasMaximum # default is 0 - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) - connack = lacallback.wait_connected() + lacallback.wait_connected() laclient.loop_start() - clientTopicAliasMaximum = 0 - if hasattr(connack["properties"], "TopicAliasMaximum"): - clientTopicAliasMaximum = connack["properties"].TopicAliasMaximum - laclient.subscribe(topics[0], qos=2) lacallback.wait_subscribed() for qos in range(3): laclient.publish(topics[0], b"topic alias 2", qos) - self.waitfor(lacallback.messages, 3, 3) - self.assertEqual(len(lacallback.messages), 3, lacallback.messages) + msgs = lacallback.get_messages(3) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() # No topic aliases self.assertFalse(hasattr( - lacallback.messages[0]["message"].properties, "TopicAlias"), lacallback.messages[0]["message"].properties) + msgs[0]["message"].properties, "TopicAlias"), msgs[0]["message"].properties) self.assertFalse(hasattr( - lacallback.messages[1]["message"].properties, "TopicAlias"), lacallback.messages[1]["message"].properties) + msgs[1]["message"].properties, "TopicAlias"), msgs[1]["message"].properties) self.assertFalse(hasattr( - lacallback.messages[2]["message"].properties, "TopicAlias"), lacallback.messages[2]["message"].properties) + msgs[2]["message"].properties, "TopicAlias"), msgs[2]["message"].properties) serverTopicAliasMaximum = 0 # no server topic alias allowed connect_properties = Properties(PacketTypes.CONNECT) connect_properties.TopicAliasMaximum = serverTopicAliasMaximum # default is 0 - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) - connack = lacallback.wait_connected() + lacallback.wait_connected() laclient.loop_start() - clientTopicAliasMaximum = 0 - if hasattr(connack["properties"], "TopicAliasMaximum"): - clientTopicAliasMaximum = connack["properties"].TopicAliasMaximum - laclient.subscribe(topics[0], qos=2) lacallback.wait_subscribed() for qos in range(3): laclient.publish(topics[0], b"topic alias 3", qos) - self.waitfor(lacallback.messages, 3, 3) - self.assertEqual(len(lacallback.messages), 3, lacallback.messages) + msgs = lacallback.get_messages(3) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() # No topic aliases self.assertFalse(hasattr( - lacallback.messages[0]["message"].properties, "TopicAlias"), lacallback.messages[0]["message"].properties) + msgs[0]["message"].properties, "TopicAlias"), msgs[0]["message"].properties) self.assertFalse(hasattr( - lacallback.messages[1]["message"].properties, "TopicAlias"), lacallback.messages[1]["message"].properties) + msgs[1]["message"].properties, "TopicAlias"), msgs[1]["message"].properties) self.assertFalse(hasattr( - lacallback.messages[2]["message"].properties, "TopicAlias"), lacallback.messages[2]["message"].properties) + msgs[2]["message"].properties, "TopicAlias"), msgs[2]["message"].properties) def test_maximum_packet_size(self): clientid = 'maximum packet size' # 1. server max packet size - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) connack = lacallback.wait_connected() laclient.loop_start() @@ -1188,8 +1210,6 @@ def test_maximum_packet_size(self): laclient.publish(topics[0], payload, 0) # should get back a disconnect with packet size too big response = lacallback.wait_disconnected() - self.assertEqual(len(lacallback.disconnecteds), - 0, lacallback.disconnecteds) self.assertEqual(response["reasonCode"].getName(), "Packet too large", response["reasonCode"].getName()) else: @@ -1202,7 +1222,7 @@ def test_maximum_packet_size(self): connect_properties = Properties(PacketTypes.CONNECT) connect_properties.MaximumPacketSize = maximumPacketSize - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) connack = lacallback.wait_connected() laclient.loop_start() @@ -1217,14 +1237,16 @@ def test_maximum_packet_size(self): # send a small enough packet, should get this one back payload = b"."*(int(maximumPacketSize/2)) laclient.publish(topics[0], payload, 0) - self.waitfor(lacallback.messages, 1, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) + lacallback.messages.get(timeout=DEFAULT_TIMEOUT) # send a packet too big to receive payload = b"."*maximumPacketSize laclient.publish(topics[0], payload, 1) - self.waitfor(lacallback.messages, 2, 3) - self.assertEqual(len(lacallback.messages), 1, lacallback.messages) + try: + lacallback.messages.get(timeout=WAIT_NON_EVENT_TIMEOUT) + raise ValueError("unexpected message received") + except queue.Empty: + pass laclient.disconnect() lacallback.wait_disconnected() @@ -1260,7 +1282,7 @@ def test_will_delay(self): will_properties.WillDelayInterval = 3 # in seconds connect_properties.SessionExpiryInterval = 5 - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.will_set( topics[0], payload=b"test_will_delay will message", properties=will_properties) laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) @@ -1269,7 +1291,7 @@ def test_will_delay(self): self.assertEqual(connack["flags"]["session present"], False) laclient.loop_start() - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) connack = lbcallback.wait_connected() lbclient.loop_start() @@ -1281,13 +1303,12 @@ def test_will_delay(self): laclient.loop_stop() laclient.socket().close() start = time.time() - while lbcallback.messages == []: - time.sleep(.1) + msg = lbcallback.messages.get(DEFAULT_TIMEOUT) duration = time.time() - start self.assertAlmostEqual(duration, 4, delta=1) - self.assertEqual(lbcallback.messages[0]["message"].topic, topics[0]) + self.assertEqual(msg["message"].topic, topics[0]) self.assertEqual( - lbcallback.messages[0]["message"].payload, b"test_will_delay will message") + msg["message"].payload, b"test_will_delay will message") lbclient.disconnect() lbcallback.wait_disconnected() @@ -1296,10 +1317,10 @@ def test_will_delay(self): def test_shared_subscriptions(self): clientid = 'shared subscriptions' - shared_sub_topic = '$share/sharename/' + topic_prefix + 'x' - shared_pub_topic = topic_prefix + 'x' + shared_sub_topic = f"$share/sharename/{topic_prefix}x" + shared_pub_topic = f"{topic_prefix}x" - laclient, lacallback = self.new_client(clientid+" a") + laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) connack = lacallback.wait_connected() laclient.loop_start() @@ -1309,9 +1330,9 @@ def test_shared_subscriptions(self): laclient.subscribe( [(shared_sub_topic, SubscribeOptions(2)), (topics[0], SubscribeOptions(2))]) - response = lacallback.wait_subscribed() + lacallback.wait_subscribed() - lbclient, lbcallback = self.new_client(clientid+" b") + lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) connack = lbcallback.wait_connected() lbclient.loop_start() @@ -1321,35 +1342,56 @@ def test_shared_subscriptions(self): lbclient.subscribe( [(shared_sub_topic, SubscribeOptions(2)), (topics[0], 2)]) - response = lbcallback.wait_subscribed() + lbcallback.wait_subscribed() lacallback.clear() lbcallback.clear() count = 1 for i in range(count): - lbclient.publish(topics[0], "message "+str(i), 0) - j = 0 - while len(lacallback.messages) + len(lbcallback.messages) < 2*count and j < 20: - time.sleep(.1) - j += 1 - time.sleep(1) - self.assertEqual(len(lacallback.messages), count) - self.assertEqual(len(lbcallback.messages), count) + lbclient.publish(topics[0], f"message {i}", 0) + + lacallback.get_messages(count) + lbcallback.get_messages(count) + + self.assertTrue(lacallback.messages.empty()) + self.assertTrue(lbcallback.messages.empty()) lacallback.clear() lbcallback.clear() for i in range(count): - lbclient.publish(shared_pub_topic, "message "+str(i), 0) - j = 0 - while len(lacallback.messages) + len(lbcallback.messages) < count and j < 20: - time.sleep(.1) - j += 1 - time.sleep(1) + lbclient.publish(shared_pub_topic, f"message {i}", 0) # Each message should only be received once - self.assertEqual(len(lacallback.messages) + - len(lbcallback.messages), count) + result = [] + deadline = time.time() + DEFAULT_TIMEOUT + while len(result) < count and time.time() < deadline: + get_timeout = deadline - time.time() + try: + if get_timeout <= 0: + result.append(lacallback.messages.get_nowait()) + else: + result.append(lacallback.messages.get(timeout=get_timeout)) + except queue.Empty: + # The message could be sent to other client, so empty queue + # could be normal + pass + + try: + get_timeout = deadline - time.time() + if get_timeout <= 0: + result.append(lbcallback.messages.get_nowait()) + else: + result.append(lbcallback.messages.get(timeout=get_timeout)) + except queue.Empty: + # The message could be sent to other client, so empty queue + # could be normal + pass + + self.assertEqual( + {x["message"].payload for x in result}, + {f"message {i}".encode() for i in range(count)} + ) laclient.disconnect() lacallback.wait_disconnected() diff --git a/tests/test_reasoncodes.py b/tests/test_reasoncodes.py new file mode 100644 index 00000000..24f7ff32 --- /dev/null +++ b/tests/test_reasoncodes.py @@ -0,0 +1,47 @@ +import pytest +from paho.mqtt.packettypes import PacketTypes +from paho.mqtt.reasoncodes import ReasonCode, ReasonCodes + + +class TestReasonCode: + def test_equality(self): + rc_success = ReasonCode(PacketTypes.CONNACK, "Success") + assert rc_success == 0 + assert rc_success == "Success" + assert rc_success != "Protocol error" + assert rc_success == ReasonCode(PacketTypes.CONNACK, "Success") + + rc_protocol_error = ReasonCode(PacketTypes.CONNACK, "Protocol error") + assert rc_protocol_error == 130 + assert rc_protocol_error == "Protocol error" + assert rc_protocol_error != "Success" + assert rc_protocol_error == ReasonCode(PacketTypes.CONNACK, "Protocol error") + + def test_comparison(self): + rc_success = ReasonCode(PacketTypes.CONNACK, "Success") + rc_protocol_error = ReasonCode(PacketTypes.CONNACK, "Protocol error") + + assert not rc_success > 0 + assert rc_protocol_error > 0 + assert not rc_success != 0 + assert rc_protocol_error != 0 + + def test_compatibility(self): + rc_success = ReasonCode(PacketTypes.CONNACK, "Success") + with pytest.deprecated_call(): + rc_success_old = ReasonCodes(PacketTypes.CONNACK, "Success") + assert rc_success == rc_success_old + + assert isinstance(rc_success, ReasonCode) + assert isinstance(rc_success_old, ReasonCodes) + # User might use isinstance with the old name (plural) + # while the library give them a ReasonCode (singular) in the callbacks + assert isinstance(rc_success, ReasonCodes) + # The other way around is probably never used... but still support it + assert isinstance(rc_success_old, ReasonCode) + + # Check that isinstance implementation don't always return True + assert not isinstance(rc_success, dict) + assert not isinstance(rc_success_old, dict) + assert not isinstance({}, ReasonCode) + assert not isinstance({}, ReasonCodes) diff --git a/tests/test_websocket_integration.py b/tests/test_websocket_integration.py index 86388dc5..b44f3475 100644 --- a/tests/test_websocket_integration.py +++ b/tests/test_websocket_integration.py @@ -1,15 +1,15 @@ import base64 import hashlib import re +import socketserver from collections import OrderedDict -import pytest -from six.moves import socketserver -from testsupport.broker import fake_websocket_broker - import paho.mqtt.client as client +import pytest from paho.mqtt.client import WebsocketConnectionError +from tests.testsupport.broker import fake_websocket_broker # noqa: F401 + @pytest.fixture def init_response_headers(): @@ -32,7 +32,7 @@ def get_websocket_response(response_headers): """ response = "\r\n".join([ "HTTP/1.1 101 Switching Protocols", - "\r\n".join("{}: {}".format(i, j) for i, j in response_headers.items()), + "\r\n".join(f"{i}: {j}" for i, j in response_headers.items()), "\r\n", ]).encode("utf8") @@ -43,11 +43,12 @@ def get_websocket_response(response_headers): (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) -class TestInvalidWebsocketResponse(object): +class TestInvalidWebsocketResponse: def test_unexpected_response(self, proto_ver, proto_name, fake_websocket_broker): """ Server responds with a valid code, but it's not what the client expected """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_unexpected_response", protocol=proto_ver, transport="websockets" @@ -56,11 +57,10 @@ def test_unexpected_response(self, proto_ver, proto_name, fake_websocket_broker) class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): # Respond with data passed in to serve() - _self.request.sendall("200 OK".encode("utf8")) + _self.request.sendall(b"200 OK") - with fake_websocket_broker.serve(WebsocketHandler): - with pytest.raises(WebsocketConnectionError) as exc: - mqttc.connect("localhost", 1888, keepalive=10) + with fake_websocket_broker.serve(WebsocketHandler), pytest.raises(WebsocketConnectionError) as exc: + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) assert str(exc.value) == "WebSocket handshake error" @@ -69,7 +69,7 @@ def handle(_self): (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) -class TestBadWebsocketHeaders(object): +class TestBadWebsocketHeaders: """ Testing for basic functionality in checking for headers """ def _get_basic_handler(self, response_headers): @@ -82,7 +82,7 @@ def _get_basic_handler(self, response_headers): class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): self.data = _self.request.recv(1024).strip() - print("Received '{:s}'".format(self.data.decode("utf8"))) + print('Received', self.data.decode('utf8')) # Respond with data passed in to serve() _self.request.sendall(response) @@ -93,6 +93,7 @@ def test_no_upgrade(self, proto_ver, proto_name, fake_websocket_broker, """ Server doesn't respond with 'connection: upgrade' """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_no_upgrade", protocol=proto_ver, transport="websockets" @@ -101,9 +102,8 @@ def test_no_upgrade(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers["Connection"] = "bad" response = self._get_basic_handler(init_response_headers) - with fake_websocket_broker.serve(response): - with pytest.raises(WebsocketConnectionError) as exc: - mqttc.connect("localhost", 1888, keepalive=10) + with fake_websocket_broker.serve(response), pytest.raises(WebsocketConnectionError) as exc: + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) assert str(exc.value) == "WebSocket handshake error, connection not upgraded" @@ -112,6 +112,7 @@ def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, """ Server doesn't give anything after connection: upgrade """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_bad_secret_key", protocol=proto_ver, transport="websockets" @@ -119,9 +120,8 @@ def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, response = self._get_basic_handler(init_response_headers) - with fake_websocket_broker.serve(response): - with pytest.raises(WebsocketConnectionError) as exc: - mqttc.connect("localhost", 1888, keepalive=10) + with fake_websocket_broker.serve(response), pytest.raises(WebsocketConnectionError) as exc: + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) assert str(exc.value) == "WebSocket handshake error, invalid secret key" @@ -130,7 +130,7 @@ def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) -class TestValidHeaders(object): +class TestValidHeaders: """ Testing for functionality in request/response headers """ def _get_callback_handler(self, response_headers, check_request=None): @@ -141,7 +141,7 @@ def _get_callback_handler(self, response_headers, check_request=None): class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): self.data = _self.request.recv(1024).strip() - print("Received '{:s}'".format(self.data.decode("utf8"))) + print('Received', self.data.decode('utf8')) decoded = self.data.decode("utf8") @@ -152,8 +152,8 @@ def handle(_self): GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" key = re.search("sec-websocket-key: ([A-Za-z0-9+/=]*)", decoded, re.IGNORECASE).group(1) - to_hash = "{:s}{:s}".format(key, GUID) - hashed = hashlib.sha1(to_hash.encode("utf8")) + to_hash = f"{key:s}{GUID:s}" + hashed = hashlib.sha1(to_hash.encode("utf8")) # noqa: S324 encoded = base64.b64encode(hashed.digest()).decode("utf8") response_headers["Sec-WebSocket-Accept"] = encoded @@ -171,6 +171,7 @@ def test_successful_connection(self, proto_ver, proto_name, """ Connect successfully, on correct path """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_successful_connection", protocol=proto_ver, transport="websockets" @@ -179,7 +180,7 @@ def test_successful_connection(self, proto_ver, proto_name, response = self._get_callback_handler(init_response_headers) with fake_websocket_broker.serve(response): - mqttc.connect("localhost", 1888, keepalive=10) + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() @@ -193,6 +194,7 @@ def test_correct_path(self, proto_ver, proto_name, fake_websocket_broker, """ Make sure it can connect on user specified paths """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_correct_path", protocol=proto_ver, transport="websockets" @@ -205,7 +207,7 @@ def test_correct_path(self, proto_ver, proto_name, fake_websocket_broker, def check_path_correct(decoded): # Make sure it connects to the right path if mqtt_path: - assert re.search("GET {:s} HTTP/1.1".format(mqtt_path), decoded, re.IGNORECASE) is not None + assert re.search(f"GET {mqtt_path} HTTP/1.1", decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, @@ -213,7 +215,7 @@ def check_path_correct(decoded): ) with fake_websocket_broker.serve(response): - mqttc.connect("localhost", 1888, keepalive=10) + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() @@ -228,6 +230,7 @@ def test_correct_auth(self, proto_ver, proto_name, fake_websocket_broker, """ Make sure it sends the right auth headers """ mqttc = client.Client( + client.CallbackAPIVersion.VERSION1, "test_correct_path", protocol=proto_ver, transport="websockets" @@ -240,8 +243,8 @@ def test_correct_auth(self, proto_ver, proto_name, fake_websocket_broker, def check_headers_used(decoded): # Make sure it connects to the right path if auth_headers: - for h in auth_headers: - assert re.search("{:s}: {:s}".format(h, auth_headers[h]), decoded, re.IGNORECASE) is not None + for k, v in auth_headers.items(): + assert f"{k}: {v}" in decoded response = self._get_callback_handler( init_response_headers, @@ -249,6 +252,6 @@ def check_headers_used(decoded): ) with fake_websocket_broker.serve(response): - mqttc.connect("localhost", 1888, keepalive=10) + mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() diff --git a/tests/test_websockets.py b/tests/test_websockets.py index 6aa55f72..78a7cd4a 100644 --- a/tests/test_websockets.py +++ b/tests/test_websockets.py @@ -1,20 +1,92 @@ import socket -import sys - -if sys.version_info < (3, 0): - from mock import Mock -else: - from unittest.mock import Mock +from unittest.mock import Mock import pytest - -from paho.mqtt.client import WebsocketConnectionError, WebsocketWrapper +from paho.mqtt.client import WebsocketConnectionError, _WebsocketWrapper -class TestHeaders(object): +class TestHeaders: """ Make sure headers are used correctly """ - def test_normal_headers(self): + @pytest.mark.parametrize("wargs,expected_sent", [ + ( + # HTTPS on non-default port + { + "host": "testhost.com", + "port": 1234, + "path": "/mqtt", + "extra_headers": None, + "is_ssl": True, + }, + [ + "GET /mqtt HTTP/1.1", + "Host: testhost.com:1234", + "Upgrade: websocket", + "Connection: Upgrade", + "Sec-Websocket-Protocol: mqtt", + "Sec-Websocket-Version: 13", + "Origin: https://testhost.com:1234", + ], + ), + ( + # HTTPS on default port + { + "host": "testhost.com", + "port": 443, + "path": "/mqtt", + "extra_headers": None, + "is_ssl": True, + }, + [ + "GET /mqtt HTTP/1.1", + "Host: testhost.com", + "Upgrade: websocket", + "Connection: Upgrade", + "Sec-Websocket-Protocol: mqtt", + "Sec-Websocket-Version: 13", + "Origin: https://testhost.com", + ], + ), + ( + # HTTP on default port + { + "host": "testhost.com", + "port": 80, + "path": "/mqtt", + "extra_headers": None, + "is_ssl": False, + }, + [ + "GET /mqtt HTTP/1.1", + "Host: testhost.com", + "Upgrade: websocket", + "Connection: Upgrade", + "Sec-Websocket-Protocol: mqtt", + "Sec-Websocket-Version: 13", + "Origin: http://testhost.com", + ], + ), + ( + # HTTP on non-default port + { + "host": "testhost.com", + "port": 443, # This isn't the default *HTTP* port. It's on purpose to use httpS port + "path": "/mqtt", + "extra_headers": None, + "is_ssl": False, + }, + [ + "GET /mqtt HTTP/1.1", + "Host: testhost.com:443", + "Upgrade: websocket", + "Connection: Upgrade", + "Sec-Websocket-Protocol: mqtt", + "Sec-Websocket-Version: 13", + "Origin: http://testhost.com:443", + ], + ), + ]) + def test_normal_headers(self, wargs, expected_sent): """ Normal headers as specified in RFC 6455 """ response = [ @@ -30,16 +102,13 @@ def iter_response(): for i in "\r\n".join(response).encode("utf8"): yield i - for i in "\r\n".encode("utf8"): + for i in b"\r\n": yield i it = iter_response() def fakerecv(*args): - if sys.version_info < (3, 0): - return next(it) - else: - return bytes([next(it)]) + return bytes([next(it)]) mocksock = Mock( spec_set=socket.socket, @@ -47,34 +116,27 @@ def fakerecv(*args): send=Mock(), ) - wargs = { - "host": "testhost.com", - "port": 1234, - "path": "/mqtt", - "extra_headers": None, - "is_ssl": True, - "socket": mocksock, - } + # Do a copy to avoid modifying input + wargs_with_socket = dict(wargs) + wargs_with_socket["socket"] = mocksock with pytest.raises(WebsocketConnectionError) as exc: - WebsocketWrapper(**wargs) + _WebsocketWrapper(**wargs_with_socket) # We're not creating the response hash properly so it should raise this # error assert str(exc.value) == "WebSocket handshake error, invalid secret key" - expected_sent = [i.format(**wargs) for i in [ - "GET {path:s} HTTP/1.1", - "Host: {host:s}", - "Upgrade: websocket", - "Connection: Upgrade", - "Sec-Websocket-Protocol: mqtt", - "Sec-Websocket-Version: 13", - "Origin: https://{host:s}:{port:d}", - ]] - # Only sends the header once assert mocksock.send.call_count == 1 - for i in expected_sent: - assert i in mocksock.send.call_args[0][0].decode("utf8") + got_lines = mocksock.send.call_args[0][0].decode("utf8").splitlines() + + # First line must be the GET line + # 2nd line is required to be Host (rfc9110 said that it SHOULD be first header) + assert expected_sent[0] == got_lines[0] + assert expected_sent[1] == got_lines[1] + + # Other line order don't matter + for line in expected_sent: + assert line in got_lines diff --git a/tests/testsupport/broker.py b/tests/testsupport/broker.py index 92b80451..e08cf73e 100644 --- a/tests/testsupport/broker.py +++ b/tests/testsupport/broker.py @@ -1,30 +1,43 @@ import contextlib +import os import socket +import socketserver import threading import pytest -from six.moves import socketserver + +from tests import paho_test class FakeBroker: - def __init__(self): - # Bind to "localhost" for maximum performance, as described in: - # http://docs.python.org/howto/sockets.html#ipc - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - sock.settimeout(30) - sock.bind(("localhost", 1888)) + def __init__(self, transport): + if transport == "tcp": + # Bind to "localhost" for maximum performance, as described in: + # http://docs.python.org/howto/sockets.html#ipc + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + sock.bind(("localhost", 0)) + self.port = sock.getsockname()[1] + elif transport == "unix": + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.bind("localhost") + self.port = 1883 + else: + raise ValueError(f"unsupported transport {transport}") + + sock.settimeout(5) sock.listen(1) self._sock = sock self._conn = None + self.transport = transport def start(self): if self._sock is None: raise ValueError('Socket is not open') (conn, address) = self._sock.accept() - conn.settimeout(10) + conn.settimeout(5) self._conn = conn def finish(self): @@ -36,6 +49,12 @@ def finish(self): self._sock.close() self._sock = None + if self.transport == 'unix': + try: + os.unlink('localhost') + except OSError: + pass + def receive_packet(self, num_bytes): if self._conn is None: raise ValueError('Connection is not open') @@ -50,11 +69,17 @@ def send_packet(self, packet_out): count = self._conn.send(packet_out) return count + def expect_packet(self, name, packet): + if self._conn is None: + raise ValueError('Connection is not open') -@pytest.fixture -def fake_broker(): + paho_test.expect_packet(self._conn, name, packet) + + +@pytest.fixture(params=["tcp"] + (["unix"] if hasattr(socket, 'AF_UNIX') else [])) +def fake_broker(request): # print('Setup broker') - broker = FakeBroker() + broker = FakeBroker(request.param) yield broker @@ -68,10 +93,10 @@ class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): class FakeWebsocketBroker(threading.Thread): def __init__(self): - super(FakeWebsocketBroker, self).__init__() + super().__init__() self.host = "localhost" - self.port = 1888 + self.port = -1 # Will be set by `serve()` self._server = None self._running = True @@ -79,10 +104,11 @@ def __init__(self): @contextlib.contextmanager def serve(self, tcphandler): - self._server = ThreadedTCPServer((self.host, self.port), tcphandler) + self._server = ThreadedTCPServer((self.host, 0), tcphandler) try: self.start() + self.port = self._server.server_address[1] if not self._running: raise RuntimeError("Error starting server") @@ -99,8 +125,6 @@ def run(self): @pytest.fixture def fake_websocket_broker(): - socketserver.TCPServer.allow_reuse_address = True - broker = FakeWebsocketBroker() yield broker diff --git a/tox.ini b/tox.ini index f5e39787..d5963354 100644 --- a/tox.ini +++ b/tox.ini @@ -1,19 +1,29 @@ [tox] -envlist = py{27,35,36,37,38,39} - -[testenv:py27] -setenv = EXCLUDE = --exclude=./.*,./examples/loop_asyncio.py,*/MQTTV5.py,*/MQTTV311.py +envlist = py{37,38,39,310,311,312} [testenv] -whitelist_externals = echo make deps = - -rrequirements.txt - flake8 + pytest + pytest-cov +commands = + pytest --cov=. --cov={envsitepackagesdir}/paho {posargs} + coverage xml -o coverage.xml +env = + PYTHONDEVMODE=1 + +[testenv:lint] +deps = + -e .[proxy] + dnspython + black + codespell + mypy + pre-commit + safety commands = - # $EXCLUDE is defined above in testenv:py27 as a workaround for Python 2 - # which does not support asyncio and type hints - flake8 . --count --select=E9,F63,F7,F822,F823 --show-source --statistics {env:EXCLUDE:} - python setup.py test - make -C test test - # TODO (cclauss) Fix up all these undefined names - flake8 . --count --exit-zero --select=F821 --show-source --statistics + # The "-" in front of command tells tox to ignore errors + pre-commit run --all-files + - black --check src + - codespell + mypy src + safety check