mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Fix import statements and improve error handling in examples
This commit is contained in:
@ -19,6 +19,7 @@ try:
|
||||
except ImportError:
|
||||
# Fallbacks if utilities are missing - use minimal network discovery
|
||||
import socket
|
||||
|
||||
def get_available_interfaces(port: int, protocol: str = "tcp"):
|
||||
# Try to get local network interfaces, fallback to loopback
|
||||
addrs = []
|
||||
@ -28,7 +29,7 @@ except ImportError:
|
||||
local_ip = socket.gethostbyname(hostname)
|
||||
if local_ip != "127.0.0.1":
|
||||
addrs.append(Multiaddr(f"/ip4/{local_ip}/{protocol}/{port}"))
|
||||
except exception:
|
||||
except Exception:
|
||||
pass
|
||||
# Always include loopback as fallback
|
||||
addrs.append(Multiaddr(f"/ip4/127.0.0.1/{protocol}/{port}"))
|
||||
|
||||
@ -8,7 +8,7 @@ from libp2p import (
|
||||
from libp2p.crypto.secp256k1 import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, Transport
|
||||
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport
|
||||
from libp2p.utils.address_validation import (
|
||||
get_available_interfaces,
|
||||
get_optimal_binding_address,
|
||||
@ -21,7 +21,7 @@ async def main():
|
||||
key_pair = create_new_key_pair(secret)
|
||||
|
||||
# Create an insecure transport (not recommended for production)
|
||||
insecure_transport = Transport(
|
||||
insecure_transport = InsecureTransport(
|
||||
# local_key_pair: The key pair used for libp2p identity
|
||||
local_key_pair=key_pair,
|
||||
# secure_bytes_provider: Optional function to generate secure random bytes
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import secrets
|
||||
|
||||
from multiaddr import Multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -66,7 +67,7 @@ async def main():
|
||||
|
||||
for addr in bootstrap_list:
|
||||
try:
|
||||
peer_info = info_from_p2p_addr(multiaddr.Multiaddr(addr))
|
||||
peer_info = info_from_p2p_addr(Multiaddr(addr))
|
||||
await host.connect(peer_info)
|
||||
print(f"Connected to {peer_info.peer_id.to_string()}")
|
||||
except Exception as e:
|
||||
|
||||
@ -30,6 +30,7 @@ async def main():
|
||||
for addr in listen_addrs:
|
||||
addr_str = str(addr).replace("/tcp/", "/udp/") + "/quic-v1"
|
||||
from multiaddr import Multiaddr
|
||||
|
||||
quic_addrs.append(Multiaddr(addr_str))
|
||||
|
||||
optimal_addr = get_optimal_binding_address(port, protocol="udp")
|
||||
|
||||
@ -93,7 +93,8 @@ async def run_server(port: int, seed: int | None = None) -> None:
|
||||
# Use optimal address for the client command
|
||||
optimal_tcp = get_optimal_binding_address(port)
|
||||
optimal_quic_str = str(optimal_tcp).replace("/tcp/", "/udp/") + "/quic"
|
||||
optimal_quic_with_peer = f"{optimal_quic_str}/p2p/{host.get_id().to_string()}"
|
||||
peer_id = host.get_id().to_string()
|
||||
optimal_quic_with_peer = f"{optimal_quic_str}/p2p/{peer_id}"
|
||||
print(
|
||||
f"\nRun this from the same folder in another console:\n\n"
|
||||
f"python3 ./examples/echo/echo_quic.py -d {optimal_quic_with_peer}\n"
|
||||
|
||||
Reference in New Issue
Block a user