1 from pcucontrol.reboot import *
3 class ePowerSwitchNew(PCUControl):
5 # The old code used Python's HTTPPasswordMgrWithDefaultRealm()
6 # For some reason this both doesn't work and in some cases, actually
7 # hangs the PCU. Definitely not what we want.
9 # The code below is much simpler. Just letting things fail first,
10 # and then, trying again with authentication string in the header.
12 def run_http(self, node_port, dryrun):
14 self.url = "http://%s:%d/" % (self.host,80)
15 uri = "%s:%d" % (self.host,80)
17 req = urllib2.Request(self.url)
19 handle = urllib2.urlopen(req)
21 # NOTE: this is expected to fail initially
28 return "ERROR: not protected by HTTP authentication"
30 if not hasattr(e, 'code') or e.code != 401:
31 return "ERROR: failed for: %s" % str(e)
33 base64data = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
34 # NOTE: assuming basic realm authentication.
35 authheader = "Basic %s" % base64data
36 req.add_header("Authorization", authheader)
39 f = urllib2.urlopen(req)
41 # failing here means the User/passwd is wrong (hopefully)
42 raise ExceptionPassword("Incorrect username/password")
44 # NOTE: after verifying that the user/password is correct,
45 # actually reboot the given node.
48 data = urllib.urlencode({'P%d' % node_port : "r"})
49 req = urllib2.Request(self.url + "cmd.html")
50 req.add_header("Authorization", authheader)
51 # add data to handler,
52 f = urllib2.urlopen(req, data)
53 if self.transport.verbose: print f.read()
55 import traceback; traceback.print_exc()
57 # fetch url one more time on cmd.html, econtrol.html or whatever.
60 if self.transport.verbose: print f.read()
64 class ePowerSwitchOld(PCUControl):
65 def run_http(self, node_port, dryrun):
66 self.url = "http://%s:%d/" % (self.host,80)
67 uri = "%s:%d" % (self.host,80)
70 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
71 authinfo.add_password (None, uri, self.username, self.password)
72 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
74 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
75 transport = urllib2.build_opener(authinfo)
76 f = transport.open(self.url)
77 if self.transport.verbose: print f.read()
80 transport = urllib2.build_opener(authhandler)
81 f = transport.open(self.url + "cmd.html", "P%d=r" % node_port)
82 if self.transport.verbose: print f.read()
84 self.transport.close()
87 class ePowerSwitchOld(PCUControl):
88 supported_ports = [80]
89 def run_http(self, node_port, dryrun):
90 self.url = "http://%s:%d/" % (self.host,80)
91 uri = "%s:%d" % (self.host,80)
93 # TODO: I'm still not sure what the deal is here.
94 # two independent calls appear to need to be made before the
95 # reboot will succeed. It doesn't seem to be possible to do
96 # this with a single call. I have no idea why.
99 authinfo = urllib2.HTTPPasswordMgrWithDefaultRealm()
100 authinfo.add_password (None, uri, self.username, self.password)
101 authhandler = urllib2.HTTPBasicAuthHandler( authinfo )
103 # NOTE: it doesn't seem to matter whether this authinfo is here or not.
104 transport = urllib2.build_opener()
105 f = transport.open(self.url + "elogin.html", "pwd=%s" % self.password)
106 if self.transport.verbose: print f.read()
109 transport = urllib2.build_opener(authhandler)
110 f = transport.open(self.url + "econtrol.html", "P%d=r" % node_port)
111 if self.transport.verbose: print f.read()
113 # data= "P%d=r" % node_port
114 #self.open(self.host, self.username, self.password)
115 #self.sendHTTP("elogin.html", "pwd=%s" % self.password)
116 #self.sendHTTP("econtrol.html", data)
117 #self.sendHTTP("cmd.html", data)