Skip to content

Instantly share code, notes, and snippets.

@tuksik
Forked from miohtama/gist:4210349
Last active September 13, 2015 06:35
Show Gist options
  • Save tuksik/9418cd4beef05bf928bd to your computer and use it in GitHub Desktop.
Save tuksik/9418cd4beef05bf928bd to your computer and use it in GitHub Desktop.
Testing if Apache is running in certain local ports in Python
"""
Check if we have a local apache running and listening to certain ports.
"""
# http://pypi.python.org/pypi/psutil/
import psutil
def is_apache_running_in_ports(process_name="apache", ports=(80, 443)):
"""
Check if a local Apache instance is running and listening to certain ports.
:param ports: List of ports to listen (all must be on)
"""
ports_to_go = list(ports)
for proc in psutil.process_iter():
if proc.name != process_name:
# Not target
continue
for con in proc.get_connections():
# Tuple ip, port
port = con.local_address[1]
if port in ports_to_go:
ports_to_go.remove(port)
return len(ports_to_go) == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment