Created
December 4, 2012 23:44
-
-
Save miohtama/4210349 to your computer and use it in GitHub Desktop.
Testing if Apache is running in certain local ports in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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