Code Monkey home page Code Monkey logo

erlport's Introduction

Erlang port protocol for Python

Project URL: http://erlport.org

The erlport Python library implements Erlang external term format and Erlang port protocol for easier integration of Python and Erlang.

The library exports the following classes and functions:

  • Port(packet=1, use_stdio=False, compressed=False) - class implementing port which connects with the corresponding Erlang port. See open_port/2 for description of packet and use_stdio arguments. compressed is the zlib compression level or True for the default of 6.
  • Protocol() - class which simplifies creation of request-response protocols.
  • Atom(str) - class which represents an Erlang atom.
  • String(unicode | list) - class representing an Erlang string. Must be used as a wrapper if Unicode string expected instead of a list.
  • BitBinary(str) - class representing an Erlang bitstring whose length in bits is not a multiple of 8.
  • decode(str) - function to convert binary data into a term.
  • encode(term, compressed=False) - function to convert a term into the external format. compressed is the zlib compression level or True for the default of 6.
  • IncompleteData - exception raised by decode() in case of incomplete input data.

Prerequisites:

  • Erlang >= R11B-4
  • Python >= 2.4

To install the library use easy_install from setuptools package like this:

$ easy_install erlport

See examples directory in the source distribution for additional examples.

For simple request-response protocol use Port and Protocol on the Python side like this:

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))

Note that if you are sending a string from Erlang (which is actually just a list of integers and can't be recognized as a string at the protocol level), then in Python you must wrap the received data in the erlport.String class, as shown in the example above.

On the Erlang side function hello() can be called like this:

-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.

Test it 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"}
  • It seems Erlang's open_port function ignores nouse_stdio option on Windows. So the Port class must be instantiated with use_stdio=True argument.
  • Python must be ran with -u option to open stdin/stdout in binary mode.

Please report bugs, offer suggestions or feedback at:

erlport's People

Contributors

hdima avatar etrepum avatar japerk avatar pib avatar

Watchers

Boris Timokhin avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.