解决对等握手期间的套接字错误 10060

问题描述 投票:0回答:1

我正在使用 python 构建 BitTorrent 终端原型,在使用 udp 跟踪器给出的 IP 进行对等握手时,出现套接字错误 10060

下面是我的peer_Handshake函数的代码: defpeer_handshake(peer_ip,peer_port,info_hash,peer_id): peer_connect = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 编码的对等体 ID = 对等体 ID.encode('utf-8')

try:
    peer_connect.connect((peer_ip, peer_port))
    peer_connect.settimeout(5)
    print("CONNECTION WITH THE PEER IS SUCCESSFUL.... ")
    print("STARTING A HANDSHAKE .....")
    
    pstr = b"BitTorrent protocol"
    reserved = b'\x00' * 8
    
    # Corrected handshake format
    handshake_format = ">B19s8x20s20s"
    handshake = struct.pack(handshake_format, len(pstr), pstr, reserved, info_hash, encoded_peer_id)
    peer_connect.sendall(handshake)
    print("Handshake successful with the peer.")
    
    print("Response from the peer is :-")
    response = peer_connect.recv(68)
    print(response)
    
    print("Extracting useful information from the response :-")
    peerid_len = len(peer_id)
    extracted_peer_id = response[-peerid_len:]
    print("Extracted Peer ID :-", extracted_peer_id.decode('utf-8'))
    
    extracted_info_hash = response[28:48]
    print("Extracted Info Hash :-", extracted_info_hash)
    print("\nCHECKING IF THE INFO HASH IS VALID:-")
    
    if extracted_info_hash == info_hash:
        print("Valid")
        print("Peer is Interested")
        return "Interested"
    else:
        print("Invalid")
        return "Not Interested"
except socket.error as e:
    print(f"Socket error connecting to {peer_ip}:{peer_port}: {e}")
except Exception as e:
    print(f"Error connecting to {peer_ip}:{peer_port}: {e}")
finally:
    peer_connect.close()

输出如下: enter image description here

我尝试运行代码并期待握手成功

python sockets bittorrent
1个回答
0
投票

套接字错误10060表示“连接超时”。 您正在

connect()
之后设置超时值。我想你想先设置它。

© www.soinside.com 2019 - 2024. All rights reserved.