aaron bill
aaron bill

Reputation: 55

HTTping in python

So my python program needs to be able to ping a website to see if it is up or not, i have made the ping program and then found out that this site only works with httping, after doing some googling about it i found almost nothing on the subject. Has anybody httping in python before? if so how did you do it?, Thanks for the help.
Here is my code for the normal ping (which works but not for the site i need it to work for)

 import os
 hostname = "sitename"
 response = os.system("ping -c 1 " + hostname)
 if response == 0:
    print "good"
 else:
    print "bad"

Upvotes: 1

Views: 964

Answers (2)

user1907906
user1907906

Reputation:

Use requests to make a HTTP HEAD request.

import requests

response = requests.head("http://www.example.com/")
if response.status_code == 200:
    print(response.elapsed)
else:
    print("did not return 200 OK")

Output:

0:00:00.238418

Upvotes: 8

Anthony Mykhailenko
Anthony Mykhailenko

Reputation: 21

To check accessibility of HTTP server you can make a GET request to the URL, which is as easy as:

import urllib2
response = urllib2.urlopen("http://example.com/foo/bar")
print response.getcode()

Upvotes: 2

Related Questions