1. Home
  2. Computing & Technology
  3. Python

Checking URLs and IP Addresses With Python's socket Module
Checking the DNS Entry of URLs or IP Addresses

From Al Lukaszewski, for About.com

A recent report from Georgia Tech and co-sponsored by Google indicates that the number of poisoned DNS servers is on the rise. As you may know, the Internet runs on a system of domain name servers (DNS) that convert the website URL (e.g., 'www.about.com') into a series of four numbers that is unique within the larger, world-wide network (e.g., '207.126.123.20'). Whenever you type in the first, your computer first reconciles the URL to the IP address.

The problem for Python programmers -- or any web app programmer -- is that one tends to use the URL over the IP address. This has the benefit of greater permanency in a world where hosting services can be changed overnight. However, using the IP address is not a silver bullet. Modern DNS services use domain name pointers which then point to the domain name entry, the URL. So one never has direct access to the IP address, unless no domain name is registered. To make matters worse, rogue DNS servers seem to serve bad addresses only some of the time.

At this point, it seems the best one can do is to keep a cache of IP addresses that will serve as a touchstone; one would then perform a DNS check, and flag the URL if the DNS check returns a different IP address. To do this, one uses the socket module.

First, import the socket module:

>>> import socket
For retrieving the IP address of a host, use socket.gethostbyname_ex(). To retrieve the domain name pointer of an IP address, use socket.gethostbyaddr(). For example:
>>> socket.gethostbyname_ex('cnn.com')
('cnn.com', [], ['64.236.16.20', '64.236.16.52', '64.236.24.12', '64.236.29.120')
>>>
>>> socket.gethostbyaddr('64.236.16.20')
('www2.cnn.com', [], ['64.236.16.20'])
>>>
>>> socket.gethostbyname_ex('about.com')
('about.com', [], ['207.241.148.80'])
>>> socket.gethostbyaddr('207.241.148.80')
('gcny.about.com', [], ['207.241.148.80'])
>>>
For the other methods of the socket module, see the three part quick reference guide, parts one, two, and three.

More Python Quick Tips
Explore Python
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Python Library
  5. Python Modules - socket Module - DNS Checking - Checking URLs and IP Addresses

©2009 About.com, a part of The New York Times Company.

All rights reserved.