我想使用 UDT library在 Python 中,所以我需要一个包装器。我找到了这个:pyudt ,但我不知道如何使用它来点对点发送文件。谁能指出我正确的方向?
最佳答案
经过这么多次我发现了这个问题及其解决方案:
安装pyudt-0.1a的步骤是:
安装: libboost-python1.46-dev 或等价物 (例如,在 linux-ubuntu12.04 中它在 reps 中。)
将 udt.h(来自:http://sourceforge.net/projects/udt/)安装到系统目录中,
或
(将udt.h文件放在与pyudt-0.1a文件相同的路径下,然后将“pyudt.cpp”的一行改成:
#include <udt.h>
到:
#include "udt.h"
).
- 将“setup.py”中的 boost_python 库版本更新为您要使用的版本 使用,
例如:
... libraries=['udt', 'boost_python-py27'])
- 更改“pyudt.cpp”中的以下行:
您必须更正错误,从:
int r = UDT::send(_sock, data.c_str(), data.length(), 0);
到:
int r = UDT::send(_sock, data.c_str(), data.length()+1, 0);
因为还必须发送表示字符串结尾的字符“\0”,否则垃圾将附加到您的字符串中。
您可以选择:
_sock = UDT::socket(AF_INET, SOCK_DGRAM, 0); --» default
或:
_sock = UDT::socket(AF_INET, SOCK_STREAM, 0); --» optional
- 最后,运行,
在对应的文件夹中:
python2.7 ./setup.py build
sudo python2.7 ./setup.py install
或者,(如果您没有为所有用户安装它的管理员权限,并且只想为您尝试一下:
python2.7 ./setup.py build
python2.7 ./setup.py install --prefix=~/pyudt-0.1a/installation_dir/ #in this case, pyudt would only work if called from that directory
)
那么,一个简单客户端的代码可以是:
import pyudt
socket = pyudt.pyudt_socket()
socket.connect(("127.0.0.1", 7000))
socket.send("hello_world!")
它可以工作,它可以与我的 cpp 服务器通信!
注意:如果您需要更多帮助,您可以在 python 的控制台中编写:
import pyudt
dir(pyudt.pyudt_socket) # to list the available functions
help(pyudt) # to get more help
附言。使用此安装教程创建的文件是: /usr/local/lib/python2.7/dist-packages/pyudt.so, and/usr/local/lib/python2.7/dist-packages/pyudt-0.1a.egg-info
关于UDT C++ 库的 Python 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7222852/