1*4882a593SmuzhiyunFrom bbbf474b2ebdbdac4d557e3351210f3fe2175c33 Mon Sep 17 00:00:00 2001
2*4882a593SmuzhiyunFrom: Mingli Yu <mingli.yu@windriver.com>
3*4882a593SmuzhiyunDate: Fri, 14 Feb 2020 10:09:55 +0000
4*4882a593SmuzhiyunSubject: [PATCH] Make ndiff support python3
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunBackport a patch from debian to make ndiff support
7*4882a593Smuzhiyunpython3.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunRefer to https://sources.debian.org/data/main/n/nmap/7.80+dfsg1-2/debian/patches/0004-Python3-port-of-ndiff.patch
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunUpstream-Status: Pending
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunSigned-off-by: Mingli Yu <mingli.yu@windriver.com>
14*4882a593Smuzhiyun---
15*4882a593Smuzhiyun Makefile.in                   |  12 +-
16*4882a593Smuzhiyun ndiff/ndiff.py                | 495 +++++++++++++++++-----------------
17*4882a593Smuzhiyun ndiff/ndifftest.py            |  94 +++----
18*4882a593Smuzhiyun ndiff/scripts/ndiff           |  14 +-
19*4882a593Smuzhiyun ndiff/setup.py                |  34 +--
20*4882a593Smuzhiyun ndiff/test-scans/anonymize.py |  18 +-
21*4882a593Smuzhiyun 6 files changed, 333 insertions(+), 334 deletions(-)
22*4882a593Smuzhiyun mode change 100644 => 100755 ndiff/setup.py
23*4882a593Smuzhiyun
24*4882a593Smuzhiyundiff --git a/Makefile.in b/Makefile.in
25*4882a593Smuzhiyunindex eee8863..32f86ba 100644
26*4882a593Smuzhiyun--- a/Makefile.in
27*4882a593Smuzhiyun+++ b/Makefile.in
28*4882a593Smuzhiyun@@ -35,6 +35,7 @@ ZENMAPDIR = @ZENMAPDIR@
29*4882a593Smuzhiyun NDIFFDIR = @NDIFFDIR@
30*4882a593Smuzhiyun NPINGDIR = @NPINGDIR@
31*4882a593Smuzhiyun PYTHON = @PYTHON@
32*4882a593Smuzhiyun+PYTHON3 = /usr/bin/env python3
33*4882a593Smuzhiyun DEFS = @DEFS@ -DNMAP_PLATFORM=\"$(NMAP_PLATFORM)\" -DNMAPDATADIR=\"$(nmapdatadir)\"
34*4882a593Smuzhiyun # With GCC, add extra security checks to source code.
35*4882a593Smuzhiyun # http://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html
36*4882a593Smuzhiyun@@ -260,7 +261,7 @@ clean-zenmap:
37*4882a593Smuzhiyun 	rm -f $(ZENMAPDIR)/zenmapCore/Name.pyc
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun clean-ndiff:
40*4882a593Smuzhiyun-	-cd $(NDIFFDIR) && $(PYTHON) setup.py clean --all
41*4882a593Smuzhiyun+	-cd $(NDIFFDIR) && $(PYTHON3) setup.py clean --all
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun clean-nping:
44*4882a593Smuzhiyun 	-cd $(NPINGDIR) && $(MAKE) clean
45*4882a593Smuzhiyun@@ -368,6 +369,7 @@ tests/check_dns: $(OBJS)
46*4882a593Smuzhiyun # this as the location of the interpreter whenever we're not doing a
47*4882a593Smuzhiyun # local installation.
48*4882a593Smuzhiyun DEFAULT_PYTHON_PATH = /usr/bin/env python
49*4882a593Smuzhiyun+DEFAULT_PYTHON3_PATH = /usr/bin/env python3
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun build-zenmap: $(ZENMAPDIR)/setup.py $(ZENMAPDIR)/zenmapCore/Version.py
52*4882a593Smuzhiyun # When DESTDIR is defined, assume we're building an executable
53*4882a593Smuzhiyun@@ -388,13 +390,13 @@ install-zenmap: $(ZENMAPDIR)/setup.py
54*4882a593Smuzhiyun 	ln -sf zenmap $(DESTDIR)$(bindir)/xnmap
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun build-ndiff:
57*4882a593Smuzhiyun-	cd $(NDIFFDIR) && $(PYTHON) setup.py build $(if $(DESTDIR),--executable "$(DEFAULT_PYTHON_PATH)")
58*4882a593Smuzhiyun+	cd $(NDIFFDIR) && $(PYTHON3) setup.py build $(if $(DESTDIR),--executable "$(DEFAULT_PYTHON3_PATH)")
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun build-nping: $(NPINGDIR)/Makefile build-nbase build-nsock build-netutil $(NPINGDIR)/nping.h @DNET_BUILD@ @PCAP_BUILD@
61*4882a593Smuzhiyun 	@cd $(NPINGDIR) && $(MAKE)
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun install-ndiff:
64*4882a593Smuzhiyun-	cd $(NDIFFDIR) && $(PYTHON) setup.py install --prefix "$(prefix)" --install-lib="${PYTHON_SITEPACKAGES_DIR}" $(if $(DESTDIR),--root "$(DESTDIR)")
65*4882a593Smuzhiyun+	cd $(NDIFFDIR) && $(PYTHON3) setup.py install --prefix "$(prefix)" --install-lib="${PYTHON_SITEPACKAGES_DIR}" $(if $(DESTDIR),--root "$(DESTDIR)")
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun NSE_FILES = scripts/script.db scripts/*.nse
68*4882a593Smuzhiyun NSE_LIB_LUA_FILES = nselib/*.lua nselib/*.luadoc
69*4882a593Smuzhiyun@@ -443,7 +445,7 @@ uninstall-zenmap:
70*4882a593Smuzhiyun 	rm -f $(DESTDIR)$(bindir)/xnmap
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun uninstall-ndiff:
73*4882a593Smuzhiyun-	cd $(NDIFFDIR) && $(PYTHON) setup.py uninstall
74*4882a593Smuzhiyun+	cd $(NDIFFDIR) && $(PYTHON3) setup.py uninstall
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun uninstall-ncat:
77*4882a593Smuzhiyun 	@cd $(NCATDIR) && $(MAKE) uninstall
78*4882a593Smuzhiyun@@ -458,7 +460,7 @@ check-ncat:
79*4882a593Smuzhiyun 	@cd $(NCATDIR) && $(MAKE) check
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun check-ndiff:
82*4882a593Smuzhiyun-	@cd $(NDIFFDIR) && $(PYTHON) ndifftest.py
83*4882a593Smuzhiyun+	@cd $(NDIFFDIR) && $(PYTHON3) ndifftest.py
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun check-nsock:
86*4882a593Smuzhiyun 	@cd $(NSOCKDIR)/src && $(MAKE) check
87*4882a593Smuzhiyundiff --git a/ndiff/ndiff.py b/ndiff/ndiff.py
88*4882a593Smuzhiyunindex 043273f..abbd1c5 100755
89*4882a593Smuzhiyun--- a/ndiff/ndiff.py
90*4882a593Smuzhiyun+++ b/ndiff/ndiff.py
91*4882a593Smuzhiyun@@ -1,4 +1,4 @@
92*4882a593Smuzhiyun-#!/usr/bin/env python
93*4882a593Smuzhiyun+#!/usr/bin/env python3
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun # Ndiff
96*4882a593Smuzhiyun #
97*4882a593Smuzhiyun@@ -26,11 +26,11 @@ xml.__path__ = [x for x in xml.__path__ if "_xmlplus" not in x]
98*4882a593Smuzhiyun import xml.sax
99*4882a593Smuzhiyun import xml.sax.saxutils
100*4882a593Smuzhiyun import xml.dom.minidom
101*4882a593Smuzhiyun-from StringIO import StringIO
102*4882a593Smuzhiyun+from io import StringIO
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun verbose = False
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun-NDIFF_XML_VERSION = u"1"
107*4882a593Smuzhiyun+NDIFF_XML_VERSION = "1"
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun class OverrideEntityResolver(xml.sax.handler.EntityResolver):
111*4882a593Smuzhiyun@@ -78,35 +78,35 @@ class Scan(object):
112*4882a593Smuzhiyun     def write_nmaprun_open(self, writer):
113*4882a593Smuzhiyun         attrs = {}
114*4882a593Smuzhiyun         if self.scanner is not None:
115*4882a593Smuzhiyun-            attrs[u"scanner"] = self.scanner
116*4882a593Smuzhiyun+            attrs["scanner"] = self.scanner
117*4882a593Smuzhiyun         if self.args is not None:
118*4882a593Smuzhiyun-            attrs[u"args"] = self.args
119*4882a593Smuzhiyun+            attrs["args"] = self.args
120*4882a593Smuzhiyun         if self.start_date is not None:
121*4882a593Smuzhiyun-            attrs[u"start"] = "%d" % time.mktime(self.start_date.timetuple())
122*4882a593Smuzhiyun-            attrs[u"startstr"] = self.start_date.strftime(
123*4882a593Smuzhiyun+            attrs["start"] = "%d" % time.mktime(self.start_date.timetuple())
124*4882a593Smuzhiyun+            attrs["startstr"] = self.start_date.strftime(
125*4882a593Smuzhiyun                     "%a %b %d %H:%M:%S %Y")
126*4882a593Smuzhiyun         if self.version is not None:
127*4882a593Smuzhiyun-            attrs[u"version"] = self.version
128*4882a593Smuzhiyun-        writer.startElement(u"nmaprun", attrs)
129*4882a593Smuzhiyun+            attrs["version"] = self.version
130*4882a593Smuzhiyun+        writer.startElement("nmaprun", attrs)
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun     def write_nmaprun_close(self, writer):
133*4882a593Smuzhiyun-        writer.endElement(u"nmaprun")
134*4882a593Smuzhiyun+        writer.endElement("nmaprun")
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun     def nmaprun_to_dom_fragment(self, document):
137*4882a593Smuzhiyun         frag = document.createDocumentFragment()
138*4882a593Smuzhiyun-        elem = document.createElement(u"nmaprun")
139*4882a593Smuzhiyun+        elem = document.createElement("nmaprun")
140*4882a593Smuzhiyun         if self.scanner is not None:
141*4882a593Smuzhiyun-            elem.setAttribute(u"scanner", self.scanner)
142*4882a593Smuzhiyun+            elem.setAttribute("scanner", self.scanner)
143*4882a593Smuzhiyun         if self.args is not None:
144*4882a593Smuzhiyun-            elem.setAttribute(u"args", self.args)
145*4882a593Smuzhiyun+            elem.setAttribute("args", self.args)
146*4882a593Smuzhiyun         if self.start_date is not None:
147*4882a593Smuzhiyun             elem.setAttribute(
148*4882a593Smuzhiyun-                    u"start", "%d" % time.mktime(self.start_date.timetuple()))
149*4882a593Smuzhiyun+                    "start", "%d" % time.mktime(self.start_date.timetuple()))
150*4882a593Smuzhiyun             elem.setAttribute(
151*4882a593Smuzhiyun-                    u"startstr",
152*4882a593Smuzhiyun+                    "startstr",
153*4882a593Smuzhiyun                     self.start_date.strftime("%a %b %d %H:%M:%S %Y"))
154*4882a593Smuzhiyun         if self.version is not None:
155*4882a593Smuzhiyun-            elem.setAttribute(u"version", self.version)
156*4882a593Smuzhiyun+            elem.setAttribute("version", self.version)
157*4882a593Smuzhiyun         frag.appendChild(elem)
158*4882a593Smuzhiyun         return frag
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun@@ -136,17 +136,17 @@ class Host(object):
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun     def format_name(self):
163*4882a593Smuzhiyun         """Return a human-readable identifier for this host."""
164*4882a593Smuzhiyun-        address_s = u", ".join(a.s for a in sorted(self.addresses))
165*4882a593Smuzhiyun-        hostname_s = u", ".join(sorted(self.hostnames))
166*4882a593Smuzhiyun+        address_s = ", ".join(a.s for a in sorted(self.addresses))
167*4882a593Smuzhiyun+        hostname_s = ", ".join(sorted(self.hostnames))
168*4882a593Smuzhiyun         if len(hostname_s) > 0:
169*4882a593Smuzhiyun             if len(address_s) > 0:
170*4882a593Smuzhiyun-                return u"%s (%s)" % (hostname_s, address_s)
171*4882a593Smuzhiyun+                return "%s (%s)" % (hostname_s, address_s)
172*4882a593Smuzhiyun             else:
173*4882a593Smuzhiyun                 return hostname_s
174*4882a593Smuzhiyun         elif len(address_s) > 0:
175*4882a593Smuzhiyun             return address_s
176*4882a593Smuzhiyun         else:
177*4882a593Smuzhiyun-            return u"<no name>"
178*4882a593Smuzhiyun+            return "<no name>"
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun     def add_port(self, port):
181*4882a593Smuzhiyun         self.ports[port.spec] = port
182*4882a593Smuzhiyun@@ -163,46 +163,46 @@ class Host(object):
183*4882a593Smuzhiyun         return state is None or state in self.extraports
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun     def extraports_string(self):
186*4882a593Smuzhiyun-        list = [(count, state) for (state, count) in self.extraports.items()]
187*4882a593Smuzhiyun+        locallist = [(count, state) for (state, count) in list(self.extraports.items())]
188*4882a593Smuzhiyun         # Reverse-sort by count.
189*4882a593Smuzhiyun-        list.sort(reverse=True)
190*4882a593Smuzhiyun-        return u", ".join(
191*4882a593Smuzhiyun-                [u"%d %s ports" % (count, state) for (count, state) in list])
192*4882a593Smuzhiyun+        locallist.sort(reverse=True)
193*4882a593Smuzhiyun+        return ", ".join(
194*4882a593Smuzhiyun+                ["%d %s ports" % (count, state) for (count, state) in locallist])
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun     def state_to_dom_fragment(self, document):
197*4882a593Smuzhiyun         frag = document.createDocumentFragment()
198*4882a593Smuzhiyun         if self.state is not None:
199*4882a593Smuzhiyun-            elem = document.createElement(u"status")
200*4882a593Smuzhiyun-            elem.setAttribute(u"state", self.state)
201*4882a593Smuzhiyun+            elem = document.createElement("status")
202*4882a593Smuzhiyun+            elem.setAttribute("state", self.state)
203*4882a593Smuzhiyun             frag.appendChild(elem)
204*4882a593Smuzhiyun         return frag
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun     def hostname_to_dom_fragment(self, document, hostname):
207*4882a593Smuzhiyun         frag = document.createDocumentFragment()
208*4882a593Smuzhiyun-        elem = document.createElement(u"hostname")
209*4882a593Smuzhiyun-        elem.setAttribute(u"name", hostname)
210*4882a593Smuzhiyun+        elem = document.createElement("hostname")
211*4882a593Smuzhiyun+        elem.setAttribute("name", hostname)
212*4882a593Smuzhiyun         frag.appendChild(elem)
213*4882a593Smuzhiyun         return frag
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun     def extraports_to_dom_fragment(self, document):
216*4882a593Smuzhiyun         frag = document.createDocumentFragment()
217*4882a593Smuzhiyun-        for state, count in self.extraports.items():
218*4882a593Smuzhiyun-            elem = document.createElement(u"extraports")
219*4882a593Smuzhiyun-            elem.setAttribute(u"state", state)
220*4882a593Smuzhiyun-            elem.setAttribute(u"count", unicode(count))
221*4882a593Smuzhiyun+        for state, count in list(self.extraports.items()):
222*4882a593Smuzhiyun+            elem = document.createElement("extraports")
223*4882a593Smuzhiyun+            elem.setAttribute("state", state)
224*4882a593Smuzhiyun+            elem.setAttribute("count", str(count))
225*4882a593Smuzhiyun             frag.appendChild(elem)
226*4882a593Smuzhiyun         return frag
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun     def os_to_dom_fragment(self, document, os):
229*4882a593Smuzhiyun         frag = document.createDocumentFragment()
230*4882a593Smuzhiyun-        elem = document.createElement(u"osmatch")
231*4882a593Smuzhiyun-        elem.setAttribute(u"name", os)
232*4882a593Smuzhiyun+        elem = document.createElement("osmatch")
233*4882a593Smuzhiyun+        elem.setAttribute("name", os)
234*4882a593Smuzhiyun         frag.appendChild(elem)
235*4882a593Smuzhiyun         return frag
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun     def to_dom_fragment(self, document):
238*4882a593Smuzhiyun         frag = document.createDocumentFragment()
239*4882a593Smuzhiyun-        elem = document.createElement(u"host")
240*4882a593Smuzhiyun+        elem = document.createElement("host")
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun         if self.state is not None:
243*4882a593Smuzhiyun             elem.appendChild(self.state_to_dom_fragment(document))
244*4882a593Smuzhiyun@@ -211,13 +211,13 @@ class Host(object):
245*4882a593Smuzhiyun             elem.appendChild(addr.to_dom_fragment(document))
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun         if len(self.hostnames) > 0:
248*4882a593Smuzhiyun-            hostnames_elem = document.createElement(u"hostnames")
249*4882a593Smuzhiyun+            hostnames_elem = document.createElement("hostnames")
250*4882a593Smuzhiyun             for hostname in self.hostnames:
251*4882a593Smuzhiyun                 hostnames_elem.appendChild(
252*4882a593Smuzhiyun                         self.hostname_to_dom_fragment(document, hostname))
253*4882a593Smuzhiyun             elem.appendChild(hostnames_elem)
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun-        ports_elem = document.createElement(u"ports")
256*4882a593Smuzhiyun+        ports_elem = document.createElement("ports")
257*4882a593Smuzhiyun         ports_elem.appendChild(self.extraports_to_dom_fragment(document))
258*4882a593Smuzhiyun         for port in sorted(self.ports.values()):
259*4882a593Smuzhiyun             if not self.is_extraports(port.state):
260*4882a593Smuzhiyun@@ -226,13 +226,13 @@ class Host(object):
261*4882a593Smuzhiyun             elem.appendChild(ports_elem)
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun         if len(self.os) > 0:
264*4882a593Smuzhiyun-            os_elem = document.createElement(u"os")
265*4882a593Smuzhiyun+            os_elem = document.createElement("os")
266*4882a593Smuzhiyun             for os in self.os:
267*4882a593Smuzhiyun                 os_elem.appendChild(self.os_to_dom_fragment(document, os))
268*4882a593Smuzhiyun             elem.appendChild(os_elem)
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun         if len(self.script_results) > 0:
271*4882a593Smuzhiyun-            hostscript_elem = document.createElement(u"hostscript")
272*4882a593Smuzhiyun+            hostscript_elem = document.createElement("hostscript")
273*4882a593Smuzhiyun             for sr in self.script_results:
274*4882a593Smuzhiyun                 hostscript_elem.appendChild(sr.to_dom_fragment(document))
275*4882a593Smuzhiyun             elem.appendChild(hostscript_elem)
276*4882a593Smuzhiyun@@ -246,7 +246,7 @@ class Address(object):
277*4882a593Smuzhiyun         self.s = s
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun     def __eq__(self, other):
280*4882a593Smuzhiyun-        return self.__cmp__(other) == 0
281*4882a593Smuzhiyun+        return self.sort_key() == other.sort_key()
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun     def __ne__(self, other):
284*4882a593Smuzhiyun         return not self.__eq__(other)
285*4882a593Smuzhiyun@@ -254,8 +254,8 @@ class Address(object):
286*4882a593Smuzhiyun     def __hash__(self):
287*4882a593Smuzhiyun         return hash(self.sort_key())
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun-    def __cmp__(self, other):
290*4882a593Smuzhiyun-        return cmp(self.sort_key(), other.sort_key())
291*4882a593Smuzhiyun+    def __lt__(self, other):
292*4882a593Smuzhiyun+        return self.sort_key() < other.sort_key()
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun     def __str__(self):
295*4882a593Smuzhiyun         return str(self.s)
296*4882a593Smuzhiyun@@ -264,21 +264,21 @@ class Address(object):
297*4882a593Smuzhiyun         return self.s
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun     def new(type, s):
300*4882a593Smuzhiyun-        if type == u"ipv4":
301*4882a593Smuzhiyun+        if type == "ipv4":
302*4882a593Smuzhiyun             return IPv4Address(s)
303*4882a593Smuzhiyun-        elif type == u"ipv6":
304*4882a593Smuzhiyun+        elif type == "ipv6":
305*4882a593Smuzhiyun             return IPv6Address(s)
306*4882a593Smuzhiyun-        elif type == u"mac":
307*4882a593Smuzhiyun+        elif type == "mac":
308*4882a593Smuzhiyun             return MACAddress(s)
309*4882a593Smuzhiyun         else:
310*4882a593Smuzhiyun-            raise ValueError(u"Unknown address type %s." % type)
311*4882a593Smuzhiyun+            raise ValueError("Unknown address type %s." % type)
312*4882a593Smuzhiyun     new = staticmethod(new)
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun     def to_dom_fragment(self, document):
315*4882a593Smuzhiyun         frag = document.createDocumentFragment()
316*4882a593Smuzhiyun-        elem = document.createElement(u"address")
317*4882a593Smuzhiyun-        elem.setAttribute(u"addr", self.s)
318*4882a593Smuzhiyun-        elem.setAttribute(u"addrtype", self.type)
319*4882a593Smuzhiyun+        elem = document.createElement("address")
320*4882a593Smuzhiyun+        elem.setAttribute("addr", self.s)
321*4882a593Smuzhiyun+        elem.setAttribute("addrtype", self.type)
322*4882a593Smuzhiyun         frag.appendChild(elem)
323*4882a593Smuzhiyun         return frag
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun@@ -287,21 +287,21 @@ class Address(object):
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun class IPv4Address(Address):
329*4882a593Smuzhiyun-    type = property(lambda self: u"ipv4")
330*4882a593Smuzhiyun+    type = property(lambda self: "ipv4")
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun     def sort_key(self):
333*4882a593Smuzhiyun         return (0, self.s)
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun class IPv6Address(Address):
337*4882a593Smuzhiyun-    type = property(lambda self: u"ipv6")
338*4882a593Smuzhiyun+    type = property(lambda self: "ipv6")
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun     def sort_key(self):
341*4882a593Smuzhiyun         return (1, self.s)
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun class MACAddress(Address):
345*4882a593Smuzhiyun-    type = property(lambda self: u"mac")
346*4882a593Smuzhiyun+    type = property(lambda self: "mac")
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun     def sort_key(self):
349*4882a593Smuzhiyun         return (2, self.s)
350*4882a593Smuzhiyun@@ -320,28 +320,25 @@ class Port(object):
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun     def state_string(self):
353*4882a593Smuzhiyun         if self.state is None:
354*4882a593Smuzhiyun-            return u"unknown"
355*4882a593Smuzhiyun+            return "unknown"
356*4882a593Smuzhiyun         else:
357*4882a593Smuzhiyun-            return unicode(self.state)
358*4882a593Smuzhiyun+            return str(self.state)
359*4882a593Smuzhiyun
360*4882a593Smuzhiyun     def spec_string(self):
361*4882a593Smuzhiyun-        return u"%d/%s" % self.spec
362*4882a593Smuzhiyun+        return "%d/%s" % self.spec
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun-    def __cmp__(self, other):
365*4882a593Smuzhiyun-        d = cmp(self.spec, other.spec)
366*4882a593Smuzhiyun-        if d != 0:
367*4882a593Smuzhiyun-            return d
368*4882a593Smuzhiyun-        return cmp((self.spec, self.service, self.script_results),
369*4882a593Smuzhiyun-            (other.spec, other.service, other.script_results))
370*4882a593Smuzhiyun+    def __lt__(self, other):
371*4882a593Smuzhiyun+        return (self.spec, self.service, self.script_results) < (
372*4882a593Smuzhiyun+            other.spec, other.service, other.script_results)
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun     def to_dom_fragment(self, document):
375*4882a593Smuzhiyun         frag = document.createDocumentFragment()
376*4882a593Smuzhiyun-        elem = document.createElement(u"port")
377*4882a593Smuzhiyun-        elem.setAttribute(u"portid", unicode(self.spec[0]))
378*4882a593Smuzhiyun-        elem.setAttribute(u"protocol", self.spec[1])
379*4882a593Smuzhiyun+        elem = document.createElement("port")
380*4882a593Smuzhiyun+        elem.setAttribute("portid", str(self.spec[0]))
381*4882a593Smuzhiyun+        elem.setAttribute("protocol", self.spec[1])
382*4882a593Smuzhiyun         if self.state is not None:
383*4882a593Smuzhiyun-            state_elem = document.createElement(u"state")
384*4882a593Smuzhiyun-            state_elem.setAttribute(u"state", self.state)
385*4882a593Smuzhiyun+            state_elem = document.createElement("state")
386*4882a593Smuzhiyun+            state_elem.setAttribute("state", self.state)
387*4882a593Smuzhiyun             elem.appendChild(state_elem)
388*4882a593Smuzhiyun         elem.appendChild(self.service.to_dom_fragment(document))
389*4882a593Smuzhiyun         for sr in self.script_results:
390*4882a593Smuzhiyun@@ -385,7 +382,7 @@ class Service(object):
391*4882a593Smuzhiyun         if len(parts) == 0:
392*4882a593Smuzhiyun             return None
393*4882a593Smuzhiyun         else:
394*4882a593Smuzhiyun-            return u"/".join(parts)
395*4882a593Smuzhiyun+            return "/".join(parts)
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun     def version_string(self):
398*4882a593Smuzhiyun         """Get a string like in the VERSION column of Nmap output."""
399*4882a593Smuzhiyun@@ -395,17 +392,17 @@ class Service(object):
400*4882a593Smuzhiyun         if self.version is not None:
401*4882a593Smuzhiyun             parts.append(self.version)
402*4882a593Smuzhiyun         if self.extrainfo is not None:
403*4882a593Smuzhiyun-            parts.append(u"(%s)" % self.extrainfo)
404*4882a593Smuzhiyun+            parts.append("(%s)" % self.extrainfo)
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun         if len(parts) == 0:
407*4882a593Smuzhiyun             return None
408*4882a593Smuzhiyun         else:
409*4882a593Smuzhiyun-            return u" ".join(parts)
410*4882a593Smuzhiyun+            return " ".join(parts)
411*4882a593Smuzhiyun
412*4882a593Smuzhiyun     def to_dom_fragment(self, document):
413*4882a593Smuzhiyun         frag = document.createDocumentFragment()
414*4882a593Smuzhiyun-        elem = document.createElement(u"service")
415*4882a593Smuzhiyun-        for attr in (u"name", u"product", u"version", u"extrainfo", u"tunnel"):
416*4882a593Smuzhiyun+        elem = document.createElement("service")
417*4882a593Smuzhiyun+        for attr in ("name", "product", "version", "extrainfo", "tunnel"):
418*4882a593Smuzhiyun             v = getattr(self, attr)
419*4882a593Smuzhiyun             if v is None:
420*4882a593Smuzhiyun                 continue
421*4882a593Smuzhiyun@@ -435,53 +432,53 @@ class ScriptResult(object):
422*4882a593Smuzhiyun         result = []
423*4882a593Smuzhiyun         lines = self.output.splitlines()
424*4882a593Smuzhiyun         if len(lines) > 0:
425*4882a593Smuzhiyun-            lines[0] = self.id + u": " + lines[0]
426*4882a593Smuzhiyun+            lines[0] = self.id + ": " + lines[0]
427*4882a593Smuzhiyun         for line in lines[:-1]:
428*4882a593Smuzhiyun-            result.append(u"|  " + line)
429*4882a593Smuzhiyun+            result.append("|  " + line)
430*4882a593Smuzhiyun         if len(lines) > 0:
431*4882a593Smuzhiyun-            result.append(u"|_ " + lines[-1])
432*4882a593Smuzhiyun+            result.append("|_ " + lines[-1])
433*4882a593Smuzhiyun         return result
434*4882a593Smuzhiyun
435*4882a593Smuzhiyun     def to_dom_fragment(self, document):
436*4882a593Smuzhiyun         frag = document.createDocumentFragment()
437*4882a593Smuzhiyun-        elem = document.createElement(u"script")
438*4882a593Smuzhiyun-        elem.setAttribute(u"id", self.id)
439*4882a593Smuzhiyun-        elem.setAttribute(u"output", self.output)
440*4882a593Smuzhiyun+        elem = document.createElement("script")
441*4882a593Smuzhiyun+        elem.setAttribute("id", self.id)
442*4882a593Smuzhiyun+        elem.setAttribute("output", self.output)
443*4882a593Smuzhiyun         frag.appendChild(elem)
444*4882a593Smuzhiyun         return frag
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun def format_banner(scan):
448*4882a593Smuzhiyun     """Format a startup banner more or less like Nmap does."""
449*4882a593Smuzhiyun-    scanner = u"Nmap"
450*4882a593Smuzhiyun-    if scan.scanner is not None and scan.scanner != u"nmap":
451*4882a593Smuzhiyun+    scanner = "Nmap"
452*4882a593Smuzhiyun+    if scan.scanner is not None and scan.scanner != "nmap":
453*4882a593Smuzhiyun         scanner = scan.scanner
454*4882a593Smuzhiyun     parts = [scanner]
455*4882a593Smuzhiyun     if scan.version is not None:
456*4882a593Smuzhiyun         parts.append(scan.version)
457*4882a593Smuzhiyun-    parts.append(u"scan")
458*4882a593Smuzhiyun+    parts.append("scan")
459*4882a593Smuzhiyun     if scan.start_date is not None:
460*4882a593Smuzhiyun-        parts.append(u"initiated %s" % scan.start_date.strftime(
461*4882a593Smuzhiyun+        parts.append("initiated %s" % scan.start_date.strftime(
462*4882a593Smuzhiyun             "%a %b %d %H:%M:%S %Y"))
463*4882a593Smuzhiyun     if scan.args is not None:
464*4882a593Smuzhiyun-        parts.append(u"as: %s" % scan.args)
465*4882a593Smuzhiyun-    return u" ".join(parts)
466*4882a593Smuzhiyun+        parts.append("as: %s" % scan.args)
467*4882a593Smuzhiyun+    return " ".join(parts)
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun def print_script_result_diffs_text(title, script_results_a, script_results_b,
471*4882a593Smuzhiyun         script_result_diffs, f=sys.stdout):
472*4882a593Smuzhiyun-    table = Table(u"*")
473*4882a593Smuzhiyun+    table = Table("*")
474*4882a593Smuzhiyun     for sr_diff in script_result_diffs:
475*4882a593Smuzhiyun         sr_diff.append_to_port_table(table)
476*4882a593Smuzhiyun     if len(table) > 0:
477*4882a593Smuzhiyun-        print >> f
478*4882a593Smuzhiyun+        print(file=f)
479*4882a593Smuzhiyun         if len(script_results_b) == 0:
480*4882a593Smuzhiyun-            print >> f, u"-%s:" % title
481*4882a593Smuzhiyun+            print("-%s:" % title, file=f)
482*4882a593Smuzhiyun         elif len(script_results_a) == 0:
483*4882a593Smuzhiyun-            print >> f, u"+%s:" % title
484*4882a593Smuzhiyun+            print("+%s:" % title, file=f)
485*4882a593Smuzhiyun         else:
486*4882a593Smuzhiyun-            print >> f, u" %s:" % title
487*4882a593Smuzhiyun-        print >> f, table
488*4882a593Smuzhiyun+            print(" %s:" % title, file=f)
489*4882a593Smuzhiyun+        print(table, file=f)
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun def script_result_diffs_to_dom_fragment(elem, script_results_a,
493*4882a593Smuzhiyun@@ -489,13 +486,13 @@ def script_result_diffs_to_dom_fragment(elem, script_results_a,
494*4882a593Smuzhiyun     if len(script_results_a) == 0 and len(script_results_b) == 0:
495*4882a593Smuzhiyun         return document.createDocumentFragment()
496*4882a593Smuzhiyun     elif len(script_results_b) == 0:
497*4882a593Smuzhiyun-        a_elem = document.createElement(u"a")
498*4882a593Smuzhiyun+        a_elem = document.createElement("a")
499*4882a593Smuzhiyun         for sr in script_results_a:
500*4882a593Smuzhiyun             elem.appendChild(sr.to_dom_fragment(document))
501*4882a593Smuzhiyun         a_elem.appendChild(elem)
502*4882a593Smuzhiyun         return a_elem
503*4882a593Smuzhiyun     elif len(script_results_a) == 0:
504*4882a593Smuzhiyun-        b_elem = document.createElement(u"b")
505*4882a593Smuzhiyun+        b_elem = document.createElement("b")
506*4882a593Smuzhiyun         for sr in script_results_b:
507*4882a593Smuzhiyun             elem.appendChild(sr.to_dom_fragment(document))
508*4882a593Smuzhiyun         b_elem.appendChild(elem)
509*4882a593Smuzhiyun@@ -581,10 +578,10 @@ class ScanDiffText(ScanDiff):
510*4882a593Smuzhiyun         banner_a = format_banner(self.scan_a)
511*4882a593Smuzhiyun         banner_b = format_banner(self.scan_b)
512*4882a593Smuzhiyun         if banner_a != banner_b:
513*4882a593Smuzhiyun-            print >> self.f, u"-%s" % banner_a
514*4882a593Smuzhiyun-            print >> self.f, u"+%s" % banner_b
515*4882a593Smuzhiyun+            print("-%s" % banner_a, file=self.f)
516*4882a593Smuzhiyun+            print("+%s" % banner_b, file=self.f)
517*4882a593Smuzhiyun         elif verbose:
518*4882a593Smuzhiyun-            print >> self.f, u" %s" % banner_a
519*4882a593Smuzhiyun+            print(" %s" % banner_a, file=self.f)
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun     def output_pre_scripts(self, pre_script_result_diffs):
522*4882a593Smuzhiyun         print_script_result_diffs_text("Pre-scan script results",
523*4882a593Smuzhiyun@@ -597,7 +594,7 @@ class ScanDiffText(ScanDiff):
524*4882a593Smuzhiyun             post_script_result_diffs, self.f)
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun     def output_host_diff(self, h_diff):
527*4882a593Smuzhiyun-        print >> self.f
528*4882a593Smuzhiyun+        print(file=self.f)
529*4882a593Smuzhiyun         h_diff.print_text(self.f)
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun     def output_ending(self):
532*4882a593Smuzhiyun@@ -622,8 +619,8 @@ class ScanDiffXML(ScanDiff):
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun     def output_beginning(self):
535*4882a593Smuzhiyun         self.writer.startDocument()
536*4882a593Smuzhiyun-        self.writer.startElement(u"nmapdiff", {u"version": NDIFF_XML_VERSION})
537*4882a593Smuzhiyun-        self.writer.startElement(u"scandiff", {})
538*4882a593Smuzhiyun+        self.writer.startElement("nmapdiff", {"version": NDIFF_XML_VERSION})
539*4882a593Smuzhiyun+        self.writer.startElement("scandiff", {})
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun         if self.nmaprun_differs():
542*4882a593Smuzhiyun             self.writer.frag_a(
543*4882a593Smuzhiyun@@ -636,7 +633,7 @@ class ScanDiffXML(ScanDiff):
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun     def output_pre_scripts(self, pre_script_result_diffs):
546*4882a593Smuzhiyun         if len(pre_script_result_diffs) > 0 or verbose:
547*4882a593Smuzhiyun-            prescript_elem = self.document.createElement(u"prescript")
548*4882a593Smuzhiyun+            prescript_elem = self.document.createElement("prescript")
549*4882a593Smuzhiyun             frag = script_result_diffs_to_dom_fragment(
550*4882a593Smuzhiyun                 prescript_elem, self.scan_a.pre_script_results,
551*4882a593Smuzhiyun                 self.scan_b.pre_script_results, pre_script_result_diffs,
552*4882a593Smuzhiyun@@ -646,7 +643,7 @@ class ScanDiffXML(ScanDiff):
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun     def output_post_scripts(self, post_script_result_diffs):
555*4882a593Smuzhiyun         if len(post_script_result_diffs) > 0 or verbose:
556*4882a593Smuzhiyun-            postscript_elem = self.document.createElement(u"postscript")
557*4882a593Smuzhiyun+            postscript_elem = self.document.createElement("postscript")
558*4882a593Smuzhiyun             frag = script_result_diffs_to_dom_fragment(
559*4882a593Smuzhiyun                 postscript_elem, self.scan_a.post_script_results,
560*4882a593Smuzhiyun                 self.scan_b.post_script_results, post_script_result_diffs,
561*4882a593Smuzhiyun@@ -660,8 +657,8 @@ class ScanDiffXML(ScanDiff):
562*4882a593Smuzhiyun         frag.unlink()
563*4882a593Smuzhiyun
564*4882a593Smuzhiyun     def output_ending(self):
565*4882a593Smuzhiyun-        self.writer.endElement(u"scandiff")
566*4882a593Smuzhiyun-        self.writer.endElement(u"nmapdiff")
567*4882a593Smuzhiyun+        self.writer.endElement("scandiff")
568*4882a593Smuzhiyun+        self.writer.endElement("nmapdiff")
569*4882a593Smuzhiyun         self.writer.endDocument()
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun@@ -719,9 +716,9 @@ class HostDiff(object):
573*4882a593Smuzhiyun         self.cost += os_cost
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun         extraports_a = tuple((count, state)
576*4882a593Smuzhiyun-                for (state, count) in self.host_a.extraports.items())
577*4882a593Smuzhiyun+                for (state, count) in list(self.host_a.extraports.items()))
578*4882a593Smuzhiyun         extraports_b = tuple((count, state)
579*4882a593Smuzhiyun-                for (state, count) in self.host_b.extraports.items())
580*4882a593Smuzhiyun+                for (state, count) in list(self.host_b.extraports.items()))
581*4882a593Smuzhiyun         if extraports_a != extraports_b:
582*4882a593Smuzhiyun             self.extraports_changed = True
583*4882a593Smuzhiyun             self.cost += 1
584*4882a593Smuzhiyun@@ -747,69 +744,69 @@ class HostDiff(object):
585*4882a593Smuzhiyun         # Names and addresses.
586*4882a593Smuzhiyun         if self.id_changed:
587*4882a593Smuzhiyun             if host_a.state is not None:
588*4882a593Smuzhiyun-                print >> f, u"-%s:" % host_a.format_name()
589*4882a593Smuzhiyun+                print("-%s:" % host_a.format_name(), file=f)
590*4882a593Smuzhiyun             if self.host_b.state is not None:
591*4882a593Smuzhiyun-                print >> f, u"+%s:" % host_b.format_name()
592*4882a593Smuzhiyun+                print("+%s:" % host_b.format_name(), file=f)
593*4882a593Smuzhiyun         else:
594*4882a593Smuzhiyun-            print >> f, u" %s:" % host_a.format_name()
595*4882a593Smuzhiyun+            print(" %s:" % host_a.format_name(), file=f)
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun         # State.
598*4882a593Smuzhiyun         if self.state_changed:
599*4882a593Smuzhiyun             if host_a.state is not None:
600*4882a593Smuzhiyun-                print >> f, u"-Host is %s." % host_a.state
601*4882a593Smuzhiyun+                print("-Host is %s." % host_a.state, file=f)
602*4882a593Smuzhiyun             if host_b.state is not None:
603*4882a593Smuzhiyun-                print >> f, u"+Host is %s." % host_b.state
604*4882a593Smuzhiyun+                print("+Host is %s." % host_b.state, file=f)
605*4882a593Smuzhiyun         elif verbose:
606*4882a593Smuzhiyun-            print >> f, u" Host is %s." % host_b.state
607*4882a593Smuzhiyun+            print(" Host is %s." % host_b.state, file=f)
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun         # Extraports.
610*4882a593Smuzhiyun         if self.extraports_changed:
611*4882a593Smuzhiyun             if len(host_a.extraports) > 0:
612*4882a593Smuzhiyun-                print >> f, u"-Not shown: %s" % host_a.extraports_string()
613*4882a593Smuzhiyun+                print("-Not shown: %s" % host_a.extraports_string(), file=f)
614*4882a593Smuzhiyun             if len(host_b.extraports) > 0:
615*4882a593Smuzhiyun-                print >> f, u"+Not shown: %s" % host_b.extraports_string()
616*4882a593Smuzhiyun+                print("+Not shown: %s" % host_b.extraports_string(), file=f)
617*4882a593Smuzhiyun         elif verbose:
618*4882a593Smuzhiyun             if len(host_a.extraports) > 0:
619*4882a593Smuzhiyun-                print >> f, u" Not shown: %s" % host_a.extraports_string()
620*4882a593Smuzhiyun+                print(" Not shown: %s" % host_a.extraports_string(), file=f)
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun         # Port table.
623*4882a593Smuzhiyun-        port_table = Table(u"** * * *")
624*4882a593Smuzhiyun+        port_table = Table("** * * *")
625*4882a593Smuzhiyun         if host_a.state is None:
626*4882a593Smuzhiyun-            mark = u"+"
627*4882a593Smuzhiyun+            mark = "+"
628*4882a593Smuzhiyun         elif host_b.state is None:
629*4882a593Smuzhiyun-            mark = u"-"
630*4882a593Smuzhiyun+            mark = "-"
631*4882a593Smuzhiyun         else:
632*4882a593Smuzhiyun-            mark = u" "
633*4882a593Smuzhiyun-        port_table.append((mark, u"PORT", u"STATE", u"SERVICE", u"VERSION"))
634*4882a593Smuzhiyun+            mark = " "
635*4882a593Smuzhiyun+        port_table.append((mark, "PORT", "STATE", "SERVICE", "VERSION"))
636*4882a593Smuzhiyun
637*4882a593Smuzhiyun         for port in self.ports:
638*4882a593Smuzhiyun             port_diff = self.port_diffs[port]
639*4882a593Smuzhiyun             port_diff.append_to_port_table(port_table, host_a, host_b)
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun         if len(port_table) > 1:
642*4882a593Smuzhiyun-            print >> f, port_table
643*4882a593Smuzhiyun+            print(port_table, file=f)
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun         # OS changes.
646*4882a593Smuzhiyun         if self.os_changed or verbose:
647*4882a593Smuzhiyun             if len(host_a.os) > 0:
648*4882a593Smuzhiyun                 if len(host_b.os) > 0:
649*4882a593Smuzhiyun-                    print >> f, u" OS details:"
650*4882a593Smuzhiyun+                    print(" OS details:", file=f)
651*4882a593Smuzhiyun                 else:
652*4882a593Smuzhiyun-                    print >> f, u"-OS details:"
653*4882a593Smuzhiyun+                    print("-OS details:", file=f)
654*4882a593Smuzhiyun             elif len(host_b.os) > 0:
655*4882a593Smuzhiyun-                print >> f, u"+OS details:"
656*4882a593Smuzhiyun+                print("+OS details:", file=f)
657*4882a593Smuzhiyun             # os_diffs is a list of 5-tuples returned by
658*4882a593Smuzhiyun             # difflib.SequenceMatcher.
659*4882a593Smuzhiyun             for op, i1, i2, j1, j2 in self.os_diffs:
660*4882a593Smuzhiyun                 if op == "replace" or op == "delete":
661*4882a593Smuzhiyun                     for i in range(i1, i2):
662*4882a593Smuzhiyun-                        print >> f, "-  %s" % host_a.os[i]
663*4882a593Smuzhiyun+                        print("-  %s" % host_a.os[i], file=f)
664*4882a593Smuzhiyun                 if op == "replace" or op == "insert":
665*4882a593Smuzhiyun                     for i in range(j1, j2):
666*4882a593Smuzhiyun-                        print >> f, "+  %s" % host_b.os[i]
667*4882a593Smuzhiyun+                        print("+  %s" % host_b.os[i], file=f)
668*4882a593Smuzhiyun                 if op == "equal":
669*4882a593Smuzhiyun                     for i in range(i1, i2):
670*4882a593Smuzhiyun-                        print >> f, "   %s" % host_a.os[i]
671*4882a593Smuzhiyun+                        print("   %s" % host_a.os[i], file=f)
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun         print_script_result_diffs_text("Host script results",
674*4882a593Smuzhiyun             host_a.script_results, host_b.script_results,
675*4882a593Smuzhiyun@@ -820,32 +817,32 @@ class HostDiff(object):
676*4882a593Smuzhiyun         host_b = self.host_b
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun         frag = document.createDocumentFragment()
679*4882a593Smuzhiyun-        hostdiff_elem = document.createElement(u"hostdiff")
680*4882a593Smuzhiyun+        hostdiff_elem = document.createElement("hostdiff")
681*4882a593Smuzhiyun         frag.appendChild(hostdiff_elem)
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun         if host_a.state is None or host_b.state is None:
684*4882a593Smuzhiyun             # The host is missing in one scan. Output the whole thing.
685*4882a593Smuzhiyun             if host_a.state is not None:
686*4882a593Smuzhiyun-                a_elem = document.createElement(u"a")
687*4882a593Smuzhiyun+                a_elem = document.createElement("a")
688*4882a593Smuzhiyun                 a_elem.appendChild(host_a.to_dom_fragment(document))
689*4882a593Smuzhiyun                 hostdiff_elem.appendChild(a_elem)
690*4882a593Smuzhiyun             elif host_b.state is not None:
691*4882a593Smuzhiyun-                b_elem = document.createElement(u"b")
692*4882a593Smuzhiyun+                b_elem = document.createElement("b")
693*4882a593Smuzhiyun                 b_elem.appendChild(host_b.to_dom_fragment(document))
694*4882a593Smuzhiyun                 hostdiff_elem.appendChild(b_elem)
695*4882a593Smuzhiyun             return frag
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun-        host_elem = document.createElement(u"host")
698*4882a593Smuzhiyun+        host_elem = document.createElement("host")
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun         # State.
701*4882a593Smuzhiyun         if host_a.state == host_b.state:
702*4882a593Smuzhiyun             if verbose:
703*4882a593Smuzhiyun                 host_elem.appendChild(host_a.state_to_dom_fragment(document))
704*4882a593Smuzhiyun         else:
705*4882a593Smuzhiyun-            a_elem = document.createElement(u"a")
706*4882a593Smuzhiyun+            a_elem = document.createElement("a")
707*4882a593Smuzhiyun             a_elem.appendChild(host_a.state_to_dom_fragment(document))
708*4882a593Smuzhiyun             host_elem.appendChild(a_elem)
709*4882a593Smuzhiyun-            b_elem = document.createElement(u"b")
710*4882a593Smuzhiyun+            b_elem = document.createElement("b")
711*4882a593Smuzhiyun             b_elem.appendChild(host_b.state_to_dom_fragment(document))
712*4882a593Smuzhiyun             host_elem.appendChild(b_elem)
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun@@ -854,31 +851,31 @@ class HostDiff(object):
715*4882a593Smuzhiyun         addrset_b = set(host_b.addresses)
716*4882a593Smuzhiyun         for addr in sorted(addrset_a.intersection(addrset_b)):
717*4882a593Smuzhiyun             host_elem.appendChild(addr.to_dom_fragment(document))
718*4882a593Smuzhiyun-        a_elem = document.createElement(u"a")
719*4882a593Smuzhiyun+        a_elem = document.createElement("a")
720*4882a593Smuzhiyun         for addr in sorted(addrset_a - addrset_b):
721*4882a593Smuzhiyun             a_elem.appendChild(addr.to_dom_fragment(document))
722*4882a593Smuzhiyun         if a_elem.hasChildNodes():
723*4882a593Smuzhiyun             host_elem.appendChild(a_elem)
724*4882a593Smuzhiyun-        b_elem = document.createElement(u"b")
725*4882a593Smuzhiyun+        b_elem = document.createElement("b")
726*4882a593Smuzhiyun         for addr in sorted(addrset_b - addrset_a):
727*4882a593Smuzhiyun             b_elem.appendChild(addr.to_dom_fragment(document))
728*4882a593Smuzhiyun         if b_elem.hasChildNodes():
729*4882a593Smuzhiyun             host_elem.appendChild(b_elem)
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun         # Host names.
732*4882a593Smuzhiyun-        hostnames_elem = document.createElement(u"hostnames")
733*4882a593Smuzhiyun+        hostnames_elem = document.createElement("hostnames")
734*4882a593Smuzhiyun         hostnameset_a = set(host_a.hostnames)
735*4882a593Smuzhiyun         hostnameset_b = set(host_b.hostnames)
736*4882a593Smuzhiyun         for hostname in sorted(hostnameset_a.intersection(hostnameset_b)):
737*4882a593Smuzhiyun             hostnames_elem.appendChild(
738*4882a593Smuzhiyun                     host_a.hostname_to_dom_fragment(document, hostname))
739*4882a593Smuzhiyun-        a_elem = document.createElement(u"a")
740*4882a593Smuzhiyun+        a_elem = document.createElement("a")
741*4882a593Smuzhiyun         for hostname in sorted(hostnameset_a - hostnameset_b):
742*4882a593Smuzhiyun             a_elem.appendChild(
743*4882a593Smuzhiyun                     host_a.hostname_to_dom_fragment(document, hostname))
744*4882a593Smuzhiyun         if a_elem.hasChildNodes():
745*4882a593Smuzhiyun             hostnames_elem.appendChild(a_elem)
746*4882a593Smuzhiyun-        b_elem = document.createElement(u"b")
747*4882a593Smuzhiyun+        b_elem = document.createElement("b")
748*4882a593Smuzhiyun         for hostname in sorted(hostnameset_b - hostnameset_a):
749*4882a593Smuzhiyun             b_elem.appendChild(
750*4882a593Smuzhiyun                     host_b.hostname_to_dom_fragment(document, hostname))
751*4882a593Smuzhiyun@@ -887,15 +884,15 @@ class HostDiff(object):
752*4882a593Smuzhiyun         if hostnames_elem.hasChildNodes():
753*4882a593Smuzhiyun             host_elem.appendChild(hostnames_elem)
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun-        ports_elem = document.createElement(u"ports")
756*4882a593Smuzhiyun+        ports_elem = document.createElement("ports")
757*4882a593Smuzhiyun         # Extraports.
758*4882a593Smuzhiyun         if host_a.extraports == host_b.extraports:
759*4882a593Smuzhiyun             ports_elem.appendChild(host_a.extraports_to_dom_fragment(document))
760*4882a593Smuzhiyun         else:
761*4882a593Smuzhiyun-            a_elem = document.createElement(u"a")
762*4882a593Smuzhiyun+            a_elem = document.createElement("a")
763*4882a593Smuzhiyun             a_elem.appendChild(host_a.extraports_to_dom_fragment(document))
764*4882a593Smuzhiyun             ports_elem.appendChild(a_elem)
765*4882a593Smuzhiyun-            b_elem = document.createElement(u"b")
766*4882a593Smuzhiyun+            b_elem = document.createElement("b")
767*4882a593Smuzhiyun             b_elem.appendChild(host_b.extraports_to_dom_fragment(document))
768*4882a593Smuzhiyun             ports_elem.appendChild(b_elem)
769*4882a593Smuzhiyun         # Port list.
770*4882a593Smuzhiyun@@ -911,18 +908,18 @@ class HostDiff(object):
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun         # OS changes.
773*4882a593Smuzhiyun         if self.os_changed or verbose:
774*4882a593Smuzhiyun-            os_elem = document.createElement(u"os")
775*4882a593Smuzhiyun+            os_elem = document.createElement("os")
776*4882a593Smuzhiyun             # os_diffs is a list of 5-tuples returned by
777*4882a593Smuzhiyun             # difflib.SequenceMatcher.
778*4882a593Smuzhiyun             for op, i1, i2, j1, j2 in self.os_diffs:
779*4882a593Smuzhiyun                 if op == "replace" or op == "delete":
780*4882a593Smuzhiyun-                    a_elem = document.createElement(u"a")
781*4882a593Smuzhiyun+                    a_elem = document.createElement("a")
782*4882a593Smuzhiyun                     for i in range(i1, i2):
783*4882a593Smuzhiyun                         a_elem.appendChild(host_a.os_to_dom_fragment(
784*4882a593Smuzhiyun                             document, host_a.os[i]))
785*4882a593Smuzhiyun                     os_elem.appendChild(a_elem)
786*4882a593Smuzhiyun                 if op == "replace" or op == "insert":
787*4882a593Smuzhiyun-                    b_elem = document.createElement(u"b")
788*4882a593Smuzhiyun+                    b_elem = document.createElement("b")
789*4882a593Smuzhiyun                     for i in range(j1, j2):
790*4882a593Smuzhiyun                         b_elem.appendChild(host_b.os_to_dom_fragment(
791*4882a593Smuzhiyun                             document, host_b.os[i]))
792*4882a593Smuzhiyun@@ -936,7 +933,7 @@ class HostDiff(object):
793*4882a593Smuzhiyun
794*4882a593Smuzhiyun         # Host script changes.
795*4882a593Smuzhiyun         if len(self.script_result_diffs) > 0 or verbose:
796*4882a593Smuzhiyun-            hostscript_elem = document.createElement(u"hostscript")
797*4882a593Smuzhiyun+            hostscript_elem = document.createElement("hostscript")
798*4882a593Smuzhiyun             host_elem.appendChild(script_result_diffs_to_dom_fragment(
799*4882a593Smuzhiyun                 hostscript_elem, host_a.script_results,
800*4882a593Smuzhiyun                 host_b.script_results, self.script_result_diffs,
801*4882a593Smuzhiyun@@ -989,38 +986,38 @@ class PortDiff(object):
802*4882a593Smuzhiyun             self.port_b.service.version_string()]
803*4882a593Smuzhiyun         if a_columns == b_columns:
804*4882a593Smuzhiyun             if verbose or self.script_result_diffs > 0:
805*4882a593Smuzhiyun-                table.append([u" "] + a_columns)
806*4882a593Smuzhiyun+                table.append([" "] + a_columns)
807*4882a593Smuzhiyun         else:
808*4882a593Smuzhiyun             if not host_a.is_extraports(self.port_a.state):
809*4882a593Smuzhiyun-                table.append([u"-"] + a_columns)
810*4882a593Smuzhiyun+                table.append(["-"] + a_columns)
811*4882a593Smuzhiyun             if not host_b.is_extraports(self.port_b.state):
812*4882a593Smuzhiyun-                table.append([u"+"] + b_columns)
813*4882a593Smuzhiyun+                table.append(["+"] + b_columns)
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun         for sr_diff in self.script_result_diffs:
816*4882a593Smuzhiyun             sr_diff.append_to_port_table(table)
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun     def to_dom_fragment(self, document):
819*4882a593Smuzhiyun         frag = document.createDocumentFragment()
820*4882a593Smuzhiyun-        portdiff_elem = document.createElement(u"portdiff")
821*4882a593Smuzhiyun+        portdiff_elem = document.createElement("portdiff")
822*4882a593Smuzhiyun         frag.appendChild(portdiff_elem)
823*4882a593Smuzhiyun         if (self.port_a.spec == self.port_b.spec and
824*4882a593Smuzhiyun                 self.port_a.state == self.port_b.state):
825*4882a593Smuzhiyun-            port_elem = document.createElement(u"port")
826*4882a593Smuzhiyun-            port_elem.setAttribute(u"portid", unicode(self.port_a.spec[0]))
827*4882a593Smuzhiyun-            port_elem.setAttribute(u"protocol", self.port_a.spec[1])
828*4882a593Smuzhiyun+            port_elem = document.createElement("port")
829*4882a593Smuzhiyun+            port_elem.setAttribute("portid", str(self.port_a.spec[0]))
830*4882a593Smuzhiyun+            port_elem.setAttribute("protocol", self.port_a.spec[1])
831*4882a593Smuzhiyun             if self.port_a.state is not None:
832*4882a593Smuzhiyun-                state_elem = document.createElement(u"state")
833*4882a593Smuzhiyun-                state_elem.setAttribute(u"state", self.port_a.state)
834*4882a593Smuzhiyun+                state_elem = document.createElement("state")
835*4882a593Smuzhiyun+                state_elem.setAttribute("state", self.port_a.state)
836*4882a593Smuzhiyun                 port_elem.appendChild(state_elem)
837*4882a593Smuzhiyun             if self.port_a.service == self.port_b.service:
838*4882a593Smuzhiyun                 port_elem.appendChild(
839*4882a593Smuzhiyun                         self.port_a.service.to_dom_fragment(document))
840*4882a593Smuzhiyun             else:
841*4882a593Smuzhiyun-                a_elem = document.createElement(u"a")
842*4882a593Smuzhiyun+                a_elem = document.createElement("a")
843*4882a593Smuzhiyun                 a_elem.appendChild(
844*4882a593Smuzhiyun                         self.port_a.service.to_dom_fragment(document))
845*4882a593Smuzhiyun                 port_elem.appendChild(a_elem)
846*4882a593Smuzhiyun-                b_elem = document.createElement(u"b")
847*4882a593Smuzhiyun+                b_elem = document.createElement("b")
848*4882a593Smuzhiyun                 b_elem.appendChild(
849*4882a593Smuzhiyun                         self.port_b.service.to_dom_fragment(document))
850*4882a593Smuzhiyun                 port_elem.appendChild(b_elem)
851*4882a593Smuzhiyun@@ -1028,10 +1025,10 @@ class PortDiff(object):
852*4882a593Smuzhiyun                 port_elem.appendChild(sr_diff.to_dom_fragment(document))
853*4882a593Smuzhiyun             portdiff_elem.appendChild(port_elem)
854*4882a593Smuzhiyun         else:
855*4882a593Smuzhiyun-            a_elem = document.createElement(u"a")
856*4882a593Smuzhiyun+            a_elem = document.createElement("a")
857*4882a593Smuzhiyun             a_elem.appendChild(self.port_a.to_dom_fragment(document))
858*4882a593Smuzhiyun             portdiff_elem.appendChild(a_elem)
859*4882a593Smuzhiyun-            b_elem = document.createElement(u"b")
860*4882a593Smuzhiyun+            b_elem = document.createElement("b")
861*4882a593Smuzhiyun             b_elem.appendChild(self.port_b.to_dom_fragment(document))
862*4882a593Smuzhiyun             portdiff_elem.appendChild(b_elem)
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun@@ -1086,13 +1083,13 @@ class ScriptResultDiff(object):
865*4882a593Smuzhiyun             for op, i1, i2, j1, j2 in diffs.get_opcodes():
866*4882a593Smuzhiyun                 if op == "replace" or op == "delete":
867*4882a593Smuzhiyun                     for k in range(i1, i2):
868*4882a593Smuzhiyun-                        table.append_raw(u"-" + a_lines[k])
869*4882a593Smuzhiyun+                        table.append_raw("-" + a_lines[k])
870*4882a593Smuzhiyun                 if op == "replace" or op == "insert":
871*4882a593Smuzhiyun                     for k in range(j1, j2):
872*4882a593Smuzhiyun-                        table.append_raw(u"+" + b_lines[k])
873*4882a593Smuzhiyun+                        table.append_raw("+" + b_lines[k])
874*4882a593Smuzhiyun                 if op == "equal":
875*4882a593Smuzhiyun                     for k in range(i1, i2):
876*4882a593Smuzhiyun-                        table.append_raw(u" " + a_lines[k])
877*4882a593Smuzhiyun+                        table.append_raw(" " + a_lines[k])
878*4882a593Smuzhiyun
879*4882a593Smuzhiyun     def to_dom_fragment(self, document):
880*4882a593Smuzhiyun         frag = document.createDocumentFragment()
881*4882a593Smuzhiyun@@ -1102,11 +1099,11 @@ class ScriptResultDiff(object):
882*4882a593Smuzhiyun             frag.appendChild(self.sr_a.to_dom_fragment(document))
883*4882a593Smuzhiyun         else:
884*4882a593Smuzhiyun             if self.sr_a is not None:
885*4882a593Smuzhiyun-                a_elem = document.createElement(u"a")
886*4882a593Smuzhiyun+                a_elem = document.createElement("a")
887*4882a593Smuzhiyun                 a_elem.appendChild(self.sr_a.to_dom_fragment(document))
888*4882a593Smuzhiyun                 frag.appendChild(a_elem)
889*4882a593Smuzhiyun             if self.sr_b is not None:
890*4882a593Smuzhiyun-                b_elem = document.createElement(u"b")
891*4882a593Smuzhiyun+                b_elem = document.createElement("b")
892*4882a593Smuzhiyun                 b_elem.appendChild(self.sr_b.to_dom_fragment(document))
893*4882a593Smuzhiyun                 frag.appendChild(b_elem)
894*4882a593Smuzhiyun         return frag
895*4882a593Smuzhiyun@@ -1120,7 +1117,7 @@ class Table(object):
896*4882a593Smuzhiyun         copied to the output."""
897*4882a593Smuzhiyun         self.widths = []
898*4882a593Smuzhiyun         self.rows = []
899*4882a593Smuzhiyun-        self.prefix = u""
900*4882a593Smuzhiyun+        self.prefix = ""
901*4882a593Smuzhiyun         self.padding = []
902*4882a593Smuzhiyun         j = 0
903*4882a593Smuzhiyun         while j < len(template) and template[j] != "*":
904*4882a593Smuzhiyun@@ -1145,7 +1142,7 @@ class Table(object):
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun         for i in range(len(row)):
907*4882a593Smuzhiyun             if row[i] is None:
908*4882a593Smuzhiyun-                s = u""
909*4882a593Smuzhiyun+                s = ""
910*4882a593Smuzhiyun             else:
911*4882a593Smuzhiyun                 s = str(row[i])
912*4882a593Smuzhiyun             if i == len(self.widths):
913*4882a593Smuzhiyun@@ -1167,7 +1164,7 @@ class Table(object):
914*4882a593Smuzhiyun         for row in self.rows:
915*4882a593Smuzhiyun             parts = [self.prefix]
916*4882a593Smuzhiyun             i = 0
917*4882a593Smuzhiyun-            if isinstance(row, basestring):
918*4882a593Smuzhiyun+            if isinstance(row, str):
919*4882a593Smuzhiyun                 # A raw string.
920*4882a593Smuzhiyun                 lines.append(row)
921*4882a593Smuzhiyun             else:
922*4882a593Smuzhiyun@@ -1176,13 +1173,13 @@ class Table(object):
923*4882a593Smuzhiyun                     if i < len(self.padding):
924*4882a593Smuzhiyun                         parts.append(self.padding[i])
925*4882a593Smuzhiyun                     i += 1
926*4882a593Smuzhiyun-                lines.append(u"".join(parts).rstrip())
927*4882a593Smuzhiyun-        return u"\n".join(lines)
928*4882a593Smuzhiyun+                lines.append("".join(parts).rstrip())
929*4882a593Smuzhiyun+        return "\n".join(lines)
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun def warn(str):
933*4882a593Smuzhiyun     """Print a warning to stderr."""
934*4882a593Smuzhiyun-    print >> sys.stderr, str
935*4882a593Smuzhiyun+    print(str, file=sys.stderr)
936*4882a593Smuzhiyun
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun class NmapContentHandler(xml.sax.handler.ContentHandler):
939*4882a593Smuzhiyun@@ -1200,22 +1197,22 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
940*4882a593Smuzhiyun         self.current_port = None
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun         self._start_elem_handlers = {
943*4882a593Smuzhiyun-            u"nmaprun": self._start_nmaprun,
944*4882a593Smuzhiyun-            u"host": self._start_host,
945*4882a593Smuzhiyun-            u"status": self._start_status,
946*4882a593Smuzhiyun-            u"address": self._start_address,
947*4882a593Smuzhiyun-            u"hostname": self._start_hostname,
948*4882a593Smuzhiyun-            u"extraports": self._start_extraports,
949*4882a593Smuzhiyun-            u"port": self._start_port,
950*4882a593Smuzhiyun-            u"state": self._start_state,
951*4882a593Smuzhiyun-            u"service": self._start_service,
952*4882a593Smuzhiyun-            u"script": self._start_script,
953*4882a593Smuzhiyun-            u"osmatch": self._start_osmatch,
954*4882a593Smuzhiyun-            u"finished": self._start_finished,
955*4882a593Smuzhiyun+            "nmaprun": self._start_nmaprun,
956*4882a593Smuzhiyun+            "host": self._start_host,
957*4882a593Smuzhiyun+            "status": self._start_status,
958*4882a593Smuzhiyun+            "address": self._start_address,
959*4882a593Smuzhiyun+            "hostname": self._start_hostname,
960*4882a593Smuzhiyun+            "extraports": self._start_extraports,
961*4882a593Smuzhiyun+            "port": self._start_port,
962*4882a593Smuzhiyun+            "state": self._start_state,
963*4882a593Smuzhiyun+            "service": self._start_service,
964*4882a593Smuzhiyun+            "script": self._start_script,
965*4882a593Smuzhiyun+            "osmatch": self._start_osmatch,
966*4882a593Smuzhiyun+            "finished": self._start_finished,
967*4882a593Smuzhiyun         }
968*4882a593Smuzhiyun         self._end_elem_handlers = {
969*4882a593Smuzhiyun-            u'host': self._end_host,
970*4882a593Smuzhiyun-            u'port': self._end_port,
971*4882a593Smuzhiyun+            'host': self._end_host,
972*4882a593Smuzhiyun+            'port': self._end_port,
973*4882a593Smuzhiyun         }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun     def parent_element(self):
976*4882a593Smuzhiyun@@ -1245,68 +1242,68 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
977*4882a593Smuzhiyun     def _start_nmaprun(self, name, attrs):
978*4882a593Smuzhiyun         assert self.parent_element() is None
979*4882a593Smuzhiyun         if "start" in attrs:
980*4882a593Smuzhiyun-            start_timestamp = int(attrs.get(u"start"))
981*4882a593Smuzhiyun+            start_timestamp = int(attrs.get("start"))
982*4882a593Smuzhiyun             self.scan.start_date = datetime.datetime.fromtimestamp(
983*4882a593Smuzhiyun                     start_timestamp)
984*4882a593Smuzhiyun-        self.scan.scanner = attrs.get(u"scanner")
985*4882a593Smuzhiyun-        self.scan.args = attrs.get(u"args")
986*4882a593Smuzhiyun-        self.scan.version = attrs.get(u"version")
987*4882a593Smuzhiyun+        self.scan.scanner = attrs.get("scanner")
988*4882a593Smuzhiyun+        self.scan.args = attrs.get("args")
989*4882a593Smuzhiyun+        self.scan.version = attrs.get("version")
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun     def _start_host(self, name, attrs):
992*4882a593Smuzhiyun-        assert self.parent_element() == u"nmaprun"
993*4882a593Smuzhiyun+        assert self.parent_element() == "nmaprun"
994*4882a593Smuzhiyun         self.current_host = Host()
995*4882a593Smuzhiyun         self.scan.hosts.append(self.current_host)
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun     def _start_status(self, name, attrs):
998*4882a593Smuzhiyun-        assert self.parent_element() == u"host"
999*4882a593Smuzhiyun+        assert self.parent_element() == "host"
1000*4882a593Smuzhiyun         assert self.current_host is not None
1001*4882a593Smuzhiyun-        state = attrs.get(u"state")
1002*4882a593Smuzhiyun+        state = attrs.get("state")
1003*4882a593Smuzhiyun         if state is None:
1004*4882a593Smuzhiyun             warn(u'%s element of host %s is missing the "state" attribute; '
1005*4882a593Smuzhiyun-                    'assuming \unknown\.' % (
1006*4882a593Smuzhiyun+                    r'assuming \unknown\.' % (
1007*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1008*4882a593Smuzhiyun             return
1009*4882a593Smuzhiyun         self.current_host.state = state
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun     def _start_address(self, name, attrs):
1012*4882a593Smuzhiyun-        assert self.parent_element() == u"host"
1013*4882a593Smuzhiyun+        assert self.parent_element() == "host"
1014*4882a593Smuzhiyun         assert self.current_host is not None
1015*4882a593Smuzhiyun-        addr = attrs.get(u"addr")
1016*4882a593Smuzhiyun+        addr = attrs.get("addr")
1017*4882a593Smuzhiyun         if addr is None:
1018*4882a593Smuzhiyun-            warn(u'%s element of host %s is missing the "addr" '
1019*4882a593Smuzhiyun+            warn('%s element of host %s is missing the "addr" '
1020*4882a593Smuzhiyun                     'attribute; skipping.' % (
1021*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1022*4882a593Smuzhiyun             return
1023*4882a593Smuzhiyun-        addrtype = attrs.get(u"addrtype", u"ipv4")
1024*4882a593Smuzhiyun+        addrtype = attrs.get("addrtype", "ipv4")
1025*4882a593Smuzhiyun         self.current_host.add_address(Address.new(addrtype, addr))
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun     def _start_hostname(self, name, attrs):
1028*4882a593Smuzhiyun-        assert self.parent_element() == u"hostnames"
1029*4882a593Smuzhiyun+        assert self.parent_element() == "hostnames"
1030*4882a593Smuzhiyun         assert self.current_host is not None
1031*4882a593Smuzhiyun-        hostname = attrs.get(u"name")
1032*4882a593Smuzhiyun+        hostname = attrs.get("name")
1033*4882a593Smuzhiyun         if hostname is None:
1034*4882a593Smuzhiyun-            warn(u'%s element of host %s is missing the "name" '
1035*4882a593Smuzhiyun+            warn('%s element of host %s is missing the "name" '
1036*4882a593Smuzhiyun                     'attribute; skipping.' % (
1037*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1038*4882a593Smuzhiyun             return
1039*4882a593Smuzhiyun         self.current_host.add_hostname(hostname)
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun     def _start_extraports(self, name, attrs):
1042*4882a593Smuzhiyun-        assert self.parent_element() == u"ports"
1043*4882a593Smuzhiyun+        assert self.parent_element() == "ports"
1044*4882a593Smuzhiyun         assert self.current_host is not None
1045*4882a593Smuzhiyun-        state = attrs.get(u"state")
1046*4882a593Smuzhiyun+        state = attrs.get("state")
1047*4882a593Smuzhiyun         if state is None:
1048*4882a593Smuzhiyun-            warn(u'%s element of host %s is missing the "state" '
1049*4882a593Smuzhiyun+            warn('%s element of host %s is missing the "state" '
1050*4882a593Smuzhiyun                     'attribute; assuming "unknown".' % (
1051*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1052*4882a593Smuzhiyun             state = None
1053*4882a593Smuzhiyun         if state in self.current_host.extraports:
1054*4882a593Smuzhiyun-            warn(u'Duplicate extraports state "%s" in host %s.' % (
1055*4882a593Smuzhiyun+            warn('Duplicate extraports state "%s" in host %s.' % (
1056*4882a593Smuzhiyun                 state, self.current_host.format_name()))
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun-        count = attrs.get(u"count")
1059*4882a593Smuzhiyun+        count = attrs.get("count")
1060*4882a593Smuzhiyun         if count is None:
1061*4882a593Smuzhiyun-            warn(u'%s element of host %s is missing the "count" '
1062*4882a593Smuzhiyun+            warn('%s element of host %s is missing the "count" '
1063*4882a593Smuzhiyun                     'attribute; assuming 0.' % (
1064*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1065*4882a593Smuzhiyun             count = 0
1066*4882a593Smuzhiyun@@ -1314,99 +1311,99 @@ class NmapContentHandler(xml.sax.handler.ContentHandler):
1067*4882a593Smuzhiyun             try:
1068*4882a593Smuzhiyun                 count = int(count)
1069*4882a593Smuzhiyun             except ValueError:
1070*4882a593Smuzhiyun-                warn(u"Can't convert extraports count \"%s\" "
1071*4882a593Smuzhiyun+                warn("Can't convert extraports count \"%s\" "
1072*4882a593Smuzhiyun                         "to an integer in host %s; assuming 0." % (
1073*4882a593Smuzhiyun-                            attrs[u"count"], self.current_host.format_name()))
1074*4882a593Smuzhiyun+                            attrs["count"], self.current_host.format_name()))
1075*4882a593Smuzhiyun                 count = 0
1076*4882a593Smuzhiyun         self.current_host.extraports[state] = count
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun     def _start_port(self, name, attrs):
1079*4882a593Smuzhiyun-        assert self.parent_element() == u"ports"
1080*4882a593Smuzhiyun+        assert self.parent_element() == "ports"
1081*4882a593Smuzhiyun         assert self.current_host is not None
1082*4882a593Smuzhiyun-        portid_str = attrs.get(u"portid")
1083*4882a593Smuzhiyun+        portid_str = attrs.get("portid")
1084*4882a593Smuzhiyun         if portid_str is None:
1085*4882a593Smuzhiyun-            warn(u'%s element of host %s missing the "portid" '
1086*4882a593Smuzhiyun+            warn('%s element of host %s missing the "portid" '
1087*4882a593Smuzhiyun                     'attribute; skipping.' % (
1088*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1089*4882a593Smuzhiyun             return
1090*4882a593Smuzhiyun         try:
1091*4882a593Smuzhiyun             portid = int(portid_str)
1092*4882a593Smuzhiyun         except ValueError:
1093*4882a593Smuzhiyun-            warn(u"Can't convert portid \"%s\" to an integer "
1094*4882a593Smuzhiyun+            warn("Can't convert portid \"%s\" to an integer "
1095*4882a593Smuzhiyun                     "in host %s; skipping port." % (
1096*4882a593Smuzhiyun                         portid_str, self.current_host.format_name()))
1097*4882a593Smuzhiyun             return
1098*4882a593Smuzhiyun-        protocol = attrs.get(u"protocol")
1099*4882a593Smuzhiyun+        protocol = attrs.get("protocol")
1100*4882a593Smuzhiyun         if protocol is None:
1101*4882a593Smuzhiyun-            warn(u'%s element of host %s missing the "protocol" '
1102*4882a593Smuzhiyun+            warn('%s element of host %s missing the "protocol" '
1103*4882a593Smuzhiyun                     'attribute; skipping.' % (
1104*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1105*4882a593Smuzhiyun             return
1106*4882a593Smuzhiyun         self.current_port = Port((portid, protocol))
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun     def _start_state(self, name, attrs):
1109*4882a593Smuzhiyun-        assert self.parent_element() == u"port"
1110*4882a593Smuzhiyun+        assert self.parent_element() == "port"
1111*4882a593Smuzhiyun         assert self.current_host is not None
1112*4882a593Smuzhiyun         if self.current_port is None:
1113*4882a593Smuzhiyun             return
1114*4882a593Smuzhiyun         if "state" not in attrs:
1115*4882a593Smuzhiyun-            warn(u'%s element of port %s is missing the "state" '
1116*4882a593Smuzhiyun+            warn('%s element of port %s is missing the "state" '
1117*4882a593Smuzhiyun                     'attribute; assuming "unknown".' % (
1118*4882a593Smuzhiyun                         name, self.current_port.spec_string()))
1119*4882a593Smuzhiyun             return
1120*4882a593Smuzhiyun-        self.current_port.state = attrs[u"state"]
1121*4882a593Smuzhiyun+        self.current_port.state = attrs["state"]
1122*4882a593Smuzhiyun         self.current_host.add_port(self.current_port)
1123*4882a593Smuzhiyun
1124*4882a593Smuzhiyun     def _start_service(self, name, attrs):
1125*4882a593Smuzhiyun-        assert self.parent_element() == u"port"
1126*4882a593Smuzhiyun+        assert self.parent_element() == "port"
1127*4882a593Smuzhiyun         assert self.current_host is not None
1128*4882a593Smuzhiyun         if self.current_port is None:
1129*4882a593Smuzhiyun             return
1130*4882a593Smuzhiyun-        self.current_port.service.name = attrs.get(u"name")
1131*4882a593Smuzhiyun-        self.current_port.service.product = attrs.get(u"product")
1132*4882a593Smuzhiyun-        self.current_port.service.version = attrs.get(u"version")
1133*4882a593Smuzhiyun-        self.current_port.service.extrainfo = attrs.get(u"extrainfo")
1134*4882a593Smuzhiyun-        self.current_port.service.tunnel = attrs.get(u"tunnel")
1135*4882a593Smuzhiyun+        self.current_port.service.name = attrs.get("name")
1136*4882a593Smuzhiyun+        self.current_port.service.product = attrs.get("product")
1137*4882a593Smuzhiyun+        self.current_port.service.version = attrs.get("version")
1138*4882a593Smuzhiyun+        self.current_port.service.extrainfo = attrs.get("extrainfo")
1139*4882a593Smuzhiyun+        self.current_port.service.tunnel = attrs.get("tunnel")
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun     def _start_script(self, name, attrs):
1142*4882a593Smuzhiyun         result = ScriptResult()
1143*4882a593Smuzhiyun-        result.id = attrs.get(u"id")
1144*4882a593Smuzhiyun+        result.id = attrs.get("id")
1145*4882a593Smuzhiyun         if result.id is None:
1146*4882a593Smuzhiyun-            warn(u'%s element missing the "id" attribute; skipping.' % name)
1147*4882a593Smuzhiyun+            warn('%s element missing the "id" attribute; skipping.' % name)
1148*4882a593Smuzhiyun             return
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun-        result.output = attrs.get(u"output")
1151*4882a593Smuzhiyun+        result.output = attrs.get("output")
1152*4882a593Smuzhiyun         if result.output is None:
1153*4882a593Smuzhiyun-            warn(u'%s element missing the "output" attribute; skipping.'
1154*4882a593Smuzhiyun+            warn('%s element missing the "output" attribute; skipping.'
1155*4882a593Smuzhiyun                     % name)
1156*4882a593Smuzhiyun             return
1157*4882a593Smuzhiyun-        if self.parent_element() == u"prescript":
1158*4882a593Smuzhiyun+        if self.parent_element() == "prescript":
1159*4882a593Smuzhiyun             self.scan.pre_script_results.append(result)
1160*4882a593Smuzhiyun-        elif self.parent_element() == u"postscript":
1161*4882a593Smuzhiyun+        elif self.parent_element() == "postscript":
1162*4882a593Smuzhiyun             self.scan.post_script_results.append(result)
1163*4882a593Smuzhiyun-        elif self.parent_element() == u"hostscript":
1164*4882a593Smuzhiyun+        elif self.parent_element() == "hostscript":
1165*4882a593Smuzhiyun             self.current_host.script_results.append(result)
1166*4882a593Smuzhiyun-        elif self.parent_element() == u"port":
1167*4882a593Smuzhiyun+        elif self.parent_element() == "port":
1168*4882a593Smuzhiyun             self.current_port.script_results.append(result)
1169*4882a593Smuzhiyun         else:
1170*4882a593Smuzhiyun-            warn(u"%s element not inside prescript, postscript, hostscript, "
1171*4882a593Smuzhiyun+            warn("%s element not inside prescript, postscript, hostscript, "
1172*4882a593Smuzhiyun                     "or port element; ignoring." % name)
1173*4882a593Smuzhiyun             return
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun     def _start_osmatch(self, name, attrs):
1176*4882a593Smuzhiyun-        assert self.parent_element() == u"os"
1177*4882a593Smuzhiyun+        assert self.parent_element() == "os"
1178*4882a593Smuzhiyun         assert self.current_host is not None
1179*4882a593Smuzhiyun         if "name" not in attrs:
1180*4882a593Smuzhiyun-            warn(u'%s element of host %s is missing the "name" '
1181*4882a593Smuzhiyun+            warn('%s element of host %s is missing the "name" '
1182*4882a593Smuzhiyun                     'attribute; skipping.' % (
1183*4882a593Smuzhiyun                         name, self.current_host.format_name()))
1184*4882a593Smuzhiyun             return
1185*4882a593Smuzhiyun-        self.current_host.os.append(attrs[u"name"])
1186*4882a593Smuzhiyun+        self.current_host.os.append(attrs["name"])
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun     def _start_finished(self, name, attrs):
1189*4882a593Smuzhiyun-        assert self.parent_element() == u"runstats"
1190*4882a593Smuzhiyun+        assert self.parent_element() == "runstats"
1191*4882a593Smuzhiyun         if "time" in attrs:
1192*4882a593Smuzhiyun-            end_timestamp = int(attrs.get(u"time"))
1193*4882a593Smuzhiyun+            end_timestamp = int(attrs.get("time"))
1194*4882a593Smuzhiyun             self.scan.end_date = datetime.datetime.fromtimestamp(end_timestamp)
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun     def _end_host(self, name):
1197*4882a593Smuzhiyun@@ -1425,23 +1422,23 @@ class XMLWriter (xml.sax.saxutils.XMLGenerator):
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun     def frag(self, frag):
1200*4882a593Smuzhiyun         for node in frag.childNodes:
1201*4882a593Smuzhiyun-            node.writexml(self.f, newl=u"\n")
1202*4882a593Smuzhiyun+            node.writexml(self.f, newl="\n")
1203*4882a593Smuzhiyun
1204*4882a593Smuzhiyun     def frag_a(self, frag):
1205*4882a593Smuzhiyun-        self.startElement(u"a", {})
1206*4882a593Smuzhiyun+        self.startElement("a", {})
1207*4882a593Smuzhiyun         for node in frag.childNodes:
1208*4882a593Smuzhiyun-            node.writexml(self.f, newl=u"\n")
1209*4882a593Smuzhiyun-        self.endElement(u"a")
1210*4882a593Smuzhiyun+            node.writexml(self.f, newl="\n")
1211*4882a593Smuzhiyun+        self.endElement("a")
1212*4882a593Smuzhiyun
1213*4882a593Smuzhiyun     def frag_b(self, frag):
1214*4882a593Smuzhiyun-        self.startElement(u"b", {})
1215*4882a593Smuzhiyun+        self.startElement("b", {})
1216*4882a593Smuzhiyun         for node in frag.childNodes:
1217*4882a593Smuzhiyun-            node.writexml(self.f, newl=u"\n")
1218*4882a593Smuzhiyun-        self.endElement(u"b")
1219*4882a593Smuzhiyun+            node.writexml(self.f, newl="\n")
1220*4882a593Smuzhiyun+        self.endElement("b")
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun def usage():
1224*4882a593Smuzhiyun-    print u"""\
1225*4882a593Smuzhiyun+    print("""\
1226*4882a593Smuzhiyun Usage: %s [option] FILE1 FILE2
1227*4882a593Smuzhiyun Compare two Nmap XML files and display a list of their differences.
1228*4882a593Smuzhiyun Differences include host state changes, port state changes, and changes to
1229*4882a593Smuzhiyun@@ -1451,7 +1448,7 @@ service and OS detection.
1230*4882a593Smuzhiyun   -v, --verbose  also show hosts and ports that haven't changed.
1231*4882a593Smuzhiyun   --text         display output in text format (default)
1232*4882a593Smuzhiyun   --xml          display output in XML format\
1233*4882a593Smuzhiyun-""" % sys.argv[0]
1234*4882a593Smuzhiyun+""" % sys.argv[0])
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun EXIT_EQUAL = 0
1237*4882a593Smuzhiyun EXIT_DIFFERENT = 1
1238*4882a593Smuzhiyun@@ -1459,8 +1456,8 @@ EXIT_ERROR = 2
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun def usage_error(msg):
1242*4882a593Smuzhiyun-    print >> sys.stderr, u"%s: %s" % (sys.argv[0], msg)
1243*4882a593Smuzhiyun-    print >> sys.stderr, u"Try '%s -h' for help." % sys.argv[0]
1244*4882a593Smuzhiyun+    print("%s: %s" % (sys.argv[0], msg), file=sys.stderr)
1245*4882a593Smuzhiyun+    print("Try '%s -h' for help." % sys.argv[0], file=sys.stderr)
1246*4882a593Smuzhiyun     sys.exit(EXIT_ERROR)
1247*4882a593Smuzhiyun
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun@@ -1471,7 +1468,7 @@ def main():
1250*4882a593Smuzhiyun     try:
1251*4882a593Smuzhiyun         opts, input_filenames = getopt.gnu_getopt(
1252*4882a593Smuzhiyun                 sys.argv[1:], "hv", ["help", "text", "verbose", "xml"])
1253*4882a593Smuzhiyun-    except getopt.GetoptError, e:
1254*4882a593Smuzhiyun+    except getopt.GetoptError as e:
1255*4882a593Smuzhiyun         usage_error(e.msg)
1256*4882a593Smuzhiyun     for o, a in opts:
1257*4882a593Smuzhiyun         if o == "-h" or o == "--help":
1258*4882a593Smuzhiyun@@ -1481,15 +1478,15 @@ def main():
1259*4882a593Smuzhiyun             verbose = True
1260*4882a593Smuzhiyun         elif o == "--text":
1261*4882a593Smuzhiyun             if output_format is not None and output_format != "text":
1262*4882a593Smuzhiyun-                usage_error(u"contradictory output format options.")
1263*4882a593Smuzhiyun+                usage_error("contradictory output format options.")
1264*4882a593Smuzhiyun             output_format = "text"
1265*4882a593Smuzhiyun         elif o == "--xml":
1266*4882a593Smuzhiyun             if output_format is not None and output_format != "xml":
1267*4882a593Smuzhiyun-                usage_error(u"contradictory output format options.")
1268*4882a593Smuzhiyun+                usage_error("contradictory output format options.")
1269*4882a593Smuzhiyun             output_format = "xml"
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun     if len(input_filenames) != 2:
1272*4882a593Smuzhiyun-        usage_error(u"need exactly two input filenames.")
1273*4882a593Smuzhiyun+        usage_error("need exactly two input filenames.")
1274*4882a593Smuzhiyun
1275*4882a593Smuzhiyun     if output_format is None:
1276*4882a593Smuzhiyun         output_format = "text"
1277*4882a593Smuzhiyun@@ -1502,8 +1499,8 @@ def main():
1278*4882a593Smuzhiyun         scan_a.load_from_file(filename_a)
1279*4882a593Smuzhiyun         scan_b = Scan()
1280*4882a593Smuzhiyun         scan_b.load_from_file(filename_b)
1281*4882a593Smuzhiyun-    except IOError, e:
1282*4882a593Smuzhiyun-        print >> sys.stderr, u"Can't open file: %s" % str(e)
1283*4882a593Smuzhiyun+    except IOError as e:
1284*4882a593Smuzhiyun+        print("Can't open file: %s" % str(e), file=sys.stderr)
1285*4882a593Smuzhiyun         sys.exit(EXIT_ERROR)
1286*4882a593Smuzhiyun
1287*4882a593Smuzhiyun     if output_format == "text":
1288*4882a593Smuzhiyundiff --git a/ndiff/ndifftest.py b/ndiff/ndifftest.py
1289*4882a593Smuzhiyunindex 2fa4ae0..27fc525 100755
1290*4882a593Smuzhiyun--- a/ndiff/ndifftest.py
1291*4882a593Smuzhiyun+++ b/ndiff/ndifftest.py
1292*4882a593Smuzhiyun@@ -1,4 +1,4 @@
1293*4882a593Smuzhiyun-#!/usr/bin/env python
1294*4882a593Smuzhiyun+#!/usr/bin/env python3
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun # Unit tests for Ndiff.
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun@@ -22,7 +22,7 @@ for x in dir(ndiff):
1299*4882a593Smuzhiyun sys.dont_write_bytecode = dont_write_bytecode
1300*4882a593Smuzhiyun del dont_write_bytecode
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun-import StringIO
1303*4882a593Smuzhiyun+import io
1304*4882a593Smuzhiyun
1305*4882a593Smuzhiyun
1306*4882a593Smuzhiyun class scan_test(unittest.TestCase):
1307*4882a593Smuzhiyun@@ -52,7 +52,7 @@ class scan_test(unittest.TestCase):
1308*4882a593Smuzhiyun         scan.load_from_file("test-scans/single.xml")
1309*4882a593Smuzhiyun         host = scan.hosts[0]
1310*4882a593Smuzhiyun         self.assertEqual(len(host.ports), 5)
1311*4882a593Smuzhiyun-        self.assertEqual(host.extraports.items(), [("filtered", 95)])
1312*4882a593Smuzhiyun+        self.assertEqual(list(host.extraports.items()), [("filtered", 95)])
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun     def test_extraports_multi(self):
1315*4882a593Smuzhiyun         """Test that the correct number of known ports is returned when there
1316*4882a593Smuzhiyun@@ -68,9 +68,9 @@ class scan_test(unittest.TestCase):
1317*4882a593Smuzhiyun         """Test that nmaprun information is recorded."""
1318*4882a593Smuzhiyun         scan = Scan()
1319*4882a593Smuzhiyun         scan.load_from_file("test-scans/empty.xml")
1320*4882a593Smuzhiyun-        self.assertEqual(scan.scanner, u"nmap")
1321*4882a593Smuzhiyun-        self.assertEqual(scan.version, u"4.90RC2")
1322*4882a593Smuzhiyun-        self.assertEqual(scan.args, u"nmap -oX empty.xml -p 1-100")
1323*4882a593Smuzhiyun+        self.assertEqual(scan.scanner, "nmap")
1324*4882a593Smuzhiyun+        self.assertEqual(scan.version, "4.90RC2")
1325*4882a593Smuzhiyun+        self.assertEqual(scan.args, "nmap -oX empty.xml -p 1-100")
1326*4882a593Smuzhiyun
1327*4882a593Smuzhiyun     def test_addresses(self):
1328*4882a593Smuzhiyun         """Test that addresses are recorded."""
1329*4882a593Smuzhiyun@@ -84,7 +84,7 @@ class scan_test(unittest.TestCase):
1330*4882a593Smuzhiyun         scan = Scan()
1331*4882a593Smuzhiyun         scan.load_from_file("test-scans/simple.xml")
1332*4882a593Smuzhiyun         host = scan.hosts[0]
1333*4882a593Smuzhiyun-        self.assertEqual(host.hostnames, [u"scanme.nmap.org"])
1334*4882a593Smuzhiyun+        self.assertEqual(host.hostnames, ["scanme.nmap.org"])
1335*4882a593Smuzhiyun
1336*4882a593Smuzhiyun     def test_os(self):
1337*4882a593Smuzhiyun         """Test that OS information is recorded."""
1338*4882a593Smuzhiyun@@ -99,7 +99,7 @@ class scan_test(unittest.TestCase):
1339*4882a593Smuzhiyun         scan.load_from_file("test-scans/complex.xml")
1340*4882a593Smuzhiyun         host = scan.hosts[0]
1341*4882a593Smuzhiyun         self.assertTrue(len(host.script_results) > 0)
1342*4882a593Smuzhiyun-        self.assertTrue(len(host.ports[(22, u"tcp")].script_results) > 0)
1343*4882a593Smuzhiyun+        self.assertTrue(len(host.ports[(22, "tcp")].script_results) > 0)
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun # This test is commented out because Nmap XML doesn't store any information
1346*4882a593Smuzhiyun # about down hosts, not even the fact that they are down. Recovering the list
1347*4882a593Smuzhiyun@@ -128,16 +128,16 @@ class host_test(unittest.TestCase):
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun     def test_format_name(self):
1350*4882a593Smuzhiyun         h = Host()
1351*4882a593Smuzhiyun-        self.assertTrue(isinstance(h.format_name(), basestring))
1352*4882a593Smuzhiyun-        h.add_address(IPv4Address(u"127.0.0.1"))
1353*4882a593Smuzhiyun-        self.assertTrue(u"127.0.0.1" in h.format_name())
1354*4882a593Smuzhiyun+        self.assertTrue(isinstance(h.format_name(), str))
1355*4882a593Smuzhiyun+        h.add_address(IPv4Address("127.0.0.1"))
1356*4882a593Smuzhiyun+        self.assertTrue("127.0.0.1" in h.format_name())
1357*4882a593Smuzhiyun         h.add_address(IPv6Address("::1"))
1358*4882a593Smuzhiyun-        self.assertTrue(u"127.0.0.1" in h.format_name())
1359*4882a593Smuzhiyun-        self.assertTrue(u"::1" in h.format_name())
1360*4882a593Smuzhiyun-        h.add_hostname(u"localhost")
1361*4882a593Smuzhiyun-        self.assertTrue(u"127.0.0.1" in h.format_name())
1362*4882a593Smuzhiyun-        self.assertTrue(u"::1" in h.format_name())
1363*4882a593Smuzhiyun-        self.assertTrue(u"localhost" in h.format_name())
1364*4882a593Smuzhiyun+        self.assertTrue("127.0.0.1" in h.format_name())
1365*4882a593Smuzhiyun+        self.assertTrue("::1" in h.format_name())
1366*4882a593Smuzhiyun+        h.add_hostname("localhost")
1367*4882a593Smuzhiyun+        self.assertTrue("127.0.0.1" in h.format_name())
1368*4882a593Smuzhiyun+        self.assertTrue("::1" in h.format_name())
1369*4882a593Smuzhiyun+        self.assertTrue("localhost" in h.format_name())
1370*4882a593Smuzhiyun
1371*4882a593Smuzhiyun     def test_empty_get_port(self):
1372*4882a593Smuzhiyun         h = Host()
1373*4882a593Smuzhiyun@@ -197,8 +197,8 @@ class host_test(unittest.TestCase):
1374*4882a593Smuzhiyun         h = s.hosts[0]
1375*4882a593Smuzhiyun         self.assertEqual(len(h.ports), 5)
1376*4882a593Smuzhiyun         self.assertEqual(len(h.extraports), 1)
1377*4882a593Smuzhiyun-        self.assertEqual(h.extraports.keys()[0], u"filtered")
1378*4882a593Smuzhiyun-        self.assertEqual(h.extraports.values()[0], 95)
1379*4882a593Smuzhiyun+        self.assertEqual(list(h.extraports.keys())[0], "filtered")
1380*4882a593Smuzhiyun+        self.assertEqual(list(h.extraports.values())[0], 95)
1381*4882a593Smuzhiyun         self.assertEqual(h.state, "up")
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun@@ -241,13 +241,13 @@ class port_test(unittest.TestCase):
1385*4882a593Smuzhiyun     """Test the Port class."""
1386*4882a593Smuzhiyun     def test_spec_string(self):
1387*4882a593Smuzhiyun         p = Port((10, "tcp"))
1388*4882a593Smuzhiyun-        self.assertEqual(p.spec_string(), u"10/tcp")
1389*4882a593Smuzhiyun+        self.assertEqual(p.spec_string(), "10/tcp")
1390*4882a593Smuzhiyun         p = Port((100, "ip"))
1391*4882a593Smuzhiyun-        self.assertEqual(p.spec_string(), u"100/ip")
1392*4882a593Smuzhiyun+        self.assertEqual(p.spec_string(), "100/ip")
1393*4882a593Smuzhiyun
1394*4882a593Smuzhiyun     def test_state_string(self):
1395*4882a593Smuzhiyun         p = Port((10, "tcp"))
1396*4882a593Smuzhiyun-        self.assertEqual(p.state_string(), u"unknown")
1397*4882a593Smuzhiyun+        self.assertEqual(p.state_string(), "unknown")
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun
1400*4882a593Smuzhiyun class service_test(unittest.TestCase):
1401*4882a593Smuzhiyun@@ -255,47 +255,47 @@ class service_test(unittest.TestCase):
1402*4882a593Smuzhiyun     def test_compare(self):
1403*4882a593Smuzhiyun         """Test that services with the same contents compare equal."""
1404*4882a593Smuzhiyun         a = Service()
1405*4882a593Smuzhiyun-        a.name = u"ftp"
1406*4882a593Smuzhiyun-        a.product = u"FooBar FTP"
1407*4882a593Smuzhiyun-        a.version = u"1.1.1"
1408*4882a593Smuzhiyun-        a.tunnel = u"ssl"
1409*4882a593Smuzhiyun+        a.name = "ftp"
1410*4882a593Smuzhiyun+        a.product = "FooBar FTP"
1411*4882a593Smuzhiyun+        a.version = "1.1.1"
1412*4882a593Smuzhiyun+        a.tunnel = "ssl"
1413*4882a593Smuzhiyun         self.assertEqual(a, a)
1414*4882a593Smuzhiyun         b = Service()
1415*4882a593Smuzhiyun-        b.name = u"ftp"
1416*4882a593Smuzhiyun-        b.product = u"FooBar FTP"
1417*4882a593Smuzhiyun-        b.version = u"1.1.1"
1418*4882a593Smuzhiyun-        b.tunnel = u"ssl"
1419*4882a593Smuzhiyun+        b.name = "ftp"
1420*4882a593Smuzhiyun+        b.product = "FooBar FTP"
1421*4882a593Smuzhiyun+        b.version = "1.1.1"
1422*4882a593Smuzhiyun+        b.tunnel = "ssl"
1423*4882a593Smuzhiyun         self.assertEqual(a, b)
1424*4882a593Smuzhiyun-        b.name = u"http"
1425*4882a593Smuzhiyun+        b.name = "http"
1426*4882a593Smuzhiyun         self.assertNotEqual(a, b)
1427*4882a593Smuzhiyun         c = Service()
1428*4882a593Smuzhiyun         self.assertNotEqual(a, c)
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun     def test_tunnel(self):
1431*4882a593Smuzhiyun         serv = Service()
1432*4882a593Smuzhiyun-        serv.name = u"http"
1433*4882a593Smuzhiyun-        serv.tunnel = u"ssl"
1434*4882a593Smuzhiyun-        self.assertEqual(serv.name_string(), u"ssl/http")
1435*4882a593Smuzhiyun+        serv.name = "http"
1436*4882a593Smuzhiyun+        serv.tunnel = "ssl"
1437*4882a593Smuzhiyun+        self.assertEqual(serv.name_string(), "ssl/http")
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun     def test_version_string(self):
1440*4882a593Smuzhiyun         serv = Service()
1441*4882a593Smuzhiyun-        serv.product = u"FooBar"
1442*4882a593Smuzhiyun+        serv.product = "FooBar"
1443*4882a593Smuzhiyun         self.assertTrue(len(serv.version_string()) > 0)
1444*4882a593Smuzhiyun         serv = Service()
1445*4882a593Smuzhiyun-        serv.version = u"1.2.3"
1446*4882a593Smuzhiyun+        serv.version = "1.2.3"
1447*4882a593Smuzhiyun         self.assertTrue(len(serv.version_string()) > 0)
1448*4882a593Smuzhiyun         serv = Service()
1449*4882a593Smuzhiyun-        serv.extrainfo = u"misconfigured"
1450*4882a593Smuzhiyun+        serv.extrainfo = "misconfigured"
1451*4882a593Smuzhiyun         self.assertTrue(len(serv.version_string()) > 0)
1452*4882a593Smuzhiyun         serv = Service()
1453*4882a593Smuzhiyun-        serv.product = u"FooBar"
1454*4882a593Smuzhiyun-        serv.version = u"1.2.3"
1455*4882a593Smuzhiyun+        serv.product = "FooBar"
1456*4882a593Smuzhiyun+        serv.version = "1.2.3"
1457*4882a593Smuzhiyun         # Must match Nmap output.
1458*4882a593Smuzhiyun         self.assertEqual(serv.version_string(),
1459*4882a593Smuzhiyun-                u"%s %s" % (serv.product, serv.version))
1460*4882a593Smuzhiyun-        serv.extrainfo = u"misconfigured"
1461*4882a593Smuzhiyun+                "%s %s" % (serv.product, serv.version))
1462*4882a593Smuzhiyun+        serv.extrainfo = "misconfigured"
1463*4882a593Smuzhiyun         self.assertEqual(serv.version_string(),
1464*4882a593Smuzhiyun-                u"%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
1465*4882a593Smuzhiyun+                "%s %s (%s)" % (serv.product, serv.version, serv.extrainfo))
1466*4882a593Smuzhiyun
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun class ScanDiffSub(ScanDiff):
1469*4882a593Smuzhiyun@@ -703,7 +703,7 @@ class scan_diff_xml_test(unittest.TestCase):
1470*4882a593Smuzhiyun         a.load_from_file("test-scans/empty.xml")
1471*4882a593Smuzhiyun         b = Scan()
1472*4882a593Smuzhiyun         b.load_from_file("test-scans/simple.xml")
1473*4882a593Smuzhiyun-        f = StringIO.StringIO()
1474*4882a593Smuzhiyun+        f = io.StringIO()
1475*4882a593Smuzhiyun         self.scan_diff = ScanDiffXML(a, b, f)
1476*4882a593Smuzhiyun         self.scan_diff.output()
1477*4882a593Smuzhiyun         self.xml = f.getvalue()
1478*4882a593Smuzhiyun@@ -712,8 +712,8 @@ class scan_diff_xml_test(unittest.TestCase):
1479*4882a593Smuzhiyun     def test_well_formed(self):
1480*4882a593Smuzhiyun         try:
1481*4882a593Smuzhiyun             document = xml.dom.minidom.parseString(self.xml)
1482*4882a593Smuzhiyun-        except Exception, e:
1483*4882a593Smuzhiyun-            self.fail(u"Parsing XML diff output caused the exception: %s"
1484*4882a593Smuzhiyun+        except Exception as e:
1485*4882a593Smuzhiyun+            self.fail("Parsing XML diff output caused the exception: %s"
1486*4882a593Smuzhiyun                     % str(e))
1487*4882a593Smuzhiyun
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun@@ -739,8 +739,8 @@ def host_apply_diff(host, diff):
1490*4882a593Smuzhiyun         host.os = diff.host_b.os[:]
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun     if diff.extraports_changed:
1493*4882a593Smuzhiyun-        for state in host.extraports.keys():
1494*4882a593Smuzhiyun-            for port in host.ports.values():
1495*4882a593Smuzhiyun+        for state in list(host.extraports.keys()):
1496*4882a593Smuzhiyun+            for port in list(host.ports.values()):
1497*4882a593Smuzhiyun                 if port.state == state:
1498*4882a593Smuzhiyun                     del host.ports[port.spec]
1499*4882a593Smuzhiyun         host.extraports = diff.host_b.extraports.copy()
1500*4882a593Smuzhiyundiff --git a/ndiff/scripts/ndiff b/ndiff/scripts/ndiff
1501*4882a593Smuzhiyunindex 8517c07..4671e73 100755
1502*4882a593Smuzhiyun--- a/ndiff/scripts/ndiff
1503*4882a593Smuzhiyun+++ b/ndiff/scripts/ndiff
1504*4882a593Smuzhiyun@@ -1,4 +1,4 @@
1505*4882a593Smuzhiyun-#!/usr/bin/env python
1506*4882a593Smuzhiyun+#!/usr/bin/env python3
1507*4882a593Smuzhiyun
1508*4882a593Smuzhiyun # Ndiff
1509*4882a593Smuzhiyun #
1510*4882a593Smuzhiyun@@ -67,15 +67,15 @@ if INSTALL_LIB is not None and is_secure_dir(INSTALL_LIB):
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun try:
1513*4882a593Smuzhiyun     import ndiff
1514*4882a593Smuzhiyun-except ImportError, e:
1515*4882a593Smuzhiyun-    print >> sys.stderr, """\
1516*4882a593Smuzhiyun+except ImportError as e:
1517*4882a593Smuzhiyun+    print("""\
1518*4882a593Smuzhiyun Could not import the ndiff module: %s.
1519*4882a593Smuzhiyun-I checked in these directories:""" % repr(e.message)
1520*4882a593Smuzhiyun+I checked in these directories:""" % repr(e), file=sys.stderr)
1521*4882a593Smuzhiyun     for dir in sys.path:
1522*4882a593Smuzhiyun-        print >> sys.stderr, "    %s" % dir
1523*4882a593Smuzhiyun-    print >> sys.stderr, """\
1524*4882a593Smuzhiyun+        print("    %s" % dir, file=sys.stderr)
1525*4882a593Smuzhiyun+    print("""\
1526*4882a593Smuzhiyun If you installed Ndiff in another directory, you may have to add the
1527*4882a593Smuzhiyun-modules directory to the PYTHONPATH environment variable."""
1528*4882a593Smuzhiyun+modules directory to the PYTHONPATH environment variable.""", file=sys.stderr)
1529*4882a593Smuzhiyun     sys.exit(1)
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun import ndiff
1532*4882a593Smuzhiyundiff --git a/ndiff/setup.py b/ndiff/setup.py
1533*4882a593Smuzhiyunold mode 100644
1534*4882a593Smuzhiyunnew mode 100755
1535*4882a593Smuzhiyunindex b5e254c..c49bcf3
1536*4882a593Smuzhiyun--- a/ndiff/setup.py
1537*4882a593Smuzhiyun+++ b/ndiff/setup.py
1538*4882a593Smuzhiyun@@ -94,7 +94,7 @@ class checked_install(distutils.command.install.install):
1539*4882a593Smuzhiyun         self.saved_prefix = sys.prefix
1540*4882a593Smuzhiyun         try:
1541*4882a593Smuzhiyun             distutils.command.install.install.finalize_options(self)
1542*4882a593Smuzhiyun-        except distutils.errors.DistutilsPlatformError, e:
1543*4882a593Smuzhiyun+        except distutils.errors.DistutilsPlatformError as e:
1544*4882a593Smuzhiyun             raise distutils.errors.DistutilsPlatformError(str(e) + """
1545*4882a593Smuzhiyun Installing your distribution's python-dev package may solve this problem.""")
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun@@ -155,13 +155,13 @@ Installing your distribution's python-dev package may solve this problem.""")
1548*4882a593Smuzhiyun #!/usr/bin/env python
1549*4882a593Smuzhiyun import errno, os, os.path, sys
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun-print 'Uninstall %(name)s'
1552*4882a593Smuzhiyun+print('Uninstall %(name)s')
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun answer = raw_input('Are you sure that you want to uninstall '
1555*4882a593Smuzhiyun     '%(name)s (yes/no) ')
1556*4882a593Smuzhiyun
1557*4882a593Smuzhiyun if answer != 'yes' and answer != 'y':
1558*4882a593Smuzhiyun-    print 'Not uninstalling.'
1559*4882a593Smuzhiyun+    print('Not uninstalling.')
1560*4882a593Smuzhiyun     sys.exit(0)
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun """ % {'name': APP_NAME}
1563*4882a593Smuzhiyun@@ -177,8 +177,8 @@ if answer != 'yes' and answer != 'y':
1564*4882a593Smuzhiyun                     # This should never happen (everything gets installed
1565*4882a593Smuzhiyun                     # inside the root), but if it does, be safe and don't
1566*4882a593Smuzhiyun                     # delete anything.
1567*4882a593Smuzhiyun-                    uninstaller += ("print '%s was not installed inside "
1568*4882a593Smuzhiyun-                        "the root %s; skipping.'\n" % (output, self.root))
1569*4882a593Smuzhiyun+                    uninstaller += ("print('%s was not installed inside "
1570*4882a593Smuzhiyun+                        "the root %s; skipping.')\n" % (output, self.root))
1571*4882a593Smuzhiyun                     continue
1572*4882a593Smuzhiyun                 output = path_strip_prefix(output, self.root)
1573*4882a593Smuzhiyun                 assert os.path.isabs(output)
1574*4882a593Smuzhiyun@@ -202,24 +202,24 @@ for path in INSTALLED_FILES:
1575*4882a593Smuzhiyun         dirs.append(path)
1576*4882a593Smuzhiyun # Delete the files.
1577*4882a593Smuzhiyun for file in files:
1578*4882a593Smuzhiyun-    print "Removing '%s'." % file
1579*4882a593Smuzhiyun+    print("Removing '%s'." % file)
1580*4882a593Smuzhiyun     try:
1581*4882a593Smuzhiyun         os.remove(file)
1582*4882a593Smuzhiyun-    except OSError, e:
1583*4882a593Smuzhiyun-        print >> sys.stderr, '  Error: %s.' % str(e)
1584*4882a593Smuzhiyun+    except OSError as e:
1585*4882a593Smuzhiyun+        print('  Error: %s.' % str(e), file=sys.stderr)
1586*4882a593Smuzhiyun # Delete the directories. First reverse-sort the normalized paths by
1587*4882a593Smuzhiyun # length so that child directories are deleted before their parents.
1588*4882a593Smuzhiyun dirs = [os.path.normpath(dir) for dir in dirs]
1589*4882a593Smuzhiyun dirs.sort(key = len, reverse = True)
1590*4882a593Smuzhiyun for dir in dirs:
1591*4882a593Smuzhiyun     try:
1592*4882a593Smuzhiyun-        print "Removing the directory '%s'." % dir
1593*4882a593Smuzhiyun+        print("Removing the directory '%s'." % dir)
1594*4882a593Smuzhiyun         os.rmdir(dir)
1595*4882a593Smuzhiyun-    except OSError, e:
1596*4882a593Smuzhiyun+    except OSError as e:
1597*4882a593Smuzhiyun         if e.errno == errno.ENOTEMPTY:
1598*4882a593Smuzhiyun-            print "Directory '%s' not empty; not removing." % dir
1599*4882a593Smuzhiyun+            print("Directory '%s' not empty; not removing." % dir)
1600*4882a593Smuzhiyun         else:
1601*4882a593Smuzhiyun-            print >> sys.stderr, str(e)
1602*4882a593Smuzhiyun+            print(str(e), file=sys.stderr)
1603*4882a593Smuzhiyun """
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun         uninstaller_file = open(uninstaller_filename, 'w')
1606*4882a593Smuzhiyun@@ -227,7 +227,7 @@ for dir in dirs:
1607*4882a593Smuzhiyun         uninstaller_file.close()
1608*4882a593Smuzhiyun
1609*4882a593Smuzhiyun         # Set exec bit for uninstaller
1610*4882a593Smuzhiyun-        mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0555) & 07777
1611*4882a593Smuzhiyun+        mode = ((os.stat(uninstaller_filename)[ST_MODE]) | 0o555) & 0o7777
1612*4882a593Smuzhiyun         os.chmod(uninstaller_filename, mode)
1613*4882a593Smuzhiyun
1614*4882a593Smuzhiyun     def write_installed_files(self):
1615*4882a593Smuzhiyun@@ -242,7 +242,7 @@ for dir in dirs:
1616*4882a593Smuzhiyun         try:
1617*4882a593Smuzhiyun             for output in self.get_installed_files():
1618*4882a593Smuzhiyun                 assert "\n" not in output
1619*4882a593Smuzhiyun-                print >> f, output
1620*4882a593Smuzhiyun+                print(output, file=f)
1621*4882a593Smuzhiyun         finally:
1622*4882a593Smuzhiyun             f.close()
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun@@ -266,7 +266,7 @@ class my_uninstall(distutils.cmd.Command):
1625*4882a593Smuzhiyun         # Read the list of installed files.
1626*4882a593Smuzhiyun         try:
1627*4882a593Smuzhiyun             f = open(INSTALLED_FILES_NAME, "r")
1628*4882a593Smuzhiyun-        except IOError, e:
1629*4882a593Smuzhiyun+        except IOError as e:
1630*4882a593Smuzhiyun             if e.errno == errno.ENOENT:
1631*4882a593Smuzhiyun                 log.error("Couldn't open the installation record '%s'. "
1632*4882a593Smuzhiyun                         "Have you installed yet?" % INSTALLED_FILES_NAME)
1633*4882a593Smuzhiyun@@ -289,7 +289,7 @@ class my_uninstall(distutils.cmd.Command):
1634*4882a593Smuzhiyun             try:
1635*4882a593Smuzhiyun                 if not self.dry_run:
1636*4882a593Smuzhiyun                     os.remove(file)
1637*4882a593Smuzhiyun-            except OSError, e:
1638*4882a593Smuzhiyun+            except OSError as e:
1639*4882a593Smuzhiyun                 log.error(str(e))
1640*4882a593Smuzhiyun         # Delete the directories. First reverse-sort the normalized paths by
1641*4882a593Smuzhiyun         # length so that child directories are deleted before their parents.
1642*4882a593Smuzhiyun@@ -300,7 +300,7 @@ class my_uninstall(distutils.cmd.Command):
1643*4882a593Smuzhiyun                 log.info("Removing the directory '%s'." % dir)
1644*4882a593Smuzhiyun                 if not self.dry_run:
1645*4882a593Smuzhiyun                     os.rmdir(dir)
1646*4882a593Smuzhiyun-            except OSError, e:
1647*4882a593Smuzhiyun+            except OSError as e:
1648*4882a593Smuzhiyun                 if e.errno == errno.ENOTEMPTY:
1649*4882a593Smuzhiyun                     log.info("Directory '%s' not empty; not removing." % dir)
1650*4882a593Smuzhiyun                 else:
1651*4882a593Smuzhiyundiff --git a/ndiff/test-scans/anonymize.py b/ndiff/test-scans/anonymize.py
1652*4882a593Smuzhiyunindex 9ba612a..fd251fe 100755
1653*4882a593Smuzhiyun--- a/ndiff/test-scans/anonymize.py
1654*4882a593Smuzhiyun+++ b/ndiff/test-scans/anonymize.py
1655*4882a593Smuzhiyun@@ -1,4 +1,4 @@
1656*4882a593Smuzhiyun-#!/usr/bin/env python
1657*4882a593Smuzhiyun+#!/usr/bin/env python3
1658*4882a593Smuzhiyun
1659*4882a593Smuzhiyun # Anonymize an Nmap XML file, replacing host name and IP addresses with random
1660*4882a593Smuzhiyun # anonymous ones. Anonymized names will be consistent between runs of the
1661*4882a593Smuzhiyun@@ -20,20 +20,20 @@ r = random.Random()
1662*4882a593Smuzhiyun
1663*4882a593Smuzhiyun
1664*4882a593Smuzhiyun def hash(s):
1665*4882a593Smuzhiyun-    digest = hashlib.sha512(s).hexdigest()
1666*4882a593Smuzhiyun+    digest = hashlib.sha512(s.encode()).hexdigest()
1667*4882a593Smuzhiyun     return int(digest, 16)
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun
1670*4882a593Smuzhiyun def anonymize_mac_address(addr):
1671*4882a593Smuzhiyun     r.seed(hash(addr))
1672*4882a593Smuzhiyun     nums = (0, 0, 0) + tuple(r.randrange(256) for i in range(3))
1673*4882a593Smuzhiyun-    return u":".join(u"%02X" % x for x in nums)
1674*4882a593Smuzhiyun+    return ":".join("%02X" % x for x in nums)
1675*4882a593Smuzhiyun
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun def anonymize_ipv4_address(addr):
1678*4882a593Smuzhiyun     r.seed(hash(addr))
1679*4882a593Smuzhiyun     nums = (10,) + tuple(r.randrange(256) for i in range(3))
1680*4882a593Smuzhiyun-    return u".".join(unicode(x) for x in nums)
1681*4882a593Smuzhiyun+    return ".".join(str(x) for x in nums)
1682*4882a593Smuzhiyun
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun def anonymize_ipv6_address(addr):
1685*4882a593Smuzhiyun@@ -41,7 +41,7 @@ def anonymize_ipv6_address(addr):
1686*4882a593Smuzhiyun     # RFC 4193.
1687*4882a593Smuzhiyun     nums = (0xFD00 + r.randrange(256),)
1688*4882a593Smuzhiyun     nums = nums + tuple(r.randrange(65536) for i in range(7))
1689*4882a593Smuzhiyun-    return u":".join("%04X" % x for x in nums)
1690*4882a593Smuzhiyun+    return ":".join("%04X" % x for x in nums)
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun # Maps to memoize address and host name conversions.
1693*4882a593Smuzhiyun hostname_map = {}
1694*4882a593Smuzhiyun@@ -54,11 +54,11 @@ def anonymize_hostname(name):
1695*4882a593Smuzhiyun     LETTERS = "acbdefghijklmnopqrstuvwxyz"
1696*4882a593Smuzhiyun     r.seed(hash(name))
1697*4882a593Smuzhiyun     length = r.randrange(5, 10)
1698*4882a593Smuzhiyun-    prefix = u"".join(r.sample(LETTERS, length))
1699*4882a593Smuzhiyun+    prefix = "".join(r.sample(LETTERS, length))
1700*4882a593Smuzhiyun     num = r.randrange(1000)
1701*4882a593Smuzhiyun-    hostname_map[name] = u"%s-%d.example.com" % (prefix, num)
1702*4882a593Smuzhiyun+    hostname_map[name] = "%s-%d.example.com" % (prefix, num)
1703*4882a593Smuzhiyun     if VERBOSE:
1704*4882a593Smuzhiyun-        print >> sys.stderr, "Replace %s with %s" % (name, hostname_map[name])
1705*4882a593Smuzhiyun+        print("Replace %s with %s" % (name, hostname_map[name]), file=sys.stderr)
1706*4882a593Smuzhiyun     return hostname_map[name]
1707*4882a593Smuzhiyun
1708*4882a593Smuzhiyun mac_re = re.compile(r'\b([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}\b')
1709*4882a593Smuzhiyun@@ -78,7 +78,7 @@ def anonymize_address(addr):
1710*4882a593Smuzhiyun     else:
1711*4882a593Smuzhiyun         assert False
1712*4882a593Smuzhiyun     if VERBOSE:
1713*4882a593Smuzhiyun-        print >> sys.stderr, "Replace %s with %s" % (addr, address_map[addr])
1714*4882a593Smuzhiyun+        print("Replace %s with %s" % (addr, address_map[addr]), file=sys.stderr)
1715*4882a593Smuzhiyun     return address_map[addr]
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun
1718*4882a593Smuzhiyun--
1719*4882a593Smuzhiyun2.24.1
1720*4882a593Smuzhiyun
1721