I'm trying to write a function that checks if a web server is up.
I run a wget command then check the result for 200 OK
in the string. I'm using the in
operator but it keeps failing and I'm not sure what I'm doing wrong.
I have posted my code below.
Thanks for reading.
import subprocess
web_address = "reddit.com"
wget = subprocess.Popen(["wget", "--spider", web_address], stdout=subprocess.PIPE)
output, err = wget.communicate()
response = output.decode('utf-8')
if '200 OK' in response:
print("its up")
else:
print("its down")
Edit: subprocess.getoutput()
solved my problem.