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