socket.gethostbyaddr( os.environ['REMOTE_ADDR'] error

Heiko Wundram modelnine at modelnine.org
Mon Jan 2 09:00:26 EST 2012


Am 02.01.2012 14:25, schrieb Νικόλαος Κούρας:
> On 23 Δεκ 2011, 19:14, Νικόλαος Κούρας<nikos.kou... at gmail.com>  wrote:
>> I dont know why this line host =
>> socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0] fails sometimes
>> and some other times works ok retrieving the hostnames correctly.
>
> Please i need some help. My webpage doesn't work due to this error...

The error "herror: (1, ...)" says it all: the DNS-name (i.e., the 
<something>.in-addr.arpa name) you're trying to resolve is unknown. Not 
all hosts (or rather, IPs) on the internet have reverse lookups: try the 
IP 81.14.209.35 from which I'm posting, and dig/nslookup will tell you 
that it has no reverse resolution, which would result in gethostbyaddr() 
throwing an herror-instance.

Basically: make the reverse lookup conditional by wrapping it in a 
try:/except herror: and assigning an appropriate default for host in 
case reverse lookup fails.

-- 
--- Heiko.



More information about the Python-list mailing list