Epmd

An Erlang Port Mapper Daemon (EPMD) in Python on asyncio. Erlang nodes register their name and port in it and look each other up through it, as they do with the epmd program that ships with Erlang/OTP. It follows the EPMD protocol of Erlang/OTP 25 to 29 and needs Python 3.11 or newer and nothing outside the standard library.

It is one of three projects that work together and can be used apart:

  • Pyrlang: an Erlang node in Python.

  • Term: Erlang terms and the External Term Format codec.

  • Epmd: this project.

Installing

pip install .

in a clone of https://github.com/Pyrlang/Epmd.

Running it

From the command line, configured like OTP’s epmd through the environment variables ERL_EPMD_PORT, ERL_EPMD_ADDRESS and ERL_EPMD_RELAXED_COMMAND_CHECK, or through options (python -m epmd --help):

python -m epmd --port 4370 --address 127.0.0.1

Erlang nodes find it through the same variable. Start them with -start_epmd false, or erl starts OTP’s own epmd if nothing answers on the port:

ERL_EPMD_PORT=4370 erl -start_epmd false -name a@127.0.0.1
ERL_EPMD_PORT=4370 erl -start_epmd false -name b@127.0.0.1
(a@127.0.0.1)1> net_adm:ping('b@127.0.0.1').
pong

Using it from Python

examples/run_epmd.py serves until it is interrupted:

"""Serve EPMD on port 4370 until interrupted.

Erlang nodes use it when they are started with the same port:
``ERL_EPMD_PORT=4370 erl -start_epmd false -name a@127.0.0.1``.
"""

import asyncio
import logging
import sys

from epmd import Epmd


def main(port: int = 4370) -> None:
    # A port other than 4369 leaves an EPMD of Erlang/OTP on this host alone.
    logging.basicConfig(level=logging.INFO)
    server = Epmd(port=port)
    asyncio.run(server.start_server())


if __name__ == "__main__":
    main(*(int(arg) for arg in sys.argv[1:2]))

Next to other asyncio code, await server.start() returns once the server listens and await server.stop() shuts it down. Epmd(port=0) picks a free port; server.port has it after start():

"""Run EPMD inside an asyncio program: start it, use it, stop it."""

import asyncio

from epmd import Epmd


async def main() -> int:
    server = Epmd(host="127.0.0.1", port=0)
    await server.start()
    try:
        # Erlang nodes started with ERL_EPMD_PORT=<port> register here.
        print("EPMD listens on port", server.port)
        return server.port
    finally:
        await server.stop()


if __name__ == "__main__":
    asyncio.run(main())

Where it listens:

  • Epmd() listens on every IPv4 and IPv6 address.

  • Epmd(host="127.0.0.1") listens on that address only.

  • Epmd(addresses=["10.0.0.5"]) listens there and on the loopback addresses, as OTP’s epmd does with ERL_EPMD_ADDRESS.

The other options (packet_timeout, max_connections, relaxed_command_check) are described with epmd.Epmd.

Registering, and the DUMP, KILL and STOP requests, are served only to clients on the same host, as in OTP.