1
2
3
4
5
6
7
8
9
10
11
12
13
14 from urllib2 import HTTPCookieProcessor, build_opener, HTTPError
15 from urllib import quote
16 from urlparse import urljoin
17
18
19 BASE_URL = 'http://localhost:8081/ZenossDotNet/'
20
21 _DotNetSessions = {}
22
30
32 """
33 A cookie-enabled http client that can log in to and retrieve data from
34 Zenoss.net.
35 """
36
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
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
75
76
77 self.open(loginUrl, params)
78
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
90 else:
91 return None
92