简体   繁体   English

Python3.6如何安装libtorrent?

[英]Python3.6 how to install libtorrent?

Does libtorrent support python3 now? libtorrent 现在支持 python3 吗? if supported how to install it .如果支持如何安装它。

I want to code a DHT Crawler with python3 , i don't know why my code alaways got Connection reset by peer error , so i want to use libtorrent , if there are another lib , i'm happy to use it .我想用 python3 编写一个 DHT Crawler,我不知道为什么我的代码总是被对等错误重置连接,所以我想使用 libtorrent,如果有另一个 lib,我很乐意使用它。 My biggest problem is , conversing the infohash to torrent file .我最大的问题是,将 infohash 转换为 Torrent 文件。 Could it be a code problem?会不会是代码问题?

class Crawler(Maga):
async def handler(self, infohash, addr):
    fetchMetadata(infohash, addr)


def fetchMetadata(infohash, addr, timeout=5):
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.settimeout(timeout)
if tcpServer.connect_ex(addr) == 0:
    try:
        # handshake
        send_handshake(tcpServer, infohash)
        packet = tcpServer.recv(4096)
        # handshake error
        if not check_handshake(packet, infohash):
            return
        # ext handshake
        send_ext_handshake(tcpServer)
        packet = tcpServer.recv(4096)

        # get ut_metadata and metadata_size
        ut_metadata, metadata_size = get_ut_metadata(packet), get_metadata_size(packet)

        # request each piece of metadata
        metadata = []
        for piece in range(int(math.ceil(metadata_size / (16.0 * 1024)))):
            request_metadata(tcpServer, ut_metadata, piece)
            packet = recvall(tcpServer, timeout)  # the_socket.recv(1024*17) #
            metadata.append(packet[packet.index("ee") + 2:])

        metadata = "".join(metadata)

        logging.info(bencoder.bdecode(metadata))
    except ConnectionResetError as e:
        logging.error(e)
    except Exception as e:
        logging.error(e)
    finally:
        tcpServer.close()

Yes, libtorrent (is supposed to) support python 3.是的,libtorrent(应该)支持 python 3。

The basic way to build and install the python binding is by running the setup.py .构建和安装 python 绑定的基本方法是运行setup.py That requires that all dependencies are properly installed where distutils expects them though.这要求所有依赖项都正确安装在 distutils 期望它们的位置。 If this works it's probably the simplest way so it's worth a shot.如果这有效,它可能是最简单的方法,所以值得一试。 I believe you'll have to invoke this python script using python3, if that's what you're building for.我相信你必须使用 python3 调用这个 python 脚本,如果这是你正在构建的。

To build using the main build system (and install the resulting module manually) you can follow the steps in the .travis file (for unix) or the appeyor file for windows.要使用主构建系统构建(并手动安装生成的模块),您可以按照.travis文件(对于 unix)或appeyor文件(对于 Windows)中的 步骤进行操作。 In your case you want to specify a 3.x version of python, but the essence of it is:在你的情况下,你想指定一个 3.x 版本的 python,但它的本质是:

brew install boost-python
echo "using python : 2.7 ;" >> ~/user-config.jam
cd bindings/python
bjam -j3 stage_module libtorrent-link=static boost-link=static

To test:去测试:

python test.py

Note, in the actual build step you may want to link shared against boost if you already have that installed, and shared against libtorrent if you have that installed, or will install it too.请注意,在实际的构建步骤中,如果您已经安装了 boost,您可能希望与 boost 链接共享,如果您安装了它,则与 libtorrent 共享,或者也将安装它。

On windows:在窗户上:

add the following to $HOMEPATH\\user-config.jam :将以下内容添加到$HOMEPATH\\user-config.jam

using msvc : 14.0 ;
using gcc : : : <cxxflags>-std=c++11 ;
using python : 3.5 : c:\\Python35-x64 : c:\\Python35-x64\\include : c:\\Python35-x64\\libs ;

Then run:然后运行:

b2.exe --hash openssl-version=pre1.1 link=shared libtorrent-link=shared stage_module stage_dependencies

To test:去测试:

c:\Python35-x64\python.exe test.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM