ErlPort - Erlang port protocol for Python

Fork me on GitHub

Installation

Easiest method to install ErlPort is to use easy_install tool from setuptools package:

$ easy_install erlport

Main prerequisites:

Also source code of the library can be obtained from GitHub or PyPi.

About

ErlPort is a Python library which implements Erlang external term format and Erlang port protocol for easier integration of Erlang and Python.

Check out the following topics:

Feedback

Please report bugs, offer suggestions or feedback at:

Example

Erlang module (hello.erl):

-module(hello).
-export([hello/1]).


hello(Name) ->
    % Spawn hello.py script and open communication channels
    Port = open_port({spawn, "python -u hello.py"},
        [{packet, 1}, binary, use_stdio]),
    % Convert tuple {hello, Name} to external term format
    ReqData = term_to_binary({hello, Name}),
    % Send binary data to hello.py script
    port_command(Port, ReqData),
    % Wait for reply from hello.py script
    receive
        {Port, {data, RespData}} ->
            % Convert binary data to term
            {ok, binary_to_term(RespData)}
    after
        5000 ->
            {error, timeout}
    end.

Python module (hello.py):

from erlport import Port, Protocol, String


# Inherit custom protocol from erlport.Protocol
class HelloProtocol(Protocol):

    # Function handle_NAME will be called for incoming tuple {NAME, ...}
    def handle_hello(self, name):
        # String wrapper forces name to be a string instead of a list
        return "Hello, %s" % String(name)


if __name__ == "__main__":
    proto = HelloProtocol()
    # Run protocol with port open on STDIO
    proto.run(Port(use_stdio=True))

Test the modules above in the Erlang shell:

1> % Compile hello.erl module
1> c(hello).
{ok,hello}
2> % Call hello:hello() -> HelloProtocol.handle_hello()
2> hello:hello("Bob").
{ok,"Hello, Bob"}
Updated on 2010-07-28