Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether my server is UP and RUNNING using Python?

Tags:

python

http

url

How do I make a HTTP call to a particular URL in Python and then figure out that whether the server is UP and RUNNING or not.

As I recently started working with Python so not sure of any such libraries.

If I am opening the below URL -

http://dbx45.dc1.host.com:8082/console

then some page gets opened to me which means my server is UP and RUNNING. But if it is not UP and RUNNING, then it shows me like this on CHROME -

Oops! Google Chrome could not connect to http://dbx45.dc1.host.com:8082/console

And on FIREFOX -

Unable to connect
Firefox can't establish a connection to the server at dbx45.dc1.host.com:8082

So how do I figure out this thing from the Python that whether my server is UP and RUNNING or not?

UPDATE:-

This is what I have tried so far -

status = urllib2.urlopen("http://dbx45.dc1.host.com:8082/console").read()
print status

How do I simplify this to understand whether the server is UP and RUNNING or not?


1 Answers

an easy library to use is requests.

import requests
page = requests.get('http://dbx45.dc1.host.com:8082/console')

then you can check the status like this

page.status_code

200 means it's ok and something in the 400's or 500's means something went wrong.

like image 56
Back2Basics Avatar answered Mar 16 '26 16:03

Back2Basics



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!