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

Roy Smith roy at panix.com
Mon Jan 2 10:47:09 EST 2012


In article 
<6b787f23-5813-4831-a349-02883f564da4 at q7g2000yqn.googlegroups.com>,
 ÉΪÉ«É»όλαος Κούρας <nikos.kouras at gmail.com> wrote:

> On 2 ÉßÉΩÉΥ, 16:00, Heiko Wundram <modeln... at modelnine.org> wrote:
> > 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.
> 
> I see
> 
> > 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.
> 
> Can tou show me how to write this please?

try:
   host =  socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0]
except socket.herror:
   host = "<unknown host>"



More information about the Python-list mailing list