user130894: This link looks very practical and "hands in the dirty". But in the example of Python code for node discovery and connections, it often gives warning about the fact it is of "educational purposes" and not real world.
Could you please explain what the caveats of the implementation showed in the link are, and how to correct them in the real world:
Example of such limitations highlighted:

Is the limitation about the fact that "except Exception" excepts nearly everything and that is not a good practice?
Would putting a security (like don't call recursively indefinitely) be a solution?

user-1: Using recursion is not a bad practice IMO, they can be used to do lot of interesting things although I am not the best person to comment on such things.
There are few interesting comments in this thread about recursion vs while loop: https://www.reddit.com/r/csharp/comments/rnoy7h/difference_between_tail_recursion_and_while_loop/
As far as the code is concerned, I could not find a line that would stop recursion at some point so adding an if statement with some condition to terminate could make it usable in production.

user38618: It's bad practice to use recursion when a while loop would suffice. Recursive code is harder to read, harder to debug, harder for the compiler to optimize and it may cause a stack overflow.
Besides, connections shouldn't be made from the main thread.
EDIT: Maybe it's because there's no limit of trials after which the code should assume that the user went offline. Otherwise the stack will grow and if the stack doesn't overflow the iterator may go out-of-bounds.

130894: <p><a href="http://sebastianappelt.com/understanding-blockchain-peer-discovery-and-establishing-a-connection-with-python/" rel="nofollow noreferrer">This link</a> looks very practical and &quot;hands in the dirty&quot;. But in the example of Python code for node discovery and connections, it often gives warning about the fact it is of &quot;educational purposes&quot; and not real world.</p>
<p>Could you please explain what the caveats of the implementation showed in the link are, and how to correct them in the real world:</p>
<p>Example of such limitations highlighted:</p>
<p><a href="https://i.stack.imgur.com/Q32u7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Q32u7.png" alt="Example of code" /></a></p>
<p>Is the limitation about the fact that &quot;except Exception&quot; excepts nearly everything and that is not a good practice?
Would putting a security (like don't call recursively indefinitely) be a solution?</p>
 -1: <p>Using recursion is not a bad practice IMO, they can be used to do lot of interesting things although I am not the best person to comment on such things.</p>
<p>There are few interesting comments in this thread about recursion vs while loop: <a href="https://www.reddit.com/r/csharp/comments/rnoy7h/difference_between_tail_recursion_and_while_loop/" rel="nofollow noreferrer">https://www.reddit.com/r/csharp/comments/rnoy7h/difference_between_tail_recursion_and_while_loop/</a></p>
<p>As far as the code is concerned, I could not find a line that would stop recursion at some point so adding an <code>if</code> statement with some condition to terminate could make it usable in production.</p>
 38618: <p>It's bad practice to use recursion when a while loop would suffice. Recursive code is harder to read, harder to debug, harder for the compiler to optimize and it may cause a stack overflow.</p>
<p>Besides, connections shouldn't be made from the main thread.</p>
<p>EDIT: Maybe it's because there's no limit of trials after which the code should assume that the user went offline. Otherwise the stack will grow and if the stack doesn't overflow the iterator may go out-of-bounds.</p>
