Python DTLS

raw JSON →
1.3.0 verified Fri May 01 auth: no python

Implementation of Datagram Transport Layer Security (DTLS) for Python. Provides DTLS client and server support over UDP. Current version 1.3.0 has no declared Python version requirement. Release cadence is low, typically one release every few years.

pip install python3-dtls
error ModuleNotFoundError: No module named 'dtls'
cause Library not installed or installed as python3-dtls but import name mismatch.
fix
Run 'pip install python3-dtls' and import as 'from dtls import ...'.
error AttributeError: module 'dtls' has no attribute 'DTLSClientSocket'
cause Using an older version of the library where class names were different.
fix
Upgrade to the latest version (pip install --upgrade python3-dtls) and use DTLSClientSocket, DTLSServerSocket.
error ImportError: No module named Crypto
cause Missing pycrypto dependency.
fix
Install pycrypto: 'pip install pycrypto' or 'pip install pycryptodome' as alternative.
gotcha The library does not automatically handle DTLS retransmission timers. You must implement application-level timeouts or use threading to avoid blocking on recv() indefinitely if packets are lost.
fix Wrap socket operations with timeout or use select module.
deprecated Version 1.3.0 lacks support for DTLS 1.2; only DTLS 1.0 is supported. Upcoming versions may drop DTLS 1.0 completely.
fix Be prepared to migrate to a library supporting DTLS 1.2 or later (e.g., pyOpenSSL with DTLS).
gotcha The library depends on pycrypto and pyopenssl. On some platforms, pycrypto may require a C compiler or additional system libraries (e.g., libffi-dev, libssl-dev).
fix Install system dependencies: on Ubuntu/Debian run 'sudo apt-get install build-essential libssl-dev libffi-dev' before pip install.
breaking In version 1.3.0, the DTLSClientSocket and DTLSServerSocket classes were renamed from older names. Code using old class names will break.
fix Use DTLSClientSocket and DTLSServerSocket instead of any previous names.

Simple DTLS client and server example using DTLSClientSocket and DTLSServerSocket.

import os
from dtls import DTLSClientSocket, DTLSServerSocket

# Client example
client_socket = DTLSClientSocket()
client_socket.connect(('localhost', 4433))
client_socket.send(b'Hello over DTLS')
data = client_socket.recv(1024)
print(data)
client_socket.close()

# Server example
server_socket = DTLSServerSocket()
server_socket.bind(('', 4433))
client, addr = server_socket.accept()
data = client.recv(1024)
print('Received:', data)
client.send(b'Hello from server')
client.close()