Connection tester

S

Sparky

Hey! I am developing a small application that tests multiple websites
and compares their "response time". Some of these sites do not respond
to a ping and, for the measurement to be standardized, all sites must
have the same action preformed upon them. Another problem is that not
all of the sites have the same page size and I am not interested in
how long it takes to load a page but instead just how long it takes
for the website to respond. Finally, I am looking to keep this script
platform independent, if at all possible.

Here is the code:

try:
# Get the starting time
origTime = time.time()

# Create the socket connection and then close
s = socket.socket(AF_INET, SOCK_STREAM)
s.connect((targetIP, port))
s.send("GET / HTTP/1.0\r\n\r\n")
result = s.recv(1024)
s.shutdown(SHUT_RDWR)

except:
result = ""

# Check for problems and report back the time
if result == "":
return Result((time.time() - origTime) * 1000, True)
else:
return Result((time.time() - origTime) * 1000, False)

Result is just an object that holds the time it took for the method to
finish and if there were any errors. What I am worried about is that
the socket is potentially closed before the website can finish sending
in all the data. Does anyone have any suggestions or is the script
fine as it is?
 
N

Nigel Rantor

Sparky said:
Hey! I am developing a small application that tests multiple websites
and compares their "response time". Some of these sites do not respond
to a ping and, for the measurement to be standardized, all sites must
have the same action preformed upon them. Another problem is that not
all of the sites have the same page size and I am not interested in
how long it takes to load a page but instead just how long it takes
for the website to respond. Finally, I am looking to keep this script
platform independent, if at all possible.

Yes, lots of people block ICMP so you can't use it to reliably tell
whether a machine is there or not.

At least three possible solutions.

1) Perform a HEAD request against the document root. This is likely to
be a static page and making it a HEAD request will make most responses
take similar times.

2) Perform an OPTIONS request as specified in the RFC below for the *
resource. This doesn't always work.

3) Perform a request you believe will fail so that you are provided with
a 4XX error code, the only time this should take any appreciable time is
when someone has cute server-generated error pages.

HTTP/1.1 RFC - http://www.ietf.org/rfc/rfc2616.txt

n
 
J

Jeff McNeil

Hey! I am developing a small application that tests multiple websites
and compares their "response time". Some of these sites do not respond
to a ping and, for the measurement to be standardized, all sites must
have the same action preformed upon them. Another problem is that not
all of the sites have the same page size and I am not interested in
how long it takes to load a page but instead just how long it takes
for the website to respond. Finally, I am looking to keep this script
platform independent, if at all possible.

Here is the code:

    try:
        # Get the starting time
        origTime = time.time()

        # Create the socket connection and then close
        s = socket.socket(AF_INET, SOCK_STREAM)
        s.connect((targetIP, port))
        s.send("GET / HTTP/1.0\r\n\r\n")
        result = s.recv(1024)
        s.shutdown(SHUT_RDWR)

    except:
        result = ""

    # Check for problems and report back the time
    if result == "":
        return Result((time.time() - origTime) * 1000, True)
    else:
        return Result((time.time() - origTime) * 1000, False)

Result is just an object that holds the time it took for the method to
finish and if there were any errors. What I am worried about is that
the socket is potentially closed before the website can finish sending
in all the data. Does anyone have any suggestions or is the script
fine as it is?

ICMP and application-level response times are two different animals.
Are you interested in simply whether or not a server is up and
responding, or do you care about the actual response time and
performance of the web site you're checking? I did something like this
recently and there were a few different metrics we wound up using.
Connect time, first-byte, page download, DNS resolution, and so on.

Since you don't care about any of that, just use a HEAD request. It
will return the response headers, but as per specification it will not
return a message body. Take a look at "http://www.w3.org/Protocols/
rfc2616/rfc2616-sec9.html" for a full primer on the different verbs.

A somewhat simplistic alternative would be to connect to port 80 on
the destination server and drop the connection once it has been made.
This will tell you how long it took to establish a TCP session and
that something is indeed listening on the destination port. That's
slightly more information than you would get from an ICMP reply.
 
S

Sparky

ICMP and application-level response times are two different animals.
Are you interested in simply whether or not a server is up and
responding, or do you care about the actual response time and
performance of the web site you're checking? I did something like this
recently and there were a few different metrics we wound up using.
Connect time, first-byte, page download, DNS resolution, and so on.

Since you don't care about any of that, just use a HEAD request. It
will return the response headers, but as per specification it will not
return a message body.  Take a look at "http://www.w3.org/Protocols/
rfc2616/rfc2616-sec9.html" for a full primer on the different verbs.

A somewhat simplistic alternative would be to connect to port 80 on
the destination server and drop the connection once it has been made.
This will tell you how long it took to establish a TCP session and
that something is indeed listening on the destination port. That's
slightly more information than you would get from an ICMP reply.

Thank you all for your responses. I will play with everything but the
HEAD request seems to be what I was looking for.

Sam
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top