Generic-user-small Stephan Nies 1 post

Hello,
I am just learning Erlang. But since much of my
codebase is in Python i wanted to interface Erlang and
Python. So i ported the ports example in Chapter 12.1 to Python.

I thought that might be of interest for some other
people using Erlang and Python. So here is the link:

“http://www.physik.uni-dortmund.de/~nies/example1.py”

You can just use it as a replacement for the C-code,
just make sure to rename it to example1, make it executeable
and place it in the same dir as example1.erl

Oh and while searching the net for python <-> erlang examples
I found this neat project using twisted to interface with erlang: https://launchpad.net/twotp

Cheers,
Stephan

 
Generic-user-small Alain O'Dea 18 posts

Very nice Stephan. Thank you for posting that. I was looking for a cleaner way to do communication between Erlang and Python. I could not figure out how to make Python implementation for the ports example.

Until now I have been using TCP/IP line-based communication. I could not figure out how to get the packet size thing to work. If you are interested I have included my old approach below.

TCP line-based integration with Erlang:

-module (echo_server).
-export([start/0]).

start() ->
    {ok, Listen} = gen_tcp:listen(4321, [list, {packet, line},
                                         {reuseaddr, true},
                                         {active, true}]),
    spawn(fun() -> par_connect(Listen) end).

par_connect(Listen) ->
    {ok, Socket} = gen_tcp:accept(Listen),
    spawn(fun() -> par_connect(Listen) end),
    io:format("Client connected~n"),
    echo(Socket). 

echo(Socket) ->
    receive
        {tcp, Socket, Line} ->
            gen_tcp:send(Socket, Line),
            echo(Socket);
        {tcp_closed, Socket} ->
            io:format("Client disconnected~n")
    end.

Telnet can then be used to test the interface: telnet 127.0.0.1 4321. Once you are satisfied it is very easy to build a TCP client in Python. I used this to implement a very basic Erlang code completion server for use with TextMate.

2 posts, 2 voices