Getting server status response from HTTP request

Discussion in 'Python' started by Mike C, Feb 13, 2004.

  1. Mike C

    Mike C Guest

    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
     
    Mike C, Feb 13, 2004
    #1
    1. Advertising

  2. Mike C

    pythonhda Guest

    On Fri, 13 Feb 2004 10:36:44 -0800
    Mike C <> 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.
     
    pythonhda, Feb 16, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page