Skip to content

Commit acdf349

Browse files
committed
Finish support for python 3
1 parent a1f904b commit acdf349

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

README

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ git clone git://github.com/seveas/python-hpilo
1818
$ cd python-hpilo
1919
$ python setup.py install
2020

21-
The minimum python version supported is 2.4 as I need this on CentOS 5
21+
The minimum python version supported is 2.4. Python 3 is also supported.
2222

2323
Usage
2424
-----

hpilo.py

+34-24
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,22 @@
55
import random
66
import re
77
import socket
8-
try:
9-
import cStringIO as StringIO
10-
except ImportError:
11-
import io as StringIO
128
import subprocess
139
import sys
10+
import warnings
11+
import hpilo_fw
12+
13+
PY3 = sys.version_info[0] >= 3
14+
if PY3:
15+
import io as StringIO
16+
b = lambda x: bytes(x, 'ascii')
17+
class Bogus(Exception): pass
18+
socket.sslerror = Bogus
19+
basestring = str
20+
else:
21+
import cStringIO as StringIO
22+
b = lambda x: x
23+
1424
try:
1525
import ssl
1626
except ImportError:
@@ -20,12 +30,10 @@ class ssl:
2030
@staticmethod
2131
def wrap_socket(sock, *args, **kwargs):
2232
return socket.ssl(sock)
23-
import warnings
2433
try:
2534
import xml.etree.cElementTree as etree
2635
except ImportError:
2736
import cElementTree as etree
28-
import hpilo_fw
2937

3038
# Which protocol to use
3139
ILO_RAW = 1
@@ -63,7 +71,7 @@ class Ilo(object):
6371
and use hponcfg instead. Username and password are ignored for ILO_LOCAL
6472
connections."""
6573

66-
XML_HEADER = '<?xml version="1.0"?>\r\n'
74+
XML_HEADER = b('<?xml version="1.0"?>\r\n')
6775
HTTP_HEADER = "POST /ribcl HTTP/1.1\r\nHost: localhost\r\nContent-Length: %d\r\nConnection: Close%s\r\n\r\n"
6876
HTTP_UPLOAD_HEADER = "POST /cgi-bin/uploadRibclFiles HTTP/1.1\r\nHost: localhost\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: multipart/form-data; boundary=%s\r\n\r\n"
6977
BLOCK_SIZE = 4096
@@ -83,12 +91,14 @@ def __str__(self):
8391
return "iLO interface of %s" % self.hostname
8492

8593
def _debug(self, level, message):
94+
if message.__class__.__name__ == 'bytes':
95+
message = message.decode('latin-1')
8696
if self.debug >= level:
97+
sys.stderr.write(re.sub(r'PASSWORD=".*"', 'PASSWORD="********"', message))
8798
if message.startswith('\r'):
88-
sys.stderr.write(re.sub(r'PASSWORD=".*"','PASSWORD="********"', message))
8999
sys.stderr.flush()
90100
else:
91-
print >>sys.stderr, re.sub(r'PASSWORD=".*"','PASSWORD="********"', message)
101+
sys.stderr.write('\n')
92102

93103
def _request(self, xml, progress=None):
94104
"""Given an ElementTree.Element, serialize it and do the request.
@@ -99,7 +109,7 @@ def _request(self, xml, progress=None):
99109

100110
# Serialize the XML
101111
if hasattr(etree, 'tostringlist'):
102-
xml = "\r\n".join(etree.tostringlist(xml)) + '\r\n'
112+
xml = b("\r\n").join(etree.tostringlist(xml)) + b('\r\n')
103113
else:
104114
xml = etree.tostring(xml)
105115

@@ -135,28 +145,28 @@ def _detect_protocol(self):
135145
# Do a bogus request, using the HTTP protocol. If there is no
136146
# header (see special case in communicate(), we should be using the
137147
# raw protocol
138-
header, data = self._communicate('<RIBCL VERSION="2.0"></RIBCL>', ILO_HTTP)
148+
header, data = self._communicate(b('<RIBCL VERSION="2.0"></RIBCL>'), ILO_HTTP)
139149
if header:
140150
self.protocol = ILO_HTTP
141151
else:
142152
self.protocol = ILO_RAW
143153

144154
def _upload_file(self, filename, progress):
145155
firmware = open(filename, 'rb').read()
146-
boundary = '------hpiLO3t' + str(random.randint(100000,1000000)) + 'z'
156+
boundary = b('------hpiLO3t' + str(random.randint(100000,1000000)) + 'z')
147157
while boundary in firmware:
148-
boundary = '------hpiLO3t' + str(random.randint(100000,1000000)) + 'z'
158+
boundary = b('------hpiLO3t' + str(random.randint(100000,1000000)) + 'z')
149159
parts = [
150-
"""--%s\r\nContent-Disposition: form-data; name="fileType"\r\n\r\n""" % boundary,
151-
"""\r\n--%s\r\nContent-Disposition: form-data; name="fwimgfile"; filename="%s"\r\nContent-Type: application/octet-stream\r\n\r\n""" % (boundary, filename),
160+
b("--") + boundary + b("""\r\nContent-Disposition: form-data; name="fileType"\r\n\r\n"""),
161+
b("\r\n--") + boundary + b('''\r\nContent-Disposition: form-data; name="fwimgfile"; filename="''') + b(filename) + b('''"\r\nContent-Type: application/octet-stream\r\n\r\n'''),
152162
firmware,
153-
"""\r\n--%s--\r\n""" % boundary
163+
b("\r\n--") + boundary + b("--\r\n"),
154164
]
155165
total_bytes = sum([len(x) for x in parts])
156166
sock = self._get_socket()
157167

158-
self._debug(2, self.HTTP_UPLOAD_HEADER % (total_bytes, boundary))
159-
sock.write(self.HTTP_UPLOAD_HEADER % (total_bytes, boundary))
168+
self._debug(2, self.HTTP_UPLOAD_HEADER % (total_bytes, boundary.decode('ascii')))
169+
sock.write(b(self.HTTP_UPLOAD_HEADER % (total_bytes, boundary.decode('ascii'))))
160170
for part in parts:
161171
if len(part) < 2048:
162172
self._debug(2, part)
@@ -182,7 +192,7 @@ def _upload_file(self, filename, progress):
182192
try:
183193
while True:
184194
d = sock.read()
185-
data += d
195+
data += d.decode('latin-1')
186196
if not d:
187197
break
188198
except socket.sslerror: # Connection closed
@@ -237,15 +247,15 @@ def _communicate(self, xml, protocol, progress=None):
237247

238248
if protocol == ILO_HTTP:
239249
self._debug(2, http_header)
240-
sock.write(http_header)
250+
sock.write(b(http_header))
241251

242252
self._debug(2, self.XML_HEADER + xml)
243253

244254
# XML header and data need to arrive in 2 distinct packets
245255
if self.protocol != ILO_LOCAL:
246256
sock.write(self.XML_HEADER)
247-
if '$EMBED' in xml:
248-
pre, name, post = re.compile(r'(.*)\$EMBED:(.*)\$(.*)', re.DOTALL).match(xml).groups()
257+
if b('$EMBED') in xml:
258+
pre, name, post = re.compile(b(r'(.*)\$EMBED:(.*)\$(.*)'), re.DOTALL).match(xml).groups()
249259
sock.write(pre)
250260
sent = 0
251261
fwlen = os.path.getsize(name)
@@ -273,7 +283,7 @@ def _communicate(self, xml, protocol, progress=None):
273283
data = ''
274284
try:
275285
while True:
276-
d = sock.read()
286+
d = sock.read().decode('latin-1')
277287
data += d
278288
if not d:
279289
break
@@ -411,7 +421,7 @@ def _element_children_to_dict(self, element):
411421
def _element_to_dict(self, element):
412422
"""Returns a dict with tag attributes as items"""
413423
retval = {}
414-
for key, val in element.attrib.iteritems():
424+
for key, val in element.attrib.items():
415425
retval[key.lower()] = self._coerce(val)
416426
return retval
417427

hpilo_fw.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import urllib.request as urllib2
1010
import configparser as ConfigParser
1111
import io as StringIO
12-
b = bytes
12+
b = lambda x: bytes(x, 'ascii')
1313
else:
1414
import urllib2
1515
import ConfigParser
16-
import StringIO
16+
import cStringIO as StringIO
1717
b = lambda x: x
1818

1919
_config = None

0 commit comments

Comments
 (0)