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 supported_ports = [80]
68 def run_http(self, node_port, dryrun):
69 self.url = "http://%s" % (self.host)
71 authinfo = { "pwd" : self.password }
72 data = urllib.urlencode(authinfo)
73 req = urllib2.Request(self.url + "/elogin.html", data)
76 response = urllib2.urlopen(req)
77 reply = response.read()
81 data = urllib.urlencode({'P%d' % node_port : "r"})
82 req = urllib2.Request(self.url + "econtrol.html")
83 f = urllib2.urlopen(req, data)
85 import traceback; traceback.print_exc()