Connection tester

Jeff McNeil jeff at jmcneil.net
Wed Jun 10 12:01:02 EDT 2009


On Jun 10, 10:26 am, Sparky <Samnspa... at gmail.com> wrote:
> 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.



More information about the Python-list mailing list