Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting server status response from HTTP request

Reply
Thread Tools

Getting server status response from HTTP request

 
 
Mike C
Guest
Posts: n/a
 
      02-13-2004
First of all, I am a complete newbie to Python, but have been impressed
with how easy the language has been to pick up.

I am trying to write a script that checks to see if the web /
application server is up. I have the following code that works:

--
import httplib

try:
httpobj = httplib.HTTPConnection('www.domain.com', 80)
httpobj.connect();
httpobj.putrequest('GET', '/foo/ping.cfm')
httpobj.putheader('Accept', '*/*')
httpobj.endheaders()

reply = httpobj.getresponse()
httpobj.close();

if reply.status != 200:
print "There may be a problem with the server. Response Status :",
reply.status
else:
print "server is fine"

except Exception:
print "An exception occured"
--

I was curious if anyone had suggestions on a better way to do this. I
tried to use urllin2, but could not figure out how to retrieve the
status code.

mike c
 
Reply With Quote
 
 
 
 
pythonhda
Guest
Posts: n/a
 
      02-16-2004
On Fri, 13 Feb 2004 10:36:44 -0800
Mike C <(E-Mail Removed)> wrote:

> First of all, I am a complete newbie to Python, but have been impressed
> with how easy the language has been to pick up.
>
> I am trying to write a script that checks to see if the web /
> application server is up. I have the following code that works:
>
> --
> import httplib
>
> try:
> httpobj = httplib.HTTPConnection('www.domain.com', 80)
> httpobj.connect();
> httpobj.putrequest('GET', '/foo/ping.cfm')
> httpobj.putheader('Accept', '*/*')
> httpobj.endheaders()
>
> reply = httpobj.getresponse()
> httpobj.close();
>
> if reply.status != 200:
> print "There may be a problem with the server. Response Status :",
> reply.status
> else:
> print "server is fine"
>
> except Exception:
> print "An exception occured"
> --
>
> I was curious if anyone had suggestions on a better way to do this. I
> tried to use urllin2, but could not figure out how to retrieve the
> status code.
>
> mike c


Suggestion...don't use a "GET" request, use a "HEAD" request. That way the server will only return the headers and not the complete file (the status code will be the same) so you can check any file you want using only a minimal amount of bandwidth.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I change the response URL to http://server/page.XLS instead of http://server/page.ASPX ? guillermojco@gmail.com ASP .Net 3 04-26-2007 04:49 AM
Why getInputStream in a http servlet request isn't getting the datasent by browser HTTP POST action? James Java 3 11-25-2005 11:17 PM
web server gets "The Request Failed with HTTP Status 400: Bad Requ Eskimo ASP .Net Web Services 0 04-18-2005 02:36 PM
Getting "The request failed with HTTP status 403: Error" ?? cmay ASP .Net Web Services 3 01-10-2005 03:34 PM
how can i simulate my pc as http server by writing java code to send the http response by socketstream to some pc in the internet?! mike Java 5 09-21-2004 02:04 PM



Advertisments