A somewhat simple-- yet useful-- SRV resolver in around 100 lines of Python code.
Project description
srvres
A somewhat simple-- yet useful-- SRV resolver in around 100 lines of Python code.
How to use
import srvres, socket
sock = None
for target in srvres.SRVResolver("xmpp-client","xmpp.example.com"):
sock = socket.socket()
sock.settimeout(5)
try:
sock.connect(target)
break
except socket.timeout: pass
if sock is None:
# the service is unavailable
else:
# the service is available and connected to
Explanation
The magic occurs in the srvres.SRVResolver
object. When you iterate over a SRVResolver object, it makes the DNS query for the SRV record for the specified service. If it gets an answer, it returns a srvres.SRVResolver.Iterator
object, which handles priority and weighting. If it doesn't get an answer, it falls back to the given domain and a known port (if a port is known). If a port is not known, the default response will include a port of 0. This can be changed by supplying a port
argument to the srvres.SRVResolver
constructor like so:
# "unknownprotocol" listens on port 49151
for target in srvres.SRVResolver("unknownprotocol","example.com",port=49151):
# now the unknown response will have port 49151 as opposed to 0
What ports are classified as "known"?
When you first import the library, it will download the IANA registry of assigned service names and port numbers, which it ports into a format it can more easily use. It also downloads a separate registry, maintained by me, that includes a handful of other useful ports that aren't defined by the IANA registry.
Basically, if you have an IANA registration for your service name, and it includes a port and transport, you'll be able to use your service name with no issues. If you don't, and your service is a widely used one, ask me and I'll probably add it to my list.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file srvres-1.0.0.tar.gz
.
File metadata
- Download URL: srvres-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d9a502e50bd6ddea8279b8058efc9d8c4ae674bc98fd4d12f62bc9086ee341f |
|
MD5 | ae39bf8510d4304b096f48d81f94de6e |
|
BLAKE2b-256 | 2a0ec44a9bfc8fa6815902485c34b7f4eece3de2a9cb0ad35381cc503072061a |
File details
Details for the file srvres-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: srvres-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.4.2 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.48.2 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1762f3177c247737147fced99c60cdf8ab380c4e308aeb38f759e7a91a88bde |
|
MD5 | 93ca949137e766d18dd1ee37eac592ca |
|
BLAKE2b-256 | 7295b8dd9c6acea5bead28eb79df9c0123d2b6321012af2e9660516ff88b675d |