1 from pcucontrol.reboot import *
3 class ePowerSwitchNew(PCUControl):
6 # The old code used Python's HTTPPasswordMgrWithDefaultRealm()
7 # For some reason this both doesn't work and in some cases, actually
8 # hangs the PCU. Definitely not what we want.
10 # The code below is much simpler. Just letting things fail first,
11 # and then, trying again with authentication string in the header.
13 def run_http(self, node_port, dryrun):
15 self.url = "http://%s:%d/" % (self.host,80)
16 uri = "%s:%d" % (self.host,80)
18 req = urllib2.Request(self.url)
20 handle = urllib2.urlopen(req)
22 # NOTE: this is expected to fail initially
29 return "ERROR: not protected by HTTP authentication"
31 if not hasattr(e, 'code') or e.code != 401:
32 return "ERROR: failed for: %s" % str(e)
34 base64data = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
35 # NOTE: assuming basic realm authentication.
36 authheader = "Basic %s" % base64data
37 req.add_header("Authorization", authheader)
40 f = urllib2.urlopen(req)
42 # failing here means the User/passwd is wrong (hopefully)
43 raise ExceptionPassword("Incorrect username/password")
45 # NOTE: after verifying that the user/password is correct,
46 # actually reboot the given node.
49 data = urllib.urlencode({'P%d' % node_port : "r"})
50 req = urllib2.Request(self.url + "cmd.html")
51 req.add_header("Authorization", authheader)
52 # add data to handler,
53 f = urllib2.urlopen(req, data)
54 #if self.transport.verbose: print f.read()
56 import traceback; traceback.print_exc()
58 # fetch url one more time on cmd.html, econtrol.html or whatever.
61 #if self.transport.verbose: print f.read()
66 class ePowerSwitchOld(PCUControl):
67 def run_http(self, node_port, dryrun):
68 self.url = "http://%s:%d/" % (self.host,80)
69 uri = "%s:%d" % (self.host,80)
72 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
73 authinfo.add_password (None, uri, self.username, self.password)
74 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
76 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
77 transport = urllib2.build_opener(authinfo)
78 f = transport.open(self.url)
79 if self.transport.verbose: print f.read()
82 transport = urllib2.build_opener(authhandler)
83 f = transport.open(self.url + "cmd.html", "P%d=r" % node_port)
84 if self.transport.verbose: print f.read()
86 self.transport.close()
89 class ePowerSwitchOld(PCUControl):
90 supported_ports = [80]
91 def run_http(self, node_port, dryrun):
92 self.url = "http://%s:%d/" % (self.host,80)
93 uri = "%s:%d" % (self.host,80)
95 # TODO: I'm still not sure what the deal is here.
96 # two independent calls appear to need to be made before the
97 # reboot will succeed. It doesn't seem to be possible to do
98 # this with a single call. I have no idea why.
101 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
102 authinfo.add_password (None, uri, self.username, self.password)
103 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
105 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
106 transport = urllib2.build_opener()
107 f = transport.open(self.url + "elogin.html", "pwd=%s" % self.password)
108 if self.transport.verbose: print f.read()
111 transport = urllib2.build_opener(authhandler)
112 f = transport.open(self.url + "econtrol.html", "P%d=r" % node_port)
113 if self.transport.verbose: print f.read()
115 # data= "P%d=r" % node_port
116 #self.open(self.host, self.username, self.password)
117 #self.sendHTTP("elogin.html", "pwd=%s" % self.password)
118 #self.sendHTTP("econtrol.html", data)
119 #self.sendHTTP("cmd.html", data)