1

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()
J.Joe
  • 81
  • 1
  • 7

1 Answers1

2

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
Arvid
  • 10,915
  • 1
  • 32
  • 40
  • I just run the test.py , i got the error : Traceback (most recent call last): File ".\test.py", line 3, in import libtorrent as lt ImportError: DLL load failed: %1 不是有效的 Win32 应用程序。 – J.Joe Sep 25 '17 at 06:43
  • Python Version is Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32 – J.Joe Sep 25 '17 at 07:08
  • that suggests that either the build failed or it built for the wrong version of python. It could also mean the module couldn't find one of its dependencies. You could try libtorrent-link=static and boost-link=static. Which version of libtorrent are you building? The appveyor script builds for python 3 on windows. – Arvid Sep 25 '17 at 13:58
  • the libtorrent version is 1.14 , the python version is 3.6.2 x64 . i've never build the libtorret before , my first step is run the test.py . i got this error . i installed libtorrent before by msi way to install in windows , but got the same error , so i uninstall . – J.Joe Sep 26 '17 at 07:41
  • **step 1: py setup.py build ** **step 2: py setup.py install ** **step 3: py test.py** failed , but i find it in pip list – J.Joe Sep 26 '17 at 07:45
  • **step 1: py setup.py build ** **step 2: py setup.py install ** **step 3: py test.py** failed , but i find it in pip list – J.Joe Sep 26 '17 at 07:57