mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Update examples to use dynamic host IP instead of hardcoded localhost
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
import secrets
|
||||
|
||||
import multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -9,9 +8,10 @@ from libp2p import (
|
||||
from libp2p.crypto.secp256k1 import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.security.insecure.transport import (
|
||||
PLAINTEXT_PROTOCOL_ID,
|
||||
InsecureTransport,
|
||||
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, Transport
|
||||
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 = InsecureTransport(
|
||||
insecure_transport = Transport(
|
||||
# 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
|
||||
@ -38,17 +38,19 @@ async def main():
|
||||
# Create a host with the key pair and insecure transport
|
||||
host = new_host(key_pair=key_pair, sec_opt=security_options)
|
||||
|
||||
# Configure the listening address
|
||||
# Configure the listening address using the new paradigm
|
||||
port = 8000
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}")
|
||||
listen_addrs = get_available_interfaces(port)
|
||||
optimal_addr = get_optimal_binding_address(port)
|
||||
|
||||
# Start the host
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
async with host.run(listen_addrs=listen_addrs):
|
||||
print(
|
||||
"libp2p has started with insecure transport "
|
||||
"(not recommended for production)"
|
||||
)
|
||||
print("libp2p is listening on:", host.get_addrs())
|
||||
print(f"Optimal address: {optimal_addr}")
|
||||
# Keep the host running
|
||||
await trio.sleep_forever()
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import secrets
|
||||
|
||||
import multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -13,6 +12,10 @@ from libp2p.security.noise.transport import (
|
||||
PROTOCOL_ID as NOISE_PROTOCOL_ID,
|
||||
Transport as NoiseTransport,
|
||||
)
|
||||
from libp2p.utils.address_validation import (
|
||||
get_available_interfaces,
|
||||
get_optimal_binding_address,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
@ -39,14 +42,16 @@ async def main():
|
||||
# Create a host with the key pair and Noise security
|
||||
host = new_host(key_pair=key_pair, sec_opt=security_options)
|
||||
|
||||
# Configure the listening address
|
||||
# Configure the listening address using the new paradigm
|
||||
port = 8000
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}")
|
||||
listen_addrs = get_available_interfaces(port)
|
||||
optimal_addr = get_optimal_binding_address(port)
|
||||
|
||||
# Start the host
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
async with host.run(listen_addrs=listen_addrs):
|
||||
print("libp2p has started with Noise encryption")
|
||||
print("libp2p is listening on:", host.get_addrs())
|
||||
print(f"Optimal address: {optimal_addr}")
|
||||
# Keep the host running
|
||||
await trio.sleep_forever()
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import secrets
|
||||
|
||||
import multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -13,6 +12,10 @@ from libp2p.security.secio.transport import (
|
||||
ID as SECIO_PROTOCOL_ID,
|
||||
Transport as SecioTransport,
|
||||
)
|
||||
from libp2p.utils.address_validation import (
|
||||
get_available_interfaces,
|
||||
get_optimal_binding_address,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
@ -32,14 +35,16 @@ async def main():
|
||||
# Create a host with the key pair and SECIO security
|
||||
host = new_host(key_pair=key_pair, sec_opt=security_options)
|
||||
|
||||
# Configure the listening address
|
||||
# Configure the listening address using the new paradigm
|
||||
port = 8000
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}")
|
||||
listen_addrs = get_available_interfaces(port)
|
||||
optimal_addr = get_optimal_binding_address(port)
|
||||
|
||||
# Start the host
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
async with host.run(listen_addrs=listen_addrs):
|
||||
print("libp2p has started with SECIO encryption")
|
||||
print("libp2p is listening on:", host.get_addrs())
|
||||
print(f"Optimal address: {optimal_addr}")
|
||||
# Keep the host running
|
||||
await trio.sleep_forever()
|
||||
|
||||
|
||||
@ -234,7 +234,7 @@ async def run_enhanced_demo(
|
||||
|
||||
def main() -> None:
|
||||
example_maddr = (
|
||||
"/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||
"/ip4/[HOST_IP]/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
|
||||
)
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import secrets
|
||||
|
||||
import multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -16,6 +15,10 @@ from libp2p.security.noise.transport import (
|
||||
PROTOCOL_ID as NOISE_PROTOCOL_ID,
|
||||
Transport as NoiseTransport,
|
||||
)
|
||||
from libp2p.utils.address_validation import (
|
||||
get_available_interfaces,
|
||||
get_optimal_binding_address,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
@ -42,14 +45,16 @@ async def main():
|
||||
# Create a host with the key pair, Noise security, and mplex multiplexer
|
||||
host = new_host(key_pair=key_pair, sec_opt=security_options)
|
||||
|
||||
# Configure the listening address
|
||||
# Configure the listening address using the new paradigm
|
||||
port = 8000
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/tcp/{port}")
|
||||
listen_addrs = get_available_interfaces(port)
|
||||
optimal_addr = get_optimal_binding_address(port)
|
||||
|
||||
# Start the host
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
async with host.run(listen_addrs=listen_addrs):
|
||||
print("libp2p has started")
|
||||
print("libp2p is listening on:", host.get_addrs())
|
||||
print(f"Optimal address: {optimal_addr}")
|
||||
|
||||
# Connect to bootstrap peers manually
|
||||
bootstrap_list = [
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
import secrets
|
||||
|
||||
import multiaddr
|
||||
import trio
|
||||
|
||||
from libp2p import (
|
||||
@ -9,6 +8,10 @@ from libp2p import (
|
||||
from libp2p.crypto.secp256k1 import (
|
||||
create_new_key_pair,
|
||||
)
|
||||
from libp2p.utils.address_validation import (
|
||||
get_available_interfaces,
|
||||
get_optimal_binding_address,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
@ -19,14 +22,24 @@ async def main():
|
||||
# Create a host with the key pair
|
||||
host = new_host(key_pair=key_pair, enable_quic=True)
|
||||
|
||||
# Configure the listening address
|
||||
# Configure the listening address using the new paradigm
|
||||
port = 8000
|
||||
listen_addr = multiaddr.Multiaddr(f"/ip4/127.0.0.1/udp/{port}/quic-v1")
|
||||
listen_addrs = get_available_interfaces(port, protocol="udp")
|
||||
# Convert TCP addresses to QUIC-v1 addresses
|
||||
quic_addrs = []
|
||||
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")
|
||||
optimal_quic_str = str(optimal_addr).replace("/tcp/", "/udp/") + "/quic-v1"
|
||||
|
||||
# Start the host
|
||||
async with host.run(listen_addrs=[listen_addr]):
|
||||
async with host.run(listen_addrs=quic_addrs):
|
||||
print("libp2p has started with QUIC transport")
|
||||
print("libp2p is listening on:", host.get_addrs())
|
||||
print(f"Optimal address: {optimal_quic_str}")
|
||||
# Keep the host running
|
||||
await trio.sleep_forever()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user