5656# Configure logging
5757logger = logging .getLogger ("libp2p.identity.identify-push-example" )
5858
59- # Default port configuration
60- DEFAULT_PORT = 8888
61-
6259
6360def custom_identify_push_handler_for (host ):
6461 """
@@ -241,25 +238,16 @@ def main() -> None:
241238 """Parse arguments and start the appropriate mode."""
242239 description = """
243240 This program demonstrates the libp2p identify/push protocol.
244- Without arguments, it runs as a listener on port 8888 .
245- With -d parameter, it runs as a dialer on port 8889 .
241+ Without arguments, it runs as a listener on random port .
242+ With -d parameter, it runs as a dialer on random port .
246243 """
247244
248245 example = (
249- f"/ip4/127.0.0.1/tcp/{ DEFAULT_PORT } /p2p/"
250- "QmQn4SwGkDZkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
246+ "/ip4/127.0.0.1/tcp/8000/p2p/QmQn4SwGkDZKkUEpBRBvTmheQycxAHJUNmVEnjA2v1qe8Q"
251247 )
252248
253249 parser = argparse .ArgumentParser (description = description )
254- parser .add_argument (
255- "-p" ,
256- "--port" ,
257- type = int ,
258- help = (
259- f"port to listen on (default: { DEFAULT_PORT } for listener, "
260- f"{ DEFAULT_PORT + 1 } for dialer)"
261- ),
262- )
250+ parser .add_argument ("-p" , "--port" , default = 0 , type = int , help = "source port number" )
263251 parser .add_argument (
264252 "-d" ,
265253 "--destination" ,
@@ -270,13 +258,11 @@ def main() -> None:
270258
271259 try :
272260 if args .destination :
273- # Run in dialer mode with default port DEFAULT_PORT + 1 if not specified
274- port = args .port if args .port is not None else DEFAULT_PORT + 1
275- trio .run (run_dialer , port , args .destination )
261+ # Run in dialer mode with random available port if not specified
262+ trio .run (run_dialer , args .port , args .destination )
276263 else :
277- # Run in listener mode with default port DEFAULT_PORT if not specified
278- port = args .port if args .port is not None else DEFAULT_PORT
279- trio .run (run_listener , port )
264+ # Run in listener mode with random available port if not specified
265+ trio .run (run_listener , args .port )
280266 except KeyboardInterrupt :
281267 print ("\n Interrupted by user" )
282268 logger .info ("Interrupted by user" )
0 commit comments