<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import time <EOL> import gevent <EOL> from gevent import socket <EOL> from slimta import logging <EOL> __all__ = [ '<STR_LIT>' ] <EOL> class PtrLookup ( gevent . Greenlet ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ip ) : <EOL> super ( PtrLookup , self ) . __init__ ( ) <EOL> self . ip = ip or '<STR_LIT>' <EOL> self . start_time = None <EOL> @ classmethod <EOL> def from_getpeername ( cls , sock ) : <EOL> """<STR_LIT>""" <EOL> ip , port = sock . getpeername ( ) <EOL> return cls ( ip ) , port <EOL> @ classmethod <EOL> def from_getsockname ( cls , sock ) : <EOL> """<STR_LIT>""" <EOL> ip , port = sock . getsockname ( ) <EOL> return cls ( ip ) , port <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . start_time = time . time ( ) <EOL> super ( PtrLookup , self ) . start ( ) <EOL> def _run ( self ) : <EOL> try : <EOL> hostname , _ , _ = socket . gethostbyaddr ( self . ip ) <EOL> except ( socket . herror , socket . gaierror , gevent . GreenletExit ) : <EOL> pass <EOL> except Exception : <EOL> logging . log_exception ( __name__ , query = self . ip ) <EOL> else : <EOL> return hostname <EOL> def finish ( self , runtime = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if runtime is None : <EOL> result = self . get ( block = False ) <EOL> else : <EOL> timeout = time . time ( ) - self . start_time <EOL> result = self . get ( block = True , timeout = timeout ) <EOL> except gevent . Timeout : <EOL> result = None <EOL> self . kill ( block = False ) <EOL> return result </s>
