Light Speed Socket Connections

T Angell tangell at kicker.com
Thu Jul 12 07:32:16 CEST 2001


I wrote some code to test how long it takes to make socket connections
and ran it against several hosts around the world, here are some
sample times:

socket time: 0.0047459602356
socket time: 0.00469899177551
socket time: 0.00404000282288
socket time: 0.00537407398224

Someone in another newsgroup suggested that was probably just the time
it took the o/s to fire off a SYN packet and never waited for the
result. So, I thought I'd post the Python code here to see what I'm
doing wrong. The o/s is Red Hat 7.0. Note: the results I posted above
are all from result = "OK" responses, so I know it's getting past the
second connect()

def trySocket( host, port, timeout ):

  result = ""

  s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
  t1 = time.time()

  try:
    s.setblocking( 0 )
    s.connect( ( host, port ) )
  except:
    r, w, e = select.select( [], [s], [], timeout )

    if w:
      try:
        s.connect( ( host, port ) )
        result = "OK"
      except socket.error, reason:
        result = reason[1]

    else:
      result = "Timeout"

  s.close()
  delay = time.time() - t1

  return result, delay

Thanks.



More information about the Python-list mailing list