1
1
#!/usr/bin/env python
2
2
3
3
import urllib2
4
+ import ssl
4
5
5
6
class RAC (object ):
6
7
7
- def __init__ (self , host , username , password ):
8
+ def __init__ (self , host , username , password , certfile = None ):
8
9
self .sid = None
9
10
self .host = host
10
11
self .username = username
11
12
self .password = password
13
+ self .certfile = certfile
12
14
13
15
def __enter__ (self ):
14
16
return self
@@ -35,11 +37,19 @@ def _extract_cmd_output(self, data):
35
37
return self ._extract_value (data , 'CMDOUTPUT' )
36
38
37
39
def _make_request (self , uri , data = None ):
38
- opener = urllib2 .build_opener ( )
40
+ req = urllib2 .Request ( 'https://%s/cgi-bin/%s' % ( self . host , uri ), data = self . _inject_header ( data ) )
39
41
if self .sid :
40
- opener .addheaders .append (('Cookie' , 'sid=%s' % self .sid ))
41
- return opener .open ('https://%s/cgi-bin/%s' % (self .host , uri ),
42
- self ._inject_header (data )).read ()
42
+ req .add_header ('Cookie' , 'sid=%s' % self .sid )
43
+ if self .certfile is None :
44
+ try :
45
+ return urllib2 .urlopen (req ).read ()
46
+ except urllib2 .URLError :
47
+ ctx = ssl .create_default_context ()
48
+ ctx .check_hostname = False
49
+ ctx .verify_mode = ssl .CERT_NONE
50
+ return urllib2 .urlopen (req , context = ctx ).read ()
51
+ else :
52
+ return urllib2 .urlopen (req , cafile = self .certfile ).read ()
43
53
44
54
def _login (self ):
45
55
data = '<LOGIN><REQ><USERNAME>%s</USERNAME><PASSWORD>%s</PASSWORD></REQ></LOGIN>' % (self .username , self .password )
@@ -73,5 +83,4 @@ def powerdown(self):
73
83
return self .run_command ('serveraction powerdown' )
74
84
75
85
def powerup (self ):
76
- return self .run_command ('serveraction powerup' )
77
-
86
+ return self .run_command ('serveraction powerup' )
0 commit comments