Package Products :: Package ZenUtils :: Module DotNetCommunication |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.DotNetCommunication

     1  ##############################################################################
     2  #
     3  # Copyright (C) Zenoss, Inc. 2008, all rights reserved.
     4  #
     5  # This content is made available according to terms specified in
     6  # License.zenoss under the directory where your Zenoss product is installed.
     7  #
     8  ##############################################################################
     9
    10
    11  from urllib2 import HTTPCookieProcessor, build_opener, HTTPError
    12  from urllib import quote
    13  from urlparse import urljoin
    14
    15  #BASE_URL = 'http://dotnet.zenoss.loc:8080/ZenossDotNet/'
    16  BASE_URL = 'http://localhost:8081/ZenossDotNet/'
    17
    18  _DotNetSessions = {}
    19
    20 -def getDotNetSession(username, password):
    21      session = _DotNetSessions.get(username, None)
    22      if not session:
    23          session = DotNetSession()
    24          session.login(username, password)
    25          _DotNetSessions[username] = session
    26      return session
    27
    28 -class DotNetSession(object):
    29      """
    30      A cookie-enabled http client that can log in to and retrieve data from
    31      Zenoss.net.
    32      """
    33
    34 -    def __init__(self):
    35          """
    36          Set up the cookie jar.
    37          """
    38          import MultipartPostHandler
    39          self.opener = build_opener(HTTPCookieProcessor(),
    40                                  MultipartPostHandler.MultipartPostHandler)
    41          self.base_url = BASE_URL
    42
    43 -    def open(self, url, params={}):
    44          """
    45          Wrapper for the opener that encodes the query string.
    46          """
    47          url = urljoin(self.base_url, quote(url))
    48          try:
    49              response = self.opener.open(url, params)
    50          except HTTPError, e:
    51              print "Unable to access the Zenoss.net url:" + e.geturl()
    52              return None
    53          else:
    54              return response
    55
    56 -    def login(self, username, password):
    57          """
    58          Log in to Zenoss.net.
    59          """
    60          loginUrl = 'login_form'
    61
    62          # The fields just come from the login_form template
    63          params = {
    64              '__ac_name': username,
    65              '__ac_password': password,
    66              'came_from':'',
    67              'form.submitted':1,
    68              'js_enabled':1,
    69              'cookies_enabled':1
    70          }
    71          # POST the params to the loginUrl. The resulting
    72          # cookie will be stored in the cookie jar.
    73          # TODO: What happens on login failure?
    74          self.open(loginUrl, params)
    75
    76 -    def retrieve(self, url, params={}):
    77          """
    78          Open a url within the session and return the response data.
    79
    80          """
    81          response = self.open(url, params)
    82          if response:
    83              data = response.read()
    84              response.close()
    85              return data
    86              #return data.strip()
    87          else:
    88              return None
    89