Skip to main content

Matrix server resolver library for Python

Project description

ResolveMatrix

ResolveMatrix is a Python library providing the utilities required to resolve both client and server to server API endpoints. It fully conforms to the Matrix specification outlined in the client-to-server specification and server-to-server specification.

Installing

You can install the latest release from PyPI:

pip install --pre resolvematrix

Or get the latest via Git:

pip install git+https://codeberg.org/timedout/resolvematrix.git

Usage

Command line

You can use the command mxresolve to resolve a server name from the command line:

$ mxresolve example.com
https://matrix.example.com

Client to Server

You can resolve a server name as follows:

import resolvematrix

resolver = resolvematrix.ClientResolver()
result = resolver.resolve("example.com")
print(result)  # "https://matrix.example.com"

# You can also use the server_from_user_id helper utility to extract the server name from a user ID:
result = resolver.resolve(resolvematrix.server_from_user_id("@alice:example.com"))
print(result)  # "https://matrix.example.com"

# And even manually pass a server URL!
result = resolver.resolve("https://matrix.example.com")
print(result)  # "https://matrix.example.com"

Server to Server

You can resolve a server name as follows:

import resolvematrix

resolver = resolvematrix.ServerResolver()
result = resolver.resolve("matrix.org")
print(repr(result))  # "ServerDestination(hostname='matrix-federation.matrix.org:443', host_header='matrix-federation.matrix.org:443', sni='matrix-federation.matrix.org')"

# You then need to do a little bit of wrangling to get an actual connection.
import httpx

response = resolver.client.get(
    f"{result.base_url}/_matrix/federation/v1/version",
    headers={"Host": result.host_header},
    extensions={"sni_hostname": result.sni} if result.sni else {},
).raise_for_status()

# Other libraries may have different ways of specifying SNI and custom Host headers.
# See also: https://stackoverflow.com/a/77743443

Contact

Talk to me in my matrix room: #ontopic:timedout.uk.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

resolvematrix-1.0.0b1.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

resolvematrix-1.0.0b1-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file resolvematrix-1.0.0b1.tar.gz.

File metadata

  • Download URL: resolvematrix-1.0.0b1.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for resolvematrix-1.0.0b1.tar.gz
Algorithm Hash digest
SHA256 823d6be3bfd1352d66ec1316598a303a537c0432b23c9642f2720e9b18169668
MD5 e8c6ee23049e5c01d13970513871f12e
BLAKE2b-256 536eafd6c0944d42265a6469e1912acba472016907e08e0411fc50df3eb2e6b5

See more details on using hashes here.

File details

Details for the file resolvematrix-1.0.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for resolvematrix-1.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 6cbe3168f786441f41546d67f81229c0172b1c7c4d8ec62db2fbeb781e6791a5
MD5 c701ceab80e235f7bc35bc5c3770a86c
BLAKE2b-256 f0253a06d40f081575ff1a1c274027b1bf98c3912bf930f928cf60373c7b3e54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page