Python3.6 how to install libtorrent?

2019-07-27 02:26发布

问题:

Does libtorrent support python3 now? 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 . My biggest problem is , conversing the infohash to torrent file . 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()

回答1:

Yes, libtorrent (is supposed to) support python 3.

The basic way to build and install the python binding is by running the setup.py. That requires that all dependencies are properly installed where distutils expects them though. 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.

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. In your case you want to specify a 3.x version of python, but the essence of it is:

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.

On windows:

add the following to $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