0

I have made a program that tells me if I am connected to the internet or not. Now I want it to ping www.google.com and show me the ping time in ms. I don't want to use any 3rd party software or download anything.

Edit: My code is:

def is_connected():
  try:
    # see if we can resolve the host name -- tells us if there is
    # a DNS listening
    host = socket.gethostbyname(REMOTE_SERVER)
    # connect to the host -- tells us if the host is actually
    # reachable
    s = socket.create_connection((host, 80), 2)
    return True
  except:
      pass
  return False

The above code just tells me if I am connected to the internet. What I want is a simple way to show the ping of a website. This is not a duplicate as it doesn't answer my question.

Ian Stegner
  • 23
  • 1
  • 1
  • 6
  • 1
    Where is your code? And what exactly do you want from us? :) – Patrick Trentin Jun 15 '16 at 06:44
  • So now your question is "I have code which does something different entirely, please show me how to do the thing I want to do" which is off-topic as well (but the duplicate answers that, too; you just haven't read the answers properly). – tripleee Jun 15 '16 at 09:04
  • It exists some Python libs for doing ping. Even if you don't want to use them, they are on Git repos. Did you have a look at their source code? – David Guyon Jun 15 '16 at 09:30
  • @tripleee my question has always been "I have code which does something different entirely, please show me how to do the thing I want to do". Also, the 'duplicate' doesn't give me what I want. – Ian Stegner Jun 16 '16 at 00:41
  • Then you should clarify what you do want, and how the code you posted attempts to approach that goal. The duplicate has a pure-python implementation of `ping` which creates ICMP packets and times them; if that is not what you want, what *do* you want? Note that we get a lot of questions where the poster wants to "ping" a "web site" but the terminology doesn't match up. A web site has an IP address and you an ping that, but doing so does not indicate whether the site is actually operational. – tripleee Jun 16 '16 at 04:24
  • Possible duplicate of [Ping a site in Python?](https://stackoverflow.com/questions/316866/ping-a-site-in-python) – Stevoisiak Jun 24 '19 at 18:46

1 Answers1

2

A ping is not the same thing as a HTTP connection! The first is a low level ICMP packet that is used to test connectiviy and find round-trip time mainly on a local network. It is generaly not used on broad internet, because for security reasons it is often blocked by firewall and external routers.

If you want to know the time necessary to establish the connexion to a server, do what you would in real world: look at your watch, do the job, look again at your watch to see elapsed time. In Python it gives

#import time
...
def connect_time():
  try:
    # see if we can resolve the host name -- tells us if there is
    # a DNS listening
    host = socket.gethostbyname(REMOTE_SERVER)
    # connect to the host -- tells us if the host is actually
    # reachable
    before = time.clock()      # from Python 3.3 and above use before = time.perf_counter()
    s = socket.create_connection((host, 80), 2)
    after = time.clock()      # from Python 3.3 and above use after = time.perf_counter()
    return after - before
  except:
    return -1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252