pax_global_header00006660000000000000000000000064145616772510014530gustar00rootroot0000000000000052 comment=ebffdc2ca0a43fd87dfd270bbb16007591139626 paho.mqtt.python-2.0.0/000077500000000000000000000000001456167725100147625ustar00rootroot00000000000000paho.mqtt.python-2.0.0/.github/000077500000000000000000000000001456167725100163225ustar00rootroot00000000000000paho.mqtt.python-2.0.0/.github/ISSUE_TEMPLATE/000077500000000000000000000000001456167725100205055ustar00rootroot00000000000000paho.mqtt.python-2.0.0/.github/ISSUE_TEMPLATE/bug_report.md000066400000000000000000000041461456167725100232040ustar00rootroot00000000000000--- 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) paho.mqtt.python-2.0.0/.github/ISSUE_TEMPLATE/feature_request.md000066400000000000000000000025251456167725100242360ustar00rootroot00000000000000--- 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?* paho.mqtt.python-2.0.0/.github/ISSUE_TEMPLATE/question.md000066400000000000000000000036161456167725100227040ustar00rootroot00000000000000--- 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):paho.mqtt.python-2.0.0/.github/workflows/000077500000000000000000000000001456167725100203575ustar00rootroot00000000000000paho.mqtt.python-2.0.0/.github/workflows/build.yml000066400000000000000000000006631456167725100222060ustar00rootroot00000000000000name: 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 paho.mqtt.python-2.0.0/.github/workflows/label-issue.yml000066400000000000000000000011251456167725100233060ustar00rootroot00000000000000# This workflow uses actions that are not certified by GitHub. # They are provided by a third-party and are governed by # separate terms of service, privacy policy, and support # documentation. name: Label issues on: issues: types: - reopened - opened jobs: label_issues: runs-on: ubuntu-latest timeout-minutes: 5 permissions: issues: write steps: - name: Label issues uses: andymckay/labeler@5c59dabdfd4dd5bd9c6e6d255b01b9d764af4414 with: add-labels: "Status: Available" repo-token: ${{ secrets.GITHUB_TOKEN }} paho.mqtt.python-2.0.0/.github/workflows/lint-python.yml000066400000000000000000000004541456167725100233720ustar00rootroot00000000000000name: lint_python 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 tox - run: tox -e lint paho.mqtt.python-2.0.0/.github/workflows/precommit.yml000066400000000000000000000010111456167725100230720ustar00rootroot00000000000000# 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 paho.mqtt.python-2.0.0/.github/workflows/tox.yml000066400000000000000000000015351456167725100217200ustar00rootroot00000000000000name: tox on: pull_request: branches: [master] push: branches: [master] jobs: tox: strategy: fail-fast: false max-parallel: 4 matrix: python: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"] runs-on: ubuntu-latest timeout-minutes: 5 steps: - 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 - run: tox -e py - uses: codecov/codecov-action@v3 with: token: ${{ secrets.CODECOV_TOKEN }} paho.mqtt.python-2.0.0/.gitignore000066400000000000000000000014721456167725100167560ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python /env/ /build/ /develop-eggs/ /dist/ /downloads/ /eggs/ /.eggs/ /lib/ /lib64/ /parts/ /sdist/ /var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. MANIFEST *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover .hypothesis/ tests/ssl/demoCA tests/ssl/rootCA tests/ssl/signingCA *.csr # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ paho.mqtt.testing paho.mqtt.python-2.0.0/.pre-commit-config.yaml000066400000000000000000000020261456167725100212430ustar00rootroot00000000000000# 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 paho.mqtt.python-2.0.0/CODE_OF_CONDUCT.md000066400000000000000000000231141456167725100175620ustar00rootroot00000000000000# 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.paho.mqtt.python-2.0.0/CONTRIBUTING.md000066400000000000000000000132041456167725100172130ustar00rootroot00000000000000# Contributing to Paho Thanks for your interest in this project! You can contribute bugfixes and new features by sending pull requests through GitHub. ## Legal In order for your contribution to be accepted, it must comply with the Eclipse Foundation IP policy. Please read the [Eclipse Foundation policy on accepting contributions via Git](http://wiki.eclipse.org/Development_Resources/Contributing_via_Git). 1. Sign the [Eclipse CLA](http://www.eclipse.org/legal/CLA.php) 1. Register for an Eclipse Foundation User ID. You can register [here](https://dev.eclipse.org/site_login/createaccount.php). 2. Log into the [Projects Portal](https://projects.eclipse.org/), and click on the '[Eclipse CLA](https://projects.eclipse.org/user/sign/cla)' link. 2. Go to your [account settings](https://dev.eclipse.org/site_login/myaccount.php#open_tab_accountsettings) and add your GitHub username to your account. 3. Make sure that you _sign-off_ your Git commits in the following format: ``` Signed-off-by: John Smith ``` This is usually at the bottom of the commit message. You can automate this by adding the '-s' flag when you make the commits. e.g. ```git commit -s -m "Adding a cool feature"``` 4. Ensure that the email address that you make your commits with is the same one you used to sign up to the Eclipse Foundation website with. ## Contributing a change 1. [Fork the repository on GitHub](https://github.com/eclipse/paho.mqtt.python/fork) 2. Clone the forked repository onto your computer: ``` git clone https://github.com//paho.mqtt.python.git ``` 3. Most changes will go to branch ``master``. This include both bug fixes and new features. Bug fixes are committed to ``master`` and if required, cherry-picked to the release branch. The only changes that goes directly to the release branch (``1.4``, ``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 with ```git checkout -b YOUR_BRANCH_NAME origin/master``` 5. Make your changes 6. Ensure that all new and existing tests pass by running ```tox``` 7. Commit the changes into the branch: ``` git commit -s ``` Make sure that your commit message is meaningful and describes your changes correctly. 8. If you have a lot of commits for the change, squash them into a single / few commits. 9. Push the changes in your branch to your forked repository. 10. Finally, go to [https://github.com/eclipse/paho.mqtt.python](https://github.com/eclipse/paho.mqtt.python) and create a pull request from your "YOUR_BRANCH_NAME" branch to the ``master`` (or release branch if applicable) to request review and merge of the commits in your pushed branch. What happens next depends on the content of the patch. If it is 100% authored by the contributor and is less than 1000 lines (and meets the needs of the project), then it can be pulled into the main repository. If not, more steps are required. These are detailed in the [legal process poster](http://www.eclipse.org/legal/EclipseLegalProcessPoster.pdf). ## Developer resources: Information regarding source code management, builds, coding standards, and more. - [https://projects.eclipse.org/projects/iot.paho/developer](https://projects.eclipse.org/projects/iot.paho/developer) Contact: -------- Contact the project developers via the project's development [mailing list](https://dev.eclipse.org/mailman/listinfo/paho-dev). Search for bugs: ---------------- This project uses [Github](https://github.com/eclipse/paho.mqtt.python/issues) to track ongoing development and issues. Create a new bug: ----------------- 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: * 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. * Bump the version number in ``paho/mqtt/__init__.py``, commit the change. * Make a dry-run of build: * Build using hatch: ``python -m hatch 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 hatch 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``) paho.mqtt.python-2.0.0/ChangeLog.txt000066400000000000000000000347141456167725100173630ustar00rootroot00000000000000v2.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 migrations.md 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 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 now raise exceptions if called when the publish call produced an error. Closes #550. - Remove periodic retry checks for outgoing messages with QoS>0. This means that outgoing messages will only be retried on the client reconnecting to the server. They will *not* be retried when the client is still connected. - The `rc` parameter in the `on_disconnect` callback now has meaningful values in the case of an error. Closes #441. - Callbacks can now be applied to client instances using decorators. - PUBACK messages are now sent to the broker only after the on_message callback has returned. - Raise exceptions when attempting to set MQTT v5 properties to forbidden values. Closes #586. - Callbacks can now be updated from within a callback. - Remove _out_packet_mutex and _current_out_packet_mutex and convert the _out_packet queue use to thread safe. - Add basic MQTT v5 support to the subscribe and publish helper functions. Closes #575. - Fix on_disconnect() sometimes calling the MQTT v3.x callback when it should call the MQTT v5 callback. Closes #570. - Big performance improvement when receiving large payloads, particularly for SSL. Closes #571, - Fix connecting with MQTT v5 to a broker that doesn't support MQTT v5. Closes #566. - Removed ancient Mosquitto compatibility class. - Fix exception on calling Client(client_id="", clean_session=False). Closes #520. - Experimental support for Websockets continuation frames. Closes #500. Closes #89. - `Properties.json()` now converts Correlation Data bytes() objects to hex. Closes #555. - Only use the internal sockpair wakeup when running with loop_start() or loop(). This removes problems when running with an external event loop. - Drain all of sockpairR bytes to avoid unnecessary wakeups and possible timeouts. Closes #563. - Add timeout to MQTTMessageInfo:wait_for_publish(). 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``. Closes #365. - Fix PUBREL remaining length of > 2 not being accepted for MQTT v5 message flows. Closes #481. - Fix MQTT v5 properties not being sent on retried or queued messages. - Fix errors related to detection of MQTT v5 first connections. - Fix for changes related to Python 3.9. v1.5.0 - 2019-10-30 =================== * Add support for clean_session on subscribe helper. Closes #219 * Add support for non-standard bridge connection. Closes #282 * Fix hang with QoS 2 message and clean_session = False. The fix replace hang with message DROP. See README for known limitation. Closes #284 and #286 * Fix connection establishement timeout. Closes #291 and #288 * Add support for connecting through a proxy. Closes #315 * Add MQTT v5 support. Closes #334 * Improve error message when sending queue is full. Closes #378 * Improve error handling during initialization on edge case. Closes #387 and #388 * Allow to specify local client port used (similar to bind_address). Closes #390 * Add method is_connected to know if MQTT connection is established. Closes #414 * Set connection timeout to keepalive. Closes #425 v1.4.0 - 2018-09-02 =================== - Fix hang when client restarted and broker resumed a session with Qos2 message. Closes #284. Note: this change replace the hang by a message lost ! See README for current limitation of paho-mqtt. - Fix reconnection loop when a clean_session=True client reconnect while Qos2 message are being sent. Note: this change replace the infinite reconnection loop by a possible duplicate QoS2 message. Only clean_session=True client are affected, see README for current limitation of paho-mqtt. - Catch and log any exception raised by user callback. Closes #294. - Improve support for external event loop (like asyncio). Closes #235. - Fix order of message with publish.multiple helper. Closes #87. - Fix hang on wait_for_published() on bad network. Closes #309. - Fix an issue with Websocket that seems to happen only on Windows. Closes #268. - Fix issue with Websocket payload size between 127 and 65536. Closes #267. - Closes socket in client destructor to avoid FD leak. Closes #170. - Fix uncaught timeout exception during connection. Closes #288. - Remove dup flag on PUBREL packet. Closes #298. - Use secure entropy source for Websocket mask_key (urandom). Closes #305. - Fix mid generation that was not thread-safe. - Replace print() statements with proper logging. Closes #214. - Allow insecure TLS on publish and subscribe helpers. Closes #299. - Allow to remove authentication (reset username to None). Closes #259. - Add support for the non-standard bridge mode. Closes #282. v1.3.1 - 2017-10-09 =================== - Fix reconnect_delay_set which ignored the max_delay. Closes #218. - Fix crash when connection is lost while trying to send message. Closes #208. - Fix issue with unicode topic when some character were multi-bytes UTF-8. - Fix issue with empty Client ID with broker that don't support them. Closes #209. - Fix issue with tls_set that did not allowed cert_reqs=ssl.CERT_NONE. Closes #123. - Relax requirement on pytest-runner, it's now only required for tests. Closes #207, #227. v1.3.0 - 2017-06-20 =================== - **BREAKING** Requires Python 2.7 or 3.4+. Closes #163. - **BREAKING** Remove support for SSL without SSLContext (Requires Python 2.7.9+ or 3.2+). Closes #115. - **BREAKING** on_connect callback is now always called flags. Previously this callback could accepts 3 OR 4 arguments, now it must accepts 4. Closes #197. - **BREAKING** tls_insecure_set() must now be called *after* tls_set() - Allow username and password to be zero length (as opposed to not being present). Closes #80. - Allow zero length client ids when using MQTT v3.1.1. - Add SSLContext support, including SNI. Closes #11. - Improved support for unicode topic and binary payload. Closes #15, #16. - Allow arbitrary Websocket headers and path. Closes #169. - Fix issue with large inbound payload over Websocket. Closes #107. - Add exponential delay for reconnection. Closes #195. - Move unit tests to pytest (#164) and tox (#187) - Add support for standard Python logging. Closes #95. - Fix duplicate incoming QoS==2 message. Closes #194. v1.2.3 - 2017-04-20 =================== - Fix possible hang of TLS connection during handshake. Closes #3. - Fix issue with publish helper with TLS connection. Closes #180. - Fix installation issue on non-UTF-8 system. Closes #181. - Fix non-working Websocket over TLS connection. Closes #182. v1.2.2 - 2017-04-11 =================== - Fix message lost when using paho.mqtt.publish helper with QoS=0 message. Closes #172. v1.2.1 - 2017-04-03 =================== - Handle unicode username and passwords correctly. Closes #79. - Fix handling of invalid UTF-8 topics on incoming messages - the library now does not attempt to decode the topic - this will happen when the user accesses msg.topic in the on_message callback. If the topic is not valid UTF-8, an exception will be raised. Closes #75. - Fix issue with WebSocket connection in case of network issue (timeout or connection broken). Closes #105. - Fix issue with SSL connection, where latest incoming message may be delayed or never processed. Closes #131. - Fix possible message lost with publish.single and publish.multiple. Closes #119. v1.2 - 2016-06-03 ================= - Client.publish() now returns an MQTTMessageInfo object. The MQTTMessageInfo object behaves like a tuple of (rc, mid) for backwards compatibility but also provides two functions: is_published() and wait_for_published(). This allows a client to determine whether any given message has been published without need for a callback, and also allows the client to block waiting until the message has been sent. - Further fix for Client constructor for the case where "localhost" is unresolvable. - Add paho.mqtt.subscribe module, with simple() and callback() helper functions. - 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 inheritance. Closes #53, #54. - Add websockets support. - Default MQTT version is again changed to v3.1.1. - Client.subscribe() now accepts unicode type topic inputs on Python 2. Closes #16. - paho.mqtt.publish() now raises an MQTTException on a CONNECT failure, rather than blindly continuing. Closes #6. - Don't block on TLS sockets on Python 3. Closes #2. - Client.publish() now accepts bytes() payloads on Python 3. Closes #1. - Don't attempt to join() own thread. Closes #14. - Allow the use of Client.message_callback_add() from inside callbacks. Closes #12. - Use a monotonic time source for keeping track of time, if available. Closes #56. v1.1 - 2015-01-30 ================= - Add support for wildcard certificates. Closes #440547. - Default connection behaviour has been reverted to MQTT v3.1 instead of v3.1.1. There is as yet insufficient support for v3.1.1 to rely on, and current v3.1 implementations do not return the correct CONNACK code to allow detection of the fault. Closes #451735. - Fix incorrect handling of queued messages after reconnecting. Closes #452672. - Fix possible race condition if the connection in loop_start() does not complete before loop_stop() is called, meaning the network thread never ends. Closes #448428. Thanks to Kees Bakker. v1.0.2 - 2014-09-13 =================== - Fix "protocol" not being used in publish.single() - Fix Client constructor for the case where "localhost" is unresolvable. Closes #439277. - Don't attempt to encode topic to utf-8 twice. Thanks to Luc Milland. - Handle "unicode" type payloads on Python 2.7. Thanks to Luc Milland. - Fix reconnecting after sending more QoS>0 messages than inflight messages is set to, whilst connecting. Closes #443935. Thanks to Hiram van Paassen. - Fix possible race condition when connecting with TLS and publishing at the same time, which could lead to PUBLISH data being sent before any other messages and unencrypted. Closes #443964. Thanks to Hiram van Paassen. - Handle exceptions from select() in client loop() function. Closes #443881. Thanks to Jeff Jasper. v1.0.1 ====== - Fix incorrect reconnect that occurred if calling loop_stop() before disconnect(). v1.0 ==== - Default protocol is now MQTT v3.1.1. - Client will reconnect using MQTT v3.1 if a v3.1.1 connection fails due to the incorrect protocol version number. - All strings are now correctly encoded as utf-8. - Add support for "session present" flag in on_connect callback. v0.9.1 ====== - Fix CONNECT packet for MQTT v3.1.1. - Fix potential lockup when publishing from on_publish callback. - Add version information to paho.mqtt.__version__ paho.mqtt.python-2.0.0/LICENSE.txt000066400000000000000000000002341456167725100166040ustar00rootroot00000000000000This project is dual licensed under the Eclipse Public License 2.0 and the Eclipse Distribution License 1.0 as described in the epl-v20 and edl-v10 files. paho.mqtt.python-2.0.0/Makefile000066400000000000000000000015271456167725100164270ustar00rootroot00000000000000# Set DESTDIR if it isn't given DESTDIR?=/ PYTHON?=python3 .PHONY : all clean clean-build clean-pyc clean-test install test upload all : install : all $(PYTHON) -m pip install -e . clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts clean-build: ## remove build artifacts rm -fr build/ rm -fr dist/ rm -fr .eggs/ find . -name '*.egg-info' -exec rm -fr {} + find . -name '*.egg' -exec rm -f {} + clean-pyc: ## remove Python file artifacts find . -name '*.pyc' -exec rm -f {} + find . -name '*.pyo' -exec rm -f {} + find . -name '*~' -exec rm -f {} + find . -name '__pycache__' -exec rm -fr {} + clean-test: ## remove test and coverage artifacts rm -fr .tox/ rm -f .coverage rm -fr htmlcov/ test : $(PYTHON) -m pytest . upload : test $(PYTHON) -m hatch build $(PYTHON) -m hatch publish paho.mqtt.python-2.0.0/README.rst000066400000000000000000000532131456167725100164550ustar00rootroot00000000000000Eclipse Paho™ MQTT Python Client ================================ The `full documentation is available here `_. 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 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 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`_ * `Network loop`_ * `Callbacks`_ * `Logger`_ * `External event loop support`_ * `Global helper functions`_ * `Publish`_ * `Single`_ * `Multiple`_ * `Subscribe`_ * `Simple`_ * `Using Callback`_ * `Reporting bugs`_ * `More information`_ Installation ------------ The latest stable version is available in the Python Package Index (PyPi) and can be installed using :: pip install paho-mqtt Or with ``virtualenv``: :: virtualenv paho-mqtt source paho-mqtt/bin/activate pip install paho-mqtt To obtain the full code, including examples and tests, you can clone the git repository: :: git clone https://github.com/eclipse/paho.mqtt.python Once you have the code, it can be installed from your repository as well: :: cd paho.mqtt.python pip install -e . 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 features. 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 the client session is lost: * 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 lose this QoS 2 message. * QoS 1 and QoS 2 messages which have been sent to the server, but have not been completely acknowledged. 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 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 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 set ``clean_session = False`` if you need the QoS 2 guarantee of only one delivery. Usage and API ------------- 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 few `helpers` for simple publishing or subscribing. Getting Started *************** Here is a very simple example that subscribes to the broker $SYS topic tree and prints out the resulting messages: .. code:: python 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, 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/#") # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_connect = on_connect mqttc.on_message = on_message 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. mqttc.loop_forever() Client ****** You can use the client class as an instance, within a class or by subclassing. The general usage flow is as follows: * Create a client instance * Connect to a broker using one of the ``connect*()`` functions * Call one of the ``loop*()`` functions to maintain network traffic flow with the broker * Use ``subscribe()`` to subscribe to a topic and receive messages * Use ``publish()`` to publish messages to the broker * Use ``disconnect()`` to disconnect from the broker Callbacks will be called to allow the application to process events as necessary. These callbacks are described below. 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 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_start() / loop_stop() '''''''''''''''''''''''''' .. code:: python 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 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. 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. loop() '''''' .. code:: python run = True while run: rc = mqttc.loop(timeout=1.0) if rc != 0: # need to handle error, possible reconnecting or stopping the application 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. Using this kind of loop, require you to handle reconnection strategie. Callbacks ````````` 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 import paho.mqtt.client as mqtt 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): # 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 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()}") publisher example ''''''''''''''''' .. code:: python import time import paho.mqtt.client as mqtt 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() # Our application produce some messages msg_info = mqttc.publish("paho/test/topic", "my message", qos=1) unacked_publish.add(msg_info.mid) 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) # 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() mqttc.disconnect() mqttc.loop_stop() Logger `````` 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. Example: .. code:: python import logging import paho.mqtt.client as mqtt logging.basicConfig(level=logging.DEBUG) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.enable_logger() mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.loop_start() # Do additional action needed, publish, subscribe, ... [...] It's also possible to define a on_log callback that will receive a copy of all log messages. Example: .. code:: python import paho.mqtt.client as mqtt def on_log(client, userdata, paho_log_level, messages): if paho_log_level == mqtt.LogLevel.MQTT_LOG_ERR: print(message) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_log = on_log mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.loop_start() # Do additional action needed, publish, subscribe, ... [...] The correspondence with Paho logging levels and standard ones is the following: ==================== =============== 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`` ==================== =============== External event loop support ``````````````````````````` To support other network loop like asyncio (see examples_), the library expose some method and callback to support those use-case. The following loop method exists: * `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. In pseudo code, it give the following: .. code:: python 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 The callbacks are always called in this order: - `on_socket_open` - Zero or more times: - `on_socket_register_write` - `on_socket_unregister_write` - `on_socket_close` Global helper functions ``````````````````````` The client module also offers some global helper functions. ``topic_matches_sub(sub, topic)`` can be used to check whether a ``topic`` matches a ``subscription``. For example: the topic ``foo/bar`` would match the subscription ``foo/#`` or ``+/bar`` the topic ``non/matching`` would not match the subscription ``non/+/+`` Publish ******* This module provides some helper functions to allow straightforward publishing 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()`. Both functions include support for MQTT v5.0, but do not currently let you set any properties on connection or when sending messages. Single `````` Publish a single message to a broker, then disconnect cleanly. Example: .. code:: python import paho.mqtt.publish as publish publish.single("paho/test/topic", "payload", hostname="mqtt.eclipseprojects.io") Multiple ```````` Publish multiple messages to a broker, then disconnect cleanly. Example: .. code:: python from paho.mqtt.enums import MQTTProtocolVersion import paho.mqtt.publish as publish 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 ********* This module provides some helper functions to allow straightforward subscribing and processing of messages. 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. Simple `````` Subscribe to a set of topics and return the messages received. This is a blocking function. Example: .. code:: python import paho.mqtt.subscribe as subscribe msg = subscribe.simple("paho/test/topic", hostname="mqtt.eclipseprojects.io") print("%s %s" % (msg.topic, msg.payload)) Using Callback `````````````` Subscribe to a set of topics and process the messages received using a user provided callback. Example: .. code:: python import paho.mqtt.subscribe as subscribe 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/topic", hostname="mqtt.eclipseprojects.io", userdata={"message_count": 0}) Reporting bugs -------------- Please report bugs in the issues tracker at https://github.com/eclipse/paho.mqtt.python/issues. More information ---------------- 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 paho.mqtt.python-2.0.0/SECURITY.md000066400000000000000000000005361456167725100165570ustar00rootroot00000000000000# 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.orgpaho.mqtt.python-2.0.0/Vagrantfile000066400000000000000000000017721456167725100171560ustar00rootroot00000000000000Vagrant.configure("2") do |config| # The base OS config.vm.box = "ubuntu/trusty64" config.vm.provision :shell, :inline => "sudo apt-get update" # Install make config.vm.provision :shell, :inline => "apt-get install -y make" # Provision Python 2 config.vm.provision :shell, :inline => "apt-get upgrade -y python" config.vm.provision :shell, :inline => "apt-get install -y python-pip" config.vm.provision :shell, :inline => "python -m pip install --upgrade pip" config.vm.provision :shell, :inline => "python -m pip install virtualenv" # Provision Python 3 config.vm.provision :shell, :inline => "apt-get install -y python3" config.vm.provision :shell, :inline => "apt-get install -y python3-pip" config.vm.provision :shell, :inline => "python3 -m pip install --upgrade pip" config.vm.provision :shell, :inline => "python3 -m pip install virtualenv" # reassuring message to complete: config.vm.provision "shell", inline: "echo All set!", run: "always" end paho.mqtt.python-2.0.0/about.html000066400000000000000000000037561456167725100167750ustar00rootroot00000000000000 About

About This Content

December 9, 2013

License

The Eclipse Foundation makes available all content in this plug-in ("Content"). Unless otherwise indicated below, the Content is provided to you under the terms and conditions of the Eclipse Public License Version 2.0 ("EPL") and Eclipse Distribution License Version 1.0 ("EDL"). A copy of the EPL is available at http://www.eclipse.org/legal/epl-v20.html and a copy of the EDL is available at http://www.eclipse.org/org/documents/edl-v10.php. For purposes of the EPL, "Program" will mean the Content.

If you did not receive this Content directly from the Eclipse Foundation, the Content is being redistributed by another party ("Redistributor") and different terms and conditions may apply to your use of any object code in the Content. Check the Redistributor's license that was provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise indicated below, the terms and conditions of the EPL still apply to any source code in the Content and such source code may be obtained at http://www.eclipse.org.

Third Party Content

The Content includes items that have been sourced from third parties as set out below. If you did not receive this Content directly from the Eclipse Foundation, the following is provided for informational purposes only, and you should look to the Redistributor's license for terms and conditions of use.

None



paho.mqtt.python-2.0.0/conftest.py000066400000000000000000000001421456167725100171560ustar00rootroot00000000000000# This file ensures pytest keeps this directory in sys.path, # so you can run `pytest tests/lib`. paho.mqtt.python-2.0.0/docs/000077500000000000000000000000001456167725100157125ustar00rootroot00000000000000paho.mqtt.python-2.0.0/docs/Makefile000066400000000000000000000011721456167725100173530ustar00rootroot00000000000000# 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) paho.mqtt.python-2.0.0/docs/changelog.rst000066400000000000000000000000631456167725100203720ustar00rootroot00000000000000Changelog ********* .. include:: ../ChangeLog.txt paho.mqtt.python-2.0.0/docs/client.rst000066400000000000000000000010511456167725100177170ustar00rootroot00000000000000client 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: paho.mqtt.python-2.0.0/docs/conf.py000066400000000000000000000022011456167725100172040ustar00rootroot00000000000000# 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" paho.mqtt.python-2.0.0/docs/helpers.rst000066400000000000000000000002301456167725100201010ustar00rootroot00000000000000helpers ======= .. automodule:: paho.mqtt.publish :members: :undoc-members: .. automodule:: paho.mqtt.subscribe :members: :undoc-members: paho.mqtt.python-2.0.0/docs/index.rst000066400000000000000000000010261456167725100175520ustar00rootroot00000000000000.. 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 paho.mqtt.python-2.0.0/docs/make.bat000066400000000000000000000014401456167725100173160ustar00rootroot00000000000000@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 paho.mqtt.python-2.0.0/docs/migrations.rst000066400000000000000000000203771456167725100206310ustar00rootroot00000000000000Migrations ========== 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 user-callback. To fix some inconsistency in callback arguments and to provide better support for MQTTv5, version 2.0 changed the arguments passed to user-callback. You can still use old version of callback, you are just require to tell paho-mqtt that you opt for this version. For that just change your client creation from:: # OLD code >>> mqttc = mqtt.Client() # NEW code >>> mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1) That it, remaining of the code could stay unchanged. The version 1 of callback is deprecated, but is still supported in version 2.x. If you want to upgrade to newer version of 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 rc == "Unsupported protocol version": # handle bad protocol version if rc == "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` paho.mqtt.python-2.0.0/docs/types.rst000066400000000000000000000003561456167725100176140ustar00rootroot00000000000000Types and enums =============== .. automodule:: paho.mqtt.enums :members: :undoc-members: .. automodule:: paho.mqtt.properties :members: :undoc-members: .. automodule:: paho.mqtt.reasoncodes :members: :undoc-members: paho.mqtt.python-2.0.0/edl-v10000066400000000000000000000030411456167725100160530ustar00rootroot00000000000000Eclipse Distribution License - v 1.0 Copyright (c) 2007, Eclipse Foundation, Inc. and its licensors. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Eclipse Foundation, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. paho.mqtt.python-2.0.0/epl-v20000066400000000000000000000335651456167725100161060ustar00rootroot00000000000000Eclipse Public License - v 2.0 THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 1. DEFINITIONS "Contribution" means: a) in the case of the initial Contributor, the initial content Distributed under this Agreement, and b) in the case of each subsequent Contributor: i) changes to the Program, and ii) additions to the Program; where such changes and/or additions to the Program originate from and are Distributed by that particular Contributor. A Contribution "originates" from a Contributor if it was added to the Program by such Contributor itself or anyone acting on such Contributor's behalf. Contributions do not include changes or additions to the Program that are not Modified Works. "Contributor" means any person or entity that Distributes the Program. "Licensed Patents" mean patent claims licensable by a Contributor which are necessarily infringed by the use or sale of its Contribution alone or when combined with the Program. "Program" means the Contributions Distributed in accordance with this Agreement. "Recipient" means anyone who receives the Program under this Agreement or any Secondary License (as applicable), including Contributors. "Derivative Works" shall mean any work, whether in Source Code or other form, that is based on (or derived from) the Program and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. "Modified Works" shall mean any work in Source Code or other form that results from an addition to, deletion from, or modification of the contents of the Program, including, for purposes of clarity any new file in Source Code form that contains any contents of the Program. Modified Works shall not include works that contain only declarations, interfaces, types, classes, structures, or files of the Program solely in each case in order to link to, bind by name, or subclass the Program or Modified Works thereof. "Distribute" means the acts of a) distributing or b) making available in any manner that enables the transfer of a copy. "Source Code" means the form of a Program preferred for making modifications, including but not limited to software source code, documentation source, and configuration files. "Secondary License" means either the GNU General Public License, Version 2.0, or any later versions of that license, including any exceptions or additional permissions as identified by the initial Contributor. 2. GRANT OF RIGHTS a) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, Distribute and sublicense the Contribution of such Contributor, if any, and such Derivative Works. b) Subject to the terms of this Agreement, each Contributor hereby grants Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Contribution of such Contributor, if any, in Source Code or other form. This patent license shall apply to the combination of the Contribution and the Program if, at the time the Contribution is added by the Contributor, such addition of the Contribution causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Contribution. No hardware per se is licensed hereunder. c) Recipient understands that although each Contributor grants the licenses to its Contributions set forth herein, no assurances are provided by any Contributor that the Program does not infringe the patent or other intellectual property rights of any other entity. Each Contributor disclaims any liability to Recipient for claims brought by any other entity based on infringement of intellectual property rights or otherwise. As a condition to exercising the rights and licenses granted hereunder, each Recipient hereby assumes sole responsibility to secure any other intellectual property rights needed, if any. For example, if a third party patent license is required to allow Recipient to Distribute the Program, it is Recipient's responsibility to acquire that license before distributing the Program. d) Each Contributor represents that to its knowledge it has sufficient copyright rights in its Contribution, if any, to grant the copyright license set forth in this Agreement. e) Notwithstanding the terms of any Secondary License, no Contributor makes additional grants to any Recipient (other than those set forth in this Agreement) as a result of such Recipient's receipt of the Program under the terms of a Secondary License (if permitted under the terms of Section 3). 3. REQUIREMENTS 3.1 If a Contributor Distributes the Program in any form, then: a) the Program must also be made available as Source Code, in accordance with section 3.2, and the Contributor must accompany the Program with a statement that the Source Code for the Program is available under this Agreement, and informs Recipients how to obtain it in a reasonable manner on or through a medium customarily used for software exchange; and b) the Contributor may Distribute the Program under a license different than this Agreement, provided that such license: i) effectively disclaims on behalf of all other Contributors all warranties and conditions, express and implied, including warranties or conditions of title and non-infringement, and implied warranties or conditions of merchantability and fitness for a particular purpose; ii) effectively excludes on behalf of all other Contributors all liability for damages, including direct, indirect, special, incidental and consequential damages, such as lost profits; iii) does not attempt to limit or alter the recipients' rights in the Source Code under section 3.2; and iv) requires any subsequent distribution of the Program by any party to be under a license that satisfies the requirements of this section 3. 3.2 When the Program is Distributed as Source Code: a) it must be made available under this Agreement, or if the Program (i) is combined with other material in a separate file or files made available under a Secondary License, and (ii) the initial Contributor attached to the Source Code the notice described in Exhibit A of this Agreement, then the Program may be made available under the terms of such Secondary Licenses, and b) a copy of this Agreement must be included with each copy of the Program. 3.3 Contributors may not remove or alter any copyright, patent, trademark, attribution notices, disclaimers of warranty, or limitations of liability ("notices") contained within the Program from any copy of the Program which they Distribute, provided that Contributors may add their own appropriate notices. 4. COMMERCIAL DISTRIBUTION Commercial distributors of software may accept certain responsibilities with respect to end users, business partners and the like. While this license is intended to facilitate the commercial use of the Program, the Contributor who includes the Program in a commercial product offering should do so in a manner which does not create potential liability for other Contributors. Therefore, if a Contributor includes the Program in a commercial product offering, such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify every other Contributor ("Indemnified Contributor") against any losses, damages and costs (collectively "Losses") arising from claims, lawsuits and other legal actions brought by a third party against the Indemnified Contributor to the extent caused by the acts or omissions of such Commercial Contributor in connection with its distribution of the Program in a commercial product offering. The obligations in this section do not apply to any claims or Losses relating to any actual or alleged intellectual property infringement. In order to qualify, an Indemnified Contributor must: a) promptly notify the Commercial Contributor in writing of such claim, and b) allow the Commercial Contributor to control, and cooperate with the Commercial Contributor in, the defense and any related settlement negotiations. The Indemnified Contributor may participate in any such claim at its own expense. For example, a Contributor might include the Program in a commercial product offering, Product X. That Contributor is then a Commercial Contributor. If that Commercial Contributor then makes performance claims, or offers warranties related to Product X, those performance claims and warranties are such Commercial Contributor's responsibility alone. Under this section, the Commercial Contributor would have to defend claims against the other Contributors related to those performance claims and warranties, and if a court requires any other Contributor to pay any damages as a result, the Commercial Contributor must pay those damages. 5. NO WARRANTY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the appropriateness of using and distributing the Program and assumes all risks associated with its exercise of rights under this Agreement, including but not limited to the risks and costs of program errors, compliance with applicable laws, damage to or loss of data, programs or equipment, and unavailability or interruption of operations. 6. DISCLAIMER OF LIABILITY EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 7. GENERAL If any provision of this Agreement is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this Agreement, and without further action by the parties hereto, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable. If Recipient institutes patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Program itself (excluding combinations of the Program with other software or hardware) infringes such Recipient's patent(s), then such Recipient's rights granted under Section 2(b) shall terminate as of the date such litigation is filed. All Recipient's rights under this Agreement shall terminate if it fails to comply with any of the material terms or conditions of this Agreement and does not cure such failure in a reasonable period of time after becoming aware of such noncompliance. If all Recipient's rights under this Agreement terminate, Recipient agrees to cease use and distribution of the Program as soon as reasonably practicable. However, Recipient's obligations under this Agreement and any licenses granted by Recipient relating to the Program shall continue and survive. Everyone is permitted to copy and distribute copies of this Agreement, but in order to avoid inconsistency the Agreement is copyrighted and may only be modified in the following manner. The Agreement Steward reserves the right to publish new versions (including revisions) of this Agreement from time to time. No one other than the Agreement Steward has the right to modify this Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse Foundation may assign the responsibility to serve as the Agreement Steward to a suitable separate entity. Each new version of the Agreement will be given a distinguishing version number. The Program (including Contributions) may always be Distributed subject to the version of the Agreement under which it was received. In addition, after a new version of the Agreement is published, Contributor may elect to Distribute the Program (including its Contributions) under the new version. Except as expressly stated in Sections 2(a) and 2(b) above, Recipient receives no rights or licenses to the intellectual property of any Contributor under this Agreement, whether expressly, by implication, estoppel or otherwise. All rights in the Program not expressly granted under this Agreement are reserved. Nothing in this Agreement is intended to be enforceable by any entity that is not a Contributor or Recipient. No third-party beneficiary rights are created under this Agreement. Exhibit A - Form of Secondary Licenses Notice "This Source Code may also be made available under the following Secondary Licenses when the conditions for such availability set forth in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), version(s), and exceptions or additional permissions here}." Simply including a copy of this Agreement, including this Exhibit A is not sufficient to license the Source Code under Secondary Licenses. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. paho.mqtt.python-2.0.0/examples/000077500000000000000000000000001456167725100166005ustar00rootroot00000000000000paho.mqtt.python-2.0.0/examples/aws_iot.py000077500000000000000000000103101456167725100206150ustar00rootroot00000000000000import base64 import datetime import functools import hashlib import hmac import os import uuid from paho.mqtt.client import Client, CallbackAPIVersion def get_amazon_auth_headers(access_key, secret_key, region, host, port, headers=None): """ Get the amazon auth headers for working with the amazon websockets protocol Requires a lot of extra stuff: http://docs.aws.amazon.com/general/latest/gr//sigv4-create-canonical-request.html http://docs.aws.amazon.com/general/latest/gr//signature-v4-examples.html#signature-v4-examples-pythonw http://docs.aws.amazon.com/general/latest/gr//sigv4-signed-request-examples.html#sig-v4-examples-get-auth-header Args: access_key (str): Amazon access key (AWS_ACCESS_KEY_ID) secret_key (str): Amazon secret access key (AWS_SECRET_ACCESS_KEY) region (str): aws region host (str): iot endpoint (xxxxxxxxxxxxxx.iot..amazonaws.com) headers (dict): a dictionary of the original headers- normally websocket headers Returns: dict: A string containing the headers that amazon expects in the auth request for the iot websocket service """ # pylint: disable=unused-variable,unused-argument def sign(key, msg): return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest() def getSignatureKey(key, dateStamp, regionName, serviceName): kDate = sign(("AWS4" + key).encode("utf-8"), dateStamp) kRegion = sign(kDate, regionName) kService = sign(kRegion, serviceName) kSigning = sign(kService, "aws4_request") return kSigning service = "iotdevicegateway" algorithm = "AWS4-HMAC-SHA256" t = datetime.datetime.utcnow() amzdate = t.strftime('%Y%m%dT%H%M%SZ') datestamp = t.strftime("%Y%m%d") # Date w/o time, used in credential scope if headers is None: headers = { "Host": "{0:s}:443".format(host), "Upgrade": "websocket", "Connection": "Upgrade", "Origin": "https://{0:s}:443".format(host), "Sec-WebSocket-Key": base64.b64encode(uuid.uuid4().bytes), "Sec-Websocket-Version": "13", "Sec-Websocket-Protocol": "mqtt", } headers.update({ "X-Amz-Date": amzdate, }) # get into 'canonical' form - lowercase, sorted alphabetically canonical_headers = "\n".join(sorted("{}:{}".format(i.lower(), j).strip() for i, j in headers.items())) # Headers to sign - alphabetical order signed_headers = ";".join(sorted(i.lower().strip() for i in headers.keys())) # No payload payload_hash = hashlib.sha256("").hexdigest().lower() request_parts = [ "GET", "/mqtt", # no query parameters "", canonical_headers + "\n", signed_headers, payload_hash, ] canonical_request = "\n".join(request_parts) # now actually hash request and sign hashed_request = hashlib.sha256(canonical_request).hexdigest() credential_scope = "{datestamp:s}/{region:s}/{service:s}/aws4_request".format(**locals()) string_to_sign = "{algorithm:s}\n{amzdate:s}\n{credential_scope:s}\n{hashed_request:s}".format(**locals()) signing_key = getSignatureKey(secret_key, datestamp, region, service) signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'), hashlib.sha256).hexdigest() # create auth header authorization_header = "{algorithm:s} Credential={access_key:s}/{credential_scope:s}, SignedHeaders={signed_headers:s}, Signature={signature:s}".format(**locals()) # get final header string headers["Authorization"] = authorization_header return headers def example_use(): access_key = os.environ["AWS_ACCESS_KEY_ID"] secret_key = os.environ["AWS_SECRET_ACCESS_KEY"] port = 8883 region = "eu-west-1" # This is specific to your AWS account host = "abc123def456.iot.{0:s}.amazonaws.com".format(region) extra_headers = functools.partial( get_amazon_auth_headers, access_key, secret_key, region, host, port, ) client = Client(CallbackAPIVersion.VERSION2, transport="websockets") client.ws_set_options(headers=extra_headers) # Use client as normal from here paho.mqtt.python-2.0.0/examples/client_logger.py000077500000000000000000000021371456167725100217750ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2016 James Myatt # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # James Myatt - initial implementation # This shows a simple example of standard logging with an MQTT subscriber client. import logging import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt logging.basicConfig(level=logging.DEBUG) # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2) logger = logging.getLogger(__name__) mqttc.enable_logger(logger) mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.subscribe("$SYS/#", 0) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/client_mqtt_clear_retain.py000077500000000000000000000061671456167725100242220ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2010,2011 Roger Light # All rights reserved. # This shows an example of an MQTT client that clears all of the retained messages it receives. import getopt import sys import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt final_mid = 0 def on_connect(mqttc, userdata, flags, reason_code, properties): if userdata: print(f"reason_code: {reason_code}") def on_message(mqttc, userdata, msg): global final_mid if msg.retain == 0: pass # sys.exit() else: if userdata: print("Clearing topic " + msg.topic) (rc, final_mid) = mqttc.publish(msg.topic, None, 1, True) def on_publish(mqttc, userdata, mid, reason_code, properties): global final_mid if mid == final_mid: sys.exit() def on_log(mqttc, userdata, level, string): print(string) def print_usage(): print( "mqtt_clear_retain.py [-d] [-h hostname] [-i clientid] [-k keepalive] [-p port] [-u username [-P password]] [-v] -t topic") def main(argv): debug = False host = "mqtt.eclipseprojects.io" client_id = None keepalive = 60 port = 1883 password = None topic = None username = None verbose = False try: opts, args = getopt.getopt(argv, "dh:i:k:p:P:t:u:v", ["debug", "id", "keepalive", "port", "password", "topic", "username", "verbose"]) except getopt.GetoptError: print_usage() sys.exit(2) for opt, arg in opts: if opt in ("-d", "--debug"): debug = True elif opt in ("-h", "--host"): host = arg elif opt in ("-i", "--id"): client_id = arg elif opt in ("-k", "--keepalive"): keepalive = int(arg) elif opt in ("-p", "--port"): port = int(arg) elif opt in ("-P", "--password"): password = arg elif opt in ("-t", "--topic"): topic = arg print(topic) elif opt in ("-u", "--username"): username = arg elif opt in ("-v", "--verbose"): verbose = True if not topic: print("You must provide a topic to clear.\n") print_usage() sys.exit(2) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id) mqttc._userdata = verbose mqttc.on_message = on_message mqttc.on_publish = on_publish mqttc.on_connect = on_connect if debug: mqttc.on_log = on_log if username: mqttc.username_pw_set(username, password) mqttc.connect(host, port, keepalive) mqttc.subscribe(topic) mqttc.loop_forever() if __name__ == "__main__": main(sys.argv[1:]) paho.mqtt.python-2.0.0/examples/client_pub-wait.py000077500000000000000000000032361456167725100222470ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2010,2011 Roger Light # All rights reserved. # This shows a simple example of waiting for a message to be published. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt 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, reason_code, properties): print("mid: " + str(mid)) def on_log(mqttc, obj, level, string): print(string) # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish # Uncomment to enable debug messages # mqttc.on_log = on_log mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.loop_start() print("tuple") (rc, mid) = mqttc.publish("tuple", "bar", qos=2) print("class") infot = mqttc.publish("class", "bar", qos=2) infot.wait_for_publish() paho.mqtt.python-2.0.0/examples/client_pub_opts.py000077500000000000000000000077701456167725100223610ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2017 Jon Levell # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # All rights reserved. # This shows a example of an MQTT publisher with the ability to use # user name, password CA certificates based on command line arguments import argparse import os import ssl import time import paho.mqtt.client as mqtt parser = argparse.ArgumentParser() parser.add_argument('-H', '--host', required=False, default="mqtt.eclipseprojects.io") parser.add_argument('-t', '--topic', required=False, default="paho/test/opts") parser.add_argument('-q', '--qos', required=False, type=int,default=0) parser.add_argument('-c', '--clientid', required=False, default=None) parser.add_argument('-u', '--username', required=False, default=None) 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('-k', '--keepalive', required=False, type=int, default=60) parser.add_argument('-s', '--use-tls', action='store_true') parser.add_argument('--insecure', action='store_true') parser.add_argument('-F', '--cacerts', required=False, default=None) parser.add_argument('--tls-version', required=False, default=None, help='TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1\n') parser.add_argument('-D', '--debug', action='store_true') args, unknown = parser.parse_known_args() 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, reason_code, properties): print("mid: " + str(mid)) def on_log(mqttc, obj, level, string): print(string) usetls = args.use_tls if args.cacerts: usetls = True port = args.port if port is None: if usetls: port = 8883 else: port = 1883 mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, args.clientid, clean_session = not args.disable_clean_session) if usetls: if args.tls_version == "tlsv1.2": tlsVersion = ssl.PROTOCOL_TLSv1_2 elif args.tls_version == "tlsv1.1": tlsVersion = ssl.PROTOCOL_TLSv1_1 elif args.tls_version == "tlsv1": tlsVersion = ssl.PROTOCOL_TLSv1 elif args.tls_version is None: tlsVersion = None else: print ("Unknown TLS version - ignoring") tlsVersion = None if not args.insecure: 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: mqttc.tls_insecure_set(True) if args.username or args.password: mqttc.username_pw_set(args.username, args.password) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_publish = on_publish if args.debug: mqttc.on_log = on_log print("Connecting to "+args.host+" port: "+str(port)) mqttc.connect(args.host, port, args.keepalive) mqttc.loop_start() for x in range (0, args.nummsgs): msg_txt = '{"msgnum": "'+str(x)+'"}' print("Publishing: "+msg_txt) infot = mqttc.publish(args.topic, msg_txt, qos=args.qos) infot.wait_for_publish() time.sleep(args.delay) mqttc.disconnect() paho.mqtt.python-2.0.0/examples/client_rpc_math.py000077500000000000000000000057711456167725100223220ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2020 Frank Pagliughi # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Frank Pagliughi - initial implementation # # 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 import time import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt from paho.mqtt.packettypes import PacketTypes # These will be updated with the server-assigned Client ID client_id = "mathcli" reply_to = "" # This correlates the outbound request with the returned reply corr_id = b"1" # 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, reason_code, props): global client_id, reply_to print(f"Connected: '{flags}', '{reason_code}', '{props}'") if hasattr(props, 'AssignedClientIdentifier'): client_id = props.AssignedClientIdentifier reply_to = "replies/math/" + client_id mqttc.subscribe(reply_to) # An incoming message should be the reply to our request def on_message(mqttc, userdata, msg): global reply print(msg.topic+" "+str(msg.payload)+" "+str(msg.properties)) props = msg.properties if not hasattr(props, 'CorrelationData'): print("No correlation ID") # Match the response to the request correlation ID. if props.CorrelationData == corr_id: reply = msg.payload if len(sys.argv) < 3: print("USAGE: client_rpc_math.py [add|mult] n1 n2 ...") sys.exit(1) mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="", protocol=mqtt.MQTTv5) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.connect(host="mqtt.eclipseprojects.io", clean_start=True) mqttc.loop_start() # Wait for connection to set `client_id`, etc. while not mqttc.is_connected(): time.sleep(0.1) # Properties for the request specify the ResponseTopic and CorrelationData props = mqtt.Properties(PacketTypes.PUBLISH) props.CorrelationData = corr_id props.ResponseTopic = reply_to # Uncomment to see what got set #print("Client ID: "+client_id) #print("Reply To: "+reply_to) #print(props) # The requested operation, 'add' or 'mult' func = sys.argv[1] # Gather the numeric parameters as an array of numbers # These can be int's or float's args = [] for s in sys.argv[2:]: args.append(float(s)) # Send the request topic = "requests/math/" + func payload = json.dumps(args) mqttc.publish(topic, payload, qos=1, properties=props) # Wait for the reply while reply is None: time.sleep(0.1) # Extract the response and print it. rsp = json.loads(reply) print("Response: "+str(rsp)) mqttc.loop_stop() paho.mqtt.python-2.0.0/examples/client_session_present.py000077500000000000000000000034271456167725100237440ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2014 Roger Light # All rights reserved. # This demonstrates the session present flag when connecting. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt 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(reason_code)) mqttc.disconnect() def on_disconnect(mqttc, obj, flags, reason_code, properties): mqttc.user_data_set(obj + 1) if obj == 0: mqttc.reconnect() def on_log(mqttc, obj, level, string): print(string) 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 # mqttc.on_log = on_log mqttc.user_data_set(0) mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.loop_forever() # Clear session 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) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/client_sub-class.py000077500000000000000000000033301456167725100224060ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This example shows how you can use the MQTT client in a class. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt class MyMQTTClass(mqtt.Client): 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") def on_message(self, mqttc, obj, msg): print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) def on_publish(self, mqttc, obj, mid, reason_codes, properties): print("mid: "+str(mid)) 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) def run(self): self.connect("mqtt.eclipseprojects.io", 1883, 60) self.subscribe("$SYS/#", 0) rc = 0 while rc == 0: rc = self.loop() return rc # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2) rc = mqttc.run() print("rc: "+str(rc)) paho.mqtt.python-2.0.0/examples/client_sub-multiple-callback.py000077500000000000000000000035031456167725100246700ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # All rights reserved. # This shows a simple example of an MQTT subscriber using a per-subscription message handler. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt def on_message_msgs(mosq, obj, msg): # This callback will only be called for messages with topics that match # $SYS/broker/messages/# print("MESSAGES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) def on_message_bytes(mosq, obj, msg): # This callback will only be called for messages with topics that match # $SYS/broker/bytes/# print("BYTES: " + msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) def on_message(mosq, obj, msg): # This callback will be called for messages that we receive that do not # match any patterns defined in topic specific callbacks, i.e. in this case # those messages that do not have topics $SYS/broker/messages/# nor # $SYS/broker/bytes/# print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload)) 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) mqttc.message_callback_add("$SYS/broker/bytes/#", on_message_bytes) mqttc.on_message = on_message mqttc.connect("mqtt.eclipseprojects.io", 1883, 60) mqttc.subscribe("$SYS/#", 0) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/client_sub-srv.py000077500000000000000000000032131456167725100221130ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2010,2011 Roger Light # All rights reserved. # This shows a simple example of an MQTT subscriber using connect_srv method. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt 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_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) # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages #mqttc.on_log = on_log mqttc.connect_srv("eclipseprojects.io", 60) mqttc.subscribe("$SYS/broker/version", 0) rc = 0 while rc == 0: rc = mqttc.loop() print("rc: "+str(rc)) paho.mqtt.python-2.0.0/examples/client_sub-ws.py000077500000000000000000000031201456167725100217270ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2010,2011 Roger Light # All rights reserved. # This shows a simple example of an MQTT subscriber. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt 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_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) # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2, transport="websockets") mqttc.on_message = on_message mqttc.on_connect = on_connect mqttc.on_subscribe = on_subscribe # Uncomment to enable debug messages mqttc.on_log = on_log mqttc.connect("mqtt.eclipseprojects.io", 80, 60) mqttc.subscribe("$SYS/broker/version", 0) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/client_sub.py000077500000000000000000000031001456167725100212760ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2010-2013 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # Copyright (c) 2010,2011 Roger Light # All rights reserved. # This shows a simple example of an MQTT subscriber. import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt 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_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) # If you want to use a specific client id, use # 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(mqtt.CallbackAPIVersion.VERSION2) mqttc.on_message = on_message mqttc.on_connect = on_connect 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/#") mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/client_sub_opts.py000077500000000000000000000072361456167725100223610ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2017 Jon Levell # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # All rights reserved. # This shows a example of an MQTT subscriber with the ability to use # user name, password CA certificates based on command line arguments import argparse import os import ssl import paho.mqtt.client as mqtt parser = argparse.ArgumentParser() parser.add_argument('-H', '--host', required=False, default="mqtt.eclipseprojects.io") parser.add_argument('-t', '--topic', required=False, default="$SYS/#") parser.add_argument('-q', '--qos', required=False, type=int, default=0) parser.add_argument('-c', '--clientid', required=False, default=None) parser.add_argument('-u', '--username', required=False, default=None) 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('-k', '--keepalive', required=False, type=int, default=60) parser.add_argument('-s', '--use-tls', action='store_true') parser.add_argument('--insecure', action='store_true') parser.add_argument('-F', '--cacerts', required=False, default=None) parser.add_argument('--tls-version', required=False, default=None, help='TLS protocol version, can be one of tlsv1.2 tlsv1.1 or tlsv1\n') parser.add_argument('-D', '--debug', action='store_true') args, unknown = parser.parse_known_args() 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, reason_code_list, properties): print("Subscribed: " + str(mid) + " " + str(reason_code_list)) def on_log(mqttc, obj, level, string): print(string) usetls = args.use_tls if args.cacerts: usetls = True port = args.port if port is None: if usetls: port = 8883 else: port = 1883 mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, args.clientid,clean_session = not args.disable_clean_session) if usetls: if args.tls_version == "tlsv1.2": tlsVersion = ssl.PROTOCOL_TLSv1_2 elif args.tls_version == "tlsv1.1": tlsVersion = ssl.PROTOCOL_TLSv1_1 elif args.tls_version == "tlsv1": tlsVersion = ssl.PROTOCOL_TLSv1 elif args.tls_version is None: tlsVersion = None else: print ("Unknown TLS version - ignoring") tlsVersion = None if not args.insecure: 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: mqttc.tls_insecure_set(True) if args.username or args.password: mqttc.username_pw_set(args.username, args.password) 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 print("Connecting to "+args.host+" port: "+str(port)) mqttc.connect(args.host, port, args.keepalive) mqttc.subscribe(args.topic, args.qos) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/context.py000077500000000000000000000012211456167725100206350ustar00rootroot00000000000000# -*- coding: utf-8 -*- # Ensure can import paho package try: import paho except ImportError: # This part is only required to run the examples from within the examples # directory when the module itself is not installed. import inspect import os import sys cmd_subfolder = os.path.realpath( os.path.abspath( os.path.join( os.path.split( inspect.getfile(inspect.currentframe()) )[0], "..", "src" ) ) ) if cmd_subfolder not in sys.path: sys.path.insert(0, cmd_subfolder) import paho paho.mqtt.python-2.0.0/examples/loop_asyncio.py000077500000000000000000000067321456167725100216630ustar00rootroot00000000000000#!/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 = 'paho-mqtt-python/issue72/' + str(uuid.uuid4()) topic = client_id print("Using client_id / topic: " + client_id) 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): print("Socket opened") def cb(): print("Socket is readable, calling loop_read") 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): print("Socket closed") self.loop.remove_reader(sock) self.misc.cancel() def on_socket_register_write(self, client, userdata, sock): print("Watching socket for writability.") def cb(): print("Socket is writable, calling loop_write") client.loop_write() self.loop.add_writer(sock, cb) def on_socket_unregister_write(self, client, userdata, sock): print("Stop watching socket for writability.") self.loop.remove_writer(sock) async def misc_loop(self): print("misc_loop started") while self.client.loop_misc() == mqtt.MQTT_ERR_SUCCESS: try: await asyncio.sleep(1) except asyncio.CancelledError: break print("misc_loop finished") class AsyncMqttExample: def __init__(self, loop): self.loop = loop def on_connect(self, client, userdata, flags, reason_code, properties): print("Subscribing") client.subscribe(topic) def on_message(self, client, userdata, msg): if not self.got_message: print("Got unexpected message: {}".format(msg.decode())) else: self.got_message.set_result(msg.payload) 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(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 aioh = AsyncioHelper(self.loop, self.client) self.client.connect('mqtt.eclipseprojects.io', 1883, 60) self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) for c in range(3): await asyncio.sleep(5) print("Publishing") self.got_message = self.loop.create_future() self.client.publish(topic, b'Hello' * 40000, qos=1) msg = await self.got_message print("Got response with {} bytes".format(len(msg))) self.got_message = None self.client.disconnect() print("Disconnected: {}".format(await self.disconnected)) print("Starting") loop = asyncio.get_event_loop() loop.run_until_complete(AsyncMqttExample(loop).main()) loop.close() print("Finished") paho.mqtt.python-2.0.0/examples/loop_select.py000077500000000000000000000050201456167725100214620ustar00rootroot00000000000000#!/usr/bin/env python3 import socket import uuid from select import select from time import time import paho.mqtt.client as mqtt client_id = 'paho-mqtt-python/issue72/' + str(uuid.uuid4()) topic = client_id print("Using client_id / topic: " + client_id) class SelectMqttExample: def __init__(self): pass def on_connect(self, client, userdata, flags, reason_code, properties): print("Subscribing") client.subscribe(topic) def on_message(self, client, userdata, msg): if self.state not in {1, 3, 5}: print("Got unexpected message: {}".format(msg.decode())) return print("Got message with len {}".format(len(msg.payload))) self.state += 1 self.t = time() def on_disconnect(self, client, userdata, flags, reason_code, properties): self.disconnected = True, reason_code def do_select(self): sock = self.client.socket() if not sock: raise Exception("Socket is gone") print("Selecting for reading" + (" and writing" if self.client.want_write() else "")) r, w, e = select( [sock], [sock] if self.client.want_write() else [], [], 1 ) if sock in r: print("Socket is readable, calling loop_read") self.client.loop_read() if sock in w: print("Socket is writable, calling loop_write") self.client.loop_write() self.client.loop_misc() def main(self): self.disconnected = (False, None) self.t = time() self.state = 0 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 self.client.connect('mqtt.eclipseprojects.io', 1883, 60) print("Socket opened") self.client.socket().setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) while not self.disconnected[0]: self.do_select() if self.state in {0, 2, 4}: if time() - self.t >= 5: print("Publishing") self.client.publish(topic, b'Hello' * 40000) self.state += 1 if self.state == 6: self.state += 1 self.client.disconnect() print("Disconnected: {}".format(self.disconnected[1])) print("Starting") SelectMqttExample().main() print("Finished") paho.mqtt.python-2.0.0/examples/loop_trio.py000077500000000000000000000060311456167725100211630ustar00rootroot00000000000000#!/usr/bin/env python3 import socket import uuid import trio import paho.mqtt.client as mqtt client_id = 'paho-mqtt-python/issue72/' + str(uuid.uuid4()) topic = client_id print("Using client_id / topic: " + client_id) class TrioAsyncHelper: def __init__(self, client): self.client = client self.sock = None self._event_large_write = trio.Event() self.client.on_socket_open = self.on_socket_open self.client.on_socket_register_write = self.on_socket_register_write self.client.on_socket_unregister_write = self.on_socket_unregister_write async def read_loop(self): while True: 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.lowlevel.wait_writable(self.sock) self.client.loop_write() async def misc_loop(self): print("misc_loop started") while self.client.loop_misc() == mqtt.MQTT_ERR_SUCCESS: await trio.sleep(1) print("misc_loop finished") def on_socket_open(self, client, userdata, sock): print("Socket opened") self.sock = sock self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) def on_socket_register_write(self, client, userdata, sock): print('large write request') self._event_large_write.set() def on_socket_unregister_write(self, client, userdata, sock): print("finished large write") self._event_large_write = trio.Event() class TrioAsyncMqttExample: 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, flags, reason_code, properties): print('Disconnect result {}'.format(reason_code)) async def test_write(self, cancel_scope: trio.CancelScope): for c in range(3): await trio.sleep(5) print("Publishing") self.client.publish(topic, b'Hello' * 40000, qos=1) cancel_scope.cancel() async def main(self): 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 trio_helper = TrioAsyncHelper(self.client) self.client.connect('mqtt.eclipseprojects.io', 1883, 60) async with trio.open_nursery() as nursery: nursery.start_soon(trio_helper.read_loop) nursery.start_soon(trio_helper.write_loop) nursery.start_soon(trio_helper.misc_loop) nursery.start_soon(self.test_write, nursery.cancel_scope) self.client.disconnect() print("Disconnected") print("Starting") trio.run(TrioAsyncMqttExample().main) print("Finished") paho.mqtt.python-2.0.0/examples/publish_multiple.py000077500000000000000000000014151456167725100225370ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the publish.multiple helper function. import context # Ensures paho is in PYTHONPATH 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") paho.mqtt.python-2.0.0/examples/publish_single.py000077500000000000000000000012541456167725100221660ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the publish.single helper function. import context # Ensures paho is in PYTHONPATH import paho.mqtt.publish as publish publish.single("paho/test/single", "boo", hostname="mqtt.eclipseprojects.io") paho.mqtt.python-2.0.0/examples/publish_utf8-27.py000077500000000000000000000013621456167725100220210ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the publish.single helper function with unicode topic and payload. import context # Ensures paho is in PYTHONPATH import paho.mqtt.publish as publish topic = u"paho/test/single/ô" payload = u"bôô" publish.single(topic, payload, hostname="mqtt.eclipseprojects.io") paho.mqtt.python-2.0.0/examples/publish_utf8-3.py000077500000000000000000000014361456167725100217350ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2014 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the publish.single helper function with unicode topic and payload. import context # Ensures paho is in PYTHONPATH import paho.mqtt.publish as publish topic = u"paho/test/single/ô" payload = u'German umlauts like "ä" ü"ö" are not supported' publish.single(topic, payload, hostname="mqtt.eclipseprojects.io") paho.mqtt.python-2.0.0/examples/server_rpc_math.py000077500000000000000000000054551456167725100223510ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2020 Frank Pagliughi # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Frank Pagliughi - initial implementation # # This shows an example of an MQTTv5 Remote Procedure Call (RPC) server. import json import context # Ensures paho is in PYTHONPATH import paho.mqtt.client as mqtt from paho.mqtt.packettypes import PacketTypes # The math functions exported def add(nums): sum = 0 for x in nums: sum += x return sum def mult(nums): prod = 1 for x in nums: prod *= x return prod # Remember that the MQTTv5 callback takes the additional 'props' parameter. 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 # 'requests/math/#' topic. def on_message(mqttc, userdata, msg): print(msg.topic + " " + str(msg.payload)) # Get the response properties, abort if they're not given props = msg.properties if not hasattr(props, 'ResponseTopic') or not hasattr(props, 'CorrelationData'): print("No reply requested") return corr_id = props.CorrelationData reply_to = props.ResponseTopic # The command parameters are in the payload nums = json.loads(msg.payload) # The requested command is at the end of the topic res = 0 if msg.topic.endswith("add"): res = add(nums) elif msg.topic.endswith("mult"): res = mult(nums) # Now we have the result, res, so send it back on the 'reply_to' # topic using the same correlation ID as the request. print("Sending response "+str(res)+" on '"+reply_to+"': "+str(corr_id)) props = mqtt.Properties(PacketTypes.PUBLISH) props.CorrelationData = corr_id payload = json.dumps(res) mqttc.publish(reply_to, payload, qos=1, properties=props) def on_log(mqttc, obj, level, string): print(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 # might help. 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(host="mqtt.eclipseprojects.io", clean_start=False) mqttc.loop_forever() paho.mqtt.python-2.0.0/examples/subscribe_callback.py000077500000000000000000000014211456167725100227500ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2016 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the subscribe.callback helper function. import context # Ensures paho is in PYTHONPATH import paho.mqtt.subscribe as subscribe def print_msg(client, userdata, message): print("%s : %s" % (message.topic, message.payload)) subscribe.callback(print_msg, "#", hostname="mqtt.eclipseprojects.io") paho.mqtt.python-2.0.0/examples/subscribe_simple.py000077500000000000000000000014061456167725100225100ustar00rootroot00000000000000#!/usr/bin/python3 # -*- coding: utf-8 -*- # Copyright (c) 2016 Roger Light # # 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. # # The Eclipse Distribution License is available at # http://www.eclipse.org/org/documents/edl-v10.php. # # Contributors: # Roger Light - initial implementation # This shows an example of using the subscribe.simple helper function. import context # Ensures paho is in PYTHONPATH import paho.mqtt.subscribe as subscribe topics = ['#'] m = subscribe.simple(topics, hostname="mqtt.eclipseprojects.io", retained=False, msg_count=2) for a in m: print(a.topic) print(a.payload) paho.mqtt.python-2.0.0/notice.html000066400000000000000000000220161456167725100171320ustar00rootroot00000000000000 Eclipse Foundation Software User Agreement

Eclipse Foundation Software User Agreement

February 1, 2011

Usage Of Content

THE ECLIPSE FOUNDATION MAKES AVAILABLE SOFTWARE, DOCUMENTATION, INFORMATION AND/OR OTHER MATERIALS FOR OPEN SOURCE PROJECTS (COLLECTIVELY "CONTENT"). USE OF THE CONTENT IS GOVERNED BY THE TERMS AND CONDITIONS OF THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS OF LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW. BY USING THE CONTENT, YOU AGREE THAT YOUR USE OF THE CONTENT IS GOVERNED BY THIS AGREEMENT AND/OR THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW. IF YOU DO NOT AGREE TO THE TERMS AND CONDITIONS OF THIS AGREEMENT AND THE TERMS AND CONDITIONS OF ANY APPLICABLE LICENSE AGREEMENTS OR NOTICES INDICATED OR REFERENCED BELOW, THEN YOU MAY NOT USE THE 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-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 repository ("Repository") in software modules ("Modules") and made available as downloadable archives ("Downloads").

  • Content may be structured and packaged into modules to facilitate delivering, extending, and upgrading the Content. Typical modules may include plug-ins ("Plug-ins"), plug-in fragments ("Fragments"), and features ("Features").
  • Each Plug-in or Fragment may be packaged as a sub-directory or JAR (Java™ ARchive) in a directory named "plugins".
  • A Feature is a bundle of one or more Plug-ins and/or Fragments and associated material. Each Feature may be packaged as a sub-directory in a directory named "features". Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of the Plug-ins and/or Fragments associated with that Feature.
  • Features may also include other Features ("Included Features"). Within a Feature, files named "feature.xml" may contain a list of the names and version numbers of Included Features.

The terms and conditions governing Plug-ins and Fragments should be contained in files named "about.html" ("Abouts"). The terms and conditions governing Features and Included Features should be contained in files named "license.html" ("Feature Licenses"). Abouts and Feature Licenses may be located in any directory of a Download or Module including, but not limited to the following locations:

  • The top-level (root) directory
  • Plug-in and Fragment directories
  • Inside Plug-ins and Fragments packaged as JARs
  • Sub-directories of the directory named "src" of certain Plug-ins
  • Feature directories

Note: if a Feature made available by the Eclipse Foundation is installed using the Provisioning Technology (as defined below), you must agree to a license ("Feature Update License") during the installation process. If the Feature contains Included Features, the Feature Update License should either provide you with the terms and conditions governing the Included Features or inform you where you can locate them. Feature Update Licenses may be found in the "license" property of files named "feature.properties" found within a Feature. Such Abouts, Feature Licenses, and Feature Update Licenses contain the terms and conditions (or references to such terms and conditions) that govern your use of the associated Content in that directory.

THE ABOUTS, FEATURE LICENSES, AND FEATURE UPDATE LICENSES MAY REFER TO THE EPL OR OTHER LICENSE AGREEMENTS, NOTICES OR TERMS AND CONDITIONS. SOME OF THESE OTHER LICENSE AGREEMENTS MAY INCLUDE (BUT ARE NOT LIMITED TO):

IT IS YOUR OBLIGATION TO READ AND ACCEPT ALL SUCH TERMS AND CONDITIONS PRIOR TO USE OF THE CONTENT. If no About, Feature License, or Feature Update License is provided, please contact the Eclipse Foundation to determine what terms and conditions govern that particular Content.

Use of Provisioning Technology

The Eclipse Foundation makes available provisioning software, examples of which include, but are not limited to, p2 and the Eclipse Update Manager ("Provisioning Technology") for the purpose of allowing users to install software, documentation, information and/or other materials (collectively "Installable Software"). This capability is provided with the intent of allowing such users to install, extend and update Eclipse-based products. Information about packaging Installable Software is available at http://eclipse.org/equinox/p2/repository_packaging.html ("Specification").

You may use Provisioning Technology to allow other parties to install Installable Software. You shall be responsible for enabling the applicable license agreements relating to the Installable Software to be presented to, and accepted by, the users of the Provisioning Technology in accordance with the Specification. By using Provisioning Technology in such a manner and making it available in accordance with the Specification, you further acknowledge your agreement to, and the acquisition of all necessary rights to permit the following:

  1. A series of actions may occur ("Provisioning Process") in which a user may execute the Provisioning Technology on a machine ("Target Machine") with the intent of installing, extending or updating the functionality of an Eclipse-based product.
  2. During the Provisioning Process, the Provisioning Technology may cause third party Installable Software or a portion thereof to be accessed and copied to the Target Machine.
  3. Pursuant to the Specification, you will provide to the user the terms and conditions that govern the use of the Installable Software ("Installable Software Agreement") and such Installable Software Agreement shall be accessed from the Target Machine in accordance with the Specification. Such Installable Software Agreement must inform the user of the terms and conditions that govern the Installable Software and must solicit acceptance by the end user in the manner prescribed in such Installable Software Agreement. Upon such indication of agreement by the user, the provisioning Technology will complete installation of the Installable Software.

Cryptography

Content may contain encryption software. The country in which you are currently may have restrictions on the import, possession, and use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check the country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted.

Java and all Java-based trademarks are trademarks of Oracle Corporation in the United States, other countries, or both.

paho.mqtt.python-2.0.0/pyproject.toml000066400000000000000000000054731456167725100177070ustar00rootroot00000000000000[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" paho.mqtt.python-2.0.0/src/000077500000000000000000000000001456167725100155515ustar00rootroot00000000000000paho.mqtt.python-2.0.0/src/paho/000077500000000000000000000000001456167725100165005ustar00rootroot00000000000000paho.mqtt.python-2.0.0/src/paho/__init__.py000066400000000000000000000000001456167725100205770ustar00rootroot00000000000000paho.mqtt.python-2.0.0/src/paho/mqtt/000077500000000000000000000000001456167725100174655ustar00rootroot00000000000000paho.mqtt.python-2.0.0/src/paho/mqtt/__init__.py000066400000000000000000000001011456167725100215660ustar00rootroot00000000000000__version__ = "2.0.0" class MQTTException(Exception): pass paho.mqtt.python-2.0.0/src/paho/mqtt/client.py000066400000000000000000006117761456167725100213370ustar00rootroot00000000000000# Copyright (c) 2012-2019 Roger Light and others # # 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: # 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 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 ReasonCode, ReasonCodes from .subscribeoptions import SubscribeOptions try: from typing import Literal except ImportError: 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: ... try: import ssl except ImportError: ssl = None # type: ignore[assignment] try: import socks # type: ignore[import-untyped] except ImportError: socks = None # type: ignore[assignment] try: # Use monotonic clock if available time_func = time.monotonic except AttributeError: time_func = time.time try: import dns.resolver HAVE_DNS = True except ImportError: HAVE_DNS = False if platform.system() == 'Windows': EAGAIN = errno.WSAEWOULDBLOCK # type: ignore[attr-defined] else: EAGAIN = errno.EAGAIN # 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 = 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 = { 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 = 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 = 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: 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] 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: MQTTErrorCode) -> str: """Return the error string associated with an mqtt error number.""" if mqtt_errno == MQTT_ERR_SUCCESS: return "No error." elif mqtt_errno == MQTT_ERR_NOMEM: return "Out of memory." elif mqtt_errno == MQTT_ERR_PROTOCOL: return "A network protocol error occurred when communicating with the broker." elif mqtt_errno == MQTT_ERR_INVAL: return "Invalid function arguments provided." elif mqtt_errno == MQTT_ERR_NO_CONN: return "The client is not currently connected." elif mqtt_errno == MQTT_ERR_CONN_REFUSED: return "The connection was refused." elif mqtt_errno == MQTT_ERR_NOT_FOUND: return "Message not found (internal error)." elif mqtt_errno == MQTT_ERR_CONN_LOST: return "The connection was lost." elif mqtt_errno == MQTT_ERR_TLS: return "A TLS error occurred." elif mqtt_errno == MQTT_ERR_PAYLOAD_SIZE: return "Payload too large." elif mqtt_errno == MQTT_ERR_NOT_SUPPORTED: return "This feature is not supported." elif mqtt_errno == MQTT_ERR_AUTH: return "Authorisation failed." elif mqtt_errno == MQTT_ERR_ACL_DENIED: return "Access denied by ACL." elif mqtt_errno == MQTT_ERR_UNKNOWN: return "Unknown error." elif mqtt_errno == MQTT_ERR_ERRNO: return "Error defined by errno." elif mqtt_errno == MQTT_ERR_QUEUE_SIZE: return "Message queue full." elif mqtt_errno == MQTT_ERR_KEEPALIVE: return "Client or broker did not communicate in the keepalive interval." else: return "Unknown error." 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: return "Connection Refused: unacceptable protocol version." elif connack_code == CONNACK_REFUSED_IDENTIFIER_REJECTED: return "Connection Refused: identifier rejected." elif connack_code == CONNACK_REFUSED_SERVER_UNAVAILABLE: return "Connection Refused: broker unavailable." elif connack_code == CONNACK_REFUSED_BAD_USERNAME_PASSWORD: return "Connection Refused: bad user name or password." elif connack_code == CONNACK_REFUSED_NOT_AUTHORIZED: return "Connection Refused: not authorised." else: return "Connection Refused: unknown reason." 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.""" if num < 0: raise ValueError("Number must be positive or zero") digits = [] while num: num, rest = divmod(num, 62) digits.append(base[rest]) digits.extend(base[0] for _ in range(len(digits), padding)) return ''.join(reversed(digits)) def topic_matches_sub(sub: str, topic: str) -> bool: """Check whether a topic matches a subscription. For example: * Topic "foo/bar" would match the subscription "foo/#" or "+/bar" * Topic "non/matching" would not match the subscription "non/+/+" """ matcher = MQTTMatcher() matcher[sub] = True try: next(matcher.iter_match(topic)) return True except StopIteration: return False 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) listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) listensock.bind(("127.0.0.1", 0)) listensock.listen(1) iface, port = listensock.getsockname() sock1 = socket.socket( socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP) sock1.setblocking(False) try: sock1.connect(("127.0.0.1", port)) except BlockingIOError: pass sock2, address = listensock.accept() sock2.setblocking(False) listensock.close() return (sock1, sock2) 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: 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: int): self.mid = mid """ The message Id (int)""" self._published = False self._condition = threading.Condition() 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) -> str: return str((self.rc, self.mid)) def __iter__(self) -> Iterator[MQTTErrorCode | int]: self._iterpos = 0 return self def __next__(self) -> MQTTErrorCode | int: return self.next() def next(self) -> MQTTErrorCode | int: if self._iterpos == 0: self._iterpos = 1 return self.rc elif self._iterpos == 1: self._iterpos = 2 return self.mid else: raise StopIteration def __getitem__(self, index: int) -> MQTTErrorCode | int: if index == 0: return self.rc elif index == 1: return self.mid else: raise IndexError("index out of range") def _set_as_published(self) -> None: with self._condition: self._published = True self._condition.notify() 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 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(f'Message publish failed: {error_string(self.rc)}') timeout_time = None if timeout is None else time_func() + timeout timeout_tenth = None if timeout is None else timeout / 10. 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) 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. 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 == MQTTErrorCode.MQTT_ERR_AGAIN: pass elif self.rc > 0: raise RuntimeError(f'Message publish failed: {error_string(self.rc)}') with self._condition: return self._published 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: 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: object) -> bool: """Override the default Equals behavior""" if isinstance(other, self.__class__): return self.mid == other.mid return False def __ne__(self, other: object) -> bool: """Define a non-equality test""" return not self.__eq__(other) @property 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: bytes) -> None: self._topic = value 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()` 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. :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 migrations.md contains details on how to migrate between version. :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 generate a random for the client. If using MQTT v3.1 then an id will be randomly generated. In both cases, clean_session must be True. If this is not the case a ValueError will be raised. :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 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. The clean_session argument only applies to MQTT versions v3.1.1 and v3.1. It is not accepted if the MQTT version is v5.0 - use the clean_start argument on connect() instead. :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. :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. :param transport: use "websockets" to use WebSockets as the transport mechanism. Set to "tcp" to use raw TCP, which is the default. :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. 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, client_id: str = "", clean_session: bool | None = None, userdata: Any = None, protocol: int = MQTTv311, transport: Literal["tcp", "websockets"] = "tcp", reconnect_on_failure: bool = True, manual_ack: bool = False, ) -> None: transport = transport.lower() # type: ignore if transport not in ("websockets", "tcp"): raise ValueError( f'transport must be "websockets" or "tcp", not {transport}') self._manual_ack = manual_ack self._transport = transport self._protocol = protocol self._userdata = userdata 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 migrations.md 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: raise ValueError('Clean session is not used for MQTT 5.0') else: if clean_session is None: clean_session = True if not clean_session and (client_id == "" or client_id is None): raise ValueError( 'A client id must be provided if clean session is False.') self._clean_session = clean_session # [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).encode("utf8") else: self._client_id = b"" else: self._client_id = _force_bytes(client_id) self._username: bytes | None = None self._password: bytes | None = None self._in_packet: _InPacket = { "command": 0, "have_remaining": 0, "remaining_count": [], "remaining_mult": 1, "remaining_length": 0, "packet": bytearray(b""), "to_process": 0, "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: int | None = None self._reconnect_on_failure = reconnect_on_failure self._ping_t = 0.0 self._last_mid = 0 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: Properties | None = None self._will_properties: Properties | None = None self._will = False self._will_topic = b"" self._will_payload = b"" self._will_qos = 0 self._will_retain = False self._on_message_filtered = MQTTMatcher() self._host = "" self._port = 1883 self._bind_address = "" self._bind_port = 0 self._proxy: Any = {} self._in_callback_mutex = threading.Lock() self._callback_mutex = threading.RLock() self._msgtime_mutex = threading.Lock() self._out_message_mutex = threading.RLock() self._in_message_mutex = threading.Lock() self._reconnect_delay_mutex = threading.Lock() self._mid_generate_mutex = threading.Lock() self._thread: threading.Thread | None = None self._thread_terminate = False self._ssl = False self._ssl_context: ssl.SSLContext | None = None # Only used when SSL context does not have check_hostname attribute self._tls_insecure = False self._logger: logging.Logger | None = None self._registered_write = False # No default callbacks 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: 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) -> None: self._reset_sockets() @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"]: """ 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 as err: raise BlockingIOError() from err except ssl.SSLWantWriteError as err: self._call_socket_register_write() 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") try: return self._sock.send(buf) except ssl.SSLWantReadError as err: raise BlockingIOError() from err except ssl.SSLWantWriteError as err: self._call_socket_register_write() raise BlockingIOError() from err except BlockingIOError as err: self._call_socket_register_write() raise BlockingIOError() from err def _sock_close(self) -> None: """Close the connection to the server.""" if not self._sock: return try: sock = self._sock self._sock = None self._call_socket_unregister_write(sock) self._call_socket_close(sock) finally: # In case a callback fails, still close the socket to avoid leaking the file descriptor. sock.close() def _reset_sockets(self, sockpair_only: bool = False) -> None: if not sockpair_only: self._sock_close() if self._sockpairR: self._sockpairR.close() self._sockpairR = None if self._sockpairW: self._sockpairW.close() self._sockpairW = 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) # type: ignore[misc] def ws_set_options( self, path: str = "/mqtt", headers: WebSocketHeaders | None = None, ) -> None: """ Set the path and headers for a websocket connection :param str path: a string starting with / which should be the endpoint of the mqtt connection on the remote server :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 if headers is not None: if isinstance(headers, dict) or callable(headers): self._websocket_extra_headers = headers else: raise ValueError( "'headers' option to ws_set_options has to be either a dictionary or callable") def tls_set_context( self, context: ssl.SSLContext | None = None, ) -> None: """Configure network encryption and authentication context. Enables SSL/TLS support. :param context: an ssl.SSLContext object. By default this is given by ``ssl.create_default_context()``, if available. 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.') if context is None: context = ssl.create_default_context() self._ssl = True self._ssl_context = context # Ensure _tls_insecure is consistent with check_hostname attribute if hasattr(context, 'check_hostname'): self._tls_insecure = not context.check_hostname 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. :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.') if not hasattr(ssl, 'SSLContext'): # Require Python version that has SSL context support in standard library raise ValueError( 'Python 2.7.9 and 3.2 are the minimum supported versions for TLS.') if ca_certs is None and not hasattr(ssl.SSLContext, 'load_default_certs'): raise ValueError('ca_certs must not be None.') # Create SSLContext object 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_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) if cert_reqs == ssl.CERT_NONE and hasattr(context, 'check_hostname'): context.check_hostname = False context.verify_mode = ssl.CERT_REQUIRED if cert_reqs is None else cert_reqs if ca_certs is not None: context.load_verify_locations(ca_certs) else: context.load_default_certs() 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) if cert_reqs != ssl.CERT_NONE: # Default to secure, sets context.check_hostname attribute # if available self.tls_insecure_set(False) else: # But with ssl.CERT_NONE, we can not check_hostname self.tls_insecure_set(True) 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 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 either `tls_set()` or `tls_set_context()`.""" if self._ssl_context is None: raise ValueError( 'Must configure SSL context before using tls_insecure_set.') self._tls_insecure = value # Ensure check_hostname is consistent with _tls_insecure attribute if hasattr(self._ssl_context, 'check_hostname'): # Rely on SSLContext to check host name # 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: Any) -> None: """Configure proxying of MQTT connection. Enables support for SOCKS or HTTP proxies. Proxying is done through the PySocks library. Brief descriptions of the proxy_args parameters are below; see the PySocks docs for more info. (Required) :param proxy_type: One of {socks.HTTP, socks.SOCKS4, or socks.SOCKS5} :param proxy_addr: IP address or DNS name of proxy server (Optional) :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) :param proxy_username: username for SOCKS5 proxy, or userid for SOCKS4 proxy :param proxy_password: password for SOCKS5 proxy Example:: 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): raise ValueError("proxy_type and/or proxy_addr are invalid.") else: self._proxy = proxy_args 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 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: 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: self._mqttv5_first_connect = True else: if clean_start != MQTT_CLEAN_START_FIRST_ONLY: raise ValueError("Clean start only applies to MQTT V5") 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: 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. :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".') if domain is None: domain = socket.getfqdn() domain = domain[domain.find('.') + 1:] try: rr = f'_mqtt._tcp.{domain}' if self._ssl: # IANA specifies secure-mqtt (not mqtts) for port 8883 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) 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, bind_port, clean_start, properties) except Exception: # noqa: S110 pass raise ValueError("No SRV hosts responded") 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 start. 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.') # 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 = _ConnectionState.MQTT_CS_CONNECT_ASYNC 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 double this time every attempt. The wait is capped at max_delay. Once the client is fully connected (e.g. not only TCP socket, but received a success CONNACK), the wait timer is reset to min_delay. """ with self._reconnect_delay_mutex: self._reconnect_min_delay = min_delay self._reconnect_max_delay = max_delay self._reconnect_delay = None def reconnect(self) -> MQTTErrorCode: """Reconnect the client after a disconnect. Can only be called after connect()/connect_async().""" if len(self._host) == 0: raise ValueError('Invalid host.') if self._port <= 0: raise ValueError('Invalid port number.') self._in_packet = { "command": 0, "have_remaining": 0, "remaining_count": [], "remaining_mult": 1, "remaining_length": 0, "packet": bytearray(b""), "to_process": 0, "pos": 0, } self._ping_t = 0.0 self._state = _ConnectionState.MQTT_CS_CONNECTING 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() # Put messages in progress in a valid state. self._messages_reconnect_reset() with self._callback_mutex: on_pre_connect = self.on_pre_connect if on_pre_connect: try: 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 self._sock = self._create_socket() self._sock.setblocking(False) # type: ignore[attr-defined] self._registered_write = False self._call_socket_open(self._sock) return self._send_connect(self._keepalive) 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 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 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. :param int timeout: The time in seconds to wait for incoming/outgoing network traffic before timing out and returning. Returns MQTT_ERR_SUCCESS on success. Returns >0 on error. A ValueError will be raised if timeout < 0""" if self._sockpairR is None or self._sockpairW is None: self._reset_sockets(sockpair_only=True) self._sockpairR, self._sockpairW = _socketpair_compat() return self._loop(timeout) def _loop(self, timeout: float = 1.0) -> MQTTErrorCode: if timeout < 0.0: raise ValueError('Invalid timeout.') if self.want_write(): wlist = [self._sock] 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() # type: ignore[union-attr] # if bytes are pending do not wait in select if pending_bytes > 0: timeout = 0.0 # sockpairR is used to break out of select() before the timeout, on a # call to publish() etc. if self._sockpairR is None: rlist = [self._sock] else: rlist = [self._sock, self._sockpairR] try: socklist = select.select(rlist, wlist, [], timeout) except TypeError: # Socket isn't correct type, in likelihood connection is 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. 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 MQTTErrorCode.MQTT_ERR_UNKNOWN if self._sock in socklist[0] or pending_bytes > 0: rc = self.loop_read() if rc or self._sock is None: return rc if self._sockpairR and self._sockpairR in socklist[0]: # Stimulate output write even though we didn't ask for it, because # at that point the publish or other command wasn't present. socklist[1].insert(0, self._sock) # Clear sockpairR - only ever a single byte written. try: # Read many bytes at once - this allows up to 10000 calls to # publish() inbetween calls to loop(). self._sockpairR.recv(10000) except BlockingIOError: pass if self._sock in socklist[1]: rc = self.loop_write() if rc or self._sock is None: return rc return self.loop_misc() 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. :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 :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. 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 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. :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_bytes = topic.encode('utf-8') self._raise_for_invalid_topic(topic_bytes) if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') local_payload = _encode_payload(payload) if len(local_payload) > 268435455: raise ValueError('Payload too large.') local_mid = self._mid_generate() if qos == 0: info = MQTTMessageInfo(local_mid) rc = self._send_publish( local_mid, topic_bytes, local_payload, qos, retain, False, info, properties) info.rc = rc return info else: message = MQTTMessage(local_mid, topic_bytes) message.timestamp = time_func() message.payload = local_payload message.qos = qos message.retain = retain message.dup = False message.properties = properties with self._out_message_mutex: if self._max_queued_messages > 0 and len(self._out_messages) >= self._max_queued_messages: message.info.rc = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE return message.info if local_mid in self._out_messages: message.info.rc = MQTTErrorCode.MQTT_ERR_QUEUE_SIZE return message.info self._out_messages[message.mid] = message if self._max_inflight_messages == 0 or self._inflight_messages < self._max_inflight_messages: self._inflight_messages += 1 if qos == 1: message.state = mqtt_ms_wait_for_puback elif qos == 2: message.state = mqtt_ms_wait_for_pubrec 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 == MQTTErrorCode.MQTT_ERR_NO_CONN: self._inflight_messages -= 1 message.state = mqtt_ms_publish message.info.rc = rc return message.info else: message.state = mqtt_ms_queued message.info.rc = MQTTErrorCode.MQTT_ERR_SUCCESS return message.info 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 or more. :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. :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') if isinstance(password, str): self._password = password.encode('utf-8') else: self._password = password 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. Requires brokers that support bridge mode. Under bridge mode, the broker will identify the client as a bridge and not send it's own messages back to it. Hence a subsciption of # is possible without message loops. This feature also correctly propagates the retain flag on the messages. Currently Mosquitto and RSMB support this feature. This feature can be used to create a bridge between multiple broker. """ self._client_mode = MQTT_BRIDGE 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 == _ConnectionState.MQTT_CS_CONNECTED def disconnect( self, reasoncode: ReasonCode | None = None, properties: Properties | None = None, ) -> MQTTErrorCode: """Disconnect a connected client from the broker. :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: 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): 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. :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. 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 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 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. 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, options and properties: Not used. List of string and subscribe option tuples (MQTT v5.0 only) ----------------------------------------------------------- e.g. subscribe([("my/topic", SubscribeOptions(qos=0), ("another/topic", SubscribeOptions(qos=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, 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. 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. """ topic_qos_list = None if isinstance(topic, tuple): if self._protocol == MQTTv5: topic, options = topic # type: ignore if not isinstance(options, SubscribeOptions): raise ValueError( 'Subscribe options must be instance of SubscribeOptions class.') else: topic, qos = topic # type: ignore if isinstance(topic, (bytes, str)): if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') if self._protocol == MQTTv5: if options is None: # if no options are provided, use the QoS passed instead options = SubscribeOptions(qos=qos) elif qos != 0: raise ValueError( 'Subscribe options and qos parameters cannot be combined.') if not isinstance(options, SubscribeOptions): raise ValueError( 'Subscribe options must be instance of SubscribeOptions class.') topic_qos_list = [(topic.encode('utf-8'), options)] else: if topic is None or len(topic) == 0: raise ValueError('Invalid topic.') 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: if not isinstance(o, SubscribeOptions): # then the second value should be QoS if o < 0 or o > 2: raise ValueError('Invalid QoS level.') o = SubscribeOptions(qos=o) topic_qos_list.append((t.encode('utf-8'), o)) else: for t, q in topic: 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, (bytes, str)): raise ValueError('Invalid topic.') 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.") if any(self._filter_wildcard_len_check(topic) != MQTT_ERR_SUCCESS for topic, _ in topic_qos_list): raise ValueError('Invalid subscription filter.') if self._sock is None: return (MQTT_ERR_NO_CONN, None) return self._send_subscribe(False, topic_qos_list, properties) def unsubscribe( self, topic: str, properties: Properties | None = None ) -> tuple[MQTTErrorCode, int | None]: """Unsubscribe the client from one or more topics. :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 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. :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, (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, (bytes, str)): raise ValueError('Invalid topic.') topic_list.append(t.encode('utf-8')) if topic_list is None: raise ValueError("No topic specified, or incorrect topic type.") if self._sock is None: return (MQTTErrorCode.MQTT_ERR_NO_CONN, None) return self._send_unsubscribe(False, topic_list, properties) 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 on. Do not use if you are using `loop_start()` or `loop_forever()`.""" if self._sock is None: return MQTTErrorCode.MQTT_ERR_NO_CONN max_packets = len(self._out_messages) + len(self._in_messages) if max_packets < 1: max_packets = 1 for _ in range(0, max_packets): if self._sock is None: return MQTTErrorCode.MQTT_ERR_NO_CONN rc = self._packet_read() if rc > 0: return self._loop_rc_handle(rc) elif rc == MQTTErrorCode.MQTT_ERR_AGAIN: return MQTTErrorCode.MQTT_ERR_SUCCESS return MQTTErrorCode.MQTT_ERR_SUCCESS 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 on. Use `want_write()` to determine if there is data waiting to be written. Do not use if you are using `loop_start()` or `loop_forever()`.""" if self._sock is None: return MQTTErrorCode.MQTT_ERR_NO_CONN try: rc = self._packet_write() if rc == MQTTErrorCode.MQTT_ERR_AGAIN: return MQTTErrorCode.MQTT_ERR_SUCCESS elif rc > 0: return self._loop_rc_handle(rc) else: 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) -> bool: """Call to determine if there is network data waiting to be written. Useful if you are calling select() yourself rather than using `loop()`, `loop_start()` or `loop_forever()`. """ return len(self._out_packet) > 0 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 `loop_start()` or `loop_forever()`.""" if self._sock is None: return MQTTErrorCode.MQTT_ERR_NO_CONN now = time_func() self._check_keepalive() if self._ping_t > 0 and now - self._ping_t >= self._keepalive: # client->ping_t != 0 means we are waiting for a pingresp. # This hasn't happened in the keepalive time so we should disconnect. self._sock_close() if self._state in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED): self._state = _ConnectionState.MQTT_CS_DISCONNECTED rc = MQTTErrorCode.MQTT_ERR_SUCCESS else: self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST rc = MQTTErrorCode.MQTT_ERR_KEEPALIVE self._do_on_disconnect( packet_from_broker=False, v1_rc=rc, ) return MQTTErrorCode.MQTT_ERR_CONN_LOST return MQTTErrorCode.MQTT_ERR_SUCCESS 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.""" self.max_inflight_messages = inflight 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 not isinstance(queue_size, int): raise ValueError('Invalid type of queue size.') self.max_queued_messages = queue_size return self 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 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. :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.') if qos < 0 or qos > 2: raise ValueError('Invalid QoS level.') if properties and not isinstance(properties, Properties): raise ValueError( "The properties argument must be an instance of the Properties class.") 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) -> None: """ Removes a will that was previously configured with `will_set()`. Must be called before connect() to have any effect.""" self._will = False self._will_topic = b"" self._will_payload = b"" self._will_qos = 0 self._will_retain = False def socket(self) -> SocketLike | None: """Return the socket or ssl object for this client.""" return self._sock 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 it will return. :param int timeout: The time in seconds to wait for incoming/outgoing network traffic before timing out and returning. :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: if the first connection fail unless retry_first_connection=True """ run = True while run: if self._thread_terminate is True: break if self._state == _ConnectionState.MQTT_CS_CONNECT_ASYNC: try: self.reconnect() except OSError: self._handle_on_connect_fail() if not retry_first_connection: raise self._easy_log( MQTT_LOG_DEBUG, "Connection failed, retrying") self._reconnect_wait() else: break while run: 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 # in multi threaded mode when loop_stop() has been called and # so no other threads can access _out_packet or _messages. if (self._thread_terminate is True and len(self._out_packet) == 0 and len(self._out_messages) == 0): rc = MQTTErrorCode.MQTT_ERR_NOMEM run = False 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 else: self._reconnect_wait() if should_exit(): run = False else: try: self.reconnect() except OSError: self._handle_on_connect_fail() self._easy_log( MQTT_LOG_DEBUG, "Connection failed, retrying") return rc 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. 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 MQTTErrorCode.MQTT_ERR_INVAL self._sockpairR, self._sockpairW = _socketpair_compat() self._thread_terminate = False self._thread = threading.Thread(target=self._thread_main, name=f"paho-mqtt-client-{self._client_id.decode()}") self._thread.daemon = True self._thread.start() 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 will block until the network thread finishes. 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 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 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. 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:: log_callback(client, userdata, level, buf) :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. :param str buf: the message itself 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) -> Callable[[CallbackOnLog], CallbackOnLog]: def decorator(func: CallbackOnLog) -> CallbackOnLog: self.on_log = func return func return decorator @property def on_pre_connect(self) -> CallbackOnPreConnect | None: """The callback called immediately prior to the connection is made request. 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 it's v5.0:: 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 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. 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, ) -> Callable[[CallbackOnConnect], CallbackOnConnect]: def decorator(func: CallbackOnConnect) -> CallbackOnConnect: self.on_connect = func return func return decorator @property def on_connect_fail(self) -> CallbackOnConnectFail | None: """The callback called when the client failed to connect to the broker. Expected signature is (for all callback_api_version):: connect_fail_callback(client, userdata) :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 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, ) -> Callable[[CallbackOnConnectFail], CallbackOnConnectFail]: def decorator(func: CallbackOnConnectFail) -> CallbackOnConnectFail: self.on_connect_fail = func return func return decorator @property def on_subscribe(self) -> CallbackOnSubscribe | None: """The callback called when the broker responds to a subscribe request. 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, ) -> Callable[[CallbackOnSubscribe], CallbackOnSubscribe]: def decorator(func: CallbackOnSubscribe) -> CallbackOnSubscribe: self.on_subscribe = func return func return decorator @property 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 unless a `message_callback_add()` matched the message. Expected signature is (for all callback API version): message_callback(client, userdata, message) :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 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, ) -> Callable[[CallbackOnMessage], CallbackOnMessage]: def decorator(func: CallbackOnMessage) -> CallbackOnMessage: self.on_message = func return func return decorator @property 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. See also `wait_for_publish` which could be simpler to use. Expected signature for callback API version 2:: publish_callback(client, userdata, mid, reason_code, properties) Expected signature for callback API version 1:: 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, ) -> Callable[[CallbackOnPublish], CallbackOnPublish]: def decorator(func: CallbackOnPublish) -> CallbackOnPublish: self.on_publish = func return func return decorator @property def on_unsubscribe(self) -> CallbackOnUnsubscribe | None: """The callback called when the broker responds to an unsubscribe request. Expected signature for callback API version 2:: unsubscribe_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:: unsubscribe_callback(client, userdata, mid) * 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, ) -> Callable[[CallbackOnUnsubscribe], CallbackOnUnsubscribe]: def decorator(func: CallbackOnUnsubscribe) -> CallbackOnUnsubscribe: self.on_unsubscribe = func return func return decorator @property def on_disconnect(self) -> CallbackOnDisconnect | None: """The callback called when the client disconnects from the broker. 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) * For MQTT it's v5.0:: disconnect_callback(client, userdata, 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 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 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, ) -> Callable[[CallbackOnDisconnect], CallbackOnDisconnect]: def decorator(func: CallbackOnDisconnect) -> CallbackOnDisconnect: self.on_disconnect = func return func return decorator @property 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 (for all callback API version):: socket_open_callback(client, userdata, socket) :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 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, ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self.on_socket_open = func return func return decorator 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 if on_socket_open: with self._in_callback_mutex: try: 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) if not self.suppress_exceptions: raise @property 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 (for all callback API version):: socket_close_callback(client, userdata, socket) :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 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, ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self.on_socket_close = func return func return decorator 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 if on_socket_close: with self._in_callback_mutex: try: on_socket_close(self, self._userdata, sock) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_socket_close: %s', err) if not self.suppress_exceptions: raise @property 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 (for all callback API version):: socket_register_write_callback(client, userdata, socket) :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 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, ) -> Callable[[CallbackOnSocket], CallbackOnSocket]: def decorator(func: CallbackOnSocket) -> CallbackOnSocket: self._on_socket_register_write = func return func return decorator 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 self._registered_write = True with self._callback_mutex: on_socket_register_write = self.on_socket_register_write if on_socket_register_write: try: on_socket_register_write( self, self._userdata, self._sock) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_socket_register_write: %s', err) if not self.suppress_exceptions: raise @property 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 (for all callback API version):: socket_unregister_write_callback(client, userdata, socket) :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 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, ) -> 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: 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: return self._registered_write = False with self._callback_mutex: on_socket_unregister_write = self.on_socket_unregister_write if on_socket_unregister_write: try: on_socket_unregister_write(self, self._userdata, sock) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_socket_unregister_write: %s', err) if not self.suppress_exceptions: raise 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` callback. Call multiple times with different 'sub' to define multiple topic specific callbacks. Topic specific callbacks may be removed with `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: 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: str) -> None: """Remove a message callback previously registered with `message_callback_add()`.""" if sub is None: raise ValueError("sub must defined.") with self._callback_mutex: try: del self._on_message_filtered[sub] except KeyError: # no such subscription pass # ============================================================ # Private functions # ============================================================ def _loop_rc_handle( self, rc: MQTTErrorCode, ) -> MQTTErrorCode: if rc: self._sock_close() 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) if rc == MQTT_ERR_CONN_LOST: self._state = _ConnectionState.MQTT_CS_CONNECTION_LOST return rc 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. # If we've not got a command, attempt to read one and save it. This should # always work because it's only a single byte. # Then try to read the remaining length. This may fail because it is may # be more than one byte - will need to save data pending next read if it # does fail. # Then try to read the remaining payload, where 'payload' here means the # combined variable header and actual payload. This is the most likely to # fail due to longer length, so save current data and current position. # After all data is read, send to _mqtt_handle_packet() to deal with. # Finally, free the memory and reset everything to starting conditions. if self._in_packet['command'] == 0: try: command = self._sock_recv(1) except BlockingIOError: 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 MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(command) == 0: return MQTTErrorCode.MQTT_ERR_CONN_LOST self._in_packet['command'] = command[0] if self._in_packet['have_remaining'] == 0: # Read remaining # Algorithm for decoding taken from pseudo code at # http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/topic/com.ibm.etools.mft.doc/ac10870_.htm while True: try: byte = self._sock_recv(1) except BlockingIOError: return MQTTErrorCode.MQTT_ERR_AGAIN except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(byte) == 0: 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 MQTTErrorCode.MQTT_ERR_PROTOCOL self._in_packet['remaining_length'] += ( byte_value & 127) * self._in_packet['remaining_mult'] self._in_packet['remaining_mult'] = self._in_packet['remaining_mult'] * 128 if (byte_value & 128) == 0: break self._in_packet['have_remaining'] = 1 self._in_packet['to_process'] = self._in_packet['remaining_length'] count = 100 # Don't get stuck in this loop if we have a huge message. while self._in_packet['to_process'] > 0: try: data = self._sock_recv(self._in_packet['to_process']) except BlockingIOError: return MQTTErrorCode.MQTT_ERR_AGAIN except OSError as err: self._easy_log( MQTT_LOG_ERR, 'failed to receive on socket: %s', err) return MQTTErrorCode.MQTT_ERR_CONN_LOST else: if len(data) == 0: 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 MQTTErrorCode.MQTT_ERR_AGAIN # All data for this packet is read. self._in_packet['pos'] = 0 rc = self._packet_handle() # 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, } with self._msgtime_mutex: self._last_msg_in = time_func() return rc def _packet_write(self) -> MQTTErrorCode: while True: try: packet = self._out_packet.popleft() except IndexError: return MQTTErrorCode.MQTT_ERR_SUCCESS try: write_length = self._sock_send( packet['packet'][packet['pos']:]) except (AttributeError, ValueError): self._out_packet.appendleft(packet) return MQTTErrorCode.MQTT_ERR_SUCCESS except BlockingIOError: self._out_packet.appendleft(packet) 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 MQTTErrorCode.MQTT_ERR_CONN_LOST if write_length > 0: packet['to_process'] -= write_length packet['pos'] += write_length if packet['to_process'] == 0: if (packet['command'] & 0xF0) == PUBLISH and packet['qos'] == 0: with self._callback_mutex: on_publish = self.on_publish if on_publish: with self._in_callback_mutex: try: 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 # 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( packet_from_broker=False, v1_rc=MQTTErrorCode.MQTT_ERR_SUCCESS, ) self._sock_close() # 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 self._out_packet.appendleft(packet) else: break with self._msgtime_mutex: self._last_msg_out = time_func() return MQTTErrorCode.MQTT_ERR_SUCCESS 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: # 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) -> None: if self._keepalive == 0: return now = time_func() with self._msgtime_mutex: last_msg_out = self._last_msg_out 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 == _ConnectionState.MQTT_CS_CONNECTED and self._ping_t == 0: try: self._send_pingreq() except Exception: self._sock_close() 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 self._last_msg_in = now else: self._sock_close() 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 = MQTTErrorCode.MQTT_ERR_KEEPALIVE self._do_on_disconnect( packet_from_broker=False, v1_rc=rc, ) def _mid_generate(self) -> int: with self._mid_generate_mutex: self._last_mid += 1 if self._last_mid == 65536: self._last_mid = 1 return self._last_mid @staticmethod 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: 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 MQTTErrorCode.MQTT_ERR_INVAL else: return MQTTErrorCode.MQTT_ERR_SUCCESS def _send_pingreq(self) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PINGREQ") rc = self._send_simple_command(PINGREQ) if rc == MQTTErrorCode.MQTT_ERR_SUCCESS: self._ping_t = time_func() return rc def _send_pingresp(self) -> MQTTErrorCode: self._easy_log(MQTT_LOG_DEBUG, "Sending PINGRESP") return self._send_simple_command(PINGRESP) 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: 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: bytearray, remaining_length: int ) -> bytearray: remaining_bytes = [] 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 |= 0x80 remaining_bytes.append(byte) packet.append(byte) if remaining_length == 0: # FIXME - this doesn't deal with incorrectly large payloads return packet 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: int, topic: bytes, payload: bytes = 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 if not isinstance(topic, bytes): raise TypeError('topic must be bytes, not str') if payload and not isinstance(payload, bytes): raise TypeError('payload must be bytes if set') if self._sock is None: return MQTTErrorCode.MQTT_ERR_NO_CONN command = PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain packet = bytearray() packet.append(command) payloadlen = len(payload) remaining_length = 2 + len(topic) + payloadlen if payloadlen == 0: if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s (NULL payload)", dup, qos, retain, mid, topic, properties ) else: self._easy_log( MQTT_LOG_DEBUG, "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s' (NULL payload)", dup, qos, retain, mid, topic ) else: if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s, ... (%d bytes)", dup, qos, retain, mid, topic, properties, payloadlen ) else: self._easy_log( MQTT_LOG_DEBUG, "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', ... (%d bytes)", dup, qos, retain, mid, topic, payloadlen ) if qos > 0: # For message id remaining_length += 2 if self._protocol == MQTTv5: if properties is None: packed_properties = b'\x00' else: packed_properties = properties.pack() remaining_length += len(packed_properties) self._pack_remaining_length(packet, remaining_length) self._pack_str16(packet, topic) if qos > 0: # For message id packet.extend(struct.pack("!H", mid)) if self._protocol == MQTTv5: packet.extend(packed_properties) packet.extend(payload) return self._packet_queue(PUBLISH, packet, mid, qos, info) 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: 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: int, mid: int, dup: int) -> MQTTErrorCode: # For PUBACK, PUBCOMP, PUBREC, and PUBREL if dup: command |= 0x8 remaining_length = 2 packet = struct.pack('!BBH', command, remaining_length, mid) return self._packet_queue(command, packet, mid, 1) 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: int) -> MQTTErrorCode: proto_ver = self._protocol # hard-coded UTF-8 encoded string protocol = b"MQTT" if proto_ver >= MQTTv311 else b"MQIsdp" remaining_length = 2 + len(protocol) + 1 + \ 1 + 2 + 2 + len(self._client_id) connect_flags = 0 if self._protocol == MQTTv5: 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 elif self._clean_session: connect_flags |= 0x02 if self._will: remaining_length += 2 + \ len(self._will_topic) + 2 + len(self._will_payload) connect_flags |= 0x04 | ((self._will_qos & 0x03) << 3) | ( (self._will_retain & 0x01) << 5) if self._username is not None: remaining_length += 2 + len(self._username) connect_flags |= 0x80 if self._password is not None: connect_flags |= 0x40 remaining_length += 2 + len(self._password) if self._protocol == MQTTv5: if self._connect_properties is None: packed_connect_properties = b'\x00' else: packed_connect_properties = self._connect_properties.pack() remaining_length += len(packed_connect_properties) if self._will: if self._will_properties is None: packed_will_properties = b'\x00' else: packed_will_properties = self._will_properties.pack() remaining_length += len(packed_will_properties) command = CONNECT packet = bytearray() packet.append(command) # as per the mosquitto broker, if the MSB of this version is set # to 1, then it treats the connection as a bridge if self._client_mode == MQTT_BRIDGE: proto_ver |= 0x80 self._pack_remaining_length(packet, remaining_length) 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 self._pack_str16(packet, self._client_id) if self._will: if self._protocol == MQTTv5: packet += packed_will_properties self._pack_str16(packet, self._will_topic) self._pack_str16(packet, self._will_payload) if self._username is not None: self._pack_str16(packet, self._username) if self._password is not None: self._pack_str16(packet, self._password) self._keepalive = keepalive if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Sending CONNECT (u%d, p%d, wr%d, wq%d, wf%d, c%d, k%d) client_id=%s properties=%s", (connect_flags & 0x80) >> 7, (connect_flags & 0x40) >> 6, (connect_flags & 0x20) >> 5, (connect_flags & 0x18) >> 3, (connect_flags & 0x4) >> 2, (connect_flags & 0x2) >> 1, keepalive, self._client_id, self._connect_properties ) else: self._easy_log( MQTT_LOG_DEBUG, "Sending CONNECT (u%d, p%d, wr%d, wq%d, wf%d, c%d, k%d) client_id=%s", (connect_flags & 0x80) >> 7, (connect_flags & 0x40) >> 6, (connect_flags & 0x20) >> 5, (connect_flags & 0x18) >> 3, (connect_flags & 0x4) >> 2, (connect_flags & 0x2) >> 1, keepalive, self._client_id ) return self._packet_queue(command, packet, 0, 0) 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, properties ) else: self._easy_log(MQTT_LOG_DEBUG, "Sending DISCONNECT") remaining_length = 0 command = DISCONNECT packet = bytearray() packet.append(command) if self._protocol == MQTTv5: if properties is not None or reasoncode is not None: if reasoncode is None: reasoncode = ReasonCode(DISCONNECT >> 4, identifier=0) remaining_length += 1 if properties is not None: packed_props = properties.pack() remaining_length += len(packed_props) self._pack_remaining_length(packet, remaining_length) if self._protocol == MQTTv5: if reasoncode is not None: packet += reasoncode.pack() if properties is not None: packet += packed_props return self._packet_queue(command, packet, 0, 0) 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: packed_subscribe_properties = b'\x00' else: packed_subscribe_properties = properties.pack() remaining_length += len(packed_subscribe_properties) for t, _ in topics: remaining_length += 2 + len(t) + 1 command = SUBSCRIBE | (dup << 3) | 0x2 packet = bytearray() packet.append(command) self._pack_remaining_length(packet, remaining_length) local_mid = self._mid_generate() packet.extend(struct.pack("!H", local_mid)) if self._protocol == MQTTv5: packet += packed_subscribe_properties for t, q in topics: self._pack_str16(packet, t) if self._protocol == MQTTv5: packet += q.pack() # type: ignore else: packet.append(q) # type: ignore self._easy_log( MQTT_LOG_DEBUG, "Sending SUBSCRIBE (d%d, m%d) %s", dup, local_mid, topics, ) return (self._packet_queue(command, packet, local_mid, 1), local_mid) 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: packed_unsubscribe_properties = b'\x00' else: packed_unsubscribe_properties = properties.pack() remaining_length += len(packed_unsubscribe_properties) for t in topics: remaining_length += 2 + len(t) command = UNSUBSCRIBE | (dup << 3) | 0x2 packet = bytearray() packet.append(command) self._pack_remaining_length(packet, remaining_length) local_mid = self._mid_generate() packet.extend(struct.pack("!H", local_mid)) if self._protocol == MQTTv5: packet += packed_unsubscribe_properties for t in topics: self._pack_str16(packet, t) # topics_repr = ", ".join("'"+topic.decode('utf8')+"'" for topic in topics) if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Sending UNSUBSCRIBE (d%d, m%d) %s %s", dup, local_mid, properties, topics, ) else: self._easy_log( MQTT_LOG_DEBUG, "Sending UNSUBSCRIBE (d%d, m%d) %s", dup, local_mid, topics, ) return (self._packet_queue(command, packet, local_mid, 1), local_mid) 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 # type: ignore else: return self._clean_session def _messages_reconnect_reset_out(self) -> None: with self._out_message_mutex: self._inflight_messages = 0 for m in self._out_messages.values(): m.timestamp = 0 if self._max_inflight_messages == 0 or self._inflight_messages < self._max_inflight_messages: if m.qos == 0: m.state = mqtt_ms_publish elif m.qos == 1: # self._inflight_messages = self._inflight_messages + 1 if m.state == mqtt_ms_wait_for_puback: m.dup = True m.state = mqtt_ms_publish elif m.qos == 2: # self._inflight_messages = self._inflight_messages + 1 if self._check_clean_session(): if m.state != mqtt_ms_publish: m.dup = True m.state = mqtt_ms_publish else: if m.state == mqtt_ms_wait_for_pubcomp: m.state = mqtt_ms_resend_pubrel else: if m.state == mqtt_ms_wait_for_pubrec: m.dup = True m.state = mqtt_ms_publish else: m.state = mqtt_ms_queued def _messages_reconnect_reset_in(self) -> None: with self._in_message_mutex: if self._check_clean_session(): self._in_messages = collections.OrderedDict() return for m in self._in_messages.values(): m.timestamp = 0 if m.qos != 2: self._in_messages.pop(m.mid) else: # Preserve current state pass def _messages_reconnect_reset(self) -> None: self._messages_reconnect_reset_out() self._messages_reconnect_reset_in() 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) # Write a single byte to sockpairW (connected to sockpairR) to break # out of select() if in threaded mode. if self._sockpairW is not None: try: self._sockpairW.send(sockpair_data) except BlockingIOError: pass # If we have an external event loop registered, use that instead # of calling loop_write() directly. if self._thread is None and self._on_socket_register_write is None: if self._in_callback_mutex.acquire(False): self._in_callback_mutex.release() return self.loop_write() self._call_socket_register_write() return MQTTErrorCode.MQTT_ERR_SUCCESS def _packet_handle(self) -> MQTTErrorCode: cmd = self._in_packet['command'] & 0xF0 if cmd == PINGREQ: return self._handle_pingreq() elif cmd == PINGRESP: return self._handle_pingresp() elif cmd == PUBACK: return self._handle_pubackcomp("PUBACK") elif cmd == PUBCOMP: return self._handle_pubackcomp("PUBCOMP") elif cmd == PUBLISH: return self._handle_publish() elif cmd == PUBREC: return self._handle_pubrec() elif cmd == PUBREL: return self._handle_pubrel() elif cmd == CONNACK: return self._handle_connack() elif cmd == 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 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 MQTTErrorCode.MQTT_ERR_PROTOCOL def _handle_pingreq(self) -> MQTTErrorCode: if self._in_packet['remaining_length'] != 0: return MQTTErrorCode.MQTT_ERR_PROTOCOL self._easy_log(MQTT_LOG_DEBUG, "Received PINGREQ") return self._send_pingresp() def _handle_pingresp(self) -> MQTTErrorCode: if self._in_packet['remaining_length'] != 0: return MQTTErrorCode.MQTT_ERR_PROTOCOL # No longer waiting for a PINGRESP. self._ping_t = 0 self._easy_log(MQTT_LOG_DEBUG, "Received PINGRESP") return MQTTErrorCode.MQTT_ERR_SUCCESS def _handle_connack(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL if self._protocol == MQTTv5: (flags, result) = struct.unpack( "!BB", self._in_packet['packet'][:2]) if result == 1: # This is probably a failure from a broker that doesn't support # MQTT v5. reason = ReasonCode(CONNACK >> 4, aName="Unsupported protocol version") properties = None else: 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: return MQTT_ERR_PROTOCOL self._easy_log( MQTT_LOG_DEBUG, "Received CONNACK (%s, %s), attempting downgrade to MQTT v3.1.", flags, result ) # Downgrade to MQTT v3.1 self._protocol = MQTTv31 return self.reconnect() elif (result == CONNACK_REFUSED_IDENTIFIER_REJECTED and self._client_id == b''): if not self._reconnect_on_failure: return MQTT_ERR_PROTOCOL self._easy_log( MQTT_LOG_DEBUG, "Received CONNACK (%s, %s), attempting to use non-empty CID", flags, result, ) self._client_id = _base62(uuid.uuid4().int, padding=22).encode("utf8") return self.reconnect() if result == 0: self._state = _ConnectionState.MQTT_CS_CONNECTED self._reconnect_delay = None if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Received CONNACK (%s, %s) properties=%s", flags, reason, properties) else: self._easy_log( MQTT_LOG_DEBUG, "Received CONNACK (%s, %s)", flags, result) # it won't be the first successful connect any more self._mqttv5_first_connect = False with self._callback_mutex: on_connect = self.on_connect if on_connect: flags_dict = {} flags_dict['session present'] = flags & 0x01 with self._in_callback_mutex: try: 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, 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) if not self.suppress_exceptions: raise if result == 0: rc = MQTTErrorCode.MQTT_ERR_SUCCESS with self._out_message_mutex: for m in self._out_messages.values(): m.timestamp = time_func() if m.state == mqtt_ms_queued: self.loop_write() # Process outgoing messages that have just been queued up return MQTT_ERR_SUCCESS if m.qos == 0: with self._in_callback_mutex: # Don't call loop_write after _send_publish() rc = self._send_publish( m.mid, m.topic.encode('utf-8'), m.payload, m.qos, m.retain, m.dup, properties=m.properties ) if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc elif m.qos == 1: if m.state == mqtt_ms_publish: self._inflight_messages += 1 m.state = mqtt_ms_wait_for_puback with self._in_callback_mutex: # Don't call loop_write after _send_publish() rc = self._send_publish( m.mid, m.topic.encode('utf-8'), m.payload, m.qos, m.retain, m.dup, properties=m.properties ) if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc elif m.qos == 2: if m.state == mqtt_ms_publish: self._inflight_messages += 1 m.state = mqtt_ms_wait_for_pubrec with self._in_callback_mutex: # Don't call loop_write after _send_publish() rc = self._send_publish( m.mid, m.topic.encode('utf-8'), m.payload, m.qos, m.retain, m.dup, properties=m.properties ) 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 != 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 MQTTErrorCode.MQTT_ERR_CONN_REFUSED else: return MQTTErrorCode.MQTT_ERR_PROTOCOL def _handle_disconnect(self) -> None: packet_type = DISCONNECT >> 4 reasonCode = properties = None if self._in_packet['remaining_length'] > 2: reasonCode = ReasonCode(packet_type) reasonCode.unpack(self._in_packet['packet']) if self._in_packet['remaining_length'] > 3: properties = Properties(packet_type) props, props_len = properties.unpack( self._in_packet['packet'][1:]) self._easy_log(MQTT_LOG_DEBUG, "Received DISCONNECT %s %s", reasonCode, properties ) 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) -> None: self._easy_log(MQTT_LOG_DEBUG, "Received SUBACK") 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 = [ReasonCode(SUBACK >> 4, identifier=c) for c in packet[props_len:]] else: 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 if on_subscribe: with self._in_callback_mutex: # Don't call loop_write after _send_publish() try: 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, ) else: 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 def _handle_publish(self) -> MQTTErrorCode: header = self._in_packet['command'] message = MQTTMessage() message.dup = ((header & 0x08) >> 3) != 0 message.qos = (header & 0x06) >> 1 message.retain = (header & 0x01) != 0 pack_format = f"!H{len(self._in_packet['packet']) - 2}s" (slen, packet) = struct.unpack(pack_format, self._in_packet['packet']) 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 MQTTErrorCode.MQTT_ERR_PROTOCOL # Handle topics with invalid UTF-8 # This replaces an invalid topic with a message and the hex # representation of the topic for logging. When the user attempts to # access message.topic in the callback, an exception will be raised. try: print_topic = topic.decode('utf-8') except UnicodeDecodeError: print_topic = f"TOPIC WITH INVALID UTF-8: {topic!r}" message.topic = topic if message.qos > 0: pack_format = f"!H{len(packet) - 2}s" (message.mid, packet) = struct.unpack(pack_format, packet) if self._protocol == MQTTv5: message.properties = Properties(PUBLISH >> 4) props, props_len = message.properties.unpack(packet) packet = packet[props_len:] message.payload = packet if self._protocol == MQTTv5: self._easy_log( MQTT_LOG_DEBUG, "Received PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s, ... (%d bytes)", message.dup, message.qos, message.retain, message.mid, print_topic, message.properties, len(message.payload) ) else: self._easy_log( MQTT_LOG_DEBUG, "Received PUBLISH (d%d, q%d, r%d, m%d), '%s', ... (%d bytes)", message.dup, message.qos, message.retain, message.mid, print_topic, len(message.payload) ) message.timestamp = time_func() if message.qos == 0: self._handle_on_message(message) return MQTTErrorCode.MQTT_ERR_SUCCESS elif message.qos == 1: self._handle_on_message(message) 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 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) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: 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 = 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: if mid in self._in_messages: # Only pass the message on if we have removed it from the queue - this # prevents multiple callbacks for the same message. message = self._in_messages.pop(mid) self._handle_on_message(message) self._inflight_messages -= 1 if self._max_inflight_messages > 0: with self._out_message_mutex: rc = self._update_inflight() 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 acknowledge this message (thus losing a message) but # avoid hanging. See #284. if self._manual_ack: return MQTTErrorCode.MQTT_ERR_SUCCESS else: return self._send_pubcomp(mid) 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: if m.qos > 0 and m.state == mqtt_ms_queued: self._inflight_messages += 1 if m.qos == 1: m.state = mqtt_ms_wait_for_puback elif m.qos == 2: m.state = mqtt_ms_wait_for_pubrec rc = self._send_publish( m.mid, m.topic.encode('utf-8'), m.payload, m.qos, m.retain, m.dup, properties=m.properties, ) if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc else: return MQTTErrorCode.MQTT_ERR_SUCCESS return MQTTErrorCode.MQTT_ERR_SUCCESS def _handle_pubrec(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: 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 = ReasonCode(PUBREC >> 4) reasonCode.unpack(self._in_packet['packet'][2:]) if self._in_packet['remaining_length'] > 3: properties = Properties(PUBREC >> 4) props, props_len = properties.unpack( self._in_packet['packet'][3:]) self._easy_log(MQTT_LOG_DEBUG, "Received PUBREC (Mid: %d)", mid) with self._out_message_mutex: if mid in self._out_messages: msg = self._out_messages[mid] msg.state = mqtt_ms_wait_for_pubcomp msg.timestamp = time_func() return self._send_pubrel(mid) return MQTTErrorCode.MQTT_ERR_SUCCESS def _handle_unsuback(self) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 4: return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: 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_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: on_unsubscribe = self.on_unsubscribe if on_unsubscribe: with self._in_callback_mutex: try: 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, reasoncodes_list, properties, ) else: 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 MQTTErrorCode.MQTT_ERR_SUCCESS 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._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, disconnect_flags, reason, properties, ) else: 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: 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: 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) if not self.suppress_exceptions: raise msg = self._out_messages.pop(mid) msg.info._set_as_published() if msg.qos > 0: self._inflight_messages -= 1 if self._max_inflight_messages > 0: rc = self._update_inflight() if rc != MQTTErrorCode.MQTT_ERR_SUCCESS: return rc return MQTTErrorCode.MQTT_ERR_SUCCESS def _handle_pubackcomp( self, cmd: Literal['PUBACK'] | Literal['PUBCOMP'] ) -> MQTTErrorCode: if self._protocol == MQTTv5: if self._in_packet['remaining_length'] < 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL elif self._in_packet['remaining_length'] != 2: return MQTTErrorCode.MQTT_ERR_PROTOCOL 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.unpack(self._in_packet['packet'][2:]) if self._in_packet['remaining_length'] > 3: props, props_len = properties.unpack( self._in_packet['packet'][3:]) self._easy_log(MQTT_LOG_DEBUG, "Received %s (Mid: %d)", cmd, mid) 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, reasonCode, properties) return rc return MQTTErrorCode.MQTT_ERR_SUCCESS def _handle_on_message(self, message: MQTTMessage) -> None: try: topic = message.topic except UnicodeDecodeError: topic = None on_message_callbacks = [] with self._callback_mutex: if topic is not None: on_message_callbacks = list(self._on_message_filtered.iter_match(message.topic)) if len(on_message_callbacks) == 0: on_message = self.on_message else: on_message = None for callback in on_message_callbacks: with self._in_callback_mutex: try: callback(self, self._userdata, message) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in user defined callback function %s: %s', callback.__name__, err ) if not self.suppress_exceptions: raise if on_message: with self._in_callback_mutex: try: on_message(self, self._userdata, message) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_message: %s', err) if not self.suppress_exceptions: raise def _handle_on_connect_fail(self) -> None: with self._callback_mutex: on_connect_fail = self.on_connect_fail if on_connect_fail: with self._in_callback_mutex: try: on_connect_fail(self, self._userdata) except Exception as err: self._easy_log( MQTT_LOG_ERR, 'Caught exception in on_connect_fail: %s', err) def _thread_main(self) -> None: self.loop_forever(retry_first_connection=True) def _reconnect_wait(self) -> None: # See reconnect_delay_set for details now = time_func() with self._reconnect_delay_mutex: if self._reconnect_delay is None: self._reconnect_delay = self._reconnect_min_delay else: self._reconnect_delay = min( self._reconnect_delay * 2, self._reconnect_max_delay, ) target_time = now + self._reconnect_delay remaining = target_time - now while (self._state not in (_ConnectionState.MQTT_CS_DISCONNECTING, _ConnectionState.MQTT_CS_DISCONNECTED) and not self._thread_terminate and remaining > 0): time.sleep(min(remaining, 1)) remaining = target_time - time_func() @staticmethod 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 {socks.HTTP, socks.SOCKS4, socks.SOCKS5} and a) if isinstance(p, dict): return check(p.get("proxy_type"), p.get("proxy_addr")) elif isinstance(p, (list, tuple)): return len(p) == 6 and check(p[0], p[1]) else: return False def _get_proxy(self) -> dict[str, Any] | None: if socks is None: return None # First, check if the user explicitly passed us a proxy to use if self._proxy_is_valid(self._proxy): return self._proxy # 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.request, "proxy_bypass") and urllib.request.proxy_bypass(self._host)): env_proxies = urllib.request.getproxies() if "mqtt" in env_proxies: parts = urllib.parse.urlparse(env_proxies["mqtt"]) if parts.scheme == "http": proxy = { "proxy_type": socks.HTTP, "proxy_addr": parts.hostname, "proxy_port": parts.port } return proxy elif parts.scheme == "socks": proxy = { "proxy_type": socks.SOCKS5, "proxy_addr": parts.hostname, "proxy_port": parts.port } return proxy # Finally, check if the user has monkeypatched the PySocks library with # a default proxy socks_default = socks.get_default_proxy() if self._proxy_is_valid(socks_default): proxy_keys = ("proxy_type", "proxy_addr", "proxy_port", "proxy_rdns", "proxy_username", "proxy_password") return dict(zip(proxy_keys, socks_default)) # If we didn't find a proxy through any of the above methods, return # None to indicate that the connection should be handled normally return None def _create_socket(self) -> SocketLike: 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_socket_connection(self) -> _socket.socket: proxy = self._get_proxy() addr = (self._host, self._port) source = (self._bind_address, self._bind_port) 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 return ssl_sock class _WebsocketWrapper: OPCODE_CONTINUATION = 0x0 OPCODE_TEXT = 0x1 OPCODE_BINARY = 0x2 OPCODE_CONNCLOSE = 0x8 OPCODE_PING = 0x9 OPCODE_PONG = 0xa 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 self._host = host self._port = port self._socket = socket self._path = path self._sendbuffer = bytearray() self._readbuffer = bytearray() self._requested_size = 0 self._payload_head = 0 self._readbuffer_head = 0 self._do_handshake(extra_headers) def __del__(self) -> None: self._sendbuffer = bytearray() self._readbuffer = bytearray() 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": host_port, "Upgrade": "websocket", "Connection": "Upgrade", "Origin": f"{http_schema}://{host_port}", "Sec-WebSocket-Key": sec_websocket_key.decode("utf8"), "Sec-Websocket-Version": "13", "Sec-Websocket-Protocol": "mqtt", } # This is checked in ws_set_options so it will either be None, a # dictionary, or a callable if isinstance(extra_headers, dict): websocket_headers.update(extra_headers) elif callable(extra_headers): websocket_headers = extra_headers(websocket_headers) header = "\r\n".join([ f"GET {self._path} HTTP/1.1", "\r\n".join(f"{i}: {j}" for i, j in websocket_headers.items()), "\r\n", ]).encode("utf8") self._socket.send(header) has_secret = False has_upgrade = False while True: # read HTTP response header as lines try: byte = self._socket.recv(1) except ConnectionResetError: byte = b"" self._readbuffer.extend(byte) # line end if byte == b"\n": if len(self._readbuffer) > 2: # check upgrade if b"connection" in str(self._readbuffer).lower().encode('utf-8'): if b"upgrade" not in str(self._readbuffer).lower().encode('utf-8'): raise WebsocketConnectionError( "WebSocket handshake error, connection not upgraded") else: has_upgrade = True # check key hash if b"sec-websocket-accept" in str(self._readbuffer).lower().encode('utf-8'): GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" server_hash_str = self._readbuffer.decode( 'utf-8').split(": ", 1)[1] server_hash = server_hash_str.strip().encode('utf-8') 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( "WebSocket handshake error, invalid secret key") else: has_secret = True else: # ending linebreak break # reset linebuffer self._readbuffer = bytearray() # connection reset elif not byte: raise WebsocketConnectionError("WebSocket handshake error") if not has_upgrade or not has_secret: raise WebsocketConnectionError("WebSocket handshake error") self._readbuffer = bytearray() self.connected = True def _create_frame( self, opcode: int, data: bytearray, do_masking: int = 1 ) -> bytearray: header = bytearray() length = len(data) mask_key = bytearray(os.urandom(4)) mask_flag = do_masking # 1 << 7 is the final flag, we don't send continuated data header.append(1 << 7 | opcode) if length < 126: header.append(mask_flag << 7 | length) elif length < 65536: header.append(mask_flag << 7 | 126) header += struct.pack("!H", length) elif length < 0x8000000000000001: header.append(mask_flag << 7 | 127) header += struct.pack("!Q", length) else: raise ValueError("Maximum payload size is 2^63") if mask_flag == 1: for index in range(length): data[index] ^= mask_key[index % 4] data = mask_key + data return header + data def _buffered_read(self, length: int) -> bytearray: # try to recv and store needed bytes wanted_bytes = length - (len(self._readbuffer) - self._readbuffer_head) if wanted_bytes > 0: data = self._socket.recv(wanted_bytes) if not data: raise ConnectionAbortedError else: self._readbuffer.extend(data) if len(data) < wanted_bytes: raise BlockingIOError self._readbuffer_head += length return self._readbuffer[self._readbuffer_head - length:self._readbuffer_head] def _recv_impl(self, length: int) -> bytes: # try to decode websocket payload part from data try: self._readbuffer_head = 0 result = b"" chunk_startindex = self._payload_head chunk_endindex = self._payload_head + length header1 = self._buffered_read(1) header2 = self._buffered_read(1) opcode = (header1[0] & 0x0f) maskbit = (header2[0] & 0x80) == 0x80 lengthbits = (header2[0] & 0x7f) payload_length = lengthbits mask_key = None # read length if lengthbits == 0x7e: value = self._buffered_read(2) payload_length, = struct.unpack("!H", value) elif lengthbits == 0x7f: value = self._buffered_read(8) payload_length, = struct.unpack("!Q", value) # read mask if maskbit: mask_key = self._buffered_read(4) # if frame payload is shorter than the requested data, read only the possible part readindex = chunk_endindex if payload_length < readindex: readindex = payload_length if readindex > 0: # get payload chunk payload = self._buffered_read(readindex) # unmask only the needed part if mask_key is not None: for index in range(chunk_startindex, readindex): payload[index] ^= mask_key[index % 4] result = payload[chunk_startindex:readindex] self._payload_head = readindex else: payload = bytearray() # check if full frame arrived and reset readbuffer and payloadhead if needed if readindex == payload_length: self._readbuffer = bytearray() self._payload_head = 0 # 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) self._socket.send(frame) if opcode == _WebsocketWrapper.OPCODE_PING: frame = self._create_frame( _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) \ and payload_length > 0: return result else: raise BlockingIOError except ConnectionError: self.connected = False return b'' 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)) self._sendbuffer.extend(frame) self._requested_size = len(data) # try to write out as much as possible length = self._socket.send(self._sendbuffer) self._sendbuffer = self._sendbuffer[length:] if len(self._sendbuffer) == 0: # buffer sent out completely, return with payload's size return self._requested_size else: # couldn't send whole data, request the same data again with 0 as sent length return 0 def recv(self, length: int) -> bytes: return self._recv_impl(length) def read(self, length: int) -> bytes: return self._recv_impl(length) def send(self, data: bytes) -> int: return self._send_impl(data) def write(self, data: bytes) -> int: return self._send_impl(data) def close(self) -> None: self._socket.close() def fileno(self) -> int: return self._socket.fileno() 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() # type: ignore[union-attr] else: # normal socket rely only on select() return 0 def setblocking(self, flag: bool) -> None: self._socket.setblocking(flag) paho.mqtt.python-2.0.0/src/paho/mqtt/enums.py000066400000000000000000000056411456167725100211740ustar00rootroot00000000000000import 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 paho.mqtt.python-2.0.0/src/paho/mqtt/matcher.py000066400000000000000000000053371456167725100214720ustar00rootroot00000000000000class MQTTMatcher: """Intended to manage topic filters including wildcards. Internally, MQTTMatcher use a prefix tree (trie) to store values associated with filters, and has an iter_match() method to iterate efficiently over all filters that match some topic name.""" class Node: __slots__ = '_children', '_content' def __init__(self): self._children = {} self._content = None def __init__(self): self._root = self.Node() def __setitem__(self, key, value): """Add a topic filter :key to the prefix tree and associate it to :value""" node = self._root for sym in key.split('/'): node = node._children.setdefault(sym, self.Node()) node._content = value def __getitem__(self, key): """Retrieve the value associated with some topic filter :key""" try: node = self._root for sym in key.split('/'): node = node._children[sym] if node._content is None: raise KeyError(key) return node._content except KeyError as ke: raise KeyError(key) from ke def __delitem__(self, key): """Delete the value associated with some topic filter :key""" lst = [] try: parent, node = None, self._root for k in key.split('/'): parent, node = node, node._children[k] lst.append((parent, k, node)) # TODO node._content = None 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: break del parent._children[k] def iter_match(self, topic): """Return an iterator on all values associated with filters that match the :topic""" lst = topic.split('/') normal = not topic.startswith('$') def rec(node, i=0): if i == len(lst): if node._content is not None: yield node._content else: part = lst[i] if part in node._children: for content in rec(node._children[part], i + 1): yield content if '+' in node._children and (normal or i > 0): for content in rec(node._children['+'], i + 1): yield content if '#' in node._children and (normal or i > 0): content = node._children['#']._content if content is not None: yield content return rec(self._root) paho.mqtt.python-2.0.0/src/paho/mqtt/packettypes.py000066400000000000000000000026551456167725100224030ustar00rootroot00000000000000""" ******************************************************************* 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 ******************************************************************* """ class PacketTypes: """ Packet types class. Includes the AUTH packet for MQTT v5.0. Holds constants for each packet type such as PacketTypes.PUBLISH and packet name strings: PacketTypes.Names[PacketTypes.PUBLISH]. """ indexes = range(1, 16) # Packet types CONNECT, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, \ PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, \ PINGREQ, PINGRESP, DISCONNECT, AUTH = indexes # Dummy packet type for properties use - will delay only applies to will WILLMESSAGE = 99 Names = ( "reserved", \ "Connect", "Connack", "Publish", "Puback", "Pubrec", "Pubrel", \ "Pubcomp", "Subscribe", "Suback", "Unsubscribe", "Unsuback", \ "Pingreq", "Pingresp", "Disconnect", "Auth") paho.mqtt.python-2.0.0/src/paho/mqtt/properties.py000066400000000000000000000417311456167725100222410ustar00rootroot00000000000000# ******************************************************************* # 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 from .packettypes import PacketTypes class MQTTException(Exception): pass class MalformedPacket(MQTTException): pass def writeInt16(length): # serialize a 16 bit integer to network format return bytearray(struct.pack("!H", length)) def readInt16(buf): # deserialize a 16 bit integer from network format return struct.unpack("!H", buf[:2])[0] def writeInt32(length): # serialize a 32 bit integer to network format return bytearray(struct.pack("!L", length)) def readInt32(buf): # deserialize a 32 bit integer from network format return struct.unpack("!L", buf[:4])[0] def writeUTF(data): # data could be a string, or bytes. If string, encode into bytes with utf-8 if not isinstance(data, bytes): data = bytes(data, "utf-8") return writeInt16(len(data)) + data def readUTF(buffer, maxlen): if maxlen >= 2: length = readInt16(buffer) else: raise MalformedPacket("Not enough data to read string length") maxlen -= 2 if length > maxlen: raise MalformedPacket("Length delimited string too long") buf = buffer[2:2+length].decode("utf-8") # look for chars which are invalid for MQTT for c in buf: # look for D800-DFFF in the UTF string ord_c = ord(c) if ord_c >= 0xD800 and ord_c <= 0xDFFF: raise MalformedPacket("[MQTT-1.5.4-1] D800-DFFF found in UTF-8 data") if ord_c == 0x00: # look for null in the UTF string raise MalformedPacket("[MQTT-1.5.4-2] Null found in UTF-8 data") if ord_c == 0xFEFF: raise MalformedPacket("[MQTT-1.5.4-3] U+FEFF in UTF-8 data") return buf, length+2 def writeBytes(buffer): return writeInt16(len(buffer)) + buffer def readBytes(buffer): length = readInt16(buffer) return buffer[2:2+length], length+2 class VariableByteIntegers: # Variable Byte Integer """ MQTT variable byte integer helper class. Used in several places in MQTT v5.0 properties. """ @staticmethod def encode(x): """ Convert an integer 0 <= x <= 268435455 into multi-byte format. Returns the buffer converted from the integer. """ 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 buffer += bytes([digit]) if x == 0: break return buffer @staticmethod def decode(buffer): """ Get the value of a multi-byte integer from a buffer Return the value, and the number of bytes used. [MQTT-1.5.5-1] the encoded value MUST use the minimum number of bytes necessary to represent the value """ multiplier = 1 value = 0 bytes = 0 while 1: bytes += 1 digit = buffer[0] buffer = buffer[1:] value += (digit & 127) * multiplier if digit & 128 == 0: break multiplier *= 128 return (value, bytes) 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:: 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 name without the spaces. """ def __init__(self, packetType): self.packetType = packetType self.types = ["Byte", "Two Byte Integer", "Four Byte Integer", "Variable Byte Integer", "Binary Data", "UTF-8 Encoded String", "UTF-8 String Pair"] self.names = { "Payload Format Indicator": 1, "Message Expiry Interval": 2, "Content Type": 3, "Response Topic": 8, "Correlation Data": 9, "Subscription Identifier": 11, "Session Expiry Interval": 17, "Assigned Client Identifier": 18, "Server Keep Alive": 19, "Authentication Method": 21, "Authentication Data": 22, "Request Problem Information": 23, "Will Delay Interval": 24, "Request Response Information": 25, "Response Information": 26, "Server Reference": 28, "Reason String": 31, "Receive Maximum": 33, "Topic Alias Maximum": 34, "Topic Alias": 35, "Maximum QoS": 36, "Retain Available": 37, "User Property": 38, "Maximum Packet Size": 39, "Wildcard Subscription Available": 40, "Subscription Identifier Available": 41, "Shared Subscription Available": 42 } self.properties = { # id: type, packets # payload format indicator 1: (self.types.index("Byte"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]), 2: (self.types.index("Four Byte Integer"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]), 3: (self.types.index("UTF-8 Encoded String"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]), 8: (self.types.index("UTF-8 Encoded String"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]), 9: (self.types.index("Binary Data"), [PacketTypes.PUBLISH, PacketTypes.WILLMESSAGE]), 11: (self.types.index("Variable Byte Integer"), [PacketTypes.PUBLISH, PacketTypes.SUBSCRIBE]), 17: (self.types.index("Four Byte Integer"), [PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.DISCONNECT]), 18: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK]), 19: (self.types.index("Two Byte Integer"), [PacketTypes.CONNACK]), 21: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.AUTH]), 22: (self.types.index("Binary Data"), [PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.AUTH]), 23: (self.types.index("Byte"), [PacketTypes.CONNECT]), 24: (self.types.index("Four Byte Integer"), [PacketTypes.WILLMESSAGE]), 25: (self.types.index("Byte"), [PacketTypes.CONNECT]), 26: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK]), 28: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK, PacketTypes.DISCONNECT]), 31: (self.types.index("UTF-8 Encoded String"), [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT, PacketTypes.AUTH]), 33: (self.types.index("Two Byte Integer"), [PacketTypes.CONNECT, PacketTypes.CONNACK]), 34: (self.types.index("Two Byte Integer"), [PacketTypes.CONNECT, PacketTypes.CONNACK]), 35: (self.types.index("Two Byte Integer"), [PacketTypes.PUBLISH]), 36: (self.types.index("Byte"), [PacketTypes.CONNACK]), 37: (self.types.index("Byte"), [PacketTypes.CONNACK]), 38: (self.types.index("UTF-8 String Pair"), [PacketTypes.CONNECT, PacketTypes.CONNACK, PacketTypes.PUBLISH, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP, PacketTypes.SUBSCRIBE, PacketTypes.SUBACK, PacketTypes.UNSUBSCRIBE, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT, PacketTypes.AUTH, PacketTypes.WILLMESSAGE]), 39: (self.types.index("Four Byte Integer"), [PacketTypes.CONNECT, PacketTypes.CONNACK]), 40: (self.types.index("Byte"), [PacketTypes.CONNACK]), 41: (self.types.index("Byte"), [PacketTypes.CONNACK]), 42: (self.types.index("Byte"), [PacketTypes.CONNACK]), } def allowsMultiple(self, compressedName): return self.getIdentFromName(compressedName) in [11, 38] def getIdentFromName(self, compressedName): # return the identifier corresponding to the property name result = -1 for name in self.names.keys(): if compressedName == name.replace(' ', ''): result = self.names[name] break return result def __setattr__(self, name, value): name = name.replace(' ', '') privateVars = ["packetType", "types", "names", "properties"] if name in privateVars: object.__setattr__(self, name, value) else: # 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( 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(f"Property {name} does not apply to packet type {PacketTypes.Names[self.packetType]}") # Check for forbidden values if not isinstance(value, list): if name in ["ReceiveMaximum", "TopicAlias"] \ and (value < 1 or value > 65535): 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(f"{name} property value must be in the range 0-65535") elif name in ["MaximumPacketSize", "SubscriptionIdentifier"] \ and (value < 1 or value > 268435455): 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( f"{name} property value must be 0 or 1") if self.allowsMultiple(name): if not isinstance(value, list): value = [value] if hasattr(self, name): value = object.__getattribute__(self, name) + value object.__setattr__(self, name, value) def __str__(self): buffer = "[" first = True for name in self.names.keys(): compressedName = name.replace(' ', '') if hasattr(self, compressedName): if not first: buffer += ", " buffer += f"{compressedName} : {getattr(self, compressedName)}" first = False buffer += "]" return buffer def json(self): data = {} for name in self.names.keys(): compressedName = name.replace(' ', '') if hasattr(self, compressedName): val = getattr(self, compressedName) if compressedName == 'CorrelationData' and isinstance(val, bytes): data[compressedName] = val.hex() else: data[compressedName] = val return data def isEmpty(self): rc = True for name in self.names.keys(): compressedName = name.replace(' ', '') if hasattr(self, compressedName): rc = False break return rc def clear(self): for name in self.names.keys(): compressedName = name.replace(' ', '') if hasattr(self, compressedName): delattr(self, compressedName) def writeProperty(self, identifier, type, value): buffer = b"" buffer += VariableByteIntegers.encode(identifier) # identifier if type == self.types.index("Byte"): # value buffer += bytes([value]) elif type == self.types.index("Two Byte Integer"): buffer += writeInt16(value) elif type == self.types.index("Four Byte Integer"): buffer += writeInt32(value) elif type == self.types.index("Variable Byte Integer"): buffer += VariableByteIntegers.encode(value) elif type == self.types.index("Binary Data"): buffer += writeBytes(value) elif type == self.types.index("UTF-8 Encoded String"): buffer += writeUTF(value) elif type == self.types.index("UTF-8 String Pair"): buffer += writeUTF(value[0]) + writeUTF(value[1]) return buffer def pack(self): # serialize properties into buffer for sending over network buffer = b"" for name in self.names.keys(): compressedName = name.replace(' ', '') if hasattr(self, compressedName): identifier = self.getIdentFromName(compressedName) attr_type = self.properties[identifier][0] if self.allowsMultiple(compressedName): for prop in getattr(self, compressedName): buffer += self.writeProperty(identifier, attr_type, prop) else: buffer += self.writeProperty(identifier, attr_type, getattr(self, compressedName)) return VariableByteIntegers.encode(len(buffer)) + buffer def readProperty(self, buffer, type, propslen): if type == self.types.index("Byte"): value = buffer[0] valuelen = 1 elif type == self.types.index("Two Byte Integer"): value = readInt16(buffer) valuelen = 2 elif type == self.types.index("Four Byte Integer"): value = readInt32(buffer) valuelen = 4 elif type == self.types.index("Variable Byte Integer"): value, valuelen = VariableByteIntegers.decode(buffer) elif type == self.types.index("Binary Data"): value, valuelen = readBytes(buffer) elif type == self.types.index("UTF-8 Encoded String"): value, valuelen = readUTF(buffer, propslen) elif type == self.types.index("UTF-8 String Pair"): value, valuelen = readUTF(buffer, propslen) buffer = buffer[valuelen:] # strip the bytes used by the value value1, valuelen1 = readUTF(buffer, propslen - valuelen) value = (value, value1) valuelen += valuelen1 return value, valuelen def getNameFromIdent(self, identifier): rc = None for name in self.names: if self.names[name] == identifier: rc = name return rc def unpack(self, buffer): self.clear() # deserialize properties into attributes from buffer received from network propslen, VBIlen = VariableByteIntegers.decode(buffer) buffer = buffer[VBIlen:] # strip the bytes used by the VBI propslenleft = propslen while propslenleft > 0: # properties length is 0 if there are none identifier, VBIlen2 = VariableByteIntegers.decode( buffer) # property identifier buffer = buffer[VBIlen2:] # strip the bytes used by the VBI propslenleft -= VBIlen2 attr_type = self.properties[identifier][0] value, valuelen = self.readProperty( buffer, attr_type, propslenleft) buffer = buffer[valuelen:] # strip the bytes used by the value propslenleft -= valuelen propname = self.getNameFromIdent(identifier) compressedName = propname.replace(' ', '') if not self.allowsMultiple(compressedName) and hasattr(self, compressedName): raise MQTTException( f"Property '{property}' must not exist more than once") setattr(self, propname, value) return self, propslen + VBIlen paho.mqtt.python-2.0.0/src/paho/mqtt/publish.py000066400000000000000000000264031456167725100215120ustar00rootroot00000000000000# Copyright (c) 2014 Roger Light # # 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: # Roger Light - initial API and implementation """ This module provides some helper functions to allow straightforward publishing 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 and nothing else is required. """ from __future__ import annotations import collections from collections.abc import Iterable from typing import TYPE_CHECKING, Any, List, Tuple, Union from paho.mqtt.enums import CallbackAPIVersion 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 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() if isinstance(message, dict): client.publish(**message) elif isinstance(message, (tuple, list)): client.publish(*message) else: raise TypeError('message must be a dict, tuple, or list') 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(reason_code)) def _on_publish( client: paho.Client, userdata: collections.deque[MessagesList], mid: int, reason_codes: ReasonCode, properties: Properties, ) -> None: """Internal callback""" #pylint: disable=unused-argument if len(userdata) == 0: client.disconnect() else: _do_publish(client) 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: int = 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. :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 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) :param str hostname: the address of the broker to connect to. Defaults to localhost. :param int port: the port to connect to the broker on. Defaults to 1883. :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. :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. :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. :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} 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. Alternatively, tls input can be an SSLContext object, which will be processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. :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. :param proxy_args: a dictionary that will be given to the client. """ if not isinstance(msgs, Iterable): raise TypeError('msgs must be an iterable') 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 client.on_connect = _on_connect # type: ignore if proxy_args is not None: client.proxy_set(**proxy_args) if auth: username = auth.get('username') if username: password = auth.get('password') client.username_pw_set(username, password) else: raise KeyError("The 'username' key was not found, this is " "required for auth") if will is not None: client.will_set(**will) if tls is not None: if isinstance(tls, dict): insecure = tls.pop('insecure', False) # 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. client.tls_insecure_set(insecure) else: # Assume input is SSLContext object client.tls_set_context(tls) client.connect(hostname, port, keepalive) client.loop_forever() 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: int = 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. :param str topic: the only required argument must be the topic string to which the payload will be published. :param payload: the payload to be published. If "" or None, a zero length payload will be published. :param int qos: the qos to use when publishing, default to 0. :param bool retain: set the message to be retained (True) or not (False). :param str hostname: the address of the broker to connect to. Defaults to localhost. :param int port: the port to connect to the broker on. Defaults to 1883. :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. :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. :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. :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} 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. Alternatively, tls input can be an SSLContext object, which will be processed using the tls_set_context method. :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. :param proxy_args: a dictionary that will be given to the client. """ msg: MessageDict = {'topic':topic, 'payload':payload, 'qos':qos, 'retain':retain} multiple([msg], hostname, port, client_id, keepalive, will, auth, tls, protocol, transport, proxy_args) paho.mqtt.python-2.0.0/src/paho/mqtt/py.typed000066400000000000000000000000001456167725100211520ustar00rootroot00000000000000paho.mqtt.python-2.0.0/src/paho/mqtt/reasoncodes.py000066400000000000000000000226371456167725100223560ustar00rootroot00000000000000# ******************************************************************* # 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 @functools.total_ordering class ReasonCode: """MQTT version 5.0 reason codes class. 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): """ packetType: the type of the packet, such as PacketTypes.CONNECT that this reason code will be used with. Some reason codes have different names for the same identifier when used a different packet type. aName: the String name of the reason code to be created. Ignored if the identifier is set. identifier: an integer value of the reason code to be created. """ self.packetType = packetType self.names = { 0: {"Success": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.PUBREL, PacketTypes.PUBCOMP, PacketTypes.UNSUBACK, PacketTypes.AUTH], "Normal disconnection": [PacketTypes.DISCONNECT], "Granted QoS 0": [PacketTypes.SUBACK]}, 1: {"Granted QoS 1": [PacketTypes.SUBACK]}, 2: {"Granted QoS 2": [PacketTypes.SUBACK]}, 4: {"Disconnect with will message": [PacketTypes.DISCONNECT]}, 16: {"No matching subscribers": [PacketTypes.PUBACK, PacketTypes.PUBREC]}, 17: {"No subscription found": [PacketTypes.UNSUBACK]}, 24: {"Continue authentication": [PacketTypes.AUTH]}, 25: {"Re-authenticate": [PacketTypes.AUTH]}, 128: {"Unspecified error": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT], }, 129: {"Malformed packet": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 130: {"Protocol error": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 131: {"Implementation specific error": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT], }, 132: {"Unsupported protocol version": [PacketTypes.CONNACK]}, 133: {"Client identifier not valid": [PacketTypes.CONNACK]}, 134: {"Bad user name or password": [PacketTypes.CONNACK]}, 135: {"Not authorized": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT], }, 136: {"Server unavailable": [PacketTypes.CONNACK]}, 137: {"Server busy": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 138: {"Banned": [PacketTypes.CONNACK]}, 139: {"Server shutting down": [PacketTypes.DISCONNECT]}, 140: {"Bad authentication method": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 141: {"Keep alive timeout": [PacketTypes.DISCONNECT]}, 142: {"Session taken over": [PacketTypes.DISCONNECT]}, 143: {"Topic filter invalid": [PacketTypes.SUBACK, PacketTypes.UNSUBACK, PacketTypes.DISCONNECT]}, 144: {"Topic name invalid": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]}, 145: {"Packet identifier in use": [PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.UNSUBACK]}, 146: {"Packet identifier not found": [PacketTypes.PUBREL, PacketTypes.PUBCOMP]}, 147: {"Receive maximum exceeded": [PacketTypes.DISCONNECT]}, 148: {"Topic alias invalid": [PacketTypes.DISCONNECT]}, 149: {"Packet too large": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 150: {"Message rate too high": [PacketTypes.DISCONNECT]}, 151: {"Quota exceeded": [PacketTypes.CONNACK, PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.SUBACK, PacketTypes.DISCONNECT], }, 152: {"Administrative action": [PacketTypes.DISCONNECT]}, 153: {"Payload format invalid": [PacketTypes.PUBACK, PacketTypes.PUBREC, PacketTypes.DISCONNECT]}, 154: {"Retain not supported": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 155: {"QoS not supported": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 156: {"Use another server": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 157: {"Server moved": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 158: {"Shared subscription not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]}, 159: {"Connection rate exceeded": [PacketTypes.CONNACK, PacketTypes.DISCONNECT]}, 160: {"Maximum connect time": [PacketTypes.DISCONNECT]}, 161: {"Subscription identifiers not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]}, 162: {"Wildcard subscription not supported": [PacketTypes.SUBACK, PacketTypes.DISCONNECT]}, } if identifier == -1: if packetType == PacketTypes.DISCONNECT and aName == "Success": aName = "Normal disconnection" self.set(aName) else: self.value = identifier self.getName() # check it's good def __getName__(self, packetType, identifier): """ Get the reason code string name for a specific identifier. The name can vary by packet type for the same identifier, which is why the packet type is also required. Used when displaying the reason code. """ 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]] if len(namelist) != 1: raise ValueError(f"Expected exactly one name, found {namelist!r}") return namelist[0] def getId(self, name): """ Get the numeric id corresponding to a reason code name. Used when setting the reason code for a packetType check that only valid codes for the packet are set. """ for code in self.names.keys(): if name in self.names[code].keys(): if self.packetType in self.names[code][name]: 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] name = self.__getName__(self.packetType, c) self.value = self.getId(name) return 1 def getName(self): """Returns the reason code name corresponding to the numeric value which is set. """ return self.__getName__(self.packetType, self.value) def __eq__(self, other): if isinstance(other, int): return self.value == other if isinstance(other, str): 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() def json(self): return self.getName() 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) paho.mqtt.python-2.0.0/src/paho/mqtt/subscribe.py000066400000000000000000000266231456167725100220310ustar00rootroot00000000000000# Copyright (c) 2016 Roger Light # # 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: # Roger Light - initial API and implementation """ This module provides some helper functions to allow straightforward subscribing to topics and retrieving messages. The two functions are simple(), which returns one or messages matching a set of topics, and callback() which allows you to pass a callback for processing of messages. """ from .. import mqtt from . import client as paho def _on_connect(client, userdata, flags, reason_code, properties): """Internal callback""" if reason_code != 0: raise mqtt.MQTTException(paho.connack_string(reason_code)) if isinstance(userdata['topics'], list): for topic in userdata['topics']: client.subscribe(topic, userdata['qos']) else: client.subscribe(userdata['topics'], userdata['qos']) def _on_message_callback(client, userdata, message): """Internal callback""" userdata['callback'](client, userdata['userdata'], message) def _on_message_simple(client, userdata, message): """Internal callback""" if userdata['msg_count'] == 0: return # Don't process stale retained messages if 'retained' was false if message.retain and not userdata['retained']: return userdata['msg_count'] = userdata['msg_count'] - 1 if userdata['messages'] is None and userdata['msg_count'] == 0: userdata['messages'] = message client.disconnect() return userdata['messages'].append(message) if userdata['msg_count'] == 0: client.disconnect() def callback(callback, topics, qos=0, userdata=None, hostname="localhost", port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, protocol=paho.MQTTv311, transport="tcp", clean_session=True, proxy_args=None): """Subscribe to a list of topics and process them in a callback function. This function creates an MQTT client, connects to a broker and subscribes to a list of topics. Incoming messages are processed by the user provided callback. This is a blocking function and will never return. :param callback: function with the same signature as `on_message` for processing the messages received. :param topics: either a string containing a single topic to subscribe to, or a list of topics to subscribe to. :param int qos: the qos to use when subscribing. This is applied to all topics. :param userdata: passed to the callback :param str hostname: the address of the broker to connect to. Defaults to localhost. :param int port: the port to connect to the broker on. Defaults to 1883. :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. :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. :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. :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} 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. Alternatively, tls input can be an SSLContext object, which will be processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. :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. :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. :param proxy_args: a dictionary that will be given to the client. """ if qos < 0 or qos > 2: raise ValueError('qos must be in the range 0-2') callback_userdata = { 'callback':callback, 'topics':topics, 'qos':qos, 'userdata':userdata} 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 client.on_connect = _on_connect if proxy_args is not None: client.proxy_set(**proxy_args) if auth: username = auth.get('username') if username: password = auth.get('password') client.username_pw_set(username, password) else: raise KeyError("The 'username' key was not found, this is " "required for auth") if will is not None: client.will_set(**will) if tls is not None: if isinstance(tls, dict): insecure = tls.pop('insecure', False) client.tls_set(**tls) if insecure: # Must be set *after* the `client.tls_set()` call since it sets # up the SSL context that `client.tls_insecure_set` alters. client.tls_insecure_set(insecure) else: # Assume input is SSLContext object client.tls_set_context(tls) client.connect(hostname, port, keepalive) client.loop_forever() def simple(topics, qos=0, msg_count=1, retained=True, hostname="localhost", port=1883, client_id="", keepalive=60, will=None, auth=None, tls=None, protocol=paho.MQTTv311, transport="tcp", clean_session=True, proxy_args=None): """Subscribe to a list of topics and return msg_count messages. This function creates an MQTT client, connects to a broker and subscribes to a list of topics. Once "msg_count" messages have been received, it disconnects cleanly from the broker and returns the messages. :param topics: either a string containing a single topic to subscribe to, or a list of topics to subscribe to. :param int qos: the qos to use when subscribing. This is applied to all topics. :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. :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. :param str hostname: the address of the broker to connect to. Defaults to localhost. :param int port: the port to connect to the broker on. Defaults to 1883. :param str client_id: the MQTT client id to use. If "" or None, the Paho library will generate a client id automatically. :param int keepalive: the keepalive timeout value for the client. Defaults to 60 seconds. :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. :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. :param tls: a dict containing TLS configuration parameters for the client: dict = {'ca_certs':"", 'certfile':"", 'keyfile':"", 'tls_version':"", 'ciphers':", 'insecure':""} 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. Alternatively, tls input can be an SSLContext object, which will be processed using the tls_set_context method. Defaults to None, which indicates that TLS should not be used. :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. :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. If protocol is MQTTv50, clean_session is ignored. :param proxy_args: a dictionary that will be given to the client. """ if msg_count < 1: raise ValueError('msg_count must be > 0') # Set ourselves up to return a single message if msg_count == 1, or a list # if > 1. if msg_count == 1: messages = None 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, client_id, keepalive, will, auth, tls, protocol, transport, clean_session, proxy_args) return userdata['messages'] paho.mqtt.python-2.0.0/src/paho/mqtt/subscribeoptions.py000066400000000000000000000113561456167725100234420ustar00rootroot00000000000000""" ******************************************************************* 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 ******************************************************************* """ class MQTTException(Exception): pass class SubscribeOptions: """The MQTT v5.0 subscribe options class. The options are: qos: As in MQTT v3.1.1. noLocal: True or False. If set to True, the subscriber will not receive its own publications. retainAsPublished: True or False. If set to True, the retain flag on received publications will be as set by the publisher. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND Controls when the broker should send retained messages: - RETAIN_SEND_ON_SUBSCRIBE: on any successful subscribe request - RETAIN_SEND_IF_NEW_SUB: only if the subscribe request is new - RETAIN_DO_NOT_SEND: never send retained messages """ # retain handling options RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB, RETAIN_DO_NOT_SEND = range( 0, 3) 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. retainAsPublished: True or False. False is the default and corresponds to MQTT v3.1.1 behavior. retainHandling: RETAIN_SEND_ON_SUBSCRIBE, RETAIN_SEND_IF_NEW_SUB or RETAIN_DO_NOT_SEND RETAIN_SEND_ON_SUBSCRIBE is the default and corresponds to MQTT v3.1.1 behavior. """ object.__setattr__(self, "names", ["QoS", "noLocal", "retainAsPublished", "retainHandling"]) self.QoS = qos # bits 0,1 self.noLocal = noLocal # bit 2 self.retainAsPublished = retainAsPublished # bit 3 self.retainHandling = retainHandling # bits 4 and 5: 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( f"{name} Attribute name must be one of {self.names}") object.__setattr__(self, name, value) def pack(self): 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] return bytes(data) def unpack(self, buffer): b0 = buffer[0] self.retainHandling = ((b0 >> 4) & 0x03) self.retainAsPublished = True if ((b0 >> 3) & 0x01) == 1 else False self.noLocal = True if ((b0 >> 2) & 0x01) == 1 else False self.QoS = (b0 & 0x03) 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): return str(self) def __str__(self): return "{QoS="+str(self.QoS)+", noLocal="+str(self.noLocal) +\ ", retainAsPublished="+str(self.retainAsPublished) +\ ", retainHandling="+str(self.retainHandling)+"}" def json(self): data = { "QoS": self.QoS, "noLocal": self.noLocal, "retainAsPublished": self.retainAsPublished, "retainHandling": self.retainHandling, } return data paho.mqtt.python-2.0.0/tests/000077500000000000000000000000001456167725100161245ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/__init__.py000066400000000000000000000000001456167725100202230ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/consts.py000066400000000000000000000001661456167725100200120ustar00rootroot00000000000000import pathlib tests_path = pathlib.Path(__file__).parent lib_path = tests_path.parent ssl_path = tests_path / "ssl" paho.mqtt.python-2.0.0/tests/debug_helpers.py000066400000000000000000000212551456167725100213130ustar00rootroot00000000000000import 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}") paho.mqtt.python-2.0.0/tests/lib/000077500000000000000000000000001456167725100166725ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/lib/__init__.py000066400000000000000000000000001456167725100207710ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/lib/clients/000077500000000000000000000000001456167725100203335ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/lib/clients/01-asyncio.py000066400000000000000000000047651456167725100226040ustar00rootroot00000000000000import 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()) paho.mqtt.python-2.0.0/tests/lib/clients/01-decorators.py000066400000000000000000000017321456167725100232730ustar00rootroot00000000000000import paho.mqtt.client as mqtt 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): pass # TODO: should probably test that this gets called mqttc.connect("localhost", get_test_server_port()) loop_until_keyboard_interrupt(mqttc) paho.mqtt.python-2.0.0/tests/lib/clients/01-keepalive-pingreq.py000066400000000000000000000006331456167725100245350ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-no-clean-session.py000066400000000000000000000004551456167725100243040ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-reconnect-on-failure.py000066400000000000000000000007731456167725100251510ustar00rootroot00000000000000import 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 paho.mqtt.python-2.0.0/tests/lib/clients/01-unpwd-empty-password-set.py000066400000000000000000000004641456167725100260510ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-unpwd-empty-set.py000066400000000000000000000004571456167725100242130ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-unpwd-set.py000066400000000000000000000004751456167725100230570ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-unpwd-unicode-set.py000066400000000000000000000006331456167725100244770ustar00rootroot00000000000000 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-will-set.py000066400000000000000000000005321456167725100226630ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-will-unpwd-set.py000066400000000000000000000006031456167725100240150ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/01-zero-length-clientid.py000066400000000000000000000010271456167725100251520ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/02-subscribe-qos0.py000066400000000000000000000010601456167725100237620ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/02-subscribe-qos1.py000066400000000000000000000010601456167725100237630ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/02-subscribe-qos2.py000066400000000000000000000010601456167725100237640ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/02-unsubscribe.py000066400000000000000000000010541456167725100234500ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-b2c-qos1.py000066400000000000000000000014711456167725100241230ustar00rootroot00000000000000import 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 msg.retain is not False, 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-b2c-qos2.py000066400000000000000000000016131456167725100241220ustar00rootroot00000000000000import paho.mqtt.client as mqtt from tests.paho_test import get_test_server_port, wait_for_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 msg.retain is not False, 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-qos2-test", clean_session=True) mqttc.on_connect = on_connect mqttc.on_message = on_message mqttc.connect("localhost", get_test_server_port()) with wait_for_keyboard_interrupt(): while True: if mqttc.loop(0.3): break paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-c2b-qos1-disconnect.py000066400000000000000000000015261456167725100262530ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-c2b-qos2-disconnect.py000066400000000000000000000014151456167725100262510ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-fill-inflight.py000066400000000000000000000023101456167725100253150ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-helper-qos0-v5.py000066400000000000000000000006371456167725100252660ustar00rootroot00000000000000import 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, ) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-helper-qos0.py000066400000000000000000000005351456167725100247330ustar00rootroot00000000000000import 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", ) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-helper-qos1-disconnect.py000066400000000000000000000005501456167725100270600ustar00rootroot00000000000000import 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", ) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-qos0-no-payload.py000066400000000000000000000011711456167725100255140ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/03-publish-qos0.py000066400000000000000000000012311456167725100234500ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/04-retain-qos0.py000066400000000000000000000007411456167725100232720ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/08-ssl-connect-alpn.py000077500000000000000000000012661456167725100243220ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/08-ssl-connect-cert-auth-pw.py000066400000000000000000000012451456167725100257020ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/08-ssl-connect-cert-auth.py000066400000000000000000000011731456167725100252560ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/08-ssl-connect-no-auth.py000066400000000000000000000007711456167725100247400ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/lib/clients/08-ssl-fake-cacert.py000066400000000000000000000014641456167725100241030ustar00rootroot00000000000000import 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") paho.mqtt.python-2.0.0/tests/lib/conftest.py000066400000000000000000000044261456167725100210770ustar00rootroot00000000000000import 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 paho.mqtt.python-2.0.0/tests/lib/test_01_asyncio.py000066400000000000000000000025371456167725100222570ustar00rootroot00000000000000# Test whether asyncio works 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="sub-test", qos=1) suback_packet = paho_test.gen_suback(mid=1, qos=1) 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("b2c", qos=0, payload="msg") 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() def test_01_asyncio(server_socket, start_client): proc = start_client("01-asyncio.py") (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, "subscribe", subscribe_packet) conn.send(suback_packet) paho_test.expect_packet(conn, "unsubscribe", unsubscribe_packet) conn.send(unsuback_packet) conn.send(publish_packet) paho_test.expect_packet(conn, "publish", publish_packet_in) conn.send(puback_packet_in) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() assert proc.wait() == 0 paho.mqtt.python-2.0.0/tests/lib/test_01_decorators.py000066400000000000000000000025231456167725100227520ustar00rootroot00000000000000# Test whether callback decorators work 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="sub-test", qos=1) suback_packet = paho_test.gen_suback(mid=1, qos=1) 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("b2c", qos=0, payload="msg") 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() def test_01_decorators(server_socket, start_client): start_client("01-decorators.py") (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, "subscribe", subscribe_packet) conn.send(suback_packet) paho_test.expect_packet(conn, "unsubscribe", unsubscribe_packet) conn.send(unsuback_packet) conn.send(publish_packet) paho_test.expect_packet(conn, "publish", publish_packet_in) conn.send(puback_packet_in) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_01_keepalive_pingreq.py000066400000000000000000000017241456167725100243010ustar00rootroot00000000000000# Test whether a client sends a pingreq after the keepalive time # 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 tests.paho_test as paho_test 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() def test_01_keepalive_pingreq(server_socket, start_client): start_client("01-keepalive-pingreq.py") (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, "pingreq", pingreq_packet) time.sleep(1.0) conn.send(pingresp_packet) paho_test.expect_packet(conn, "pingreq", pingreq_packet) paho.mqtt.python-2.0.0/tests/lib/test_01_no_clean_session.py000066400000000000000000000011011456167725100241150ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_reconnect_on_failure.py000066400000000000000000000021171456167725100247670ustar00rootroot00000000000000# 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 paho.mqtt.python-2.0.0/tests/lib/test_01_unpwd_empty_password_set.py000066400000000000000000000012031456167725100257470ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_unpwd_empty_set.py000066400000000000000000000011401456167725100240250ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_unpwd_set.py000066400000000000000000000011531456167725100226130ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_unpwd_unicode_set.py000066400000000000000000000013151456167725100243210ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_will_set.py000066400000000000000000000013771456167725100224350ustar00rootroot00000000000000# Test whether a client produces a correct connect with a will. # Will QoS=1, will retain=1. # 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 tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "01-will-set", keepalive=60, will_topic="topic/on/unexpected/disconnect", will_qos=1, will_retain=True, will_payload="will message") 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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_01_will_unpwd_set.py000066400000000000000000000015241456167725100236440ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_01_zero_length_clientid.py000066400000000000000000000011721456167725100247770ustar00rootroot00000000000000# Test whether a client connects correctly with a zero length clientid. 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() def test_01_zero_length_clientid(server_socket, start_client): start_client("01-zero-length-clientid.py") (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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_02_subscribe_qos0.py000066400000000000000000000026001456167725100235250ustar00rootroot00000000000000# Test whether a client sends a correct SUBSCRIBE to a topic with QoS 0. # 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 # message to subscribe to topic "qos0/test" with QoS=0. If rc!=0, the client # should exit with an error. # Upon receiving the correct SUBSCRIBE message, the test will reply with a # SUBACK message with the accepted QoS set to 0. On receiving the SUBACK # message, the client should send a DISCONNECT message. 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() mid = 1 subscribe_packet = paho_test.gen_subscribe(mid, "qos0/test", 0) suback_packet = paho_test.gen_suback(mid, 0) def test_02_subscribe_qos0(server_socket, start_client): start_client("02-subscribe-qos0.py") (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, "subscribe", subscribe_packet) conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_02_subscribe_qos1.py000066400000000000000000000026001456167725100235260ustar00rootroot00000000000000# Test whether a client sends a correct SUBSCRIBE to a topic with QoS 1. # 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 # message to subscribe to topic "qos1/test" with QoS=1. If rc!=0, the client # should exit with an error. # Upon receiving the correct SUBSCRIBE message, the test will reply with a # SUBACK message with the accepted QoS set to 1. On receiving the SUBACK # message, the client should send a DISCONNECT message. 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() mid = 1 subscribe_packet = paho_test.gen_subscribe(mid, "qos1/test", 1) suback_packet = paho_test.gen_suback(mid, 1) def test_02_subscribe_qos1(server_socket, start_client): start_client("02-subscribe-qos1.py") (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, "subscribe", subscribe_packet) conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_02_subscribe_qos2.py000066400000000000000000000026001456167725100235270ustar00rootroot00000000000000# Test whether a client sends a correct SUBSCRIBE to a topic with QoS 2. # 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 # message to subscribe to topic "qos2/test" with QoS=2. If rc!=0, the client # should exit with an error. # Upon receiving the correct SUBSCRIBE message, the test will reply with a # SUBACK message with the accepted QoS set to 2. On receiving the SUBACK # message, the client should send a DISCONNECT message. 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() mid = 1 subscribe_packet = paho_test.gen_subscribe(mid, "qos2/test", 2) suback_packet = paho_test.gen_suback(mid, 2) def test_02_subscribe_qos2(server_socket, start_client): start_client("02-subscribe-qos2.py") (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, "subscribe", subscribe_packet) conn.send(suback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_02_unsubscribe.py000066400000000000000000000015031456167725100231270ustar00rootroot00000000000000# Test whether a client sends a correct UNSUBSCRIBE packet. 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() mid = 1 unsubscribe_packet = paho_test.gen_unsubscribe(mid, "unsubscribe/test") unsuback_packet = paho_test.gen_unsuback(mid) def test_02_unsubscribe(server_socket, start_client): start_client("02-unsubscribe.py") (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, "unsubscribe", unsubscribe_packet) conn.send(unsuback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_b2c_qos1.py000066400000000000000000000025161456167725100237500ustar00rootroot00000000000000# 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 pytest import tests.paho_test as paho_test 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( "pub/qos1/receive", qos=1, mid=mid, payload="message") puback_packet = paho_test.gen_puback(mid) # msg.retain changed when typing was added @pytest.mark.xfail def test_03_publish_b2c_qos1(server_socket, start_client): start_client("03-publish-b2c-qos1.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, "puback", puback_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_c2b_qos1_disconnect.py000066400000000000000000000025271456167725100261630ustar00rootroot00000000000000# Test whether a client sends a correct PUBLISH to a topic with QoS 1, then responds correctly to a disconnect. import tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "publish-qos1-test", keepalive=60, clean_session=False, ) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() mid = 1 publish_packet = paho_test.gen_publish( "pub/qos1/test", qos=1, mid=mid, payload="message") publish_packet_dup = paho_test.gen_publish( "pub/qos1/test", qos=1, mid=mid, payload="message", dup=True) puback_packet = paho_test.gen_puback(mid) def test_03_publish_c2b_qos1_disconnect(server_socket, start_client): start_client("03-publish-c2b-qos1-disconnect.py") (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "publish", publish_packet) # Disconnect client. It should reconnect. conn.close() (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "retried publish", publish_packet_dup) conn.send(puback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_c2b_qos2_disconnect.py000066400000000000000000000035131456167725100261600ustar00rootroot00000000000000# Test whether a client sends a correct PUBLISH to a topic with QoS 2 and responds to a disconnect. import tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "publish-qos2-test", keepalive=60, clean_session=False, ) connack_packet = paho_test.gen_connack(rc=0) disconnect_packet = paho_test.gen_disconnect() mid = 1 publish_packet = paho_test.gen_publish( "pub/qos2/test", qos=2, mid=mid, payload="message") publish_dup_packet = paho_test.gen_publish( "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) def test_03_publish_c2b_qos2_disconnect(server_socket, start_client): start_client("03-publish-c2b-qos2-disconnect.py") (conn, address) = server_socket.accept() conn.settimeout(5) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "publish", publish_packet) # Disconnect client. It should reconnect. conn.close() (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "retried publish", publish_dup_packet) conn.send(pubrec_packet) paho_test.expect_packet(conn, "pubrel", pubrel_packet) # Disconnect client. It should reconnect. conn.close() (conn, address) = server_socket.accept() conn.settimeout(15) # Complete connection and message flow. paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "retried pubrel", pubrel_packet) conn.send(pubcomp_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_fill_inflight.py000066400000000000000000000055541456167725100251560ustar00rootroot00000000000000# 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) paho.mqtt.python-2.0.0/tests/lib/test_03_publish_helper_qos0.py000066400000000000000000000024261456167725100245600ustar00rootroot00000000000000# 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 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 # to topic "pub/qos0/test" with payload "message" and QoS=0. If rc!=0, the # client should exit with an error. # After sending the PUBLISH message, the client should send a # DISCONNECT message. import tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "publish-helper-qos0-test", keepalive=60, ) connack_packet = paho_test.gen_connack(rc=0) publish_packet = paho_test.gen_publish( "pub/qos0/test", qos=0, payload="message" ) disconnect_packet = paho_test.gen_disconnect() def test_03_publish_helper_qos0(server_socket, start_client): start_client("03-publish-helper-qos0.py") (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) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_helper_qos0_v5.py000066400000000000000000000025231456167725100251700ustar00rootroot00000000000000# 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 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 # to topic "pub/qos0/test" with payload "message" and QoS=0. If rc!=0, the # client should exit with an error. # After sending the PUBLISH message, the client should send a # DISCONNECT message. import tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "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( "pub/qos0/test", qos=0, payload="message", proto_ver=5 ) disconnect_packet = paho_test.gen_disconnect() def test_03_publish_helper_qos0_v5(server_socket, start_client): start_client("03-publish-helper-qos0-v5.py") (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) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_helper_qos1_disconnect.py000066400000000000000000000026141456167725100267710ustar00rootroot00000000000000# 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 tests.paho_test as paho_test connect_packet = paho_test.gen_connect( "publish-helper-qos1-disconnect-test", keepalive=60, ) connack_packet = paho_test.gen_connack(rc=0) mid = 1 publish_packet = paho_test.gen_publish( "pub/qos1/test", qos=1, mid=mid, payload="message" ) publish_packet_dup = paho_test.gen_publish( "pub/qos1/test", qos=1, mid=mid, payload="message", dup=True, ) puback_packet = paho_test.gen_puback(mid) disconnect_packet = paho_test.gen_disconnect() def test_03_publish_helper_qos1_disconnect(server_socket, start_client): start_client("03-publish-helper-qos1-disconnect.py") (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) # Disconnect client. It should reconnect. conn.close() (conn, address) = server_socket.accept() conn.settimeout(15) paho_test.expect_packet(conn, "connect", connect_packet) conn.send(connack_packet) paho_test.expect_packet(conn, "retried publish", publish_packet_dup) conn.send(puback_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_qos0.py000066400000000000000000000023011456167725100232110ustar00rootroot00000000000000# Test whether a client sends a correct PUBLISH to a topic with QoS 0. # 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 # to topic "pub/qos0/test" with payload "message" and QoS=0. If rc!=0, the # client should exit with an error. # After sending the PUBLISH message, the client should send a DISCONNECT message. 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("pub/qos0/test", qos=0, payload="message") disconnect_packet = paho_test.gen_disconnect() 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) conn.send(connack_packet) paho_test.expect_packet(conn, "publish", publish_packet) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_03_publish_qos0_no_payload.py000066400000000000000000000023621456167725100254250ustar00rootroot00000000000000# Test whether a client sends a correct PUBLISH to a topic with QoS 0 and no payload. # 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 # to topic "pub/qos0/no-payload/test" with zero length payload and QoS=0. If # rc!=0, the client should exit with an error. # After sending the PUBLISH message, the client should send a DISCONNECT message. 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("pub/qos0/no-payload/test", qos=0) disconnect_packet = paho_test.gen_disconnect() def test_03_publish_qos0_no_payload(server_socket, start_client): start_client("03-publish-qos0-no-payload.py") (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) paho_test.expect_packet(conn, "disconnect", disconnect_packet) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_04_retain_qos0.py000066400000000000000000000012671456167725100230400ustar00rootroot00000000000000# Test whether a client sends a correct retained PUBLISH to a topic with QoS 0. import tests.paho_test as paho_test mid = 16 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( "retain/qos0/test", qos=0, payload="retained message", retain=True) def test_04_retain_qos0(server_socket, start_client): start_client("04-retain-qos0.py") (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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_bad_cacert.py000066400000000000000000000003621456167725100235430ustar00rootroot00000000000000import 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") paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_connect_alpn.py000077500000000000000000000026421456167725100241450ustar00rootroot00000000000000# 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() paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_connect_cert_auth.py000066400000000000000000000021221456167725100251570ustar00rootroot00000000000000# 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-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 tests.paho_test as paho_test 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() def test_08_ssl_connect_crt_auth(ssl_server_socket, start_client): start_client("08-ssl-connect-cert-auth.py") (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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_connect_cert_auth_pw.py000066400000000000000000000022121456167725100256650ustar00rootroot00000000000000# 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 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 tests.paho_test as paho_test 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() def test_08_ssl_connect_crt_auth_pw(ssl_server_socket, start_client): start_client("08-ssl-connect-cert-auth-pw.py") (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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_connect_no_auth.py000066400000000000000000000020461456167725100246430ustar00rootroot00000000000000# Test whether a client produces a correct connect and subsequent disconnect when using SSL. # # 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 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() def test_08_ssl_connect_no_auth(ssl_server_socket, start_client): start_client("08-ssl-connect-no-auth.py") (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) conn.close() paho.mqtt.python-2.0.0/tests/lib/test_08_ssl_fake_cacert.py000066400000000000000000000004111456167725100237160ustar00rootroot00000000000000import 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() paho.mqtt.python-2.0.0/tests/mqtt5_props.py000066400000000000000000000037721456167725100210040ustar00rootroot00000000000000import struct PROP_PAYLOAD_FORMAT_INDICATOR = 1 PROP_MESSAGE_EXPIRY_INTERVAL = 2 PROP_CONTENT_TYPE = 3 PROP_RESPONSE_TOPIC = 8 PROP_CORRELATION_DATA = 9 PROP_SUBSCRIPTION_IDENTIFIER = 11 PROP_SESSION_EXPIRY_INTERVAL = 17 PROP_ASSIGNED_CLIENT_IDENTIFIER = 18 PROP_SERVER_KEEP_ALIVE = 19 PROP_AUTHENTICATION_METHOD = 21 PROP_AUTHENTICATION_DATA = 22 PROP_REQUEST_PROBLEM_INFO = 23 PROP_WILL_DELAY_INTERVAL = 24 PROP_REQUEST_RESPONSE_INFO = 25 PROP_RESPONSE_INFO = 26 PROP_SERVER_REFERENCE = 28 PROP_REASON_STRING = 31 PROP_RECEIVE_MAXIMUM = 33 PROP_TOPIC_ALIAS_MAXIMUM = 34 PROP_TOPIC_ALIAS = 35 PROP_MAXIMUM_QOS = 36 PROP_RETAIN_AVAILABLE = 37 PROP_USER_PROPERTY = 38 PROP_MAXIMUM_PACKET_SIZE = 39 PROP_WILDCARD_SUB_AVAILABLE = 40 PROP_SUBSCRIPTION_ID_AVAILABLE = 41 PROP_SHARED_SUB_AVAILABLE = 42 def gen_byte_prop(identifier, byte): prop = struct.pack('BB', identifier, byte) return prop def gen_uint16_prop(identifier, word): prop = struct.pack('!BH', identifier, word) return prop def gen_uint32_prop(identifier, word): prop = struct.pack('!BI', identifier, word) return prop def gen_string_prop(identifier, s): s = s.encode("utf-8") 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(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(f"!B{len(v)}s", identifier, v) def pack_varint(varint): s = b"" while True: byte = varint % 128 varint = varint // 128 # If there are more digits to encode, set the top bit of this digit if varint > 0: byte = byte | 0x80 s = s + struct.pack("!B", byte) if varint == 0: return s def prop_finalise(props): if props is None: return pack_varint(0) else: return pack_varint(len(props)) + props paho.mqtt.python-2.0.0/tests/paho_test.py000066400000000000000000000353351456167725100204750ustar00rootroot00000000000000import 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 """ sock.settimeout(delay) try: previous_timeout = sock.gettimeout() 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: 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']) paho.mqtt.python-2.0.0/tests/ssl/000077500000000000000000000000001456167725100167255ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/ssl/all-ca.crt000066400000000000000000000131201456167725100205650ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 1 (0x1) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, L=Derby, O=Paho Project, OU=Testing, CN=Root CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:cb:32:6c:8c:48:e8:44:58:36:18:70:36:42:3d: 2d:29:47:3c:69:12:9e:7b:f7:45:62:ef:91:44:46: 97:a0:ea:5f:da:fd:9f:98:d4:bf:43:02:e3:39:90: 33:7b:13:13:d5:31:30:9c:07:fc:ca:1b:a9:e4:89: 42:e5:d0:6e:f4:a2:e0:23:ee:9d:9a:cc:80:3b:78: bf:7e:27:a8:46:1b:28:9f:4a:64:53:7a:89:3e:ab: 65:6f:af:0b:29:fa:4d:4f:04:f1:1e:10:2c:bf:2b: ea:fc:c5:fa:77:c9:1a:7a:78:29:f5:a2:cb:25:7c: 02:bb:91:8d:76:4d:23:bc:9c:19:da:be:c5:20:04: ad:fe:bd:b9:d4:bb:29:2a:c3:e4:fc:4c:84:db:a3: 55:9f:f0:70:7f:40:38:b5:c3:78:a5:db:06:36:b7: 10:8e:ca:6c:1a:92:66:be:0e:1a:97:59:6b:18:f4: c2:b8:c9:31:7b:d1:b1:a1:00:78:7f:c0:09:f6:ef: b2:8f:94:87:5d:b1:a2:23:93:4d:ec:fa:95:09:a9: 90:c4:02:f0:1e:d9:ab:a2:8b:7f:7f:54:95:e7:da: c3:c9:7d:a7:d7:04:89:59:db:88:9d:57:16:5d:b9: 66:b0:d6:88:bb:e0:ee:43:e9:ab:02:78:fc:bd:e8: 98:d9 Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 X509v3 Authority Key Identifier: keyid:13:A0:B6:1F:F5:C7:64:C2:F9:FD:2E:08:F2:19:01:77:54:19:73:7F X509v3 Basic Constraints: CA:TRUE Signature Algorithm: sha256WithRSAEncryption 3e:70:76:69:37:e4:6e:e0:08:c6:8e:5b:2e:aa:26:fe:e9:ed: ac:02:ce:2c:37:08:6a:8a:c3:0d:c0:ef:43:51:01:2e:e0:96: 76:23:1b:1f:75:98:df:7c:d1:b7:c1:67:aa:62:c1:bd:ef:84: eb:d9:28:47:50:f2:1b:54:7f:ed:cb:52:f7:fc:c3:f8:62:22: 0c:b3:95:ed:bb:3f:74:91:bc:d2:eb:c0:81:7d:74:12:85:61: a3:7e:fb:22:4a:25:99:0b:5d:ef:69:f2:5a:e6:d5:12:a3:95: 38:30:0c:c7:d9:da:28:30:10:b4:3d:3e:ad:20:85:31:e0:bf: 30:33:2e:0b:e3:07:3d:ed:22:dc:67:f8:93:64:89:ed:e7:08: 74:b5:0a:7a:01:3d:f9:44:62:71:cf:60:12:92:c3:95:9a:e5: a5:f2:24:6a:22:64:d5:76:22:c9:03:1c:c5:d1:a5:85:4d:55: f9:80:47:ca:12:20:df:05:fb:82:12:45:6f:e8:c0:20:a8:ae: f7:17:c5:c3:b6:9c:51:bd:d8:84:e4:db:c7:03:44:d2:cb:75: 51:79:3f:86:33:3c:e4:34:1d:77:b2:60:24:5c:21:c5:c3:53: 36:08:2f:a7:14:0b:68:78:67:95:90:b9:06:0e:85:04:65:57: b4:34:31:cf -----BEGIN CERTIFICATE----- MIIDmDCCAoCgAwIBAgIBATANBgkqhkiG9w0BAQsFADBtMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwFRGVyYnkxFTATBgNVBAoMDFBh aG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQMA4GA1UEAwwHUm9vdCBDQTAe Fw0yMTA3MDcxMTE0NDJaFw0yNjA3MDYxMTE0NDJaMGAxCzAJBgNVBAYTAkdCMRMw EQYDVQQIDApEZXJieXNoaXJlMRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNV BAsMB1Rlc3RpbmcxEzARBgNVBAMMClNpZ25pbmcgQ0EwggEiMA0GCSqGSIb3DQEB AQUAA4IBDwAwggEKAoIBAQDLMmyMSOhEWDYYcDZCPS0pRzxpEp5790Vi75FERpeg 6l/a/Z+Y1L9DAuM5kDN7ExPVMTCcB/zKG6nkiULl0G70ouAj7p2azIA7eL9+J6hG GyifSmRTeok+q2Vvrwsp+k1PBPEeECy/K+r8xfp3yRp6eCn1osslfAK7kY12TSO8 nBnavsUgBK3+vbnUuykqw+T8TITbo1Wf8HB/QDi1w3il2wY2txCOymwakma+DhqX WWsY9MK4yTF70bGhAHh/wAn277KPlIddsaIjk03s+pUJqZDEAvAe2auii39/VJXn 2sPJfafXBIlZ24idVxZduWaw1oi74O5D6asCePy96JjZAgMBAAGjUDBOMB0GA1Ud DgQWBBTCjwmb1fG6xHRel1C7hp2h8frENjAfBgNVHSMEGDAWgBQToLYf9cdkwvn9 LgjyGQF3VBlzfzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQA+cHZp N+Ru4AjGjlsuqib+6e2sAs4sNwhqisMNwO9DUQEu4JZ2IxsfdZjffNG3wWeqYsG9 74Tr2ShHUPIbVH/ty1L3/MP4YiIMs5Xtuz90kbzS68CBfXQShWGjfvsiSiWZC13v afJa5tUSo5U4MAzH2dooMBC0PT6tIIUx4L8wMy4L4wc97SLcZ/iTZInt5wh0tQp6 AT35RGJxz2ASksOVmuWl8iRqImTVdiLJAxzF0aWFTVX5gEfKEiDfBfuCEkVv6MAg qK73F8XDtpxRvdiE5NvHA0TSy3VReT+GMzzkNB13smAkXCHFw1M2CC+nFAtoeGeV kLkGDoUEZVe0NDHP -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- MIIDuDCCAqCgAwIBAgIUS1Q+E18/+trcKfhT+xz8ghGukmYwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEzARBgNVBAgMCkRlcmJ5c2hpcmUxDjAMBgNVBAcM BURlcmJ5MRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNVBAsMB1Rlc3Rpbmcx EDAOBgNVBAMMB1Jvb3QgQ0EwHhcNMjEwNzA3MTExNDQyWhcNMzEwNzA1MTExNDQy WjBtMQswCQYDVQQGEwJHQjETMBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwF RGVyYnkxFTATBgNVBAoMDFBhaG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQ MA4GA1UEAwwHUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB AKpCq45dCrroNa+y3zgdBglQOtw4og3MD/3Rn6ZftyL0dv1rSMkCFU8lCtZ4bIpz iNSJKau79owCudX3qQTPfiX2pmR5uuYjvMzRiZohZtz5uqXByy/CMS8dPRI3po6i kfNx9n7EQqOlxdwkY1kae2j5ybkAld2MNci93BH4P8qqaQckVRKpv6cKq33KsXK7 jHgjAYMGrihTAwxgP1JX9NS8yxxjMUYvFqeEOLARoeWc6Nl7oDbGLs2fr0j2Yssm cz0AMu7LWcbhnfs2S8Troksztnq38yHu+YTs6hX4NhANBgon5CAdyzmmE/b2OwOX p8rQepUfG7wO5QaS0OrAEXsCAwEAAaNQME4wHQYDVR0OBBYEFBOgth/1x2TC+f0u CPIZAXdUGXN/MB8GA1UdIwQYMBaAFBOgth/1x2TC+f0uCPIZAXdUGXN/MAwGA1Ud EwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAHgE1oMwIcilQFN4xPYCf8jbsa5o zA5ljTbxv7fU3Zd+7KdlDFYroGjgHb7o3r0//b8+ZarxBqn1274u4KPs39Ow7h6m YJo7IM2Z2fC6IWZroqeidfFx5SwejAP1j7coYLblTIbNF+P08sJG5nSQ+Yx0gams 6C1x0mETaaglDwllU1KXHTm8fUpEwpISc/VfKABYgScODMpdsDghyHANvnFjmvp4 ktABnasliZYTmdl0t3szNm7zIk+bntiK4KunFea8GqgslWqGPwtNxxJFHzPjMCxK EHgubLgp1lNZzH13XSO6ZpiNRDJ6IVed3Zq+yn+24uKH+1Hqp6Bt20ZFB4E= -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/client-expired.crt000066400000000000000000000107001456167725100223510ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 3 (0x3) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Aug 20 00:00:00 2012 GMT Not After : Aug 21 00:00:00 2012 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=test client expired Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:d0:58:ed:ad:44:b4:f8:30:16:27:d9:b4:2c:b4: 24:67:9a:19:fe:32:04:0d:9a:7e:75:97:12:d6:2c: d4:97:33:fb:30:8c:ef:a2:b1:ef:e5:92:d6:56:05: 75:c8:19:82:92:4e:4b:13:8e:25:90:b9:21:72:f4: a4:bf:7a:e2:0f:75:52:08:04:4c:e8:6a:35:7e:7d: 78:d9:b8:f7:2b:3d:8e:4e:b5:f3:7a:9a:06:10:50: ca:95:63:2c:bd:3a:89:d0:8a:84:12:32:9b:00:a7: 25:33:70:d2:18:0a:43:94:12:62:e7:77:db:b8:0f: dc:23:48:95:5c:77:c6:11:4f:0f:d6:6e:73:59:7c: ed:6a:fd:ba:24:f0:b2:59:c3:a2:16:65:ad:19:7f: 92:87:8c:ea:b5:e5:0f:26:f8:b1:74:98:c3:fd:ed: 4d:74:d0:58:ce:d9:9c:24:34:9b:75:79:25:d0:aa: 6c:03:03:0c:3a:4a:4c:9a:36:50:ab:55:74:1e:8b: de:41:a7:14:b9:57:ee:8b:31:90:5c:00:af:31:9d: e0:55:07:8d:05:ed:c9:5f:e1:79:b7:96:be:d9:5b: cf:a7:5c:cd:48:fc:bd:a4:34:bf:e0:49:d5:25:60: 7a:4c:32:37:97:e4:f8:64:24:a6:79:c1:62:8d:93: 52:53 Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 61:62:90:E6:BB:8A:BB:06:6C:8A:66:9F:A5:C7:85:12:43:5C:94:6F X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption 8d:7b:2a:16:2a:2e:50:db:5d:6e:20:ec:4e:5f:2e:d0:f4:9a: a9:c8:b3:f0:73:02:9f:2f:32:a2:2a:a5:a7:83:1e:e3:36:6b: 99:d2:4c:6a:ea:09:0b:73:5e:7f:69:da:50:69:5b:dc:0f:4d: 59:ec:d2:c7:ca:0e:a8:55:c0:5a:f6:67:e8:a0:0b:4b:0a:9a: a8:1f:b3:f0:e7:e6:10:4b:db:1b:5a:18:7a:ee:52:16:93:2e: 70:1c:4f:7d:c6:eb:4a:11:35:92:db:8c:f0:86:1b:f7:64:4f: f5:1b:31:d6:da:89:97:c6:46:4b:c9:df:7f:80:c4:77:5e:c6: a8:b7:47:12:48:b5:2b:f2:73:80:e4:dd:5b:cf:a1:20:3c:3b: b3:37:34:d1:72:37:e1:a6:06:d4:22:cc:65:d3:af:0f:aa:ea: ad:dd:e9:21:c5:1e:86:81:94:33:6c:ca:68:c2:48:ed:ea:0e: c4:be:38:a5:4f:bb:0b:2b:7f:e7:63:e1:9f:e1:c8:6a:c4:4c: 7b:43:a2:56:c9:ff:56:88:2e:e3:4f:d6:d0:69:59:96:6e:26: d9:3d:f3:62:4e:c3:a3:79:8f:f9:e4:82:11:52:f0:a2:c7:79: b6:54:50:21:31:e6:4a:8c:2c:df:23:e9:2e:50:6e:9d:a8:61: 5b:e1:cb:51 -----BEGIN CERTIFICATE----- MIID1zCCAr+gAwIBAgIBAzANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTEyMDgyMDAwMDAw MFoXDTEyMDgyMTAwMDAwMFowgYAxCzAJBgNVBAYTAkdCMRgwFgYDVQQIDA9Ob3R0 aW5naGFtc2hpcmUxEzARBgNVBAcMCk5vdHRpbmdoYW0xDzANBgNVBAoMBlNlcnZl cjETMBEGA1UECwwKUHJvZHVjdGlvbjEcMBoGA1UEAwwTdGVzdCBjbGllbnQgZXhw aXJlZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANBY7a1EtPgwFifZ tCy0JGeaGf4yBA2afnWXEtYs1Jcz+zCM76Kx7+WS1lYFdcgZgpJOSxOOJZC5IXL0 pL964g91UggETOhqNX59eNm49ys9jk6183qaBhBQypVjLL06idCKhBIymwCnJTNw 0hgKQ5QSYud327gP3CNIlVx3xhFPD9Zuc1l87Wr9uiTwslnDohZlrRl/koeM6rXl Dyb4sXSYw/3tTXTQWM7ZnCQ0m3V5JdCqbAMDDDpKTJo2UKtVdB6L3kGnFLlX7osx kFwArzGd4FUHjQXtyV/hebeWvtlbz6dczUj8vaQ0v+BJ1SVgekwyN5fk+GQkpnnB Yo2TUlMCAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYdT3BlblNT TCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFGFikOa7irsGbIpmn6XH hRJDXJRvMB8GA1UdIwQYMBaAFMKPCZvV8brEdF6XULuGnaHx+sQ2MA0GCSqGSIb3 DQEBCwUAA4IBAQCNeyoWKi5Q211uIOxOXy7Q9JqpyLPwcwKfLzKiKqWngx7jNmuZ 0kxq6gkLc15/adpQaVvcD01Z7NLHyg6oVcBa9mfooAtLCpqoH7Pw5+YQS9sbWhh6 7lIWky5wHE99xutKETWS24zwhhv3ZE/1GzHW2omXxkZLyd9/gMR3Xsaot0cSSLUr 8nOA5N1bz6EgPDuzNzTRcjfhpgbUIsxl068Pquqt3ekhxR6GgZQzbMpowkjt6g7E vjilT7sLK3/nY+Gf4chqxEx7Q6JWyf9WiC7jT9bQaVmWbibZPfNiTsOjeY/55IIR UvCix3m2VFAhMeZKjCzfI+kuUG6dqGFb4ctR -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/client-pw.crt000066400000000000000000000107161456167725100213460ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 4 (0x4) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=test client with password Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:ca:cb:93:49:45:28:95:5b:c4:51:b4:2b:d0:e7: e4:b7:3b:37:34:f6:5c:ec:f8:7d:3a:8b:b8:da:3c: 94:38:85:5f:41:ea:2b:08:d7:3e:97:12:50:09:1f: 37:4f:e4:25:a1:59:b6:98:63:22:8d:80:7e:b1:b4: 24:03:2e:5e:5d:45:a4:4c:76:e8:ac:2c:5f:ca:9d: ed:6e:0a:7b:6f:2b:34:d1:4e:6a:e1:b6:72:66:42: ec:fd:b8:97:bf:40:4b:24:9c:47:6c:8c:4a:73:aa: e0:3a:db:ac:45:65:23:df:8f:4a:30:ed:d6:ad:5c: eb:a9:e9:83:da:39:d1:eb:98:31:74:98:bd:99:6b: 85:0e:1d:f8:93:cf:e2:bd:59:77:fe:b2:a0:c4:e5: 63:ae:92:10:13:47:14:55:22:a0:30:b6:f0:cb:17: b6:2d:f9:7d:f9:82:50:b2:64:88:dd:5a:3b:b6:81: 67:8c:e3:de:89:76:63:82:af:b7:ba:83:5c:3b:bc: cf:1f:8e:fe:25:04:6f:f2:70:bf:2f:b0:6b:4f:77: d2:2d:e4:37:20:84:f3:94:c3:12:80:ae:bc:c3:2b: 93:d2:fa:92:a3:1a:33:8d:d7:4a:eb:23:04:c0:38: 51:73:fb:7a:9f:f5:3a:ca:7e:2e:c7:b6:22:3e:68: 69:0f Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 8E:E7:D7:66:D5:0C:10:B5:7A:4F:7F:83:C3:43:94:E9:BC:E2:88:D0 X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption 17:54:5a:5e:9e:7d:fe:3f:6d:82:c5:e8:42:6b:61:91:13:5f: 07:d5:25:4b:3c:05:e6:4c:99:a5:ff:20:ff:d3:e8:a4:25:08: 8c:82:1b:2f:25:73:79:97:12:5e:e9:30:a1:b2:16:36:51:e2: a5:41:bf:1c:c1:db:d2:9a:36:67:75:da:e7:36:9a:b4:65:17: 74:af:73:02:b0:09:b3:ac:29:e7:ca:cd:01:12:7f:ba:39:29: 90:d4:7c:3f:99:89:66:e7:eb:79:80:77:91:e4:3d:7e:87:69: 7b:da:b5:68:07:26:ab:30:20:49:2b:46:33:3f:f7:4b:4e:e7: a0:13:19:53:7d:73:ff:4a:95:86:35:d2:cd:ff:3c:b1:14:b4: d8:d4:ca:de:b7:8d:2e:e3:47:f8:5d:2e:e7:b1:5b:b9:23:d3: 54:11:89:8e:98:12:a8:10:2a:da:bb:d0:0c:07:c7:d7:21:7e: f0:88:91:31:07:2a:a6:42:84:4a:61:9e:68:72:d4:7c:3f:59: b2:02:e1:a6:11:9b:d2:90:73:39:13:07:e1:6b:57:2a:78:b4: b4:f0:75:7c:6d:48:9d:33:cd:3f:d0:ff:43:a4:7e:3a:8d:fe: 98:10:df:ab:ee:c0:58:82:cb:23:7a:b7:f5:5c:29:29:af:d0: 40:fc:42:a3 -----BEGIN CERTIFICATE----- MIID3TCCAsWgAwIBAgIBBDANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTIxMDcwNzExMTQ0 MloXDTI2MDcwNjExMTQ0MlowgYYxCzAJBgNVBAYTAkdCMRgwFgYDVQQIDA9Ob3R0 aW5naGFtc2hpcmUxEzARBgNVBAcMCk5vdHRpbmdoYW0xDzANBgNVBAoMBlNlcnZl cjETMBEGA1UECwwKUHJvZHVjdGlvbjEiMCAGA1UEAwwZdGVzdCBjbGllbnQgd2l0 aCBwYXNzd29yZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMrLk0lF KJVbxFG0K9Dn5Lc7NzT2XOz4fTqLuNo8lDiFX0HqKwjXPpcSUAkfN0/kJaFZtphj Io2AfrG0JAMuXl1FpEx26KwsX8qd7W4Ke28rNNFOauG2cmZC7P24l79ASyScR2yM SnOq4DrbrEVlI9+PSjDt1q1c66npg9o50euYMXSYvZlrhQ4d+JPP4r1Zd/6yoMTl Y66SEBNHFFUioDC28MsXti35ffmCULJkiN1aO7aBZ4zj3ol2Y4Kvt7qDXDu8zx+O /iUEb/Jwvy+wa0930i3kNyCE85TDEoCuvMMrk9L6kqMaM43XSusjBMA4UXP7ep/1 Osp+Lse2Ij5oaQ8CAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYd T3BlblNTTCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFI7n12bVDBC1 ek9/g8NDlOm84ojQMB8GA1UdIwQYMBaAFMKPCZvV8brEdF6XULuGnaHx+sQ2MA0G CSqGSIb3DQEBCwUAA4IBAQAXVFpenn3+P22CxehCa2GRE18H1SVLPAXmTJml/yD/ 0+ikJQiMghsvJXN5lxJe6TChshY2UeKlQb8cwdvSmjZnddrnNpq0ZRd0r3MCsAmz rCnnys0BEn+6OSmQ1Hw/mYlm5+t5gHeR5D1+h2l72rVoByarMCBJK0YzP/dLTueg ExlTfXP/SpWGNdLN/zyxFLTY1Mret40u40f4XS7nsVu5I9NUEYmOmBKoECrau9AM B8fXIX7wiJExByqmQoRKYZ5octR8P1myAuGmEZvSkHM5Ewfha1cqeLS08HV8bUid M80/0P9DpH46jf6YEN+r7sBYgssjerf1XCkpr9BA/EKj -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/client-pw.key000066400000000000000000000033461456167725100213470ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: AES-128-CBC,84549D95979482A29416CC3DBA507BC0 tAbMy3JP4/R53W9E8sB+fFFvGQOb+QKSW0vtjb8Z3GlIW+9wdGJ89GhXcspP7+HN eQZy6trDmPHJ++m4sVEwBGjngLdDaajRvQVlqCIXgLvjCwIIaE2gHo3yF37umunR 0dg4NVjfEgNS3luPu7DzCv9pwIQl2YOsoPK7TuHOTGVGHCovHAr/IJ7fdaT5N9G8 ypC7rHZhneLUs02J/L0FZUPlztOXRuiqnJisZr0pPs6ZoVEoNvR4D3VgU0nOFMcF UmcWA+vJsXaPzmC0HDErYqfr0Mwc/7mZURCGm/+A6Q79PAAAR7pPoMczaRHP4szS BxegO6XyKYs8a52q/XojKTcb0FESNjX0syW3+OjZusCYpuBmK4MofSdDmXVNxBLD iBqUDLGSfU2W5H/UkHWh7O0VW1DT0RvqqFV1p1WI0dvixM80wx12rPiJ79RYLG8D HMD7lR2iODDibCXePMg0XeCz+zf8OSfqzw6YeAMxmapZWBd8cJJ4eaUq6ziZO0hE kvj6tUZk7d/nTSissQR2Tx6xlSm0AuHWdKx1s5gKnVg6xLKNKIVyeHnXdXkgCwSq dICwmtP/1iYrslWCrhnB4MLA6R2vgpglwBfh7h7rW59K2untdIzr/td+h/xkynHQ wMKJ5xZ2oRQc9oZrV0PXHQKdukniLr3owBPiu+i+QbqpzGtvhyt1wUs+NjDROrim kritxoz6SXSIH6Wv9ae1crdhK1YTaMt1YOJT4tPjTdZyhMXqYszAH25L3ar2HaEv Cv2YU9VqPno45/ZSVSA8xZ+E74AoZsgDMOWKFJimJv+P9CGNbm8d9SGlHDAsyj0U +cTeyH6AWWHuAdEtVNA36qDdWOJOwhH2vT2iLuqdDySX5EJRszoxFo2RdGF3lRuQ lVFo1v41tnvB89i9g8ZVqqkfs1IjybU2Aq+hpnqRVThRrbN75o2s2BC0K2sUvgu5 gKUzXBl2B5CX6kWrUZ9llTSi2nH6zFAMtKvvuRQx+r+qrjJbxiPkRm9HFXlRRKYG NZbYyrB0ovuNgL5mwraNBL8Ytzx/nGvnaJsxWqhNiDENEziGcjTiA0/gh/mtru7K xTAQt7vVgrsynAK7c5Yhu3BBJspjgq2S9mKNpgXadcYcKQRcJnsYR9VCNWy4f7dD sTDy9NPttZM24ayC8OjUtyusk/DxXubuqRf4mF7jKsmoTqZR/yW5/NGPOpYRs0If 9ysiBP8ctyti+snS8jSzb7PVCBJzKgEDthjLvXmV2AIeuiXTFvqTmKOlTYT8mkev ZaXl4tS+3GGJgrSmPweAJsGFo58oQA8skExrXBW0w2rRSQDqzEo/GAbmd65IDGXh YMOwsdvjiu9Ug4E7icxB2w5bKmhEsIh/Vj3Np/h5xcJ9W9O8zq0oYhjle3GLDGro yPA1g0wWLeuxwPhPk7cNHFdF2Yr2CXFVug3Q9WkGTABKbZd2zV/7kk6YMRFmieV0 h4nQBSFqR6qkF9CiUFkKS2dod0zKLPJiBRqgiopEur5QdHg1PpdEQAj6fLQo0Zfw LoQyVi3ta9IMO1wZU8fy9w+bXoo8c76VD5jzfXw1ig0bK3iu1ozZc2UjnNlBmpYp -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/client-revoked.crt000066400000000000000000000107001456167725100223500ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 5 (0x5) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Jul 7 11:14:43 2021 GMT Not After : Jul 6 11:14:43 2026 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=test client revoked Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:a5:7a:5f:a2:55:1f:50:22:cd:92:0d:9a:69:fa: 47:d4:d1:2f:6f:e5:3e:22:06:2f:f4:ed:a8:85:9b: 2a:0d:e7:e2:81:f4:23:08:68:e2:75:5b:ba:58:f5: 57:61:5a:5c:c4:e5:27:5e:9c:8e:82:77:72:25:c2: 2e:1d:e0:61:dc:32:f0:3b:be:7d:26:e3:a0:bb:5d: 75:7f:87:d8:a1:26:2f:7f:01:7b:1e:2f:25:cb:bd: 15:6c:43:12:6a:a6:02:1d:fd:7b:34:e2:1e:6c:06: 13:de:39:e8:ee:ae:ed:cd:cc:bd:1e:48:d5:e6:11: 95:12:08:61:88:13:d6:88:40:cc:9d:18:1c:c6:30: 5e:8f:e8:a4:2a:c8:62:78:19:f6:95:6a:f0:ce:27: e3:af:aa:fd:46:41:9d:83:32:f6:8e:a4:1f:32:00: c3:ca:5f:a5:3e:bc:74:6e:96:3e:50:cd:12:ca:81: 5a:ab:cf:a1:f8:3a:2f:fe:91:73:79:14:b3:fb:e6: 6b:c3:57:a9:8c:2d:f6:6c:53:4f:2e:e9:4c:25:67: 88:ac:ce:bc:84:ac:b8:d8:f5:6a:a4:ae:24:10:ea: 4e:2c:ef:90:f5:a6:68:c3:5c:a7:e0:40:99:06:6a: ec:b1:63:f5:7a:0b:a9:f1:81:26:95:12:9c:02:20: 77:df Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: C7:6A:63:58:7D:DB:19:38:77:1F:41:E8:67:38:78:9D:0B:BE:51:92 X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption 5c:b9:89:ad:18:b6:0b:93:f1:b0:0a:3e:aa:f3:d0:ad:d1:6f: 04:31:29:5a:b8:74:81:3c:4b:bb:98:8a:ca:c8:95:fa:8f:3e: 89:ba:f2:c3:83:a2:18:1c:7c:c6:56:4b:52:83:ef:fe:87:23: 7f:2d:6f:a2:36:22:46:04:ed:bf:ad:34:e6:9d:87:7e:92:72: 80:b2:5d:b1:b3:23:f6:f2:bd:74:c5:34:ef:8f:50:89:8b:64: 77:95:9d:ec:72:09:a6:c4:74:da:1d:2a:57:60:38:8f:6c:22: ff:9e:40:73:98:ac:1f:bc:b6:e4:1b:1d:2d:73:a2:9a:ad:53: 95:d2:17:b3:c5:8a:6c:5a:5a:be:e2:80:e4:f5:d6:99:06:61: ec:66:44:1a:ec:ac:86:36:ef:84:4b:c5:b3:a0:c5:d7:0d:be: 51:8c:95:46:03:e4:74:61:bf:7c:10:68:91:12:46:b8:38:94: 9f:a2:68:77:4d:92:57:43:ff:a1:c2:67:43:33:01:1d:fd:29: 13:8d:04:ed:7e:2d:4c:ed:8c:2f:f6:6f:44:33:3c:71:4d:f6: 51:04:c5:c0:cb:2c:ea:95:6e:22:32:03:37:0b:32:87:89:c0: e5:bc:72:d2:8f:73:db:40:a9:4d:f2:15:bd:c4:0d:aa:ea:2e: 0c:ce:77:9d -----BEGIN CERTIFICATE----- MIID1zCCAr+gAwIBAgIBBTANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTIxMDcwNzExMTQ0 M1oXDTI2MDcwNjExMTQ0M1owgYAxCzAJBgNVBAYTAkdCMRgwFgYDVQQIDA9Ob3R0 aW5naGFtc2hpcmUxEzARBgNVBAcMCk5vdHRpbmdoYW0xDzANBgNVBAoMBlNlcnZl cjETMBEGA1UECwwKUHJvZHVjdGlvbjEcMBoGA1UEAwwTdGVzdCBjbGllbnQgcmV2 b2tlZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKV6X6JVH1AizZIN mmn6R9TRL2/lPiIGL/TtqIWbKg3n4oH0Iwho4nVbulj1V2FaXMTlJ16cjoJ3ciXC Lh3gYdwy8Du+fSbjoLtddX+H2KEmL38Bex4vJcu9FWxDEmqmAh39ezTiHmwGE945 6O6u7c3MvR5I1eYRlRIIYYgT1ohAzJ0YHMYwXo/opCrIYngZ9pVq8M4n46+q/UZB nYMy9o6kHzIAw8pfpT68dG6WPlDNEsqBWqvPofg6L/6Rc3kUs/vma8NXqYwt9mxT Ty7pTCVniKzOvISsuNj1aqSuJBDqTizvkPWmaMNcp+BAmQZq7LFj9XoLqfGBJpUS nAIgd98CAwEAAaN7MHkwCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYdT3BlblNT TCBHZW5lcmF0ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFMdqY1h92xk4dx9B6Gc4 eJ0LvlGSMB8GA1UdIwQYMBaAFMKPCZvV8brEdF6XULuGnaHx+sQ2MA0GCSqGSIb3 DQEBCwUAA4IBAQBcuYmtGLYLk/GwCj6q89Ct0W8EMSlauHSBPEu7mIrKyJX6jz6J uvLDg6IYHHzGVktSg+/+hyN/LW+iNiJGBO2/rTTmnYd+knKAsl2xsyP28r10xTTv j1CJi2R3lZ3scgmmxHTaHSpXYDiPbCL/nkBzmKwfvLbkGx0tc6KarVOV0hezxYps Wlq+4oDk9daZBmHsZkQa7KyGNu+ES8WzoMXXDb5RjJVGA+R0Yb98EGiREka4OJSf omh3TZJXQ/+hwmdDMwEd/SkTjQTtfi1M7Ywv9m9EMzxxTfZRBMXAyyzqlW4iMgM3 CzKHicDlvHLSj3PbQKlN8hW9xA2q6i4Mzned -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/client-revoked.key000066400000000000000000000032171456167725100223550ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEApXpfolUfUCLNkg2aafpH1NEvb+U+IgYv9O2ohZsqDefigfQj CGjidVu6WPVXYVpcxOUnXpyOgndyJcIuHeBh3DLwO759JuOgu111f4fYoSYvfwF7 Hi8ly70VbEMSaqYCHf17NOIebAYT3jno7q7tzcy9HkjV5hGVEghhiBPWiEDMnRgc xjBej+ikKshieBn2lWrwzifjr6r9RkGdgzL2jqQfMgDDyl+lPrx0bpY+UM0SyoFa q8+h+Dov/pFzeRSz++Zrw1epjC32bFNPLulMJWeIrM68hKy42PVqpK4kEOpOLO+Q 9aZow1yn4ECZBmrssWP1egup8YEmlRKcAiB33wIDAQABAoIBACVlqZVLPX9jzieS 0XHf8TnkaJ8WJNuVoGLvDuXa8j8gR61s2jn9UiiJqWyPTccfn9WToDkekopjqjVk U/3Ghvc3v9kQrMIMMXgGoBZJQijxM0y1rfhdWWJZAi1sXw4hJFtYvO5vp8Zr/TN8 zOqcN/wJqDfe6BBNqu3fXQNe0F4MQeiLVYi7c1Q0ZupiALZDFPJ1u03xFiIzlMrN QLghfUoq4pFgqC08wR31XncvcWQ/iOggznxjy16Ezx1ubqGf7cMWZmXEWtdD0fsP 8P3x/VS+MBzVtX9hhTaS3pVUAZKriCLF8kQiCUgtzlbUvKNrRb6gLan+OcD+J2sE 0wopLZkCgYEA0BsLbbf0pc+PT+oCFUUueyADSl3SuH8j1YCpSU0nl0h6lE90cOen HSPRa4WHhhQm9lgCtTLXlHxJsZym0Lau7Nd90MIrjuvgRv0uOk07tRyThoGGuSCL 2fBnD+a8NGjh+s/KTxmBDdWPkRpaZ5MVl8ZSGSM5zUZzplwPLx/J+wUCgYEAy4/W nd+rU4oh3Hm6cVp4YaktZ9cU/YB0yDd/sIm7bJ+kDyz0LSw7l/FSxMzQ+Dqybqht We+jn07BOh89r70vbcxx+4kMtHbg5Ii1u/R74AgGK6JSiKKpybLi7LA+3vcW/Hg6 lemxE1U/PmmdvvSjzN/EXpeABkSgbctkJeoYRJMCgYBKLvnZ+NNrMBxEPoTTlD/H gFfr8JonTps1hpHSIYDVeu7HY7N8c/eseZIzo/v1ncVt113Pvfn/Ynbaq58Dk7uz jfW5rx3b6tWeOK579gAsxa0JK68c2y8/V2VF09iPTjwQLnZN0CejCNgOv7guZ84w tm+Zqmb2eADN8s8u20QjCQKBgQCoUHXHskKqX6Ph9nD3+zNgpQ8bNldvyMBHMMSP B0OG7HUt6yC3HUTlPLAQc74yEe6p2vAYFjK3rdnNojlST16hLhPtRQPRUB5iOLvz /pJSyq+3co9F1SII2bYSuSQzHiHOfecLP+CfuLQDejbpxsSNyVRIVoKQLDxurGdR hj+sqwKBgQC+x2dapmrh7qcCbPxbheWH32ds9GgI1Nr8eW2Wm0wIguLA4TrBN0UH HQZSdOjQoD/gTtHu79xbD1LPJk4kDtkvdzAlvbEds+3ArlaibNPcDaatkFAJt+2e 2t8UDdIKzxOaKEF8YfTaeCpyS7CZoVw3frA9nkK7h38PgFeB7Vx2Rw== -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/client.crt000066400000000000000000000106541456167725100207230ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 2 (0x2) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=test client Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:d0:58:ed:ad:44:b4:f8:30:16:27:d9:b4:2c:b4: 24:67:9a:19:fe:32:04:0d:9a:7e:75:97:12:d6:2c: d4:97:33:fb:30:8c:ef:a2:b1:ef:e5:92:d6:56:05: 75:c8:19:82:92:4e:4b:13:8e:25:90:b9:21:72:f4: a4:bf:7a:e2:0f:75:52:08:04:4c:e8:6a:35:7e:7d: 78:d9:b8:f7:2b:3d:8e:4e:b5:f3:7a:9a:06:10:50: ca:95:63:2c:bd:3a:89:d0:8a:84:12:32:9b:00:a7: 25:33:70:d2:18:0a:43:94:12:62:e7:77:db:b8:0f: dc:23:48:95:5c:77:c6:11:4f:0f:d6:6e:73:59:7c: ed:6a:fd:ba:24:f0:b2:59:c3:a2:16:65:ad:19:7f: 92:87:8c:ea:b5:e5:0f:26:f8:b1:74:98:c3:fd:ed: 4d:74:d0:58:ce:d9:9c:24:34:9b:75:79:25:d0:aa: 6c:03:03:0c:3a:4a:4c:9a:36:50:ab:55:74:1e:8b: de:41:a7:14:b9:57:ee:8b:31:90:5c:00:af:31:9d: e0:55:07:8d:05:ed:c9:5f:e1:79:b7:96:be:d9:5b: cf:a7:5c:cd:48:fc:bd:a4:34:bf:e0:49:d5:25:60: 7a:4c:32:37:97:e4:f8:64:24:a6:79:c1:62:8d:93: 52:53 Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: 61:62:90:E6:BB:8A:BB:06:6C:8A:66:9F:A5:C7:85:12:43:5C:94:6F X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption a4:ad:a8:cf:8b:f1:c0:e5:ed:e0:9f:eb:b8:46:17:fe:cf:49: ed:e2:94:3b:3c:ea:4d:8b:e5:e3:5b:5f:f0:51:f0:53:88:22: 61:fc:f9:9d:c3:67:5f:9c:20:f3:2c:bb:65:c3:66:d9:15:b8: 60:82:31:95:d4:96:43:11:c1:56:da:4b:ad:bc:3f:b2:5f:3d: 40:8d:e4:22:26:9b:d5:5d:ff:02:55:c1:f9:ca:f3:67:46:be: 7d:d0:8c:68:40:a6:64:01:f0:ce:8e:2c:c2:6c:16:96:23:64: e6:2f:95:b9:95:a2:85:8e:ec:61:56:6f:b9:3a:87:e9:cc:f1: 94:ca:51:d4:ce:50:01:91:1a:8c:ff:f9:cf:30:d4:aa:53:44: 67:44:84:4c:07:a7:ab:c3:34:3a:16:69:8c:37:7f:a0:fb:e1: fa:ec:e6:9d:3c:fd:13:a9:6f:b2:d8:dc:46:81:ae:a6:63:4f: 80:47:a7:80:51:a7:d4:d6:c8:11:85:7d:5f:ab:ef:3a:93:62: d7:fb:c2:a9:e4:b9:40:7e:d1:59:d0:d4:ff:75:bf:70:72:a2: 93:a3:47:41:d8:cf:d5:c6:8c:90:b8:d3:01:d8:53:a6:c1:3c: a9:d9:e4:ef:15:e9:47:9c:9d:eb:5a:bb:11:df:da:f1:81:5d: 89:c9:4a:8f -----BEGIN CERTIFICATE----- MIIDzjCCAragAwIBAgIBAjANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTIxMDcwNzExMTQ0 MloXDTI2MDcwNjExMTQ0MloweDELMAkGA1UEBhMCR0IxGDAWBgNVBAgMD05vdHRp bmdoYW1zaGlyZTETMBEGA1UEBwwKTm90dGluZ2hhbTEPMA0GA1UECgwGU2VydmVy MRMwEQYDVQQLDApQcm9kdWN0aW9uMRQwEgYDVQQDDAt0ZXN0IGNsaWVudDCCASIw DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANBY7a1EtPgwFifZtCy0JGeaGf4y BA2afnWXEtYs1Jcz+zCM76Kx7+WS1lYFdcgZgpJOSxOOJZC5IXL0pL964g91UggE TOhqNX59eNm49ys9jk6183qaBhBQypVjLL06idCKhBIymwCnJTNw0hgKQ5QSYud3 27gP3CNIlVx3xhFPD9Zuc1l87Wr9uiTwslnDohZlrRl/koeM6rXlDyb4sXSYw/3t TXTQWM7ZnCQ0m3V5JdCqbAMDDDpKTJo2UKtVdB6L3kGnFLlX7osxkFwArzGd4FUH jQXtyV/hebeWvtlbz6dczUj8vaQ0v+BJ1SVgekwyN5fk+GQkpnnBYo2TUlMCAwEA AaN7MHkwCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0 ZWQgQ2VydGlmaWNhdGUwHQYDVR0OBBYEFGFikOa7irsGbIpmn6XHhRJDXJRvMB8G A1UdIwQYMBaAFMKPCZvV8brEdF6XULuGnaHx+sQ2MA0GCSqGSIb3DQEBCwUAA4IB AQCkrajPi/HA5e3gn+u4Rhf+z0nt4pQ7POpNi+XjW1/wUfBTiCJh/Pmdw2dfnCDz LLtlw2bZFbhggjGV1JZDEcFW2kutvD+yXz1AjeQiJpvVXf8CVcH5yvNnRr590Ixo QKZkAfDOjizCbBaWI2TmL5W5laKFjuxhVm+5OofpzPGUylHUzlABkRqM//nPMNSq U0RnRIRMB6erwzQ6FmmMN3+g++H67OadPP0TqW+y2NxGga6mY0+AR6eAUafU1sgR hX1fq+86k2LX+8Kp5LlAftFZ0NT/db9wcqKTo0dB2M/VxoyQuNMB2FOmwTyp2eTv FelHnJ3rWrsR39rxgV2JyUqP -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/client.key000066400000000000000000000032131456167725100207140ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA0FjtrUS0+DAWJ9m0LLQkZ5oZ/jIEDZp+dZcS1izUlzP7MIzv orHv5ZLWVgV1yBmCkk5LE44lkLkhcvSkv3riD3VSCARM6Go1fn142bj3Kz2OTrXz epoGEFDKlWMsvTqJ0IqEEjKbAKclM3DSGApDlBJi53fbuA/cI0iVXHfGEU8P1m5z WXztav26JPCyWcOiFmWtGX+Sh4zqteUPJvixdJjD/e1NdNBYztmcJDSbdXkl0Kps AwMMOkpMmjZQq1V0HoveQacUuVfuizGQXACvMZ3gVQeNBe3JX+F5t5a+2VvPp1zN SPy9pDS/4EnVJWB6TDI3l+T4ZCSmecFijZNSUwIDAQABAoIBAQC60zN1jsm0T/Je E6Kz/2kxmYa7YQAvbpz9NtYGRbbwSwVwuMBdpK9Yrj4Sbtz57J4gMaKyy2E2EDxF R8i/hyJU+D/xvmF0e2CypzKKEYlaNd15CUFma90KHlg6cu74VBimbr8VTlmd0UPT h9RtCC8nBQG5S8ozl80vunNssl5iv2JeXvVOL2sRagCr787XeGur74jW2rsh65M/ ba6X3iv143D/1KNGiPAoGpP6vxvnOvM2K9+oqDf5SifmXfgSPIEoBd315YeQdGez BnOJHb7k2vokb+PsiTwjMsIf0AUqwKZPok+sLfaACxs4b2IrmFIYhcAcfyKkPD8i A1DpsEUhAoGBAPfKBnFNiMKc+JFM9lc01nQcdIKgtSmIPNUB7jOeg62oI3VzNp11 9iw2qCQGJZwc1U3QbmNTAap2MxDss98kP2UZcBNDC0pxo+6F7XxIANRURXrG4NRg iPDu1lzubrbzXoh8XMxjKYacPP/gW/2VnxwhOyeIAt3mAN/Bu57t+XRxAoGBANdA UmcYtbu1rW8tzSuXRVgFTDhK1bORao58qJBP95MpFh/nQPB2Q1nu+nReo3870xE2 6/R0gBv8gvKdEMVvhoDRlEJNhelQSg+yVQsX25gQcsKmR6/0IH+S0CH0PibS7o3G sjR9g27MitpX9MzzMR5R5IQErrDw01eOecGzzMUDAoGAc9IJiuJL33ORuBD6QC7h Yqp+RySpKT2V+ZaKabRZJk2mLVrqF1Ww+F+f3h7Fa6AKj/Gx91kwOSZAnlOVi+Kc gzwNp+M5ntVZY79UDzh0ssqlI0tcgciRmdR5fDyyoW9GK5O9qIddPJ9A3/VV6kUK dxKNXN/1PxUoKW6brSDc7fECgYB2BcCo4rWSrLThxv0+L31IG++E1hOCl/MTGWrb Zd1bhSWqbIQA1Pds8knFULbY5pZ+U9zgdphfv/6UxGYTu2jGbSObjyIjoXBaVu+m W3h+UlZ6P+4CnhrLmFYip+cEJpfCiPXhLgjI0cI4og2J6rY9560ibebTAdj/oxFD kjBuvQKBgBaKdaszx7MOBEGmmcv7HQ/prgi1s3UZbG5Jl98XS2xMVJO9N7x7k0hC SEj6kwlOSkpoiDEHSJ2s0j58Mnp5YxwPi61w3ZCnYg/iLgFfnJ972nliBw3wB/iD ZdYerxKGa6Btdbwt14rzE5QfzL8TiFapmq7JnY/rgE11Tx5SR06H -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/crl.pem000066400000000000000000000012521456167725100202100ustar00rootroot00000000000000-----BEGIN X509 CRL----- MIIBzzCBuAIBATANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjETMBEGA1UE CAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYDVQQLDAdU ZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBFw0yMTA3MDcxMTE0NDNaFw0yMTA4 MDYxMTE0NDNaMBQwEgIBBRcNMjEwNzA3MTExNDQzWqAOMAwwCgYDVR0UBAMCAQEw DQYJKoZIhvcNAQELBQADggEBAL+k0y+sONbBjgtGs6WX3AuKPNv+uEVSAdRR1UMX 3KwcwT9jy/5ypT7dv7UNz5V3IR8t41sCN6E3rVvOv+DFn6Br+eabg6GR/iZMhoUq hqknHZZTeMZ5VwzDIZvINHtRvli2bC5/sfU0S44d19lWW4rCVz78c1zf8MvsDc9U fwjtZofTZr/8p7t5KIdohYCwlHu+ANxi7qCJIuPJyZaPQ4wUSbRtu6idvkgYgmpC O74Fe+/eg6zQwd/B1MZEVdBx+66PAyELnyCW+R9PgILzET+YMDni/lT1AYwnCCJ2 lgiTIQlmyT/xlFCfmmzbsCkTcrMPym4m3zTOzbaeiYUBAco= -----END X509 CRL----- paho.mqtt.python-2.0.0/tests/ssl/gen.sh000077500000000000000000000106641456167725100200440ustar00rootroot00000000000000#!/bin/sh # This file generates the keys and certificates used for testing mosquitto. # None of the keys are encrypted, so do not just use this script to generate # files for your own use. set -e rm -f *.crt *.key *.csr for a in root signing; do rm -rf ${a}CA/ mkdir -p ${a}CA/newcerts touch ${a}CA/index.txt echo 01 > ${a}CA/serial echo 01 > ${a}CA/crlnumber done rm -rf certs BASESUBJ="/C=GB/ST=Derbyshire/L=Derby/O=Paho Project/OU=Testing" SBASESUBJ="/C=GB/ST=Nottinghamshire/L=Nottingham/O=Server/OU=Production" BBASESUBJ="/C=GB/ST=Nottinghamshire/L=Nottingham/O=Server/OU=Bridge" # The root CA openssl genrsa -out test-root-ca.key 2048 openssl req -new -x509 -days 3650 -key test-root-ca.key -out test-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Root CA/" # Another root CA that doesn't sign anything openssl genrsa -out test-bad-root-ca.key 2048 openssl req -new -x509 -days 3650 -key test-bad-root-ca.key -out test-bad-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Bad Root CA/" # This is a root CA that has the exact same details as the real root CA, but is a different key and certificate. Effectively a "fake" CA. openssl genrsa -out test-fake-root-ca.key 2048 openssl req -new -x509 -days 3650 -key test-fake-root-ca.key -out test-fake-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Root CA/" # An intermediate CA, signed by the root CA, used to sign server/client csrs. openssl genrsa -out test-signing-ca.key 2048 openssl req -out test-signing-ca.csr -key test-signing-ca.key -new -config openssl.cnf -subj "${BASESUBJ}/CN=Signing CA/" openssl ca -batch -config openssl.cnf -name CA_root -extensions v3_ca -out test-signing-ca.crt -infiles test-signing-ca.csr # An alternative intermediate CA, signed by the root CA, not used to sign anything. openssl genrsa -out test-alt-ca.key 2048 openssl req -out test-alt-ca.csr -key test-alt-ca.key -new -config openssl.cnf -subj "${BASESUBJ}/CN=Alternative Signing CA/" openssl ca -batch -config openssl.cnf -name CA_root -extensions v3_ca -out test-alt-ca.crt -infiles test-alt-ca.csr # Valid server key and certificate. openssl genrsa -out server.key 2048 openssl req -new -key server.key -out server.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=localhost/" openssl ca -batch -config openssl.cnf -name CA_signing -out server.crt -infiles server.csr # Expired server certificate, based on the above server key. openssl req -new -days 1 -key server.key -out server-expired.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=localhost/" echo -n > signingCA/index.txt echo 01 > signingCA/serial openssl ca -batch -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out server-expired.crt -infiles server-expired.csr # Valid client key and certificate. openssl genrsa -out client.key 2048 openssl req -new -key client.key -out client.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client/" openssl ca -batch -config openssl.cnf -name CA_signing -out client.crt -infiles client.csr # Expired client certificate, based on the above client key. openssl req -new -days 1 -key client.key -out client-expired.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client expired/" openssl ca -batch -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out client-expired.crt -infiles client-expired.csr # Valid client key and certificate, key is encrypted with a password. openssl genrsa -aes128 -passout pass:password -out client-pw.key 2048 openssl req -new -key client-pw.key -passin pass:password -out client-pw.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client with password/" openssl ca -batch -config openssl.cnf -name CA_signing -out client-pw.crt -infiles client-pw.csr # Revoked client certificate, based on a new client key. openssl genrsa -out client-revoked.key 2048 openssl req -new -days 1 -key client-revoked.key -out client-revoked.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client revoked/" openssl ca -batch -config openssl.cnf -name CA_signing -out client-revoked.crt -infiles client-revoked.csr openssl ca -batch -config openssl.cnf -name CA_signing -revoke client-revoked.crt openssl ca -batch -config openssl.cnf -name CA_signing -gencrl -out crl.pem cat test-signing-ca.crt test-root-ca.crt > all-ca.crt #mkdir certs #cp test-signing-ca.crt certs/test-signing-ca.pem #cp test-root-ca.crt certs/test-root.ca.pem c_rehash certs rm -f client-expired.csr client-revoked.csr server-expired.csr server.csr test-alt-ca.csr paho.mqtt.python-2.0.0/tests/ssl/openssl.cnf000066400000000000000000000270621456167725100211070ustar00rootroot00000000000000# # OpenSSL example configuration file. # This is mostly being used for generation of certificate requests. # # This definition stops the following lines choking if HOME isn't # defined. HOME = . RANDFILE = $ENV::HOME/.rnd # Extra OBJECT IDENTIFIER info: #oid_file = $ENV::HOME/.oid oid_section = new_oids # To use this configuration file with the "-extfile" option of the # "openssl x509" utility, name here the section containing the # X.509v3 extensions to use: # extensions = # (Alternatively, use a configuration file that has only # X.509v3 extensions in its main [= default] section.) [ new_oids ] # We can add new OIDs in here for use by 'ca', 'req' and 'ts'. # Add a simple OID like this: # testoid1=1.2.3.4 # Or use config file substitution like this: # testoid2=${testoid1}.5.6 # Policies used by the TSA examples. tsa_policy1 = 1.2.3.4.1 tsa_policy2 = 1.2.3.4.5.6 tsa_policy3 = 1.2.3.4.5.7 #################################################################### [ ca ] default_ca = CA_default # The default ca section #################################################################### [ CA_signing ] dir = ./signingCA # Where everything is kept 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 certificates with same subject. new_certs_dir = $dir/newcerts # default place for new certs. certificate = test-signing-ca.crt # The CA certificate serial = $dir/serial # The current serial number crlnumber = $dir/crlnumber # the current crl number # must be commented out to leave a V1 CRL 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 extensions to add to the cert # Comment out the following two lines for the "traditional" # (and highly broken) format. name_opt = ca_default # Subject Name options cert_opt = ca_default # Certificate field options # Extension copying option: use with caution. # copy_extensions = copy # Extensions to add to a CRL. Note: Netscape communicator chokes on V2 CRLs # so this is commented out by default to leave a V1 CRL. # crlnumber must also be commented out to leave a V1 CRL. # crl_extensions = crl_ext default_days = 1825 # how long to certify for default_crl_days= 30 # how long before next CRL default_md = default # use public key default MD preserve = no # keep passed DN ordering # A few difference way of specifying how similar the request should look # For type CA, the listed attributes must be the same, and the optional # and supplied fields are just that :-) policy = policy_anything [ CA_inter ] dir = ./interCA certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt new_certs_dir = $dir/newcerts certificate = test-inter-ca.crt serial = $dir/serial crlnumber = $dir/crlnumber crl = $dir/crl.pem private_key = test-inter-ca.key RANDFILE = $dir/.rand #x509_extensions = v3_ca x509_extensions = usr_cert name_opt = ca_default cert_opt = ca_default default_days = 1825 default_crl_days = 30 default_md = default preserve = no policy = policy_match unique_subject = yes [ CA_root ] dir = ./rootCA certs = $dir/certs crl_dir = $dir/crl database = $dir/index.txt new_certs_dir = $dir/newcerts certificate = test-root-ca.crt serial = $dir/serial crlnumber = $dir/crlnumber crl = $dir/crl.pem private_key = test-root-ca.key RANDFILE = $dir/.rand x509_extensions = v3_ca name_opt = ca_default cert_opt = ca_default default_days = 1825 default_crl_days = 30 default_md = default preserve = no policy = policy_match unique_subject = yes # For the CA policy [ policy_match ] countryName = match stateOrProvinceName = match organizationName = match organizationalUnitName = optional commonName = supplied emailAddress = optional # For the 'anything' policy # At this point in time, you must list all acceptable 'object' # types. [ policy_anything ] countryName = optional stateOrProvinceName = optional localityName = optional organizationName = optional organizationalUnitName = optional commonName = supplied emailAddress = optional #################################################################### [ req ] default_bits = 2048 default_keyfile = privkey.pem distinguished_name = req_distinguished_name attributes = req_attributes 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 # output_password = secret # This sets a mask for permitted string types. There are several options. # default: PrintableString, T61String, BMPString. # pkix : PrintableString, BMPString (PKIX recommendation before 2004) # utf8only: only UTF8Strings (PKIX recommendation after 2004). # nombstr : PrintableString, T61String (no BMPStrings or UTF8Strings). # MASK:XXXX a literal mask value. # WARNING: ancient versions of Netscape crash on BMPStrings or UTF8Strings. string_mask = utf8only # req_extensions = v3_req # The extensions to add to a certificate request [ req_distinguished_name ] countryName = Country Name (2 letter code) countryName_default = GB countryName_min = 2 countryName_max = 2 stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = Derbyshire localityName = Locality Name (eg, city) localityName_default = Derby 0.organizationName = Organization Name (eg, company) 0.organizationName_default = Paho Project # we can do this but it is not needed normally :-) #1.organizationName = Second Organization Name (eg, company) #1.organizationName_default = World Wide Web Pty Ltd organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = Testing commonName = Common Name (e.g. server FQDN or YOUR name) commonName_max = 64 emailAddress = Email Address emailAddress_max = 64 # SET-ex3 = SET extension number 3 [ req_attributes ] challengePassword = A challenge password challengePassword_min = 4 challengePassword_max = 20 unstructuredName = An optional company name [ usr_cert ] # These extensions are added when 'ca' signs a request. # This goes against PKIX guidelines but some CAs do it and some software # requires this to avoid interpreting an end user certificate as a CA. basicConstraints=CA:FALSE # Here are some examples of the usage of nsCertType. If it is omitted # the certificate can be used for anything *except* object signing. # This is OK for an SSL server. # nsCertType = server # For an object signing certificate this would be used. # nsCertType = objsign # For normal client use this is typical # nsCertType = client, email # and for everything including object signing: # nsCertType = client, email, objsign # This is typical in keyUsage for a client certificate. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment # This will be displayed in Netscape's comment listbox. nsComment = "OpenSSL Generated Certificate" # PKIX recommendations harmless if included in all certificates. subjectKeyIdentifier=hash authorityKeyIdentifier=keyid,issuer # This stuff is for subjectAltName and issuerAltname. # Import the email address. # subjectAltName=email:copy # An alternative to produce certificates that aren't # deprecated according to PKIX. # subjectAltName=email:move # Copy subject details # issuerAltName=issuer:copy #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem #nsBaseUrl #nsRevocationUrl #nsRenewalUrl #nsCaPolicyUrl #nsSslServerName # This is required for TSA certificates. # extendedKeyUsage = critical,timeStamping [ v3_req ] # Extensions to add to a certificate request basicConstraints = CA:FALSE keyUsage = nonRepudiation, digitalSignature, keyEncipherment [ v3_ca ] # Extensions for a typical CA # PKIX recommendation. subjectKeyIdentifier=hash authorityKeyIdentifier=keyid:always,issuer # This is what PKIX recommends but some broken software chokes on critical # extensions. #basicConstraints = critical,CA:true # So we do this instead. basicConstraints = CA:true # Key usage: this is typical for a CA certificate. However since it will # prevent it being used as an test self-signed certificate it is best # left out by default. # keyUsage = cRLSign, keyCertSign # Some might want this also # nsCertType = sslCA, emailCA # Include email address in subject alt name: another PKIX recommendation # subjectAltName=email:copy # Copy issuer details # issuerAltName=issuer:copy # DER hex encoding of an extension: beware experts only! # obj=DER:02:03 # Where 'obj' is a standard or added object # You can even override a supported extension: # basicConstraints= critical, DER:30:03:01:01:FF [ crl_ext ] # CRL extensions. # Only issuerAltName and authorityKeyIdentifier make any sense in a CRL. # issuerAltName=issuer:copy authorityKeyIdentifier=keyid:always [ proxy_cert_ext ] # These extensions should be added when creating a proxy certificate # This goes against PKIX guidelines but some CAs do it and some software # requires this to avoid interpreting an end user certificate as a CA. basicConstraints=CA:FALSE # Here are some examples of the usage of nsCertType. If it is omitted # the certificate can be used for anything *except* object signing. # This is OK for an SSL server. # nsCertType = server # For an object signing certificate this would be used. # nsCertType = objsign # For normal client use this is typical # nsCertType = client, email # and for everything including object signing: # nsCertType = client, email, objsign # This is typical in keyUsage for a client certificate. # keyUsage = nonRepudiation, digitalSignature, keyEncipherment # This will be displayed in Netscape's comment listbox. nsComment = "OpenSSL Generated Certificate" # PKIX recommendations harmless if included in all certificates. subjectKeyIdentifier=hash authorityKeyIdentifier=keyid,issuer # This stuff is for subjectAltName and issuerAltname. # Import the email address. # subjectAltName=email:copy # An alternative to produce certificates that aren't # deprecated according to PKIX. # subjectAltName=email:move # Copy subject details # issuerAltName=issuer:copy #nsCaRevocationUrl = http://www.domain.dom/ca-crl.pem #nsBaseUrl #nsRevocationUrl #nsRenewalUrl #nsCaPolicyUrl #nsSslServerName # This really needs to be in place for it to be a proxy certificate. proxyCertInfo=critical,language:id-ppl-anyLanguage,pathlen:3,policy:foo #################################################################### [ tsa ] default_tsa = tsa_config1 # the default TSA section [ tsa_config1 ] # These are used by the TSA reply generation only. dir = ./demoCA # TSA root directory serial = $dir/tsaserial # The current serial number (mandatory) crypto_device = builtin # OpenSSL engine to use for signing signer_cert = $dir/tsacert.pem # The TSA signing certificate # (optional) certs = $dir/cacert.pem # Certificate chain to include in reply # (optional) signer_key = $dir/private/tsakey.pem # The TSA private key (optional) default_policy = tsa_policy1 # Policy if request did not specify it # (optional) other_policies = tsa_policy2, tsa_policy3 # acceptable policies (optional) digests = md5, sha1 # Acceptable message digests (mandatory) accuracy = secs:1, millisecs:500, microsecs:100 # (optional) clock_precision_digits = 0 # number of digits after dot. (optional) ordering = yes # Is ordering defined for timestamps? # (optional, default: no) tsa_name = yes # Must the TSA name be included in the reply? # (optional, default: no) ess_cert_id_chain = no # Must the ESS cert id chain be included? # (optional, default: no) paho.mqtt.python-2.0.0/tests/ssl/server-expired.crt000066400000000000000000000106521456167725100224070ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 1 (0x1) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Aug 20 00:00:00 2012 GMT Not After : Aug 21 00:00:00 2012 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=localhost Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:a9:4c:88:db:56:36:f8:fc:e1:eb:6b:ad:9c:0f: 78:3e:7d:d7:34:c6:83:94:6d:83:07:d5:3e:cb:fb: 95:61:e5:73:78:43:db:51:d9:a0:4e:ec:8e:43:21: 91:1f:56:95:08:47:7c:83:38:90:bb:53:91:ed:fd: b4:bd:08:27:dd:d6:d9:5b:fd:bb:84:1e:2e:62:d9: 3c:1d:4d:c9:6b:17:45:d7:9e:b4:a5:9c:22:cd:14: 41:32:c3:41:ad:8d:f5:2f:a3:d5:59:1f:a1:2b:67: d3:01:83:64:93:80:6b:bf:5a:b8:51:86:20:a0:e4: 3f:18:0c:67:19:8d:e3:58:6d:85:83:8f:8b:37:b2: 7d:21:3f:65:cf:19:53:2e:56:df:4d:89:50:7e:8c: 6a:8e:dd:21:15:15:31:9b:c2:5c:98:68:1e:31:ff: c6:6c:1f:a8:42:b8:da:62:dc:ae:62:4c:40:f0:06: c6:e6:f4:a9:98:3d:ed:fb:c0:2a:63:da:60:69:83: 11:0e:ce:ba:93:d7:4b:27:8f:86:91:ef:e4:65:5f: 20:be:04:f2:4d:d6:d1:74:c5:ab:e9:18:df:16:f9: 9a:8a:ff:2f:23:c5:46:3e:04:16:4e:fa:c1:0a:f4: dc:8e:1a:da:5f:a1:ad:50:7a:5d:60:00:3e:09:b8: 8e:6d Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: C3:47:33:CF:07:18:14:7C:9A:E4:AB:11:62:89:88:54:3D:5D:7D:E8 X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption ca:b6:c4:8a:76:e2:14:01:2f:58:ea:5f:28:f3:1d:de:a5:73: 17:13:d0:5a:2f:51:f8:a7:79:34:06:9d:73:8e:c9:bd:ba:e4: 03:64:7b:fc:29:b1:f3:4a:22:a1:bd:31:7a:4e:03:0d:f0:0c: b0:d8:40:03:7a:b6:a5:2a:ff:78:0b:de:49:7b:ee:11:97:52: 2a:df:68:53:d3:88:ac:bd:f2:04:25:68:04:12:8f:ea:26:05: 0d:9b:71:76:a9:cd:ff:99:78:44:86:07:56:04:14:c6:d7:1d: 63:6e:9f:07:76:95:0b:a0:2b:a2:0d:c4:79:ff:80:c2:80:cb: 83:c3:ec:ae:46:62:bb:09:71:c9:65:00:b8:6a:13:a4:a7:31: ad:ff:81:97:1c:84:1e:16:d5:c2:69:83:88:63:2d:33:31:52: 1b:fc:dc:c7:40:5c:c8:3e:0a:15:87:7f:82:47:8d:3e:f2:3e: 43:34:c1:8f:9c:16:61:1e:17:3f:4b:37:e1:aa:80:ad:87:09: cb:5c:fe:5a:28:4d:85:ca:45:58:6f:a6:ab:e2:f7:7a:24:c9: 34:2a:75:b9:29:b8:db:cf:0b:72:e3:89:06:d6:6c:a9:9f:82: e6:0f:90:b9:1a:4e:d1:f1:24:32:79:77:d3:cf:8f:27:64:f3: d6:3e:ff:45 -----BEGIN CERTIFICATE----- MIIDzDCCArSgAwIBAgIBATANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTEyMDgyMDAwMDAw MFoXDTEyMDgyMTAwMDAwMFowdjELMAkGA1UEBhMCR0IxGDAWBgNVBAgMD05vdHRp bmdoYW1zaGlyZTETMBEGA1UEBwwKTm90dGluZ2hhbTEPMA0GA1UECgwGU2VydmVy MRMwEQYDVQQLDApQcm9kdWN0aW9uMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0G CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCpTIjbVjb4/OHra62cD3g+fdc0xoOU bYMH1T7L+5Vh5XN4Q9tR2aBO7I5DIZEfVpUIR3yDOJC7U5Ht/bS9CCfd1tlb/buE Hi5i2TwdTclrF0XXnrSlnCLNFEEyw0GtjfUvo9VZH6ErZ9MBg2STgGu/WrhRhiCg 5D8YDGcZjeNYbYWDj4s3sn0hP2XPGVMuVt9NiVB+jGqO3SEVFTGbwlyYaB4x/8Zs H6hCuNpi3K5iTEDwBsbm9KmYPe37wCpj2mBpgxEOzrqT10snj4aR7+RlXyC+BPJN 1tF0xavpGN8W+ZqK/y8jxUY+BBZO+sEK9NyOGtpfoa1Qel1gAD4JuI5tAgMBAAGj ezB5MAkGA1UdEwQCMAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVk IENlcnRpZmljYXRlMB0GA1UdDgQWBBTDRzPPBxgUfJrkqxFiiYhUPV196DAfBgNV HSMEGDAWgBTCjwmb1fG6xHRel1C7hp2h8frENjANBgkqhkiG9w0BAQsFAAOCAQEA yrbEinbiFAEvWOpfKPMd3qVzFxPQWi9R+Kd5NAadc47JvbrkA2R7/Cmx80oiob0x ek4DDfAMsNhAA3q2pSr/eAveSXvuEZdSKt9oU9OIrL3yBCVoBBKP6iYFDZtxdqnN /5l4RIYHVgQUxtcdY26fB3aVC6Arog3Eef+AwoDLg8PsrkZiuwlxyWUAuGoTpKcx rf+BlxyEHhbVwmmDiGMtMzFSG/zcx0BcyD4KFYd/gkeNPvI+QzTBj5wWYR4XP0s3 4aqArYcJy1z+WihNhcpFWG+mq+L3eiTJNCp1uSm4288LcuOJBtZsqZ+C5g+QuRpO 0fEkMnl308+PJ2Tz1j7/RQ== -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/server.crt000066400000000000000000000106521456167725100207510ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 1 (0x1) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Nottinghamshire, L=Nottingham, O=Server, OU=Production, CN=localhost Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:a9:4c:88:db:56:36:f8:fc:e1:eb:6b:ad:9c:0f: 78:3e:7d:d7:34:c6:83:94:6d:83:07:d5:3e:cb:fb: 95:61:e5:73:78:43:db:51:d9:a0:4e:ec:8e:43:21: 91:1f:56:95:08:47:7c:83:38:90:bb:53:91:ed:fd: b4:bd:08:27:dd:d6:d9:5b:fd:bb:84:1e:2e:62:d9: 3c:1d:4d:c9:6b:17:45:d7:9e:b4:a5:9c:22:cd:14: 41:32:c3:41:ad:8d:f5:2f:a3:d5:59:1f:a1:2b:67: d3:01:83:64:93:80:6b:bf:5a:b8:51:86:20:a0:e4: 3f:18:0c:67:19:8d:e3:58:6d:85:83:8f:8b:37:b2: 7d:21:3f:65:cf:19:53:2e:56:df:4d:89:50:7e:8c: 6a:8e:dd:21:15:15:31:9b:c2:5c:98:68:1e:31:ff: c6:6c:1f:a8:42:b8:da:62:dc:ae:62:4c:40:f0:06: c6:e6:f4:a9:98:3d:ed:fb:c0:2a:63:da:60:69:83: 11:0e:ce:ba:93:d7:4b:27:8f:86:91:ef:e4:65:5f: 20:be:04:f2:4d:d6:d1:74:c5:ab:e9:18:df:16:f9: 9a:8a:ff:2f:23:c5:46:3e:04:16:4e:fa:c1:0a:f4: dc:8e:1a:da:5f:a1:ad:50:7a:5d:60:00:3e:09:b8: 8e:6d Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Basic Constraints: CA:FALSE Netscape Comment: OpenSSL Generated Certificate X509v3 Subject Key Identifier: C3:47:33:CF:07:18:14:7C:9A:E4:AB:11:62:89:88:54:3D:5D:7D:E8 X509v3 Authority Key Identifier: keyid:C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 Signature Algorithm: sha256WithRSAEncryption a9:34:8d:b4:6c:99:14:e7:10:dc:36:7e:c2:24:7f:bf:9d:65: 4c:2b:50:90:13:de:85:29:d7:b0:d5:c2:a0:d6:c2:42:40:9f: 6a:b7:6a:05:cf:7e:57:ff:2c:3c:ba:0c:cf:e7:0a:92:89:6e: 8b:bb:0c:b5:28:79:00:76:ed:12:cc:54:79:05:41:88:26:c9: e3:a8:6b:ba:1a:31:92:e6:40:2c:c6:a9:e8:4b:1b:4c:25:f1: 7b:c5:19:0b:73:37:53:86:d5:8e:d1:1c:78:73:e4:a5:84:0f: 49:5a:eb:80:15:09:c2:69:83:34:c0:da:db:9d:fa:eb:32:1f: e0:2e:99:f2:b0:76:91:8a:eb:34:b5:4d:c9:79:2a:f8:ef:f0: 6d:55:a4:9d:f9:5f:61:d3:f8:ab:95:0a:12:12:64:33:c3:2f: 6b:64:14:31:bf:42:c9:c8:9e:be:45:4f:02:c8:50:54:be:79: fe:e2:9a:fa:2d:b7:73:25:34:ea:53:dd:03:a4:f9:82:28:a7: 95:37:f7:45:56:21:7a:e6:71:eb:95:34:99:15:1c:26:ac:00: bc:95:b0:91:d8:8a:d0:0d:98:8e:28:d7:76:14:b6:94:c9:ab: df:87:40:58:12:da:ee:65:d8:08:f2:05:f2:5e:3e:d2:8d:09: 38:8f:b2:79 -----BEGIN CERTIFICATE----- MIIDzDCCArSgAwIBAgIBATANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEVMBMGA1UECgwMUGFobyBQcm9qZWN0MRAwDgYD VQQLDAdUZXN0aW5nMRMwEQYDVQQDDApTaWduaW5nIENBMB4XDTIxMDcwNzExMTQ0 MloXDTI2MDcwNjExMTQ0MlowdjELMAkGA1UEBhMCR0IxGDAWBgNVBAgMD05vdHRp bmdoYW1zaGlyZTETMBEGA1UEBwwKTm90dGluZ2hhbTEPMA0GA1UECgwGU2VydmVy MRMwEQYDVQQLDApQcm9kdWN0aW9uMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0G CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCpTIjbVjb4/OHra62cD3g+fdc0xoOU bYMH1T7L+5Vh5XN4Q9tR2aBO7I5DIZEfVpUIR3yDOJC7U5Ht/bS9CCfd1tlb/buE Hi5i2TwdTclrF0XXnrSlnCLNFEEyw0GtjfUvo9VZH6ErZ9MBg2STgGu/WrhRhiCg 5D8YDGcZjeNYbYWDj4s3sn0hP2XPGVMuVt9NiVB+jGqO3SEVFTGbwlyYaB4x/8Zs H6hCuNpi3K5iTEDwBsbm9KmYPe37wCpj2mBpgxEOzrqT10snj4aR7+RlXyC+BPJN 1tF0xavpGN8W+ZqK/y8jxUY+BBZO+sEK9NyOGtpfoa1Qel1gAD4JuI5tAgMBAAGj ezB5MAkGA1UdEwQCMAAwLAYJYIZIAYb4QgENBB8WHU9wZW5TU0wgR2VuZXJhdGVk IENlcnRpZmljYXRlMB0GA1UdDgQWBBTDRzPPBxgUfJrkqxFiiYhUPV196DAfBgNV HSMEGDAWgBTCjwmb1fG6xHRel1C7hp2h8frENjANBgkqhkiG9w0BAQsFAAOCAQEA qTSNtGyZFOcQ3DZ+wiR/v51lTCtQkBPehSnXsNXCoNbCQkCfardqBc9+V/8sPLoM z+cKkolui7sMtSh5AHbtEsxUeQVBiCbJ46hruhoxkuZALMap6EsbTCXxe8UZC3M3 U4bVjtEceHPkpYQPSVrrgBUJwmmDNMDa25366zIf4C6Z8rB2kYrrNLVNyXkq+O/w bVWknflfYdP4q5UKEhJkM8Mva2QUMb9CycievkVPAshQVL55/uKa+i23cyU06lPd A6T5giinlTf3RVYheuZx65U0mRUcJqwAvJWwkdiK0A2YjijXdhS2lMmr34dAWBLa 7mXYCPIF8l4+0o0JOI+yeQ== -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/server.key000066400000000000000000000032131456167725100207440ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAqUyI21Y2+Pzh62utnA94Pn3XNMaDlG2DB9U+y/uVYeVzeEPb UdmgTuyOQyGRH1aVCEd8gziQu1OR7f20vQgn3dbZW/27hB4uYtk8HU3JaxdF1560 pZwizRRBMsNBrY31L6PVWR+hK2fTAYNkk4Brv1q4UYYgoOQ/GAxnGY3jWG2Fg4+L N7J9IT9lzxlTLlbfTYlQfoxqjt0hFRUxm8JcmGgeMf/GbB+oQrjaYtyuYkxA8AbG 5vSpmD3t+8AqY9pgaYMRDs66k9dLJ4+Gke/kZV8gvgTyTdbRdMWr6RjfFvmaiv8v I8VGPgQWTvrBCvTcjhraX6GtUHpdYAA+CbiObQIDAQABAoIBAFAbWL6AIu7ZqYSd pL4tS7Y2ETh1nhkDYHa6XkZiuqJh0atcYFBwazwtDnuRTHvJmicavD3S7BjXSDuW SokPbN25JYwzmSDAry4yoBE1l1LG5lNKUyvxnz3ukZMVdORMQXHTUcYkAzzomZ0j sNlicJlQsdpRXusCVSBp7fbXfnV+SCRA0JZrMkCmkkQASpzlfZaDYxT+QYzNZ7aS W4c+YwLEaSyVRPmWdelj17d1XP5RdnsL6Fhho6wNRoT18tgSvvl1cWv+/e+eMGFQ hxmTJmcBxTTxVDF1+bHIYNHkxHD4OEcrYIP99wwYg9zanO9edxD1OSY5a0xupNns E9r517kCgYEA0S76Kz0UOmuLnT7KUs6dq5fXq+LJU5Dp6cnMiEtz65VqgBUEwRGn WNPKDzMQ5SrfNCw6aEpRwPSJOPRoRbFvCZ1ZqHzunjOhssIjGiMMJq+WpckgoX8b kvzzCpf8DfEHep7PAu/ixKZs5Jm6wliF52dWt6rgYEPK33A4qa9R1aMCgYEAzzBm gqQ4DZy/ZkUp0GZ1gPJ+wpKJug96Bb7PMMnCtVtcfTtjyR1q7JWaZdli1vCKlMKW /sOmydD8uPkKhnxy6Ksz5u5/ZCBxkANGnEOc3ED3v7wXHCoiQwsl59lH0yoqx4ua Ur59L/ZVTMZAjtpci2NTTMN+mezR9LXvQb2qrK8CgYEAjVjs+mKfVIpvIKXZGPM8 X0KPHTp1R95X8P3HEyHJBptEB6AsQjmnlsIlevfKps+9Wwe3v9jYPUX/o1ijTNSE bz6/4rXol0XUMXI1PegIwetMJGIvhnDZNQ1vPO1OCC2iHB1LTHTECpVaZ23pYIFo meCeHCV+0A1+/FRcNWyeI3kCgYBgFnhUOkjstzdk/MqJphr0tIHpRwCs06SpqXZ5 j/jHFxnr0nFSwlvmYPN8LLdUK7Z5i01v1dkyW8P5HTaubGT2Vv/5J77Y9tr0CTDk I89Jrq+3skmdfETrhu4Leo9+9V1lse7eVQ3GAp5IvuEN32NwGZ52SWwbguNUdFQD zyyqbQKBgBj7ltu2L59S03I1rV1Wrm+BFYbsqTZrU2PaA0nz2/mZjzkp+BYqqeRA y/LBOHiaUxsPZqyR+neOSoDuQK2HWjut5B9JFy61m2pw2E2qwdkpOmceQYuLRRO7 UAaHfCfkHE9R8k8FePBNB1HwWGGj02BpF5jP5Oph/JyuuQvPPH+M -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/test-alt-ca.crt000066400000000000000000000104461456167725100215620ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 2 (0x2) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, L=Derby, O=Paho Project, OU=Testing, CN=Root CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Alternative Signing CA Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:ca:f5:4f:c2:30:7d:fd:65:75:06:00:22:72:0a: d0:74:1e:00:03:aa:f3:64:1a:d4:d0:25:85:b9:2e: 73:72:41:06:12:d9:52:1d:39:11:78:2d:c3:0d:d7: 6f:06:05:68:3a:cc:ce:36:8d:a7:3a:a9:31:77:eb: e9:2d:87:00:7e:86:7a:bb:52:c2:02:d7:ef:07:4f: a9:88:91:d6:6e:dd:19:84:89:dc:72:bb:08:23:b4: be:1a:cf:af:b8:1a:af:62:21:d3:d4:a2:78:2f:b6: 4a:44:6f:ab:7f:d7:27:21:79:40:2b:db:bf:90:bf: fb:cf:a4:fa:8b:25:f6:ad:f9:73:57:41:49:86:1d: ed:3c:c9:d5:43:e0:ac:8a:4a:88:51:ea:cf:95:f0: 50:4b:ee:4c:fc:74:1d:92:00:5f:75:97:23:e4:b1: 79:b1:b0:b8:e1:97:38:6c:78:b6:c1:a6:e7:2e:95: 39:c8:ed:2a:65:65:b7:09:45:d4:f2:f1:4f:bf:97: 9d:98:b7:26:0d:c1:cf:93:d1:55:9f:af:39:6f:71: 29:a4:e9:74:48:2c:eb:8a:11:3d:3f:c4:3c:12:fe: 0c:d9:c9:fc:2c:77:22:de:c8:bb:8e:05:55:0a:2b: 18:38:0f:68:5d:2f:26:ea:cc:ec:04:df:fb:54:c4: 83:3b Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: 9E:74:83:0D:43:6F:21:68:72:E1:A3:FC:E0:2D:C3:D0:47:55:0C:31 X509v3 Authority Key Identifier: keyid:13:A0:B6:1F:F5:C7:64:C2:F9:FD:2E:08:F2:19:01:77:54:19:73:7F X509v3 Basic Constraints: CA:TRUE Signature Algorithm: sha256WithRSAEncryption 31:c8:c3:5c:31:7c:85:12:e0:01:9c:1a:eb:be:32:f0:19:cd: f3:55:e8:13:34:27:39:69:ca:88:1e:e9:44:47:9b:e1:bf:ff: 3f:65:62:02:9f:ae:be:21:1d:03:83:02:3e:a8:f2:d4:fa:e8: 14:50:e6:53:9e:e1:90:f9:96:5d:73:f3:da:8d:38:33:6d:5f: f9:ce:9b:60:d3:ae:86:18:7f:ef:4a:d1:69:4d:03:a7:e8:a5: c4:42:59:50:22:d1:25:bd:a4:22:d1:9c:f9:4c:72:ee:3d:e3: e1:c7:b0:c2:16:ba:46:4e:c9:29:91:e0:97:52:d8:3c:be:e2: ef:1c:aa:89:6d:ba:75:35:80:12:5d:5c:33:15:6c:fe:1b:1f: 4a:b4:1a:12:47:d3:4b:cd:d2:96:61:88:69:ac:b4:3c:d5:be: 52:7e:a0:99:5a:52:65:6a:86:ea:a7:a2:50:66:48:71:e3:82: 9f:fc:ff:89:58:ef:04:fa:af:76:98:1b:40:d6:71:14:29:1e: db:b8:31:47:2b:4b:de:f3:e2:e5:d0:a0:75:1e:b6:d9:32:3f: 8e:54:c3:92:e1:0f:74:85:0d:e9:27:5b:21:e8:f0:7b:10:3c: 14:e4:9d:97:65:18:ef:57:ce:de:b9:f7:01:d0:b9:e4:81:7a: a3:d2:35:8c -----BEGIN CERTIFICATE----- MIIDpDCCAoygAwIBAgIBAjANBgkqhkiG9w0BAQsFADBtMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwFRGVyYnkxFTATBgNVBAoMDFBh aG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQMA4GA1UEAwwHUm9vdCBDQTAe Fw0yMTA3MDcxMTE0NDJaFw0yNjA3MDYxMTE0NDJaMGwxCzAJBgNVBAYTAkdCMRMw EQYDVQQIDApEZXJieXNoaXJlMRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNV BAsMB1Rlc3RpbmcxHzAdBgNVBAMMFkFsdGVybmF0aXZlIFNpZ25pbmcgQ0EwggEi MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDK9U/CMH39ZXUGACJyCtB0HgAD qvNkGtTQJYW5LnNyQQYS2VIdORF4LcMN128GBWg6zM42jac6qTF36+kthwB+hnq7 UsIC1+8HT6mIkdZu3RmEidxyuwgjtL4az6+4Gq9iIdPUongvtkpEb6t/1ycheUAr 27+Qv/vPpPqLJfat+XNXQUmGHe08ydVD4KyKSohR6s+V8FBL7kz8dB2SAF91lyPk sXmxsLjhlzhseLbBpuculTnI7SplZbcJRdTy8U+/l52YtyYNwc+T0VWfrzlvcSmk 6XRILOuKET0/xDwS/gzZyfwsdyLeyLuOBVUKKxg4D2hdLybqzOwE3/tUxIM7AgMB AAGjUDBOMB0GA1UdDgQWBBSedIMNQ28haHLho/zgLcPQR1UMMTAfBgNVHSMEGDAW gBQToLYf9cdkwvn9LgjyGQF3VBlzfzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEB CwUAA4IBAQAxyMNcMXyFEuABnBrrvjLwGc3zVegTNCc5acqIHulER5vhv/8/ZWIC n66+IR0DgwI+qPLU+ugUUOZTnuGQ+ZZdc/PajTgzbV/5zptg066GGH/vStFpTQOn 6KXEQllQItElvaQi0Zz5THLuPePhx7DCFrpGTskpkeCXUtg8vuLvHKqJbbp1NYAS XVwzFWz+Gx9KtBoSR9NLzdKWYYhprLQ81b5SfqCZWlJlaobqp6JQZkhx44Kf/P+J WO8E+q92mBtA1nEUKR7buDFHK0ve8+Ll0KB1HrbZMj+OVMOS4Q90hQ3pJ1sh6PB7 EDwU5J2XZRjvV87eufcB0LnkgXqj0jWM -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/test-alt-ca.key000066400000000000000000000032171456167725100215600ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAyvVPwjB9/WV1BgAicgrQdB4AA6rzZBrU0CWFuS5zckEGEtlS HTkReC3DDddvBgVoOszONo2nOqkxd+vpLYcAfoZ6u1LCAtfvB0+piJHWbt0ZhInc crsII7S+Gs+vuBqvYiHT1KJ4L7ZKRG+rf9cnIXlAK9u/kL/7z6T6iyX2rflzV0FJ hh3tPMnVQ+CsikqIUerPlfBQS+5M/HQdkgBfdZcj5LF5sbC44Zc4bHi2wabnLpU5 yO0qZWW3CUXU8vFPv5edmLcmDcHPk9FVn685b3EppOl0SCzrihE9P8Q8Ev4M2cn8 LHci3si7jgVVCisYOA9oXS8m6szsBN/7VMSDOwIDAQABAoIBADVkjcP/b9Wu0Ddw 557q22YA0m4klf061cugY2qRHsvq8UcaJvELJ15fY5YLm+iQmZgGcyWE5H6ZLitn Q6O3hVjD1hvbrLCE0BwzR91myGvH/MOSZQ1FyOFj1jNFeevMEWGWlpy01TtwEF+q pQpvtpqmxEwFdoMFDqDUvRjINvoTUn1zijLA/tujEwHDriSjQPNd8/RcxYONQaAu 3SLInf7Gp3cJJ/EbE+MyK+/DpiG6kQ8Xkxdq928XtSpQAhxVjBzXaZR+hIXu+9jK 884Avl/TqRwKoMpLQIUaSVz4F65Hprz1y+Jo28OZ5x+l1oicdnWPTbNtc2xqWcQ1 3p0lO/ECgYEA96OAHgRwFwUwOmm9CDbAiYX4yssa7gmU1GVEJukqVwSb95M2Dff8 SgkhBABIHcPsYdNAi0klvJv6VqxRHC0dvGi3y0MFG+VdihtpOEh7GrH1aNyUohS0 Mx0p6fZjR2mLjCfdnTVD8mtGT97bTrkOP8jYe6r5ZpCE5V3fVHQif9MCgYEA0c+c DejT1uQMq3wQm5NwusRrMO6Eo/VOEJ22nkXNKC5kQEs8kXN8nkRdGCMznQY2Iurc MxhFPa1mBvYGVyefZFLjHJe1rWD0zujmjdjZ3cj9h0O5jf0vefQGuP5uteCtmUoj 81eGXfRac/ntEdFQLNEv9PSvBRZpup7e/koStfkCgYEAyEYYtS4NoPB3QqaFVIFD UXVh8lA0ZVKmZOfJKFbmAR4fLSiHTODDzvR3GQ9JQ5lSMQNybbMoq9LRsQsHRexO 4jMmgWKgXSEwdyMYA4bK2JoXyUirhDGOUtBBN5AmVnjLfPw4xI1xeDq90JaBcrdD CN7cBZgOv54dfIpgtaJ+zDUCgYEA0CzQSDTVzAgmUhgdWmAmoAm32asvzIbe2DnE MrJLZyzwp6J/DEqsQVTPkd2LnqfFG0wxBDl2qkXcT9fYXq2fxyk+0uXsi4UCIjKQ X/nj4d1FQOr/t1SZwMVRzkgVjTzKwqf/l7kmRx7miOBYSy+F/5HnpYMKDWA5s8Ni uqjAe/ECgYB/ew66RJjRiAxg5DnErIw6RX3lblHuK9tZ3uwyxVLev6wJnps1X4Ar m1WHFGgOGDDqOfC1n7JBp7qvWfLtr93aMlcSUPp34XItBr4LMgPAhk4irb079aoJ pCCx0JV+8ydFi4QaQcu4BRaed9T9PITf+qqTMtyXy4Q4QolV4rqiDw== -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/test-bad-root-ca.crt000066400000000000000000000025231456167725100225060ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDwDCCAqigAwIBAgIUZQzHgL/r7su5tQBXIZvwqj+HdqIwDQYJKoZIhvcNAQEL BQAwcTELMAkGA1UEBhMCR0IxEzARBgNVBAgMCkRlcmJ5c2hpcmUxDjAMBgNVBAcM BURlcmJ5MRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNVBAsMB1Rlc3Rpbmcx FDASBgNVBAMMC0JhZCBSb290IENBMB4XDTIxMDcwNzExMTQ0MloXDTMxMDcwNTEx MTQ0MlowcTELMAkGA1UEBhMCR0IxEzARBgNVBAgMCkRlcmJ5c2hpcmUxDjAMBgNV BAcMBURlcmJ5MRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNVBAsMB1Rlc3Rp bmcxFDASBgNVBAMMC0JhZCBSb290IENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A MIIBCgKCAQEA3DRlR+CK8ZBUfaZB4RzWErQ+lewTPu+FaQuCSBvBMKgd+S0r/mZ0 dQsA2/mWymggxb1wIZt/TY9sz2v1pYmg2Cw/dld9AQvJaqMXPdn3ZAmsihYd3is8 M4c0FlFowHv0LyWUOlRJfUrAPc4aorRK4Dqssl+s8W/ikyiKsMKBk0Z1LQBxUzst AAQ3voBJW7SVsRzYgcbyITW2IXYBjsIJRWK68+TCNCqlmVKEKvg6DYFJ+1HLE/z6 jFmzb10lXgg4FKKkUtWruawkErUbb8k1le+rnjZ0Wi9FhSWdM3HL1l6NX0IMWRmC Jm7WvXBHo9KCarcp+MWnKEjP4b/gR0bEvQIDAQABo1AwTjAdBgNVHQ4EFgQUc9Tb 8nwTWl+HI3JbYIQAFL3eZYgwHwYDVR0jBBgwFoAUc9Tb8nwTWl+HI3JbYIQAFL3e ZYgwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAnhmnsmFDcZ7b0YEQ XlIj760EUHe/G1Rtur2fjjP59qz/8msP9QtVAy5O/a22aCBehhOzcK2e3NFIKlBs D5xb7UE8RV0i9btP57+ZF6kP89sMB/DBHI+TDD93cms5OvTDCteKO++CpwnkNmav xRnvQGAAOA+zxVsPlYL1Wy9Z75LQWdZKS68/JTd7b2LOQnYD2qp4omPYEYGAFtFz 38EMgRS/QyQjjiHx6rz/wU5hmQCrNOUUCw+bHumZL3mxJ/aSBNrGVBLQ2Hnofhsw 1Ik2EyzMh3+nlf2ImlSZKfjfg8PrfmgbvXvNc8AWRCad7xwt9ZPzaxj05vKupcwO 2tIkOw== -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/test-bad-root-ca.key000066400000000000000000000032171456167725100225070ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpgIBAAKCAQEA3DRlR+CK8ZBUfaZB4RzWErQ+lewTPu+FaQuCSBvBMKgd+S0r /mZ0dQsA2/mWymggxb1wIZt/TY9sz2v1pYmg2Cw/dld9AQvJaqMXPdn3ZAmsihYd 3is8M4c0FlFowHv0LyWUOlRJfUrAPc4aorRK4Dqssl+s8W/ikyiKsMKBk0Z1LQBx UzstAAQ3voBJW7SVsRzYgcbyITW2IXYBjsIJRWK68+TCNCqlmVKEKvg6DYFJ+1HL E/z6jFmzb10lXgg4FKKkUtWruawkErUbb8k1le+rnjZ0Wi9FhSWdM3HL1l6NX0IM WRmCJm7WvXBHo9KCarcp+MWnKEjP4b/gR0bEvQIDAQABAoIBAQDPcKSAo60Ah4Cw pXCmSm34TMgwn6Y5wZYiMO9YUp0Z4yXpWH57N7U5lVYH5AYDQzisTxtU7ZFtVVGh zQgqG47kVjqqlxxxYdMqm90HLVB6cwqRQuh8JKqfuBx/cc2Glr6fs30BvelFGKgl EQXShJmMxnltx+e5wjblfmm4vmMmgpf/I3ROVwaPCrB0Zu0zWBqNDk++te7jMqkG uoBQ9Zv/C93gejFUktzKEMkXUAVqKLlwXlKPc2ypzMW15Omu7YcAo+ZWZIDeQ3RU HzH2zJylVp5F/v9nQbHU5G+8RzIwiVewwEyU2z3wve7Z+9UBA2hf0M3q6NfgYDx0 UDgaZzzZAoGBAO/vHy027FpULbfX93KZ9AMBjo2BrnDtOQCdL7qHkyov4Y1O4hH5 aPBdeijZQhzJlyNF+0bB0Qht26YzQx+vfkDMIcolvLAbYLNivRNaxNElpE4eZweD T7qfhRahyrBPoKikzvIIQGScqYCmPar0fQ3CfIi0+a7GDlNQ30JsifHTAoGBAOrz FBBLzAlwS+YiKh/734xWM8l1L/4RVyT8pqW4lNCNP9di8oJj7EEVvXUV4VoA1uNS goyoED8OuKLhlGwE+RXq0hRGxyIJgbU08UwV6zEfAAYg+SiYI7t3oEp1IP9p7vZ9 5LRfQyO1U6fxpub4l+tRdA1zGxVQNQJ7FJAcNMUvAoGBAJ8pDpNdxbe9833q45jA C6Aa3kd8aQ08L/36R3kDClqH3KVyWID34+be+3QxeqvCBmI9wAwV8eYXigdcJgDU 13mAcEG6esqPvrwAmdBG/ByJTc8MV+gh8TepLg3vUZdXmwmEGktvsdeMHNzcajgH axU/mIDPHHoVo9cc5J0ZhwBFAoGBAKpiQ6+ZuEs0A+bN6eyt9S1Jql6zvG0s2By7 mILf/BPOC3lAiYvjuQZuJKoPhxCFQVEzmfc1PirsmxuMKd24MYcidt07gtf9OvJV hZPe5WQHDjZjnS1CP8+I7lZw4NA5W5GoNL5Vw1PXAObvSVGBAHMn69iBHCf1taup 5Hyp598DAoGBAOhN1mSzSeyddtJiidy2ByL5PL/6BygNxXI//vXRZHmBugLWLczI qtzyUBPMXl1AdxusDkRuQgIgmrums/szsVVgzjJcZzSlxoktbHs1JphxgTTTu7Mh Z1KIaFjXkGF+rRat8rkmy6BVi/PpoPHIWNvEdbR5JZ3jzpPfAqVkvrvO -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/test-ca.srl000066400000000000000000000000211456167725100210000ustar00rootroot00000000000000CDAE0E564A2891A9 paho.mqtt.python-2.0.0/tests/ssl/test-fake-root-ca.crt000066400000000000000000000025061456167725100226670ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDuDCCAqCgAwIBAgIUcE+qUkqyZKFChp9j3+SuflCAAwgwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEzARBgNVBAgMCkRlcmJ5c2hpcmUxDjAMBgNVBAcM BURlcmJ5MRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNVBAsMB1Rlc3Rpbmcx EDAOBgNVBAMMB1Jvb3QgQ0EwHhcNMjEwNzA3MTExNDQyWhcNMzEwNzA1MTExNDQy WjBtMQswCQYDVQQGEwJHQjETMBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwF RGVyYnkxFTATBgNVBAoMDFBhaG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQ MA4GA1UEAwwHUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB AJ9Oc5kDGXyzORIRKBNiQ0+p63LN4DUK8ux/PMD7SEuGKq+LIdieCjmExUq2nkIE 052iYCM9aYJa8PuP+8UWl8UKE4/QWW6yWl28/O5n/Hwe28PfiiH1f5kxXt9khMjI oc3WCr8YkiDrrKiFyCGvF58b87woQFRMHHqus+o+Xd9YPKhsc/n/AhV4zl0S2wUC nnV+UF5c+/vlMh/SnD84yhMlySOC7fRNHziAJqqIpj44hQTdfjM6XDHOf3jSlHfv 1JxKqyE8hAWxZVZhMBP1v14xQL5AbVhtSNZlIV/LzAGUbBztMKzPfE4GyQKIDCmi 91A7nbXbkTBz/McL3kxmmAMCAwEAAaNQME4wHQYDVR0OBBYEFDZWdWv057drndtK 9AYPQhTgHkUuMB8GA1UdIwQYMBaAFDZWdWv057drndtK9AYPQhTgHkUuMAwGA1Ud EwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAEuWZu5tSJyNZkzbFT0o/IP8zUgN lbfp9DxqqiOwUTx2ykOpMsXU94f+/HEACYQ773G/lXPDmMrz/j3mPYklhws07/SE q1jKM6ZXJh74nQekypvXtSY/Xd667JpRxU6GAedizi60owKPIUFpxkW70h4Of5j5 Py7PGRGDZ7ItGtuk1fxcSCchfm0Q2bST8nOcD8D+MQcttNxGgelp2V6c0XckmijM oFUy/3Nm1B/qv4QWckmVX+gm+iBTBANItvcj+ie2c6diFwz7htDwOVm7/1Z/73wM YyM5Z27mVKR8FwK3jHHRQa5VWtTOdnqG3kHKmAKeOlXgO91wzCh8zq1Q76o= -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/test-fake-root-ca.key000066400000000000000000000032171456167725100226670ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAn05zmQMZfLM5EhEoE2JDT6nrcs3gNQry7H88wPtIS4Yqr4sh 2J4KOYTFSraeQgTTnaJgIz1pglrw+4/7xRaXxQoTj9BZbrJaXbz87mf8fB7bw9+K IfV/mTFe32SEyMihzdYKvxiSIOusqIXIIa8XnxvzvChAVEwceq6z6j5d31g8qGxz +f8CFXjOXRLbBQKedX5QXlz7++UyH9KcPzjKEyXJI4Lt9E0fOIAmqoimPjiFBN1+ MzpcMc5/eNKUd+/UnEqrITyEBbFlVmEwE/W/XjFAvkBtWG1I1mUhX8vMAZRsHO0w rM98TgbJAogMKaL3UDudtduRMHP8xwveTGaYAwIDAQABAoIBAG4D1KsHy/MlJjWG 6aExTADY/MOkz8Bx1j9iw0cWgd++QP5H3FDnG3KLcWBeaz52bNnAyBmuEI44VZG0 5o8+QgOOKOI5ZXmf6+4uVJIj9+aTvPsxBgjbrInT4YvutBChFbS7q2I7Crd3ah5b fVFdxLdZq2H2fi54/XXv7knHVjaldxf/mlq3XX1ndAvYXIY3L9PKjeeraEppRgce oZR6nnzliz7mBwIezaWV+DOCpotiJVYefeWsbN1QjKKzObnq1M5w4fv1R4jbT/zh RKIyxL3sa/8Beo3TSl4hFF9xNbQq957QdXKMqbdKdGWO0bQN4Mh4xqrEPo1ZK6qK RLyt5xECgYEAyvrgICVB4q7VFIqMzIznLnrBSg+HtpkLBIh2JjovWEQNh88Qul3t IH9VdOVT+SPeLCjED6vwQzU4bu4TJV2xwnv+Ujty4w8Aw4sSJlxSrMniKkdSxMus yhNgYg8E4WEDHxGtBNTyGc1lC2rvfDorvQAFajj5WJqGXLB9MumgP9sCgYEAyOso nZlfGKSWidUT+Mp0Jq9PG1kmAoBDEoMdCcpvp5p6ttUAb6sLVoY9Q+7U4VVVUIbH udYBvpDklgwJD2Erc6PK81g99bS/0fTuqCMlCGfDrqVTFxtWcYd9H2E3eJfo20YQ lUKgoOudXrlc7/a1TSK4Z0qGnWrygyhYypSwNPkCgYBKMv09IwF7sPd5g9BGcfeM eRkxTo4IxNdPN+cgwEJQXMgpbhsqVW16ZLHDgpV4zJDJybkqFWtF1i2j92mOTjrN 4m+sdcjgkbpwwOTImxUpzr7bP6lVATNPx1eDYQQis0jl0ZtS2dkKb5fRXazf14/n jhtsohkcN5iIR4fs1ZRb4wKBgQCtu1HCfOVS7LbS9jGv1nf7H2na7wpD7V6R+le4 qJhFp/lmcOZQqOlD5w3A2RqwwdXkrLa1RYz6mFVgPYX0C4TEGKScKPhipumbBhz7 vHAARaFaOdCQUW48+vhBkxGhMFIEkSAzwIoeu723M7deM8jvqw8jGbkvE1Qh/1hP y6RWGQKBgQCNfn28PybCmShtMFXnmcbtYOfI9b7ycGqFiKcW3pT5Q/2C4ReAyEVH uZ7xXApAzESao5V1evp2jRYGQAhK00YX/F9CXn8C57K55B5EC5cNu7LVSA81GswF /9VRFpxIWzilLEUGmgA0rUfvgsUyx6ILREhD1Qw8ihWNKMO2gJxVog== -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/test-root-ca.crt000066400000000000000000000025061456167725100217630ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDuDCCAqCgAwIBAgIUS1Q+E18/+trcKfhT+xz8ghGukmYwDQYJKoZIhvcNAQEL BQAwbTELMAkGA1UEBhMCR0IxEzARBgNVBAgMCkRlcmJ5c2hpcmUxDjAMBgNVBAcM BURlcmJ5MRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNVBAsMB1Rlc3Rpbmcx EDAOBgNVBAMMB1Jvb3QgQ0EwHhcNMjEwNzA3MTExNDQyWhcNMzEwNzA1MTExNDQy WjBtMQswCQYDVQQGEwJHQjETMBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwF RGVyYnkxFTATBgNVBAoMDFBhaG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQ MA4GA1UEAwwHUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB AKpCq45dCrroNa+y3zgdBglQOtw4og3MD/3Rn6ZftyL0dv1rSMkCFU8lCtZ4bIpz iNSJKau79owCudX3qQTPfiX2pmR5uuYjvMzRiZohZtz5uqXByy/CMS8dPRI3po6i kfNx9n7EQqOlxdwkY1kae2j5ybkAld2MNci93BH4P8qqaQckVRKpv6cKq33KsXK7 jHgjAYMGrihTAwxgP1JX9NS8yxxjMUYvFqeEOLARoeWc6Nl7oDbGLs2fr0j2Yssm cz0AMu7LWcbhnfs2S8Troksztnq38yHu+YTs6hX4NhANBgon5CAdyzmmE/b2OwOX p8rQepUfG7wO5QaS0OrAEXsCAwEAAaNQME4wHQYDVR0OBBYEFBOgth/1x2TC+f0u CPIZAXdUGXN/MB8GA1UdIwQYMBaAFBOgth/1x2TC+f0uCPIZAXdUGXN/MAwGA1Ud EwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAHgE1oMwIcilQFN4xPYCf8jbsa5o zA5ljTbxv7fU3Zd+7KdlDFYroGjgHb7o3r0//b8+ZarxBqn1274u4KPs39Ow7h6m YJo7IM2Z2fC6IWZroqeidfFx5SwejAP1j7coYLblTIbNF+P08sJG5nSQ+Yx0gams 6C1x0mETaaglDwllU1KXHTm8fUpEwpISc/VfKABYgScODMpdsDghyHANvnFjmvp4 ktABnasliZYTmdl0t3szNm7zIk+bntiK4KunFea8GqgslWqGPwtNxxJFHzPjMCxK EHgubLgp1lNZzH13XSO6ZpiNRDJ6IVed3Zq+yn+24uKH+1Hqp6Bt20ZFB4E= -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/test-root-ca.key000066400000000000000000000032131456167725100217570ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAqkKrjl0Kuug1r7LfOB0GCVA63DiiDcwP/dGfpl+3IvR2/WtI yQIVTyUK1nhsinOI1Ikpq7v2jAK51fepBM9+JfamZHm65iO8zNGJmiFm3Pm6pcHL L8IxLx09EjemjqKR83H2fsRCo6XF3CRjWRp7aPnJuQCV3Yw1yL3cEfg/yqppByRV Eqm/pwqrfcqxcruMeCMBgwauKFMDDGA/Ulf01LzLHGMxRi8Wp4Q4sBGh5Zzo2Xug NsYuzZ+vSPZiyyZzPQAy7stZxuGd+zZLxOuiSzO2erfzIe75hOzqFfg2EA0GCifk IB3LOaYT9vY7A5enytB6lR8bvA7lBpLQ6sARewIDAQABAoIBACAK+BqM7C4M8b2l XllDLRWnocw8ZFNQaloMj41SSjcr5xD+le4ulDAW+pkuhM7xu3i0b8FAWMA06yCX wZmEK2udpecW+dPCOhAaB1mYm7FO1o/HjyPn2jXRvOKm0pPZiLpWYlutOBVwZ3Js 7r2gPEWfbRWCRLIzZxPml3pSTD8p0IMGC4gO0jKHmGyLFQN0TOCdivVOzbQeDpUU lpj/v2wCfQQpfc/jP2bwTlGAZWVgmUtoj5XcWtRSLtwcWtK5KKgyQKlDdDL7d/Et J3x+QDLIwu9JNfaW8lcie16Y6qE4yOuBl95wfOpxN3wcmfxrKh7/rtN0Df1JNxvh 4bkyrGECgYEA2Lc36y+S9fOEecTwQd+AozIrBVhfaDU3L/tuKqKRJAhjI37DHkZ3 4tRYqd85bAcd+FED9cEK7Fqb51XovHTYQx0j3y++Iq6u+gzGWgSX2JGlprOkiNMk oXMX9P48KDBtCzD2aPxAslmrkhIPKEKmW+OTqpqHG6TsCshUoF1GQYMCgYEAyR+t A68mrnEcR3iapGhnnKqAEVx4zRdaXhBXFZvC0mF15xKtMTtjCEaT2X+iOiZE2fNn Si++pi/UGgLYChD7YsgWQlJUyrMVUHBYROfZ+sUIm9XvESVNQFLSSkr+vMH03hM3 I7d4Z3pbMEwDzAnv37i1HZ91Tvm4nfIsePenRqkCgYAdWdMs+yiAPxb2FwIjKc4W TDkfZDSnvG1ZBkiJZbMamjgzGnv6obii8/d+KklwpBYfB3nt0tNT54Gt9yiqPXj8 vfmZxLGPqPDx1MEYd/7IyhERXsst7MrNQvU/rR8gok5icaMt3Nw2S4a9Jcz/uucl EtFxDbS2vcNqQm+TuI5HWQKBgQC1xLj7IWsWMQfb2DX67Jjn0HhaOHa89KQpax8p WlKjDI4gPpLkccW5DwBEi8O0Ri3nxMHPHINzcrqAn51cy6hGyIrFed9EKsHSpxY/ gENTDowPOzQLDOlafv+rQUgklC6YHkmxL/nTm5OafLjZyQlP6oFVum2s6KhfpyVm VnyJsQKBgBk2hykG1EZmkLDKbrCXU7ggTvA9/FhEAjOt5PtQBjdKWTInuoizWX3n /C8ZYig7pYNytsb2um4CrF1Divgqz2ZceTWxfEF5IKqjqfhwMkBZ/uGz27t/BFaF pE5RD8iBhG+1inxV2UVz0gzBCNGciDxKb+ZPW087yE6NphLRydHv -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/ssl/test-signing-ca.crt000066400000000000000000000104121456167725100224310ustar00rootroot00000000000000Certificate: Data: Version: 3 (0x2) Serial Number: 1 (0x1) Signature Algorithm: sha256WithRSAEncryption Issuer: C=GB, ST=Derbyshire, L=Derby, O=Paho Project, OU=Testing, CN=Root CA Validity Not Before: Jul 7 11:14:42 2021 GMT Not After : Jul 6 11:14:42 2026 GMT Subject: C=GB, ST=Derbyshire, O=Paho Project, OU=Testing, CN=Signing CA Subject Public Key Info: Public Key Algorithm: rsaEncryption RSA Public-Key: (2048 bit) Modulus: 00:cb:32:6c:8c:48:e8:44:58:36:18:70:36:42:3d: 2d:29:47:3c:69:12:9e:7b:f7:45:62:ef:91:44:46: 97:a0:ea:5f:da:fd:9f:98:d4:bf:43:02:e3:39:90: 33:7b:13:13:d5:31:30:9c:07:fc:ca:1b:a9:e4:89: 42:e5:d0:6e:f4:a2:e0:23:ee:9d:9a:cc:80:3b:78: bf:7e:27:a8:46:1b:28:9f:4a:64:53:7a:89:3e:ab: 65:6f:af:0b:29:fa:4d:4f:04:f1:1e:10:2c:bf:2b: ea:fc:c5:fa:77:c9:1a:7a:78:29:f5:a2:cb:25:7c: 02:bb:91:8d:76:4d:23:bc:9c:19:da:be:c5:20:04: ad:fe:bd:b9:d4:bb:29:2a:c3:e4:fc:4c:84:db:a3: 55:9f:f0:70:7f:40:38:b5:c3:78:a5:db:06:36:b7: 10:8e:ca:6c:1a:92:66:be:0e:1a:97:59:6b:18:f4: c2:b8:c9:31:7b:d1:b1:a1:00:78:7f:c0:09:f6:ef: b2:8f:94:87:5d:b1:a2:23:93:4d:ec:fa:95:09:a9: 90:c4:02:f0:1e:d9:ab:a2:8b:7f:7f:54:95:e7:da: c3:c9:7d:a7:d7:04:89:59:db:88:9d:57:16:5d:b9: 66:b0:d6:88:bb:e0:ee:43:e9:ab:02:78:fc:bd:e8: 98:d9 Exponent: 65537 (0x10001) X509v3 extensions: X509v3 Subject Key Identifier: C2:8F:09:9B:D5:F1:BA:C4:74:5E:97:50:BB:86:9D:A1:F1:FA:C4:36 X509v3 Authority Key Identifier: keyid:13:A0:B6:1F:F5:C7:64:C2:F9:FD:2E:08:F2:19:01:77:54:19:73:7F X509v3 Basic Constraints: CA:TRUE Signature Algorithm: sha256WithRSAEncryption 3e:70:76:69:37:e4:6e:e0:08:c6:8e:5b:2e:aa:26:fe:e9:ed: ac:02:ce:2c:37:08:6a:8a:c3:0d:c0:ef:43:51:01:2e:e0:96: 76:23:1b:1f:75:98:df:7c:d1:b7:c1:67:aa:62:c1:bd:ef:84: eb:d9:28:47:50:f2:1b:54:7f:ed:cb:52:f7:fc:c3:f8:62:22: 0c:b3:95:ed:bb:3f:74:91:bc:d2:eb:c0:81:7d:74:12:85:61: a3:7e:fb:22:4a:25:99:0b:5d:ef:69:f2:5a:e6:d5:12:a3:95: 38:30:0c:c7:d9:da:28:30:10:b4:3d:3e:ad:20:85:31:e0:bf: 30:33:2e:0b:e3:07:3d:ed:22:dc:67:f8:93:64:89:ed:e7:08: 74:b5:0a:7a:01:3d:f9:44:62:71:cf:60:12:92:c3:95:9a:e5: a5:f2:24:6a:22:64:d5:76:22:c9:03:1c:c5:d1:a5:85:4d:55: f9:80:47:ca:12:20:df:05:fb:82:12:45:6f:e8:c0:20:a8:ae: f7:17:c5:c3:b6:9c:51:bd:d8:84:e4:db:c7:03:44:d2:cb:75: 51:79:3f:86:33:3c:e4:34:1d:77:b2:60:24:5c:21:c5:c3:53: 36:08:2f:a7:14:0b:68:78:67:95:90:b9:06:0e:85:04:65:57: b4:34:31:cf -----BEGIN CERTIFICATE----- MIIDmDCCAoCgAwIBAgIBATANBgkqhkiG9w0BAQsFADBtMQswCQYDVQQGEwJHQjET MBEGA1UECAwKRGVyYnlzaGlyZTEOMAwGA1UEBwwFRGVyYnkxFTATBgNVBAoMDFBh aG8gUHJvamVjdDEQMA4GA1UECwwHVGVzdGluZzEQMA4GA1UEAwwHUm9vdCBDQTAe Fw0yMTA3MDcxMTE0NDJaFw0yNjA3MDYxMTE0NDJaMGAxCzAJBgNVBAYTAkdCMRMw EQYDVQQIDApEZXJieXNoaXJlMRUwEwYDVQQKDAxQYWhvIFByb2plY3QxEDAOBgNV BAsMB1Rlc3RpbmcxEzARBgNVBAMMClNpZ25pbmcgQ0EwggEiMA0GCSqGSIb3DQEB AQUAA4IBDwAwggEKAoIBAQDLMmyMSOhEWDYYcDZCPS0pRzxpEp5790Vi75FERpeg 6l/a/Z+Y1L9DAuM5kDN7ExPVMTCcB/zKG6nkiULl0G70ouAj7p2azIA7eL9+J6hG GyifSmRTeok+q2Vvrwsp+k1PBPEeECy/K+r8xfp3yRp6eCn1osslfAK7kY12TSO8 nBnavsUgBK3+vbnUuykqw+T8TITbo1Wf8HB/QDi1w3il2wY2txCOymwakma+DhqX WWsY9MK4yTF70bGhAHh/wAn277KPlIddsaIjk03s+pUJqZDEAvAe2auii39/VJXn 2sPJfafXBIlZ24idVxZduWaw1oi74O5D6asCePy96JjZAgMBAAGjUDBOMB0GA1Ud DgQWBBTCjwmb1fG6xHRel1C7hp2h8frENjAfBgNVHSMEGDAWgBQToLYf9cdkwvn9 LgjyGQF3VBlzfzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQA+cHZp N+Ru4AjGjlsuqib+6e2sAs4sNwhqisMNwO9DUQEu4JZ2IxsfdZjffNG3wWeqYsG9 74Tr2ShHUPIbVH/ty1L3/MP4YiIMs5Xtuz90kbzS68CBfXQShWGjfvsiSiWZC13v afJa5tUSo5U4MAzH2dooMBC0PT6tIIUx4L8wMy4L4wc97SLcZ/iTZInt5wh0tQp6 AT35RGJxz2ASksOVmuWl8iRqImTVdiLJAxzF0aWFTVX5gEfKEiDfBfuCEkVv6MAg qK73F8XDtpxRvdiE5NvHA0TSy3VReT+GMzzkNB13smAkXCHFw1M2CC+nFAtoeGeV kLkGDoUEZVe0NDHP -----END CERTIFICATE----- paho.mqtt.python-2.0.0/tests/ssl/test-signing-ca.key000066400000000000000000000032171456167725100224360ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpgIBAAKCAQEAyzJsjEjoRFg2GHA2Qj0tKUc8aRKee/dFYu+RREaXoOpf2v2f mNS/QwLjOZAzexMT1TEwnAf8yhup5IlC5dBu9KLgI+6dmsyAO3i/fieoRhson0pk U3qJPqtlb68LKfpNTwTxHhAsvyvq/MX6d8kaengp9aLLJXwCu5GNdk0jvJwZ2r7F IASt/r251LspKsPk/EyE26NVn/Bwf0A4tcN4pdsGNrcQjspsGpJmvg4al1lrGPTC uMkxe9GxoQB4f8AJ9u+yj5SHXbGiI5NN7PqVCamQxALwHtmroot/f1SV59rDyX2n 1wSJWduInVcWXblmsNaIu+DuQ+mrAnj8veiY2QIDAQABAoIBAQCh3tl6J9pgF6WA cmPHANUpPQZy7dIzDxjHZ/FhYpsIJa2W1tR8+34h8/rvsGBSezAhdb4zjmli2AbP eElCqni5icbk2QHUf3Tn65kg9pamwpvpyWmC1ureccus3NUX674KZPVv7ZK3+FSK aWzOX/Yn+fHzLGyIv/GtWpZG18zQQl0+i2sKqKYQu00qBBLp+t9GJYpqPx9qpumB JzWaVuMEwkTJ4J6j7d28V8r8fnrURIcb/3R6dZsB6QtjgnJNzJRwFpaDDKoH3ZNV IkMqRNGuzuuzhL5Rzd2nd8oRUvgUAl93ad/fxWfmVVSyVbh/LCkOBDwSC6Z2Ri4c BafNAoMBAoGBAP0E9y121/lMkiLh0KF3suwbSJ/Z/GFO9ZhP5wW2vibcrQppF6vz kdYyEmjyPH+3UoKZOYzAwkTDJFJaosaagmiPuIzsGeF20A7D7ZQ1Ru5ScxVo/XWK i2g4s4wqlEZ9vhlcg/QBOUfzi23lUyGAXuQlgORQtzbN/vzGSRkIivWNAoGBAM2X NU32Hw0RuOXtaw4aZyY4oFT2nyPP99fAzR+IGX7XMny5bt2kBnHExpC43VZevFHc qzQdot4DbOUi/kO+LOiUHcYIW2/nADJjsxnDlnxU+9L6cMv74rQgskjNgjS0l+bx /W6/QFoOOJeVDT1VXhQbjIL3PxffdKmEWPs7ZT99AoGBAKZ6SuymIorMv+alr/Fd 4eMKPKm48x9Ppba29CnFSK4nSs/rwACKva0yuvxETlw2UdrOWJhtCCXYRCDPtAR7 C00jK2nFu22nEFR2w+5dc7NBmqk+sG5TX1CO5kxWg8Mx3w+u2L+Gwpq9+0Kuvhjv 7v+sUXdoSHSN67WD/fqzrULNAoGBAKv+Qvbc329Ek0Wv0K70wbSFDQTnaY1BT9us jS5C4ultSOx1CV3c+hM1htTOA0VdbfiiPowT+wv3G6O6GbM8pz9PonToyu4b99sv 80arjPqo8h+3qqPMLwV4kQ489x/2sVngup9q2oA8g3W0mWXlRBZYUb3C8IKdS3EB qptLPlHVAoGBAMgetjin8PFljR6Wt/GF5Y84MtonxH0oyJ7F1nw4NfmxqZxPwSXG L1/Adc3qTyOTM+JhWL+mSqiF+go2RpEHB04kItFWgShGb6k84T7Hdq+Qrw6yZGR1 wX7UpLwK2mrdIkzEVRMw2+7Uvi9nAn1rVxmlCFNamPxhC4ky0TBIboKl -----END RSA PRIVATE KEY----- paho.mqtt.python-2.0.0/tests/test_client.py000066400000000000000000001035601456167725100210200ustar00rootroot00000000000000import threading import time import unicodedata 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 import tests.paho_test as paho_test # Import test fixture from tests.testsupport.broker import FakeBroker, fake_broker # noqa: F401 @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: """ Tests on connect/disconnect behaviour of the client """ def test_01_con_discon_success(self, proto_ver, callback_version, fake_broker): mqttc = client.Client( callback_version, "01-con-discon-success", protocol=proto_ver, ) 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", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect( "01-con-discon-success", keepalive=60, 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) 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 def test_01_con_failure_rc(self, proto_ver, callback_version, fake_broker): mqttc = client.Client( callback_version, "01-con-failure-rc", protocol=proto_ver) 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", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect( "01-con-failure-rc", keepalive=60, 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=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() def test_connection_properties(self, proto_ver, callback_version, fake_broker): mqttc = client.Client(CallbackAPIVersion.VERSION2, "client-id", protocol=proto_ver) 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 = "tcp" 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 = "tcp" 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) 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, ) 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, ) 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", ) 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("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") def on_message(client, userdata, msg): with pytest.raises(UnicodeDecodeError): assert msg.topic client.disconnect() mqttc.on_message = on_message mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect("client-id") packet_in = fake_broker.receive_packet(len(connect_packet)) 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) publish_packet = paho_test.gen_publish(b"\xff", qos=0) count = fake_broker.send_packet(publish_packet) assert count # Check connection was not closed assert count == len(publish_packet) disconnect_packet = paho_test.gen_disconnect() packet_in = fake_broker.receive_packet(len(disconnect_packet)) 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 def test_valid_utf8_topic_recv(self, callback_version, fake_broker): mqttc = client.Client(callback_version, "client-id") # It should be non-ascii multi-bytes character topic = unicodedata.lookup('SNOWMAN') def on_message(client, userdata, msg): assert msg.topic == topic client.disconnect() mqttc.on_message = on_message mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect("client-id") packet_in = fake_broker.receive_packet(len(connect_packet)) 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) publish_packet = paho_test.gen_publish( topic.encode('utf-8'), qos=0 ) count = fake_broker.send_packet(publish_packet) assert count # Check connection was not closed assert count == len(publish_packet) disconnect_packet = paho_test.gen_disconnect() packet_in = fake_broker.receive_packet(len(disconnect_packet)) 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 def test_valid_utf8_topic_publish(self, callback_version, fake_broker): mqttc = client.Client(callback_version, "client-id") # It should be non-ascii multi-bytes character topic = unicodedata.lookup('SNOWMAN') mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect("client-id") packet_in = fake_broker.receive_packet(len(connect_packet)) 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) mqttc.publish(topic, None, 0) # Small sleep needed to avoid connection reset. time.sleep(0.3) publish_packet = paho_test.gen_publish( topic.encode('utf-8'), qos=0 ) packet_in = fake_broker.receive_packet(len(publish_packet)) assert packet_in # Check connection was not closed assert packet_in == publish_packet mqttc.disconnect() disconnect_packet = paho_test.gen_disconnect() packet_in = fake_broker.receive_packet(len(disconnect_packet)) 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 def test_message_callback(self, callback_version, fake_broker): mqttc = client.Client(callback_version, "client-id") userdata = { 'on_message': 0, 'callback1': 0, 'callback2': 0, } mqttc.user_data_set(userdata) def on_message(client, userdata, msg): assert msg.topic == 'topic/value' userdata['on_message'] += 1 def callback1(client, userdata, msg): assert msg.topic == 'topic/callback/1' userdata['callback1'] += 1 def callback2(client, userdata, msg): assert msg.topic in ('topic/callback/3', 'topic/callback/1') userdata['callback2'] += 1 mqttc.on_message = on_message mqttc.message_callback_add('topic/callback/1', callback1) mqttc.message_callback_add('topic/callback/+', callback2) mqttc.connect_async("localhost", fake_broker.port) mqttc.loop_start() try: fake_broker.start() connect_packet = paho_test.gen_connect("client-id") packet_in = fake_broker.receive_packet(len(connect_packet)) 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) publish_packet = paho_test.gen_publish(b"topic/value", qos=1, mid=1) count = fake_broker.send_packet(publish_packet) assert count # Check connection was not closed assert count == len(publish_packet) publish_packet = paho_test.gen_publish(b"topic/callback/1", qos=1, mid=2) count = fake_broker.send_packet(publish_packet) assert count # Check connection was not closed assert count == len(publish_packet) publish_packet = paho_test.gen_publish(b"topic/callback/3", qos=1, mid=3) count = fake_broker.send_packet(publish_packet) assert count # Check connection was not closed assert count == len(publish_packet) puback_packet = paho_test.gen_puback(mid=1) packet_in = fake_broker.receive_packet(len(puback_packet)) assert packet_in # Check connection was not closed assert packet_in == puback_packet puback_packet = paho_test.gen_puback(mid=2) packet_in = fake_broker.receive_packet(len(puback_packet)) assert packet_in # Check connection was not closed assert packet_in == puback_packet puback_packet = paho_test.gen_puback(mid=3) packet_in = fake_broker.receive_packet(len(puback_packet)) assert packet_in # Check connection was not closed assert packet_in == puback_packet mqttc.disconnect() disconnect_packet = paho_test.gen_disconnect() packet_in = fake_broker.receive_packet(len(disconnect_packet)) 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 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 migrations.md"): _ = 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, ) 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, ) 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 paho.mqtt.python-2.0.0/tests/test_matcher.py000066400000000000000000000017451456167725100211670ustar00rootroot00000000000000import paho.mqtt.client as client import pytest class Test_client_function: """ Tests on topic_matches_sub function in the client module """ @pytest.mark.parametrize("sub,topic", [ ("foo/bar", "foo/bar"), ("foo/+", "foo/bar"), ("foo/+/baz", "foo/bar/baz"), ("foo/+/#", "foo/bar/baz"), ("A/B/+/#", "A/B/B/C"), ("#", "foo/bar/baz"), ("#", "/foo/bar"), ("/#", "/foo/bar"), ("$SYS/bar", "$SYS/bar"), ]) def test_matching(self, sub, topic): assert client.topic_matches_sub(sub, topic) @pytest.mark.parametrize("sub,topic", [ ("test/6/#", "test/3"), ("foo/bar", "foo"), ("foo/+", "foo/bar/baz"), ("foo/+/baz", "foo/bar/bar"), ("foo/+/#", "fo2/bar/baz"), ("/#", "foo/bar"), ("#", "$SYS/bar"), ("$BOB/bar", "$SYS/bar"), ]) def test_not_matching(self, sub, topic): assert not client.topic_matches_sub(sub, topic) paho.mqtt.python-2.0.0/tests/test_mqttv5.py000066400000000000000000001565061456167725100210120ustar00rootroot00000000000000""" ******************************************************************* Copyright (c) 2013, 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 logging import sys import threading import time 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.subscribeoptions import SubscribeOptions class Callbacks: def __init__(self): self.messages = [] self.publisheds = [] self.subscribeds = [] self.unsubscribeds = [] self.disconnecteds = [] self.connecteds = [] self.conn_failures = [] def __str__(self): return str(self.messages) + str(self.messagedicts) + str(self.publisheds) + \ str(self.subscribeds) + \ str(self.unsubscribeds) + str(self.disconnects) 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}) 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 def wait_connect_fail(self): return self.wait(self.conn_failures, timeout=10) def wait_connected(self): return self.wait(self.connecteds) def on_disconnect(self, client, userdata, reasonCode, properties=None): self.disconnecteds.append( {"reasonCode": reasonCode, "properties": properties}) def wait_disconnected(self): return self.wait(self.disconnecteds) def on_message(self, client, userdata, message): self.messages.append({"userdata": userdata, "message": message}) def published(self, client, userdata, msgid): self.publisheds.append(msgid) def wait_published(self): return self.wait(self.publisheds) def on_subscribe(self, client, userdata, mid, reasonCodes, properties): self.subscribeds.append({"mid": mid, "userdata": userdata, "properties": properties, "reasonCodes": reasonCodes}) def wait_subscribed(self): return self.wait(self.subscribeds) def unsubscribed(self, client, userdata, mid, properties, reasonCodes): self.unsubscribeds.append({"mid": mid, "userdata": userdata, "properties": properties, "reasonCodes": reasonCodes}) def wait_unsubscribed(self): return self.wait(self.unsubscribeds) def on_log(self, client, userdata, level, buf): print(buf) def register(self, client): client.on_connect = self.on_connect client.on_subscribe = self.on_subscribe client.on_publish = self.published client.on_unsubscribe = self.unsubscribed client.on_message = self.on_message client.on_disconnect = self.on_disconnect client.on_connect_fail = self.on_connect_fail client.on_log = self.on_log def cleanRetained(port): callback = Callbacks() curclient = paho.mqtt.client.Client( CallbackAPIVersion.VERSION1, b"clean retained", protocol=paho.mqtt.client.MQTTv5, ) curclient.loop_start() callback.register(curclient) curclient.connect(host="localhost", port=port) callback.wait_connected() curclient.subscribe("#", options=SubscribeOptions(qos=0)) 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) curclient.disconnect() curclient.loop_stop() time.sleep(.1) def cleanup(port): # clean all client state print("clean up starting") clientids = ("aclient", "bclient") for clientid in clientids: curclient = paho.mqtt.client.Client( CallbackAPIVersion.VERSION1, clientid.encode("utf-8"), protocol=paho.mqtt.client.MQTTv5, ) curclient.loop_start() curclient.connect(host="localhost", port=port, clean_start=True) time.sleep(.1) curclient.disconnect() time.sleep(.1) curclient.loop_stop() # clean retained messages cleanRetained(port) print("clean up finished") class Test(unittest.TestCase): @classmethod def setUpClass(cls): global callback, callback2, aclient, bclient sys.path.append("paho.mqtt.testing/interoperability/") try: import mqtt.brokers 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 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] setData() cleanup(cls._test_broker_port) callback = Callbacks() callback2 = Callbacks() #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(CallbackAPIVersion.VERSION1, b"aclient", protocol=paho.mqtt.client.MQTTv5) callback.register(aclient) bclient = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, b"bclient", protocol=paho.mqtt.client.MQTTv5) callback2.register(bclient) @classmethod def tearDownClass(cls): # Another hack to stop the test broker... we rely on fact that it use a sockserver.TCPServer import mqtt.brokers 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): aclient.connect(host="localhost", port=self._test_broker_port) aclient.loop_start() response = callback.wait_connected() self.assertEqual(response["reasonCode"].getName(), "Success") aclient.subscribe(topics[0], options=SubscribeOptions(qos=2)) response = callback.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) aclient.disconnect() callback.clear() aclient.loop_stop() def test_connect_fail(self): clientid = "connection failure" fclient, fcallback = self.new_client(clientid) fclient.user_data_set(1) fclient.connect_async("localhost", 1) response = fcallback.wait_connect_fail() self.assertEqual(response["userdata"], 1) fclient.loop_stop() def test_retained_message(self): publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.UserProperty = ("a", "2") publish_properties.UserProperty = ("c", "3") # retained messages callback.clear() aclient.connect(host="localhost", port=self._test_broker_port) aclient.loop_start() response = callback.wait_connected() aclient.publish(topics[1], b"qos 0", 0, retain=True, properties=publish_properties) aclient.publish(topics[2], b"qos 1", 1, retain=True, properties=publish_properties) aclient.publish(topics[3], b"qos 2", 2, retain=True, properties=publish_properties) # wait until those messages are published time.sleep(1) aclient.subscribe(wildtopics[5], options=SubscribeOptions(qos=2)) response = callback.wait_subscribed() self.assertEqual(response["reasonCodes"][0].getName(), "Granted QoS 2") time.sleep(1) aclient.disconnect() aclient.loop_stop() self.assertEqual(len(callback.messages), 3) userprops = callback.messages[0]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) userprops = callback.messages[1]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) userprops = callback.messages[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)] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) cleanRetained(self._test_broker_port) def test_will_message(self): # will messages and keep alive callback.clear() callback2.clear() self.assertEqual(len(callback2.messages), 0, callback2.messages) will_properties = Properties(PacketTypes.WILLMESSAGE) will_properties.WillDelayInterval = 0 # this is the default anyway will_properties.UserProperty = ("a", "2") will_properties.UserProperty = ("c", "3") aclient.will_set(topics[2], payload=b"will message", properties=will_properties) aclient.connect(host="localhost", port=self._test_broker_port, keepalive=2) aclient.loop_start() response = callback.wait_connected() bclient.connect(host="localhost", port=self._test_broker_port) bclient.loop_start() response = callback2.wait_connected() bclient.subscribe(topics[2], qos=2) response = callback2.wait_subscribed() self.assertEqual(response["reasonCodes"][0].getName(), "Granted QoS 2") # 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) bclient.disconnect() bclient.loop_stop() # should have the will message self.assertEqual(len(callback2.messages), 1, callback2.messages) props = callback2.messages[0]["message"].properties self.assertEqual(props.UserProperty, [("a", "2"), ("c", "3")]) def test_zero_length_clientid(self): logging.info("Zero length clientid test starting") callback0 = Callbacks() client0 = paho.mqtt.client.Client(CallbackAPIVersion.VERSION1, protocol=paho.mqtt.client.MQTTv5) callback0.register(client0) client0.loop_start() # should not be rejected client0.connect(host="localhost", port=self._test_broker_port, clean_start=False) response = callback0.wait_connected() self.assertEqual(response["reasonCode"].getName(), "Success") self.assertTrue( len(response["properties"].AssignedClientIdentifier) > 0) client0.disconnect() client0.loop_stop() 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 response = callback0.wait_connected() self.assertEqual(response["reasonCode"].getName(), "Success") self.assertTrue( len(response["properties"].AssignedClientIdentifier) > 0) client0.disconnect() client0.loop_stop() # when we supply a client id, we should not get one assigned client0 = paho.mqtt.client.Client( 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 response = callback0.wait_connected() self.assertEqual(response["reasonCode"].getName(), "Success") self.assertFalse( hasattr(response["properties"], "AssignedClientIdentifier")) client0.disconnect() client0.loop_stop() def test_offline_message_queueing(self): # message queueing for offline clients cleanRetained(self._test_broker_port) ocallback = Callbacks() clientid = b"offline message queueing" oclient = paho.mqtt.client.Client( 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) ocallback.wait_connected() oclient.subscribe(wildtopics[5], qos=2) ocallback.wait_subscribed() oclient.disconnect() oclient.loop_stop() bclient.loop_start() bclient.connect(host="localhost", port=self._test_broker_port) 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) bclient.disconnect() bclient.loop_stop() oclient = paho.mqtt.client.Client( 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) ocallback.wait_connected() time.sleep(2) oclient.disconnect() oclient.loop_stop() self.assertTrue(len(ocallback.messages) in [ 2, 3], len(ocallback.messages)) logging.info("This server %s queueing QoS 0 messages for offline clients" % ("is" if len(ocallback.messages) == 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 = b"overlapping subscriptions" oclient = paho.mqtt.client.Client( CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, ) ocallback.register(oclient) oclient.loop_start() oclient.connect(host="localhost", port=self._test_broker_port) ocallback.wait_connected() oclient.subscribe([(wildtopics[6], SubscribeOptions(qos=2)), (wildtopics[0], SubscribeOptions(qos=1))]) 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: 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) 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) 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 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 = b"subscribe failure" oclient = paho.mqtt.client.Client( CallbackAPIVersion.VERSION1, clientid, protocol=paho.mqtt.client.MQTTv5, ) ocallback.register(oclient) oclient.loop_start() oclient.connect(host="localhost", port=self._test_broker_port) ocallback.wait_connected() oclient.subscribe(nosubscribe_topics[0], qos=2) response = ocallback.wait_subscribed() self.assertEqual(response["reasonCodes"][0].getName(), "Unspecified error", f"return code should be 0x80 {response['reasonCodes'][0].getName()}") oclient.disconnect() oclient.loop_stop() def test_unsubscribe(self): callback2.clear() bclient.connect(host="localhost", port=self._test_broker_port) bclient.loop_start() callback2.wait_connected() bclient.subscribe(topics[0], qos=2) callback2.wait_subscribed() bclient.subscribe(topics[1], qos=2) callback2.wait_subscribed() bclient.subscribe(topics[2], qos=2) callback2.wait_subscribed() time.sleep(1) # wait for any retained messages, hopefully # Unsubscribe from one topic bclient.unsubscribe(topics[0]) callback2.wait_unsubscribed() callback2.clear() # if there were any retained messages aclient.connect(host="localhost", port=self._test_broker_port) aclient.loop_start() callback.wait_connected() 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) bclient.disconnect() bclient.loop_stop() aclient.disconnect() aclient.loop_stop() self.assertEqual(len(callback2.messages), 2, callback2.messages) def new_client(self, clientid): callback = Callbacks() 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 def test_session_expiry(self): # no session expiry property == never expire connect_properties = Properties(PacketTypes.CONNECT) connect_properties.SessionExpiryInterval = 0 # expire immediately clientid = "session expiry" eclient, ecallback = self.new_client(clientid) eclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) connack = ecallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) eclient.subscribe(topics[0], qos=2) ecallback.wait_subscribed() eclient.disconnect() ecallback.wait_disconnected() eclient.loop_stop() fclient, fcallback = self.new_client(clientid) # session should immediately expire fclient.connect_async(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) connack = fcallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) fclient.disconnect() fcallback.wait_disconnected() connect_properties.SessionExpiryInterval = 5 eclient, ecallback = self.new_client(clientid) eclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) connack = ecallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) eclient.subscribe(topics[0], qos=2) ecallback.wait_subscribed() eclient.disconnect() ecallback.wait_disconnected() eclient.loop_stop() time.sleep(2) # session should still exist fclient, fcallback = self.new_client(clientid) fclient.connect(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) connack = fcallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], True) fclient.disconnect() fcallback.wait_disconnected() fclient.loop_stop() time.sleep(6) # session should not exist fclient, fcallback = self.new_client(clientid) fclient.connect(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) connack = fcallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) fclient.disconnect() fcallback.wait_disconnected() fclient.loop_stop() eclient, ecallback = self.new_client(clientid) connect_properties.SessionExpiryInterval = 1 connack = eclient.connect( host="localhost", port=self._test_broker_port, properties=connect_properties) connack = ecallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) eclient.subscribe(topics[0], qos=2) ecallback.wait_subscribed() disconnect_properties = Properties(PacketTypes.DISCONNECT) disconnect_properties.SessionExpiryInterval = 5 eclient.disconnect(properties=disconnect_properties) ecallback.wait_disconnected() eclient.loop_stop() time.sleep(3) # session should still exist as we changed the expiry interval on disconnect fclient, fcallback = self.new_client(clientid) fclient.connect(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) connack = fcallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], True) disconnect_properties.SessionExpiryInterval = 0 fclient.disconnect(properties=disconnect_properties) fcallback.wait_disconnected() fclient.loop_stop() # session should immediately expire fclient, fcallback = self.new_client(clientid) fclient.connect(host="localhost", port=self._test_broker_port, clean_start=False, properties=connect_properties) connack = fcallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) fclient.disconnect() fcallback.wait_disconnected() fclient.loop_stop() fclient.loop_stop() eclient.loop_stop() def test_user_properties(self): clientid = "user properties" uclient, ucallback = self.new_client(clientid) uclient.loop_start() uclient.connect(host="localhost", port=self._test_broker_port) ucallback.wait_connected() uclient.subscribe(topics[0], qos=2) ucallback.wait_subscribed() publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.UserProperty = ("a", "2") publish_properties.UserProperty = ("c", "3") uclient.publish(topics[0], b"", 0, retain=False, properties=publish_properties) uclient.publish(topics[0], b"", 1, retain=False, 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 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(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) userprops = ucallback.messages[1]["message"].properties.UserProperty self.assertTrue(userprops in [[("a", "2"), ("c", "3")], [ ("c", "3"), ("a", "2")]], userprops) userprops = ucallback.messages[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)] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) def test_payload_format(self): clientid = "payload format" pclient, pcallback = self.new_client(clientid) pclient.loop_start() pclient.connect_async(host="localhost", port=self._test_broker_port) pcallback.wait_connected() pclient.subscribe(topics[0], qos=2) pcallback.wait_subscribed() publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.PayloadFormatIndicator = 1 publish_properties.ContentType = "My name" info = pclient.publish( topics[0], b"qos 0", 0, retain=False, properties=publish_properties) info.wait_for_publish() info = pclient.publish( topics[0], b"qos 1", 1, retain=False, properties=publish_properties) info.wait_for_publish() info = pclient.publish( 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 pclient.disconnect() pcallback.wait_disconnected() pclient.loop_stop() self.assertEqual(len(pcallback.messages), 3, pcallback.messages) props = pcallback.messages[0]["message"].properties self.assertEqual(props.ContentType, "My name", props.ContentType) self.assertEqual(props.PayloadFormatIndicator, 1, props.PayloadFormatIndicator) props = pcallback.messages[1]["message"].properties self.assertEqual(props.ContentType, "My name", props.ContentType) self.assertEqual(props.PayloadFormatIndicator, 1, props.PayloadFormatIndicator) props = pcallback.messages[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)] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) def test_message_expiry(self): clientid = "message expiry" connect_properties = Properties(PacketTypes.CONNECT) connect_properties.SessionExpiryInterval = 99999 lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.loop_start() lbclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) lbcallback.wait_connected() lbclient.subscribe(topics[0], qos=2) 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(f"{clientid} a") laclient.loop_start() laclient.connect(host="localhost", port=self._test_broker_port) publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.MessageExpiryInterval = 1 laclient.publish(topics[0], b"qos 1 - expire", 1, retain=False, properties=publish_properties) laclient.publish(topics[0], b"qos 2 - expire", 2, retain=False, properties=publish_properties) publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.MessageExpiryInterval = 6 laclient.publish(topics[0], b"qos 1 - don't expire", 1, retain=False, properties=publish_properties) laclient.publish(topics[0], b"qos 2 - don't expire", 2, retain=False, properties=publish_properties) time.sleep(3) 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) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() lbclient.disconnect() lbcallback.wait_disconnected() lbclient.loop_stop() def test_subscribe_options(self): # noLocal clientid = 'subscribe options - noLocal' laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.loop_start() laclient.subscribe( topics[0], options=SubscribeOptions(qos=2, noLocal=True)) lacallback.wait_subscribed() lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() lbclient.subscribe( topics[0], options=SubscribeOptions(qos=2, noLocal=True)) 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) laclient.disconnect() lacallback.wait_disconnected() lbclient.disconnect() lbcallback.wait_disconnected() laclient.loop_stop() lbclient.loop_stop() # retainAsPublished clientid = 'subscribe options - retain as published' 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) self.assertEqual(len(lacallback.messages), 2, lacallback.messages) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() self.assertEqual(lacallback.messages[0]["message"].retain, False) self.assertEqual(lacallback.messages[1]["message"].retain, True) # retainHandling clientid = 'subscribe options - retain handling' 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) laclient.publish(topics[2], b"qos 1", 1, retain=True) laclient.publish(topics[3], b"qos 2", 2, retain=True) time.sleep(1) # retain handling 1 only gives us retained messages on a new 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)] 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) # remove that subscription properties = Properties(PacketTypes.UNSUBSCRIBE) properties.UserProperty = ("a", "2") properties.UserProperty = ("c", "3") laclient.unsubscribe(wildtopics[5], properties) 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)] 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) # remove that subscription properties = Properties(PacketTypes.UNSUBSCRIBE) properties.UserProperty = ("a", "2") properties.UserProperty = ("c", "3") laclient.unsubscribe(wildtopics[5], properties) lacallback.wait_unsubscribed() lacallback.clear() laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=2)) lacallback.wait_subscribed() self.assertEqual(len(lacallback.messages), 0) laclient.subscribe( wildtopics[5], options=SubscribeOptions(2, retainHandling=2)) lacallback.wait_subscribed() self.assertEqual(len(lacallback.messages), 0) # remove that subscription laclient.unsubscribe(wildtopics[5]) 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)] 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)] self.assertTrue(1 in qoss and 2 in qoss and 0 in qoss, qoss) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() cleanRetained(self._test_broker_port) def test_subscription_identifiers(self): clientid = 'subscription identifiers' laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) lacallback.wait_connected() laclient.loop_start() sub_properties = Properties(PacketTypes.SUBSCRIBE) sub_properties.SubscriptionIdentifier = 456789 laclient.subscribe(topics[0], qos=2, properties=sub_properties) lacallback.wait_subscribed() lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() sub_properties = Properties(PacketTypes.SUBSCRIBE) sub_properties.SubscriptionIdentifier = 2 lbclient.subscribe(topics[0], qos=2, properties=sub_properties) lbcallback.wait_subscribed() sub_properties.clear() sub_properties.SubscriptionIdentifier = 3 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) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() self.waitfor(lbcallback.messages, 1, 3) self.assertEqual(len(lbcallback.messages), 1, lbcallback.messages) expected_subsids = {2, 3} received_subsids = set( lbcallback.messages[0]["message"].properties.SubscriptionIdentifier) self.assertEqual(received_subsids, expected_subsids, received_subsids) lbclient.disconnect() lbcallback.wait_disconnected() lbclient.loop_stop() def test_request_response(self): clientid = 'request response' 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(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) lbcallback.wait_connected() lbclient.loop_start() laclient.subscribe( topics[0], options=SubscribeOptions(2, noLocal=True)) lacallback.wait_subscribed() lbclient.subscribe( topics[0], options=SubscribeOptions(2, noLocal=True)) lbcallback.wait_subscribed() publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.ResponseTopic = topics[0] publish_properties.CorrelationData = b"334" # client a is the requester laclient.publish(topics[0], b"request", 1, 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) lbclient.publish(lbcallback.messages[0]["message"].properties.ResponseTopic, b"response", 1, properties=lbcallback.messages[0]["message"].properties) # client a gets the response self.waitfor(lacallback.messages, 1, 3) self.assertEqual(len(lacallback.messages), 1, lacallback.messages) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() lbclient.disconnect() lbcallback.wait_disconnected() lbclient.loop_stop() def test_client_topic_alias(self): clientid = 'client topic alias' connect_properties = Properties(PacketTypes.CONNECT) connect_properties.TopicAliasMaximum = 0 # server topic aliases not allowed connect_properties.SessionExpiryInterval = 99999 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 if hasattr(connack["properties"], "TopicAliasMaximum"): clientTopicAliasMaximum = connack["properties"].TopicAliasMaximum if clientTopicAliasMaximum == 0: laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() return laclient.subscribe(topics[0], qos=2) lacallback.wait_subscribed() publish_properties = Properties(PacketTypes.PUBLISH) 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) laclient.publish("", b"topic alias 2", 1, properties=publish_properties) self.waitfor(lacallback.messages, 2, 3) self.assertEqual(len(lacallback.messages), 2, lacallback.messages) 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(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) publish_properties = Properties(PacketTypes.PUBLISH) publish_properties.TopicAlias = 1 laclient.publish("", b"topic alias 4", 1, properties=publish_properties) # should get back a disconnect with Topic alias invalid lacallback.wait_disconnected() laclient.loop_stop() def test_server_topic_alias(self): clientid = 'server topic alias' serverTopicAliasMaximum = 1 # server topic alias allowed connect_properties = Properties(PacketTypes.CONNECT) connect_properties.TopicAliasMaximum = serverTopicAliasMaximum laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) lacallback.wait_connected() laclient.loop_start() 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) 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 self.assertTrue(topicalias > 0) self.assertEqual(lacallback.messages[0]["message"].topic, topics[0]) self.assertEqual( lacallback.messages[1]["message"].properties.TopicAlias, topicalias) self.assertEqual(lacallback.messages[1]["message"].topic, "") self.assertEqual( lacallback.messages[2]["message"].properties.TopicAlias, topicalias) self.assertEqual(lacallback.messages[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(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) lacallback.wait_connected() laclient.loop_start() 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) 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) self.assertFalse(hasattr( lacallback.messages[1]["message"].properties, "TopicAlias"), lacallback.messages[1]["message"].properties) self.assertFalse(hasattr( lacallback.messages[2]["message"].properties, "TopicAlias"), lacallback.messages[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(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port, properties=connect_properties) lacallback.wait_connected() laclient.loop_start() 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) 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) self.assertFalse(hasattr( lacallback.messages[1]["message"].properties, "TopicAlias"), lacallback.messages[1]["message"].properties) self.assertFalse(hasattr( lacallback.messages[2]["message"].properties, "TopicAlias"), lacallback.messages[2]["message"].properties) def test_maximum_packet_size(self): clientid = 'maximum packet size' # 1. server max packet size laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) connack = lacallback.wait_connected() laclient.loop_start() serverMaximumPacketSize = 2**28-1 if hasattr(connack["properties"], "MaximumPacketSize"): serverMaximumPacketSize = connack["properties"].MaximumPacketSize if serverMaximumPacketSize < 65535: # publish bigger packet than server can accept payload = b"."*serverMaximumPacketSize 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: laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() # 1. client max packet size maximumPacketSize = 64 # max packet size we want to receive connect_properties = Properties(PacketTypes.CONNECT) connect_properties.MaximumPacketSize = maximumPacketSize 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() serverMaximumPacketSize = 2**28-1 if hasattr(connack["properties"], "MaximumPacketSize"): serverMaximumPacketSize = connack["properties"].MaximumPacketSize laclient.subscribe(topics[0], qos=2) response = lacallback.wait_subscribed() # 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) # 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) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() """ def test_server_keep_alive(self): clientid = 'server keep alive' laclient, lacallback = self.new_client(clientid+" a") laclient.connect(host="localhost", port=self._test_broker_port) connack = lacallback.wait_connected() laclient.loop_start() self.assertTrue(hasattr(connack["properties"], "ServerKeepAlive")) self.assertEqual(connack["properties"].ServerKeepAlive, 60) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() """ def test_will_delay(self): # the will message should be received earlier than the session expiry clientid = 'will delay' will_properties = Properties(PacketTypes.WILLMESSAGE) connect_properties = Properties(PacketTypes.CONNECT) # set the will delay and session expiry to the same value - # then both should occur at the same time will_properties.WillDelayInterval = 3 # in seconds connect_properties.SessionExpiryInterval = 5 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) connack = lacallback.wait_connected() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) laclient.loop_start() 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() # subscribe to will message topic lbclient.subscribe(topics[0], qos=2) lbcallback.wait_subscribed() # abort client a and wait for the will message laclient.loop_stop() laclient.socket().close() start = time.time() while lbcallback.messages == []: time.sleep(.1) duration = time.time() - start self.assertAlmostEqual(duration, 4, delta=1) self.assertEqual(lbcallback.messages[0]["message"].topic, topics[0]) self.assertEqual( lbcallback.messages[0]["message"].payload, b"test_will_delay will message") lbclient.disconnect() lbcallback.wait_disconnected() lbclient.loop_stop() def test_shared_subscriptions(self): clientid = 'shared subscriptions' shared_sub_topic = f"$share/sharename/{topic_prefix}x" shared_pub_topic = f"{topic_prefix}x" laclient, lacallback = self.new_client(f"{clientid} a") laclient.connect(host="localhost", port=self._test_broker_port) connack = lacallback.wait_connected() laclient.loop_start() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) laclient.subscribe( [(shared_sub_topic, SubscribeOptions(2)), (topics[0], SubscribeOptions(2))]) lacallback.wait_subscribed() lbclient, lbcallback = self.new_client(f"{clientid} b") lbclient.connect(host="localhost", port=self._test_broker_port) connack = lbcallback.wait_connected() lbclient.loop_start() self.assertEqual(connack["reasonCode"].getName(), "Success") self.assertEqual(connack["flags"]["session present"], False) lbclient.subscribe( [(shared_sub_topic, SubscribeOptions(2)), (topics[0], 2)]) lbcallback.wait_subscribed() lacallback.clear() lbcallback.clear() count = 1 for i in range(count): lbclient.publish(topics[0], f"message {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) lacallback.clear() lbcallback.clear() for i in range(count): lbclient.publish(shared_pub_topic, f"message {i}", 0) j = 0 while len(lacallback.messages) + len(lbcallback.messages) < count and j < 20: time.sleep(.1) j += 1 time.sleep(1) # Each message should only be received once self.assertEqual(len(lacallback.messages) + len(lbcallback.messages), count) laclient.disconnect() lacallback.wait_disconnected() laclient.loop_stop() lbclient.disconnect() lbcallback.wait_disconnected() lbclient.loop_stop() def setData(): global topics, wildtopics, nosubscribe_topics, topic_prefix topics = ("TopicA", "TopicA/B", "Topic/C", "TopicA/C", "/TopicA") wildtopics = ("TopicA/+", "+/C", "#", "/#", "/+", "+/+", "TopicA/#") nosubscribe_topics = ("test/nosubscribe",) topic_prefix = "paho.mqtt.client.mqttv5/" paho.mqtt.python-2.0.0/tests/test_reasoncodes.py000066400000000000000000000037161456167725100220510ustar00rootroot00000000000000import 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) paho.mqtt.python-2.0.0/tests/test_websocket_integration.py000066400000000000000000000210711456167725100241270ustar00rootroot00000000000000import base64 import hashlib import re import socketserver from collections import OrderedDict 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(): # "Normal" websocket response from server response_headers = OrderedDict([ ("Upgrade", "websocket"), ("Connection", "Upgrade"), ("Sec-WebSocket-Accept", "testwebsocketkey"), ("Sec-WebSocket-Protocol", "chat"), ]) return response_headers def get_websocket_response(response_headers): """ Takes headers and constructs HTTP response 'HTTP/1.1 101 Switching Protocols' is the headers for the response, as expected in client.py """ response = "\r\n".join([ "HTTP/1.1 101 Switching Protocols", "\r\n".join(f"{i}: {j}" for i, j in response_headers.items()), "\r\n", ]).encode("utf8") return response @pytest.mark.parametrize("proto_ver,proto_name", [ (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) 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" ) class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): # Respond with data passed in to serve() _self.request.sendall(b"200 OK") 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" @pytest.mark.parametrize("proto_ver,proto_name", [ (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) class TestBadWebsocketHeaders: """ Testing for basic functionality in checking for headers """ def _get_basic_handler(self, response_headers): """ Get a basic BaseRequestHandler which returns the information in self._response_headers """ response = get_websocket_response(response_headers) class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): self.data = _self.request.recv(1024).strip() print('Received', self.data.decode('utf8')) # Respond with data passed in to serve() _self.request.sendall(response) return WebsocketHandler def test_no_upgrade(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't respond with 'connection: upgrade' """ mqttc = client.Client( client.CallbackAPIVersion.VERSION1, "test_no_upgrade", protocol=proto_ver, transport="websockets" ) init_response_headers["Connection"] = "bad" response = self._get_basic_handler(init_response_headers) 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" def test_bad_secret_key(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Server doesn't give anything after connection: upgrade """ mqttc = client.Client( client.CallbackAPIVersion.VERSION1, "test_bad_secret_key", protocol=proto_ver, transport="websockets" ) response = self._get_basic_handler(init_response_headers) 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" @pytest.mark.parametrize("proto_ver,proto_name", [ (client.MQTTv31, "MQIsdp"), (client.MQTTv311, "MQTT"), ]) class TestValidHeaders: """ Testing for functionality in request/response headers """ def _get_callback_handler(self, response_headers, check_request=None): """ Get a basic BaseRequestHandler which returns the information in self._response_headers """ class WebsocketHandler(socketserver.BaseRequestHandler): def handle(_self): self.data = _self.request.recv(1024).strip() print('Received', self.data.decode('utf8')) decoded = self.data.decode("utf8") if check_request is not None: check_request(decoded) # Create server hash GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" key = re.search("sec-websocket-key: ([A-Za-z0-9+/=]*)", decoded, re.IGNORECASE).group(1) 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 # Respond with the correct hash response = get_websocket_response(response_headers) _self.request.sendall(response) return WebsocketHandler def test_successful_connection(self, proto_ver, proto_name, fake_websocket_broker, init_response_headers): """ Connect successfully, on correct path """ mqttc = client.Client( client.CallbackAPIVersion.VERSION1, "test_successful_connection", protocol=proto_ver, transport="websockets" ) response = self._get_callback_handler(init_response_headers) with fake_websocket_broker.serve(response): mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() @pytest.mark.parametrize("mqtt_path", [ "/mqtt" "/special", None, ]) def test_correct_path(self, proto_ver, proto_name, fake_websocket_broker, mqtt_path, init_response_headers): """ Make sure it can connect on user specified paths """ mqttc = client.Client( client.CallbackAPIVersion.VERSION1, "test_correct_path", protocol=proto_ver, transport="websockets" ) mqttc.ws_set_options( path=mqtt_path, ) def check_path_correct(decoded): # Make sure it connects to the right path if mqtt_path: assert re.search(f"GET {mqtt_path} HTTP/1.1", decoded, re.IGNORECASE) is not None response = self._get_callback_handler( init_response_headers, check_request=check_path_correct, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() @pytest.mark.parametrize("auth_headers", [ {"Authorization": "test123"}, {"Authorization": "test123", "auth2": "abcdef"}, # Won't be checked, but make sure it still works even if the user passes it None, ]) def test_correct_auth(self, proto_ver, proto_name, fake_websocket_broker, auth_headers, init_response_headers): """ Make sure it sends the right auth headers """ mqttc = client.Client( client.CallbackAPIVersion.VERSION1, "test_correct_path", protocol=proto_ver, transport="websockets" ) mqttc.ws_set_options( headers=auth_headers, ) def check_headers_used(decoded): # Make sure it connects to the right path if auth_headers: for k, v in auth_headers.items(): assert f"{k}: {v}" in decoded response = self._get_callback_handler( init_response_headers, check_request=check_headers_used, ) with fake_websocket_broker.serve(response): mqttc.connect("localhost", fake_websocket_broker.port, keepalive=10) mqttc.disconnect() paho.mqtt.python-2.0.0/tests/test_websockets.py000066400000000000000000000104401456167725100217050ustar00rootroot00000000000000import socket from unittest.mock import Mock import pytest from paho.mqtt.client import WebsocketConnectionError, _WebsocketWrapper class TestHeaders: """ Make sure headers are used correctly """ @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 = [ "HTTP/1.1 101 Switching Protocols", "Upgrade: websocket", "Connection: Upgrade", "Sec-WebSocket-Accept: badreturnvalue=", "Sec-WebSocket-Protocol: chat", "\r\n", ] def iter_response(): for i in "\r\n".join(response).encode("utf8"): yield i for i in b"\r\n": yield i it = iter_response() def fakerecv(*args): return bytes([next(it)]) mocksock = Mock( spec_set=socket.socket, recv=fakerecv, send=Mock(), ) # 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_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" # Only sends the header once assert mocksock.send.call_count == 1 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 paho.mqtt.python-2.0.0/tests/testsupport/000077500000000000000000000000001456167725100205405ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/testsupport/__init__.py000066400000000000000000000000001456167725100226370ustar00rootroot00000000000000paho.mqtt.python-2.0.0/tests/testsupport/broker.py000066400000000000000000000053341456167725100224030ustar00rootroot00000000000000import contextlib import socket import socketserver import threading import pytest 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(5) sock.bind(("localhost", 0)) self.port = sock.getsockname()[1] sock.listen(1) self._sock = sock self._conn = None def start(self): if self._sock is None: raise ValueError('Socket is not open') (conn, address) = self._sock.accept() conn.settimeout(5) self._conn = conn def finish(self): if self._conn is not None: self._conn.close() self._conn = None if self._sock is not None: self._sock.close() self._sock = None def receive_packet(self, num_bytes): if self._conn is None: raise ValueError('Connection is not open') packet_in = self._conn.recv(num_bytes) return packet_in def send_packet(self, packet_out): if self._conn is None: raise ValueError('Connection is not open') 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') paho_test.expect_packet(self._conn, name, packet) @pytest.fixture def fake_broker(): # print('Setup broker') broker = FakeBroker() yield broker # print('Teardown broker') broker.finish() class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer): pass class FakeWebsocketBroker(threading.Thread): def __init__(self): super().__init__() self.host = "localhost" self.port = -1 # Will be set by `serve()` self._server = None self._running = True self.handler_cls = False @contextlib.contextmanager def serve(self, 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") yield finally: if self._server: self._server.shutdown() self._server.server_close() def run(self): self._running = True self._server.serve_forever() @pytest.fixture def fake_websocket_broker(): broker = FakeWebsocketBroker() yield broker paho.mqtt.python-2.0.0/tox.ini000066400000000000000000000007651456167725100163050ustar00rootroot00000000000000[tox] envlist = py{37,38,39,310,311,312} [testenv] deps = 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 = # The "-" in front of command tells tox to ignore errors pre-commit run --all-files - black --check src - codespell mypy src safety check