bneradt commented on code in PR #10103:
URL: https://github.com/apache/trafficserver/pull/10103#discussion_r1276584068
##########
tests/gold_tests/autest-site/ports.py:
##########
@@ -115,6 +127,30 @@ def _get_available_port(queue):
return port
+def _get_listening_ports() -> set[int]:
+ """Use psutil to get the set of ports that are currently listening.
+
+ :return: The set of ports that are currently listening.
+ """
+ ports: set[int] = set()
+ try:
+ connections = psutil.net_connections(kind='all')
+ for conn in connections:
+ if conn.status == psutil.CONN_LISTEN:
+ ports.add(conn.laddr.port)
+ except psutil.AccessDenied:
+ # Mac OS X doesn't allow net_connections() to be called without root.
+ for proc in psutil.process_iter(['pid', 'name']):
+ try:
+ connections = proc.connections(kind='all')
+ except (psutil.AccessDenied, psutil.NoSuchProcess):
+ continue
+ for conn in connections:
+ if conn.status == psutil.CONN_LISTEN:
+ ports.add(conn.laddr.port)
Review Comment:
Thank you for checking. Yes, Python is different that way with scoping.
Variables are scoped by function, not by inner control blocks within a
function. Thus `connections` in that try are accessible (in scope) outside of
the try in the loop.
https://stackoverflow.com/a/2829642/629530
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]