Changeset 8675


Ignore:
Timestamp:
07/11/12 10:16:21 (7 years ago)
Author:
mnagni
Message:

Improved the hostname/IP retrivial feature

Location:
mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/__init__.py

    r8669 r8675  
    1 __version__ = '0.0.2' 
     1__version__ = '0.0.3' 
  • mauRepo/dj_security_middleware/trunk/dj_security_middleware/dj_security_middleware/middleware.py

    r8669 r8675  
    4040from django.utils.http import urlencode 
    4141 
     42import logging 
     43 
     44# Get an instance of a logger 
     45logger = logging.getLogger(__name__) 
     46 
    4247LOGIN_SERVICE_ERROR = 'No LOGIN_SETTING parameter is defined in the \ 
    4348application settings.py file. Please define a proper URL to the \ 
     
    6873        if not settings.DJ_SECURITY_SHAREDSECRET: 
    6974            raise DJMiddlewareException(DJ_SECURITY_SHAREDSECRET_ERROR)         
    70         if not settings.DJ_MIDDLEWARE_IP: 
    71             raise DJMiddlewareException(DJ_MIDDLEWARE_IP_ERROR)         
     75        #if not settings.DJ_MIDDLEWARE_IP: 
     76        #    raise DJMiddlewareException(DJ_MIDDLEWARE_IP_ERROR)         
    7277          
    7378        try: 
    7479            timestamp, userid, tokens, user_data = _is_authenticated(request) 
    75         except MissingCookieException:              
     80        except (MissingCookieException, DJMiddlewareException):              
    7681            url = '%s?%s' % (settings.DJ_SECURITY_LOGIN_SERVICE, _build_ret_url(request)) 
    7782            return HttpResponseRedirect(url)         
     
    113118     
    114119def _get_host_ip(): 
    115     if settings.DJ_MIDDLEWARE_IP: 
     120    if getattr(settings, 'DJ_MIDDLEWARE_IP', None): 
    116121        return settings.DJ_MIDDLEWARE_IP 
    117122         
     123    import urlparse 
    118124    import socket 
    119     ret = 'localhost' 
    120     try: 
    121         hostname = socket.gethostname() 
    122         ret = socket.gethostbyname_ex(hostname)[2][0] 
    123     except Exception: 
    124         pass     
    125     return ret     
     125    remote_urls = socket.getaddrinfo(socket.gethostname(), None) 
     126    for remote_url in remote_urls: 
     127        ret = remote_url[4][0] 
     128        if not ret.startswith('127'): 
     129            logger.debug("remote_url: %s" % (ret)) 
     130            return ret 
Note: See TracChangeset for help on using the changeset viewer.