Browse files

Updated example code

  • Loading branch information...
hdima committed Jul 27, 2010
1 parent 77ffaa6 commit 275f07d37bb755a1be9f7c4c104de596db5c3495
Showing with 24 additions and 5 deletions.
  1. +24 −5 README.rst
@@ -68,13 +68,19 @@ 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
Note that if you are sending a string from Erlang (which is actually just a
@@ -87,20 +93,33 @@ On the Erlang side function ``hello()`` can be called like this::
hello(Name) ->
Port = open_port({spawn, "python -u"}, [{packet, 1}, binary]),
port_command(Port, term_to_binary({hello, Name})),
% Spawn script and open communication channels
Port = open_port({spawn, "python -u"},
[{packet, 1}, binary, use_stdio]),
% Convert tuple {hello, Name} to external term format
ReqData = term_to_binary({hello, Name}),
% Send binary data to script
port_command(Port, ReqData),
% Wait for reply from script
{Port, {data, Data}} ->
{Port, {data, RespData}} ->
% Convert binary data to term
{ok, binary_to_term(RespData)}
5000 ->
{error, timeout}
Test it in the Erlang shell::
1> % Compile hello.erl module
1> c(hello).
2> % Call hello:hello() -> HelloProtocol.handle_hello()
2> hello:hello("Bob").
"Hello, Bob"
{ok,"Hello, Bob"}
Notes for Windows users

0 comments on commit 275f07d

Please sign in to comment.