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

# Source Code for Module Products.ZenUtils.DotNetCommunication

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