Package tlib :: Package base :: Module NetworkHelper |
[hide private]
[frames] | no frames]

# Source Code for Module tlib.base.NetworkHelper

      1  import urlparse
      2  import socket
      3
      4  from django.core.validators import URLValidator, EmailValidator
      5  from django.core.exceptions import ValidationError
      6
      7
      8 -class NetworkHelper(object):
      9      """
     10      Helper functions for parsing URLs and validating emails and URLs
     11      """
     12
     13      @staticmethod
     14 -    def get_hostname_from_url(url):
     15          """
     16          Extracts hostname from an URL in the format
     17           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     18          @param url: URL to process
     19          @type url: str
     20          """
     21          p = urlparse.urlparse(url)
     22          return p.hostname
     23
     24      @staticmethod
     25 -    def get_scheme_from_url(url):
     26          """
     27          Extracts scheme from an URL in the format
     28           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     29          @param url: URL to process
     30          @type url: str
     31          """
     32          p = urlparse.urlparse(url)
     33          return p.scheme
     34
     35      @staticmethod
     36 -    def get_path_from_url(url):
     37          """
     38          Extracts path from an URL in the format
     39           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     40          @param url: URL to process
     41          @type url: str
     42          """
     43          p = urlparse.urlparse(url)
     44          return p.path
     45
     46      @staticmethod
     47 -    def get_port_from_url(url):
     48          """
     49          Extracts port from an URL in the format
     50           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     51          @param url: URL to process
     52          @type url: str
     53          """
     54          p = urlparse.urlparse(url)
     55          return p.port
     56
     57      @staticmethod
     58 -    def get_username_from_url(url):
     59          """
     60          Extracts username from an URL in the format
     61           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     62          @param url: URL to process
     63          @type url: str
     64          """
     65          p = urlparse.urlparse(url)
     66          return p.username
     67
     68      @staticmethod
     69 -    def get_password_from_url(url):
     70          """
     71          Extracts password from an URL in the format
     72           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     73          @param url: URL to process
     74          @type url: str
     75          """
     76          p = urlparse.urlparse(url)
     77          return p.password
     78
     79      @staticmethod
     80 -    def get_params_from_url(url):
     81          """
     82          Extracts params from an URL in the format
     83           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     84          @param url: URL to process
     85          @type url: str
     86          """
     87          p = urlparse.urlparse(url)
     88          return p.params
     89
     90      @staticmethod
     91 -    def get_query_string_from_url(url):
     92          """
     93          Extracts query from an URL in the format
     94           <scheme>://<username>:<password>@<hostname>:<port>/<path>;<params>?<query>#<fragment>
     95          @param url: URL to process
     96          @type url: str
     97          """
     98          p = urlparse.urlparse(url)
     99          return urlparse.parse_qs(p.query, keep_blank_values=True, strict_parsing=True)
    100
    101      @staticmethod
    102 -    def get_ip_from_hostname(hostname):
    103          """
    104          Resolves a hostname and returns the corresponding IP address
    105          @param hostname: Hostname to resolve
    106          @type hostname: str
    107          """
    108          return socket.gethostbyname(hostname)
    109
    110      @staticmethod
    111 -    def is_valid_url(url):
    112          """
    113          Validates a URL
    114
    115          @param url: URL to validate
    116          @type url: str
    117          @rtype: bool
    118          """
    119          #Get a validator instance
    120          validator = URLValidator()
    121          try:
    122              #Validate URL
    123              validator(url)
    124              return True
    125          except ValidationError:
    126              return False
    127
    128      @staticmethod
    129 -    def is_valid_email(email):
    130          """
    131          Validates an email
    132
    133          @param email: eMail to validate
    134          @type email: str
    135          @rtype: bool
    136          """
    137          #Get a validator instance
    138          validator = EmailValidator()
    139          try:
    140              #Validate email
    141              validator(email)
    142              return True
    143          except ValidationError:
    144              return False
    145
    146      @staticmethod
    147 -    def is_absolute_url(url):
    148          """
    149          Validates if a URL is relative or absolute
    150
    151          @param url: URL to validate
    152          @type url: str
    153          @rtype: bool
    154          """
    155          return bool(urlparse.urlparse(url).netloc)
    156