bneradt commented on code in PR #10103:
URL: https://github.com/apache/trafficserver/pull/10103#discussion_r1276588456
##########
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:
Note that this is a handy feature, btw. You don't have to "declare" a
variable in python outside of code blocks to access them later. Thus this is
fine:
```python3
╰─➤ cat e.py
#!/usr/bin/env python3
i = 3
if i > 0:
connections = "one two three"
else:
connections = 4
print(connections)
```
You don't have to explicitly declare `connections` earlier with an otherwise
useless assignment:
```python3
╰─➤ cat e.py
#!/usr/bin/env python3
i = 3
connections = None # <---- Not needed.
if i > 0:
connections = "one two three"
else:
connections = 4
print(connections)
```
##########
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:
Note that this is a handy feature, btw. You don't have to "declare" a
variable in python outside of code blocks to access them later. Thus this is
fine:
```python3
╰─➤ cat e.py
#!/usr/bin/env python3
i = 3
if i > 0:
connections = "one two three"
else:
connections = 4
print(connections)
```
You don't have to explicitly declare `connections` earlier with an otherwise
useless assignment:
```python3
╰─➤ cat e.py
#!/usr/bin/env python3
i = 3
connections = None # <---- Not needed.
if i > 0:
connections = "one two three"
else:
connections = 4
print(connections)
```
--
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]