jes516
jes516

Reputation: 552

Python socket.gethostbyname function return an ip address but I am unable to ping it

I wrote a small script to get the ip address for a list of hostnames. However when I try to ping the ip address I get request time outs. Why can this happen?

import csv
import socket

path = 'Clients.csv'

with open(path) as fopen:
    rows = csv.reader(fopen)
    for row in rows:
        try:
            resp = socket.gethostbyname(row[0])
            print(row[0], resp)
        except:
            continue

Upvotes: 1

Views: 591

Answers (1)

Stephane Martin
Stephane Martin

Reputation: 1642

The answer is in the question: the DNS server you are requesting knows about the name, so it gives you the matching IP.

It does not mean that the machine that does the ping can actually find a network path to ping the destination. For example, there could be a firewall between the originated server and the pinged server, networks between source and destination are not necessarily routed together, etc.

Timeouts are probably the sign of a blocking firewall.

Upvotes: 4

Related Questions