erlang 与 python 的接口

发布于 2024-10-18 10:37:19 字数 663 浏览 1 评论 0 原文

我一直在扩展阿姆斯特朗经典界面示例的 python 版本。当我通信字节时一切正常。但是,我想传达长整数和浮点数。甚至(哦,不)弦。这是我的代码: http://pastebin.com/epxgDmvu http://pastebin.com/rD7CWRkz

首先,我只知道如何发送字节。 erlang 可以向它的接口发送其他东西吗?或者我是否必须将浮点数转换为字节列表,将其发送到python,然后将其组装回python中的浮点数?

而且,还有其他方法:如果我用“f”格式打包,erlang 会将其识别为字节列表。有没有办法说服 erlang 将这些字节作为一个浮点数?如果没有,我在哪里可以找到如何将该 erlang 列表转换为 erlang float?

如果 erlang 只能通信字节,你建议我如何发送整数列表?如果我将整数转换为字节列表,那么我无法在一条消息中发送它们,因为接收者不知道一个整数在哪里结束而另一个整数在哪里开始,对吗?然后我应该一一发送整数吗?

是的,我正在研究 ErlPort 和 py-interface 以及其他一些,但我想从基础开始。

问候, 迪杰斯特拉

I've been expanding on python version of Armstrong's classical example of interfaces. Everything works fine while I communicate bytes. But, I'd like to communicate long integers and floats. Mabye even (oh, no) strings. Here is my code:
http://pastebin.com/epxgDmvu
http://pastebin.com/rD7CWRkz

First of all, all that I know how to send are bytes. Can erlang send anything else to it's inteface? Or do I have to convert float to list of bytes, send it to python and then assemble it back to float in python?

And, the ohter way around: if I pack with 'f' format, erlang recognises that as a list of bytes. Is there a way to persuade erlang to take those bytes as one single float? If not, where can I find out how to convert that erlang list to erlang float?

In case erlang can communicate only bytes, how do you propose that I send a list of integers? If I convert integers to lists of bytes, then I can't send them in one message since reciever won't know where one integer ends and other begins, right? Should I then send integers one by one?

Yes, I am looking into ErlPort and py-interface and some other, but I'd like to start from fundamentals.

Regards,
dijxtra

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌化 2024-10-25 10:37:19

在低级别(从编程的角度来看),您总是通过不同的外部接口(管道、网络、文件...)仅发送/接收一堆字节,与您正在使用的编程语言无关。例如,当您使用管道时(您调用了 open_port/2) 您以字节流的形式发送/接收数据。

要通过流发送任何有用的信息,您需要将其划分为通常命名为消息的块。消息格式定义如何从字节流中提取消息。例如,使用 Type-Length-Value (TLV) 编码 您可以发送以下值不同的长度标有类型。

ErlPort 中,它的工作原理如下:

  • 当您调用 open_port/2 您必须添加选项 {packet, N} 以便通过此端口发送的所有二进制消息都位于前面按其长度,以 N 个字节发送(长度值编码)。
  • 在将术语发送到端口之前,您必须使用 term_to_binary/1 使用 Erlang 外部术语格式
  • 在 Python 端,当您实例化 erlproto.Port 时类,您必须传递与传递给 open_port/2(默认情况下packet=1)。
  • Port.read方法首先读取的长度来自流的消息,然后是消息正文。
  • erlterms.decode 解码的消息正文功能。
  • 当您使用 Port.write 发送回复时方法首先通过 Erlang 外部术语格式 https://github.com/hdima/erlport/blob/master/src/erlport/erlterms.py#L237" rel="noreferrer">erlterms.encode 函数。
  • 然后将编码块加上其长度发送回 Erlang。
  • 在 Erlang 端, binary_to_term/1 用于解码数据。

At a low (from programming point of view) level you always send/receive only a bunch of bytes through different external interfaces (pipes, network, files...) independently from a programming language you're using. For example when you work with pipes (which you got calling open_port/2) you send/receive data as a stream of bytes.

To send any useful information through a stream you need to divide it to chunks which usually named messages. Message format define how messages can be extracted from a stream of bytes. For example using Type-Length-Value (TLV) encoding you can send values of different length marked with a type.

In ErlPort it all works as following:

  • When you call open_port/2 you must add option {packet, N} so all binary messages sent through this port are preceded by their length, sent in N bytes (Length-Value encoding).
  • Before to send a term to the port you must use term_to_binary/1 function which encode Erlang term to a bunch of bytes using Erlang external term format.
  • On Python side when you instantiate erlproto.Port class you must pass the same packet value as you pass to open_port/2 (packet=1 by default).
  • Port.read method first read the length of the message from the stream and then the message body.
  • The message body decoded by the erlterms.decode function.
  • When you send a reply using Port.write method it's first encoded to Erlang external term format by erlterms.encode function.
  • Then the encoded chunk prepended by its length sent back to Erlang.
  • On Erlang side binary_to_term/1 is used to decode the data.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文