Reland "[Autotest] Python3 files/server/* migration"

Fixed the division errors

This is a reland of b05fb5a3dd205d085395c3e2f7d05127cabd07ec
TEST=network_WiFi_ChannelHop && network_WiFi_SimpleConnect.wifi_check24HT20

Original change's description:
> [Autotest] Python3 files/server/* migration
>
> Does not include subdirs
> TEST=applicable unittests, compiling in py2 and py3
> BUG=chromium:990593
>
> Change-Id: I668c7531e7fc926f9a20c3bf2bab0d62eb3e3926
> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2318565
> Commit-Queue: Derek Beckett <[email protected]>
> Tested-by: Derek Beckett <[email protected]>
> Reviewed-by: Greg Edelston <[email protected]>

Bug: chromium:990593
Change-Id: I912aa024130987af313dc472a273267669e6b69c
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/autotest/+/2373204
Commit-Queue: Derek Beckett <[email protected]>
Tested-by: Derek Beckett <[email protected]>
Reviewed-by: Greg Edelston <[email protected]>
Reviewed-by: Gregory Nisbet <[email protected]>
Auto-Submit: Derek Beckett <[email protected]>
diff --git a/server/afe_urls.py b/server/afe_urls.py
index 726c296..a1b92ad 100644
--- a/server/afe_urls.py
+++ b/server/afe_urls.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright 2016 The Chromium Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -14,8 +15,8 @@
 """
 
 import logging
-import urllib
-import urlparse
+from six.moves import urllib
+import six.moves.urllib.parse
 import sys
 
 import common
@@ -34,7 +35,7 @@
         @param root_url: AFE root URL.
 
         """
-        self._root_url_parts = urlparse.urlsplit(root_url)
+        self._root_url_parts = six.moves.urllib.parse.urlsplit(root_url)
 
     _DEFAULT_URL = 'http://%s/afe/'
 
@@ -71,8 +72,8 @@
 
         """
         scheme, netloc, path, query, _fragment = self._root_url_parts
-        fragment = urllib.urlencode(params)
-        return urlparse.SplitResult(
+        fragment = urllib.parse.urlencode(params)
+        return six.moves.urllib.parse.SplitResult(
             scheme, netloc, path, query, fragment).geturl()
 
     @property
diff --git a/server/afe_urls_unittest.py b/server/afe_urls_unittest.py
index 3a84ce3..0d90848 100644
--- a/server/afe_urls_unittest.py
+++ b/server/afe_urls_unittest.py
@@ -3,7 +3,7 @@
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import urlparse
+import six.moves.urllib.parse
 import unittest
 
 import common
@@ -21,7 +21,7 @@
         @param b First URL to compare
 
         """
-        urlsplit = urlparse.urlsplit
+        urlsplit = six.moves.urllib.parse.urlsplit
         self.assertEqual(urlsplit(a), urlsplit(b))
 
     def test__geturl(self):
diff --git a/server/afe_utils.py b/server/afe_utils.py
index d8e65b3..dd297f1 100644
--- a/server/afe_utils.py
+++ b/server/afe_utils.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright 2016 The Chromium Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -9,6 +10,10 @@
       their own instance of the AFE client and interact with it directly.
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import logging
 import traceback
 
diff --git a/server/autoserv b/server/autoserv
index b449855..5226a97 100755
--- a/server/autoserv
+++ b/server/autoserv
@@ -18,7 +18,7 @@
 import sys
 import traceback
 import time
-import urllib2
+from six.moves import urllib
 
 import common
 from autotest_lib.client.bin.result_tools import utils as result_utils
@@ -140,7 +140,7 @@
                  'isolate_hash': parser.options.isolate,
                  'image': parser.options.test_source_build}
     script_locals = {}
-    execfile(STAGE_SERVER_SIDE_PACKAGE_CONTROL_FILE, namespace, script_locals)
+    exec(compile(open(STAGE_SERVER_SIDE_PACKAGE_CONTROL_FILE, "rb").read(), STAGE_SERVER_SIDE_PACKAGE_CONTROL_FILE, 'exec'), namespace, script_locals)
     ssp_url = script_locals['ssp_url']
     if not ssp_url:
         logging.error('Failed to stage SSP package: %s',
@@ -283,7 +283,7 @@
             logging.warn('Starting servod is aborted. The dut\'s servo_host '
                          'attribute is not set to localhost.')
             return
-    except (urllib2.HTTPError, urllib2.URLError):
+    except (urllib.error.HTTPError, urllib.error.URLError):
         # Ignore error if RPC failed to get board
         logging.error('Failed to get board name from AFE. Start servod is '
                       'aborted')
diff --git a/server/autotest.py b/server/autotest.py
index 533adc4..37dd1e1 100644
--- a/server/autotest.py
+++ b/server/autotest.py
@@ -1,6 +1,11 @@
+# Lint as: python2, python3
 # Copyright 2007 Google Inc. Released under the GPL v2
 #pylint: disable-msg=C0111
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import glob
 import logging
 import os
@@ -22,6 +27,8 @@
 from autotest_lib.server import utils
 from autotest_lib.server import utils as server_utils
 from autotest_lib.server.cros.dynamic_suite.constants import JOB_REPO_URL
+import six
+from six.moves import map
 
 
 try:
@@ -358,7 +365,7 @@
                              "packaging system.")
                 return
             except (error.PackageInstallError, error.AutoservRunError,
-                    global_config.ConfigError), e:
+                    global_config.ConfigError) as e:
                 logging.info("Could not install autotest using the packaging "
                              "system: %s. Trying other methods", e)
         else:
@@ -389,7 +396,7 @@
                         host.hostname)
             try:
                 host.run('svn checkout %s %s' % (AUTOTEST_SVN, autodir))
-            except error.AutoservRunError, e:
+            except error.AutoservRunError as e:
                 host.run('svn checkout %s %s' % (AUTOTEST_HTTP, autodir))
             logging.info("Installation of autotest completed using SVN.")
             self.installed = True
@@ -537,18 +544,18 @@
             else:
                 logging.debug('use_packaging is set to False, do not add any '
                               'repository.')
-        except global_config.ConfigError, e:
+        except global_config.ConfigError as e:
             # If repos is defined packaging is enabled so log the error
             if repos:
                 logging.error(e)
 
         # on full-size installs, turn on any profilers the server is using
         if not atrun.background:
-            running_profilers = host.job.profilers.add_log.iteritems()
+            running_profilers = six.iteritems(host.job.profilers.add_log)
             for profiler, (args, dargs) in running_profilers:
                 call_args = [repr(profiler)]
                 call_args += [repr(arg) for arg in args]
-                call_args += ["%s=%r" % item for item in dargs.iteritems()]
+                call_args += ["%s=%r" % item for item in six.iteritems(dargs)]
                 prologue_lines.append("job.profilers.add(%s)\n"
                                       % ", ".join(call_args))
         cfile = "".join(prologue_lines)
@@ -628,7 +635,7 @@
             self.install(host)
 
         opts = ["%s=%s" % (o[0], repr(o[1])) for o in dargs.items()]
-        cmd = ", ".join([repr(test_name)] + map(repr, args) + opts)
+        cmd = ", ".join([repr(test_name)] + list(map(repr, args)) + opts)
         control = "job.run_test(%s)\n" % cmd
         self.run(control, results_dir, host, timeout=timeout,
                  parallel_flag=parallel_flag, background=background,
@@ -997,7 +1004,7 @@
                                            timeout=timeout,
                                            stdout_tee=client_log,
                                            stderr_tee=stderr_redirector)
-                except error.AutoservRunError, e:
+                except error.AutoservRunError as e:
                     result = e.result_obj
                     result.exit_status = None
                     disconnect_warnings.append(e.description)
@@ -1126,7 +1133,7 @@
                 elif self.is_client_job_rebooting(last):
                     try:
                         self._wait_for_reboot(boot_id)
-                    except error.AutotestRunError, e:
+                    except error.AutotestRunError as e:
                         self.host.job.record("ABORT", None, "reboot", str(e))
                         self.host.job.record("END ABORT", None, None, str(e))
                         raise
@@ -1271,7 +1278,7 @@
 
     def _process_log_dict(self, log_dict):
         log_list = log_dict.pop("logs", [])
-        for key in sorted(log_dict.iterkeys()):
+        for key in sorted(six.iterkeys(log_dict)):
             log_list += self._process_log_dict(log_dict.pop(key))
         return log_list
 
diff --git a/server/base_utils.py b/server/base_utils.py
index 8b64300..fd8be19 100644
--- a/server/base_utils.py
+++ b/server/base_utils.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright 2008 Google Inc, Martin J. Bligh <[email protected]>,
 #                Benjamin Poirier, Ryan Stutsman
 # Released under the GPL v2
@@ -8,7 +9,12 @@
 import that instead
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import atexit, os, re, shutil, textwrap, sys, tempfile, types
+import six
 
 from autotest_lib.client.common_lib import barrier, utils
 from autotest_lib.server import subcommand
@@ -68,12 +74,12 @@
     # location is a file-like object
     if hasattr(location, "read"):
         tmpfile = os.path.join(tmpdir, "file")
-        tmpfileobj = file(tmpfile, 'w')
+        tmpfileobj = open(tmpfile, 'w')
         shutil.copyfileobj(location, tmpfileobj)
         tmpfileobj.close()
         return tmpfile
 
-    if isinstance(location, types.StringTypes):
+    if isinstance(location, six.string_types):
         # location is a URL
         if location.startswith('http') or location.startswith('ftp'):
             tmpfile = os.path.join(tmpdir, os.path.basename(location))
@@ -127,7 +133,7 @@
     for dir in __tmp_dirs[pid]:
         try:
             shutil.rmtree(dir)
-        except OSError, e:
+        except OSError as e:
             if e.errno == 2:
                 pass
     __tmp_dirs[pid] = []
@@ -288,15 +294,15 @@
         os.path.isfile(rsa_private_key_path)
 
     if has_dsa_keypair:
-        print 'DSA keypair found, using it'
+        print('DSA keypair found, using it')
         public_key_path = dsa_public_key_path
 
     elif has_rsa_keypair:
-        print 'RSA keypair found, using it'
+        print('RSA keypair found, using it')
         public_key_path = rsa_public_key_path
 
     else:
-        print 'Neither RSA nor DSA keypair found, creating DSA ssh key pair'
+        print('Neither RSA nor DSA keypair found, creating DSA ssh key pair')
         utils.system('ssh-keygen -t dsa -q -N "" -f %s' % dsa_private_key_path)
         public_key_path = dsa_public_key_path
 
diff --git a/server/frontend.py b/server/frontend.py
index fe942e0..b0d8f97 100644
--- a/server/frontend.py
+++ b/server/frontend.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright Martin J. Bligh, Google Inc 2008
 # Released under the GPL v2
 
@@ -15,6 +16,10 @@
 
 #pylint: disable=missing-docstring
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import getpass
 import os
 import re
@@ -28,6 +33,7 @@
 from autotest_lib.client.common_lib import priorities
 from autotest_lib.client.common_lib import utils
 from autotest_lib.tko import db
+from six.moves import zip
 
 try:
     from chromite.lib import metrics
@@ -92,8 +98,8 @@
         headers = {'AUTHORIZATION': self.user}
         rpc_server = rpc_client_lib.add_protocol(server) + path
         if debug:
-            print 'SERVER: %s' % rpc_server
-            print 'HEADERS: %s' % headers
+            print('SERVER: %s' % rpc_server)
+            print('HEADERS: %s' % headers)
         self.proxy = rpc_client_lib.get_proxy(rpc_server, headers=headers)
 
 
@@ -103,11 +109,11 @@
         """
         rpc_call = getattr(self.proxy, call)
         if self.debug:
-            print 'DEBUG: %s %s' % (call, dargs)
+            print('DEBUG: %s %s' % (call, dargs))
         try:
             result = utils.strip_unicode(rpc_call(**dargs))
             if self.reply_debug:
-                print result
+                print(result)
             return result
         except Exception:
             raise
@@ -115,7 +121,7 @@
 
     def log(self, message):
         if self.print_log:
-            print message
+            print(message)
 
 
 class TKO(RpcClient):
@@ -867,9 +873,9 @@
 
     def show(self):
         labels = list(set(self.labels) - set([self.platform]))
-        print '%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
+        print('%-6s %-7s %-7s %-16s %s' % (self.hostname, self.status,
                                            self.locked, self.platform,
-                                           ', '.join(labels))
+                                           ', '.join(labels)))
 
 
     def delete(self):
diff --git a/server/lab_status_unittest.py b/server/lab_status_unittest.py
index 87bb412..dd22a0d 100644
--- a/server/lab_status_unittest.py
+++ b/server/lab_status_unittest.py
@@ -1,13 +1,19 @@
+# Lint as: python2, python3
 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
-import StringIO
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
+from six.moves import range
+import six
 import json
 import mox
 import time
 import unittest
-import urllib2
+from six.moves import urllib
 
 import common
 from autotest_lib.client.common_lib import global_config
@@ -87,7 +93,7 @@
     """
 
     def __init__(self, code, buffer):
-        self._stringio = StringIO.StringIO(buffer)
+        self._stringio = six.StringIO(buffer)
         self._code = code
 
 
@@ -116,7 +122,7 @@
 
     def setUp(self):
         super(GetStatusTest, self).setUp()
-        self.mox.StubOutWithMock(urllib2, 'urlopen')
+        self.mox.StubOutWithMock(urllib.request, 'urlopen')
         self.mox.StubOutWithMock(time, 'sleep')
 
 
@@ -124,7 +130,7 @@
         """Test that successful calls to urlopen() succeed."""
         json_string = _OPEN_STATUS_VALUES[0]
         json_value = json.loads(json_string)
-        urllib2.urlopen(mox.IgnoreArg()).AndReturn(
+        urllib.request.urlopen(mox.IgnoreArg()).AndReturn(
                 _FakeURLResponse(200, json_string))
         self.mox.ReplayAll()
         result = site_utils._get_lab_status(_FAKE_URL)
@@ -136,10 +142,10 @@
         """Test that an IOError retries at least once."""
         json_string = _OPEN_STATUS_VALUES[0]
         json_value = json.loads(json_string)
-        urllib2.urlopen(mox.IgnoreArg()).AndRaise(
+        urllib.request.urlopen(mox.IgnoreArg()).AndRaise(
                 IOError('Fake I/O error for a fake URL'))
         time.sleep(mox.IgnoreArg()).AndReturn(None)
-        urllib2.urlopen(mox.IgnoreArg()).AndReturn(
+        urllib.request.urlopen(mox.IgnoreArg()).AndReturn(
                 _FakeURLResponse(200, json_string))
         self.mox.ReplayAll()
         result = site_utils._get_lab_status(_FAKE_URL)
@@ -151,10 +157,10 @@
         """Test that an HTTP error retries at least once."""
         json_string = _OPEN_STATUS_VALUES[0]
         json_value = json.loads(json_string)
-        urllib2.urlopen(mox.IgnoreArg()).AndReturn(
+        urllib.request.urlopen(mox.IgnoreArg()).AndReturn(
                 _FakeURLResponse(500, ''))
         time.sleep(mox.IgnoreArg()).AndReturn(None)
-        urllib2.urlopen(mox.IgnoreArg()).AndReturn(
+        urllib.request.urlopen(mox.IgnoreArg()).AndReturn(
                 _FakeURLResponse(200, json_string))
         self.mox.ReplayAll()
         result = site_utils._get_lab_status(_FAKE_URL)
@@ -167,7 +173,7 @@
         json_string = _OPEN_STATUS_VALUES[0]
         json_value = json.loads(json_string)
         for _ in range(site_utils._MAX_LAB_STATUS_ATTEMPTS):
-            urllib2.urlopen(mox.IgnoreArg()).AndRaise(
+            urllib.request.urlopen(mox.IgnoreArg()).AndRaise(
                     IOError('Fake I/O error for a fake URL'))
             time.sleep(mox.IgnoreArg()).AndReturn(None)
         self.mox.ReplayAll()
@@ -181,7 +187,7 @@
         json_string = _OPEN_STATUS_VALUES[0]
         json_value = json.loads(json_string)
         for _ in range(site_utils._MAX_LAB_STATUS_ATTEMPTS):
-            urllib2.urlopen(mox.IgnoreArg()).AndReturn(
+            urllib.request.urlopen(mox.IgnoreArg()).AndReturn(
                     _FakeURLResponse(404, 'Not here, never gonna be'))
             time.sleep(mox.IgnoreArg()).InAnyOrder().AndReturn(None)
         self.mox.ReplayAll()
diff --git a/server/profiler.py b/server/profiler.py
index 38bc9b5..f206f6c 100644
--- a/server/profiler.py
+++ b/server/profiler.py
@@ -1,5 +1,10 @@
+# Lint as: python2, python3
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
 import itertools
 import common
+import six
 
 
 def _get_unpassable_types(arg):
@@ -7,12 +12,12 @@
     unpassable. If arg is an atomic type (e.g. int) it either returns an
     empty set (if the type is passable) or a singleton of the type (if the
     type is not passable). """
-    if isinstance(arg, (basestring, int, long)):
+    if isinstance(arg, (six.string_types, int, int)):
         return set()
     elif isinstance(arg, (list, tuple, set, frozenset, dict)):
         if isinstance(arg, dict):
             # keys and values must both be passable
-            parts = itertools.chain(arg.iterkeys(), arg.itervalues())
+            parts = itertools.chain(six.iterkeys(arg), six.itervalues(arg))
         else:
             # for all other containers we just iterate
             parts = iter(arg)
diff --git a/server/profilers.py b/server/profilers.py
index d2271be..4b8fee9 100644
--- a/server/profilers.py
+++ b/server/profilers.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 import os, shutil, tempfile, logging
 
 import common
@@ -110,7 +111,7 @@
         filtered out of the list.
         """
         if host is None:
-            return self.installed_hosts.values()
+            return list(self.installed_hosts.values())
         if host.hostname in self.installed_hosts:
             return [self.installed_hosts[host.hostname]]
         return []
diff --git a/server/sequence.py b/server/sequence.py
index a44f65f..f38bc2d 100644
--- a/server/sequence.py
+++ b/server/sequence.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright 2015 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -6,6 +7,10 @@
 Adds ability to schedule jobs on given machines.
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import logging
 import os
 
@@ -16,6 +21,8 @@
 from autotest_lib.server.cros.dynamic_suite import control_file_getter
 from autotest_lib.server.cros.dynamic_suite import frontend_wrappers
 from autotest_lib.site_utils import job_directories
+import six
+from six.moves import range
 
 MINUTE_IN_SECS = 60
 HOUR_IN_MINUTES = 60
@@ -82,7 +89,7 @@
         @returns a timeout value for the test, 4h by default.
         """
         if self._duration:
-            return 2 * int(self._duration) / MINUTE_IN_SECS
+            return 2 * int(self._duration) // MINUTE_IN_SECS
         # default value:
         return DEFAULT_JOB_TIMEOUT_IN_MINS
 
@@ -108,7 +115,7 @@
             return cntl_file_getter.get_control_file_contents_by_name(
                     self._name)
         child_args = ['',]
-        for arg, value in self._args.iteritems():
+        for arg, value in six.iteritems(self._args):
             child_args.append('%s=%s' % (arg, repr(value)))
         if self._duration:
             child_args.append('duration=%d' % self._duration)
@@ -143,7 +150,7 @@
         runtime_mins = self.child_job_timeout()
         hostname = utils.get_hostname_from_machine(machine)
 
-        for i in xrange(0, self._iteration):
+        for i in range(0, self._iteration):
             child_job_name = self.child_job_name(hostname, i)
             logging.debug('Creating job: %s', child_job_name)
             afe.create_job(
diff --git a/server/server_job.py b/server/server_job.py
index e13ed49..92be2d1 100644
--- a/server/server_job.py
+++ b/server/server_job.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # pylint: disable-msg=C0111
 
 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
@@ -11,6 +12,10 @@
 Copyright Martin J. Bligh, Andy Whitcroft 2007
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import errno
 import fcntl
 import getpass
@@ -55,6 +60,7 @@
 from autotest_lib.server.hosts import ssh_multiplex
 from autotest_lib.tko import models as tko_models
 from autotest_lib.tko import parser_lib
+from six.moves import zip
 
 try:
     from chromite.lib import metrics
@@ -640,7 +646,7 @@
         results = self.parallel_simple(function, machines, timeout=timeout,
                                        return_results=True)
         success_machines = []
-        for result, machine in itertools.izip(results, machines):
+        for result, machine in zip(results, machines):
             if not isinstance(result, Exception):
                 success_machines.append(machine)
         return success_machines
@@ -1436,7 +1442,7 @@
                 existing_machines_text = None
             if machines_text != existing_machines_text:
                 utils.open_write_close(MACHINES_FILENAME, machines_text)
-        execfile(code_file, namespace, namespace)
+        exec(compile(open(code_file, "rb").read(), code_file, 'exec'), namespace, namespace)
 
 
     def preprocess_client_state(self):
@@ -1475,7 +1481,7 @@
         try:
             self._state.read_from_file(state_path)
             os.remove(state_path)
-        except OSError, e:
+        except OSError as e:
             # ignore file-not-found errors
             if e.errno != errno.ENOENT:
                 raise
diff --git a/server/site_host_attributes.py b/server/site_host_attributes.py
index 310188f..5aa859b 100644
--- a/server/site_host_attributes.py
+++ b/server/site_host_attributes.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -44,6 +45,7 @@
 
 
 import hashlib, logging, os, utils
+import six
 
 
 private_host_attributes = utils.import_site_symbol(
@@ -100,7 +102,8 @@
                 if 'netbook_' in attribute:
                     # Hash board names to prevent any accidental leaks.
                     splitnames = ['netbook_' + hashlib.sha256(
-                        attribute.split('netbook_')[1]).hexdigest()[:8], 'True']
+                        attribute.split('netbook_')[1]
+                        .encode('utf-8')).hexdigest()[:8], 'True']
                 else:
                     splitnames = attribute.split(':')
                     if len(splitnames) == 2:
diff --git a/server/site_linux_router.py b/server/site_linux_router.py
index 4f31bea..735691b 100644
--- a/server/site_linux_router.py
+++ b/server/site_linux_router.py
@@ -1,7 +1,12 @@
+# Lint as: python2, python3
 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import collections
 import copy
 import logging
@@ -20,6 +25,8 @@
 from autotest_lib.server import hosts
 from autotest_lib.server import site_linux_system
 from autotest_lib.server.cros import dnsname_mangler
+import six
+from six.moves import range
 
 
 StationInstance = collections.namedtuple('StationInstance',
@@ -292,7 +299,7 @@
         # Generate hostapd.conf.
         self.router.run("cat <<EOF >%s\n%s\nEOF\n" %
             (conf_file, '\n'.join(
-            "%s=%s" % kv for kv in hostapd_conf_dict.iteritems())))
+            "%s=%s" % kv for kv in six.iteritems(hostapd_conf_dict))))
 
         # Run hostapd.
         logging.info('Starting hostapd on %s(%s) channel=%s...',
@@ -455,7 +462,7 @@
         unique = ''
         while number or not unique:
             unique = self.SUFFIX_LETTERS[number % base] + unique
-            number = number / base
+            number = number // base
         # And salt the SSID so that tests running in adjacent cells are unlikely
         # to pick the same SSID and we're resistent to beacons leaking out of
         # cells.
@@ -789,7 +796,7 @@
         @return string interface name (e.g. 'managed0').
 
         """
-        if ap_num not in range(len(self.hostapd_instances)):
+        if ap_num not in list(range(len(self.hostapd_instances))):
             raise error.TestFail('Invalid instance number (%d) with %d '
                                  'instances configured.' %
                                  (ap_num, len(self.hostapd_instances)))
@@ -805,7 +812,7 @@
         @return string interface name (e.g. 'managed0').
 
         """
-        if instance not in range(len(self.station_instances)):
+        if instance not in list(range(len(self.station_instances))):
             raise error.TestFail('Invalid instance number (%d) with %d '
                                  'instances configured.' %
                                  (instance, len(self.station_instances)))
diff --git a/server/site_linux_system.py b/server/site_linux_system.py
index 0433a00..7ca462d 100644
--- a/server/site_linux_system.py
+++ b/server/site_linux_system.py
@@ -1,7 +1,12 @@
+# Lint as: python2, python3
 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import datetime
 import collections
 import logging
@@ -16,6 +21,8 @@
 from autotest_lib.client.common_lib.cros.network import iw_runner
 from autotest_lib.client.common_lib.cros.network import ping_runner
 from autotest_lib.server.cros.network import packet_capturer
+import six
+from six.moves import range
 
 NetDev = collections.namedtuple('NetDev',
                                 ['inherited', 'phy', 'if_name', 'if_type'])
@@ -239,7 +246,7 @@
         # across systems. (The default random class might, e.g., seed
         # itself based on wall-clock time.)
         sysrand = random.SystemRandom()
-        for tries in xrange(0, self.MAC_RETRY_LIMIT):
+        for tries in range(0, self.MAC_RETRY_LIMIT):
             mac_addr = '%02x:%02x:%02x:%02x:%02x:%02x' % (
                 (sysrand.getrandbits(8) & ~self.MAC_BIT_MULTICAST) |
                 self.MAC_BIT_LOCAL,
@@ -323,10 +330,10 @@
     def get_capabilities(self):
         caps = set()
         phymap = self.phys_for_frequency
-        if [freq for freq in phymap.iterkeys() if freq > 5000]:
+        if [freq for freq in six.iterkeys(phymap) if freq > 5000]:
             # The frequencies are expressed in megaherz
             caps.add(self.CAPABILITY_5GHZ)
-        if [freq for freq in phymap.iterkeys() if len(phymap[freq]) > 1]:
+        if [freq for freq in six.iterkeys(phymap) if len(phymap[freq]) > 1]:
             caps.add(self.CAPABILITY_MULTI_AP_SAME_BAND)
             caps.add(self.CAPABILITY_MULTI_AP)
         elif len(self.phy_bus_type) > 1:
diff --git a/server/site_server_job_utils.py b/server/site_server_job_utils.py
index e9bd398..a6c9531 100644
--- a/server/site_server_job_utils.py
+++ b/server/site_server_job_utils.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
@@ -9,7 +10,7 @@
 """
 
 
-import logging, os, Queue
+import logging, os, six.moves.queue
 from autotest_lib.client.common_lib import error, utils
 from autotest_lib.server import autotest, hosts, host_attributes
 
diff --git a/server/site_utils.py b/server/site_utils.py
index 4c71b39..434d0e5 100644
--- a/server/site_utils.py
+++ b/server/site_utils.py
@@ -1,12 +1,17 @@
+# Lint as: python2, python3
 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import collections
 import contextlib
 import grp
-import httplib
+import six.moves.http_client
 import json
 import logging
 import os
@@ -14,7 +19,9 @@
 import re
 import time
 import traceback
-import urllib2
+from six.moves import filter
+from six.moves import range
+from six.moves import urllib
 
 import common
 from autotest_lib.client.bin.result_tools import utils as result_utils
@@ -224,7 +231,7 @@
         except (ValueError, IOError) as e:
             logging.warning('could not parse sheriff from url %s%s: %s',
                              _CHROMIUM_BUILD_URL, sheriff_js, str(e))
-        except (urllib2.URLError, httplib.HTTPException) as e:
+        except (urllib.error.URLError, six.moves.http_client.HTTPException) as e:
             logging.warning('unexpected error reading from url "%s%s": %s',
                              _CHROMIUM_BUILD_URL, sheriff_js, str(e))
         else:
@@ -261,7 +268,7 @@
     retry_waittime = 1
     for _ in range(_MAX_LAB_STATUS_ATTEMPTS):
         try:
-            response = urllib2.urlopen(status_url)
+            response = urllib.request.urlopen(status_url)
         except IOError as e:
             logging.debug('Error occurred when grabbing the lab status: %s.',
                           e)
@@ -400,7 +407,7 @@
 
     """
     views = tko.run('get_detailed_test_views', afe_job_id=suite_job_id)
-    relevant_views = filter(job_status.view_is_relevant, views)
+    relevant_views = list(filter(job_status.view_is_relevant, views))
     if not relevant_views:
         raise Exception('Failed to retrieve job results.')
 
diff --git a/server/site_utils_unittest.py b/server/site_utils_unittest.py
index 07ad1fc..223bf13 100644
--- a/server/site_utils_unittest.py
+++ b/server/site_utils_unittest.py
@@ -1,7 +1,12 @@
+# Lint as: python2, python3
 # Copyright 2015 The Chromium OS Authors. All rights reserved.
 # Use of this source code is governed by a BSD-style license that can be
 # found in the LICENSE file.
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import mox
 import unittest
 
@@ -11,6 +16,7 @@
 from autotest_lib.server import site_utils
 from autotest_lib.server.cros.dynamic_suite import tools
 from autotest_lib.server.cros.dynamic_suite import suite_common
+import six
 
 
 class SiteUtilsUnittests(mox.MoxTestBase):
@@ -85,7 +91,7 @@
                          (sorted(test_views.keys()),
                           sorted(expected_test_views.keys())))
 
-        for test_name, test_status_list in test_views.iteritems():
+        for test_name, test_status_list in six.iteritems(test_views):
             self.assertEqual(sorted(test_status_list),
                              sorted(expected_test_views[test_name]),
                              'For test %s the status list %s does not match '
diff --git a/server/standalone_profiler.py b/server/standalone_profiler.py
index 6324551..4674d59 100644
--- a/server/standalone_profiler.py
+++ b/server/standalone_profiler.py
@@ -1,3 +1,4 @@
+# Lint as: python2, python3
 #
 # Copyright 2007 Google Inc. All Rights Reserved.
 
@@ -7,11 +8,16 @@
 running through autotest.
 """
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 __author__ = '[email protected] (Colby Ranger)'
 
 import platform
 import common
 from autotest_lib.client.common_lib import barrier
+import six
 
 # Client control file snippet used to synchronize profiler start & stop.
 _RUNTEST_PATTERN = ("job.run_test('profiler_sync', timeout_sync=%r,\n"
@@ -24,7 +30,7 @@
 def _encode_args(profiler, args, dargs):
     parts = [repr(profiler)]
     parts += [repr(arg) for arg in args]
-    parts += ["%s=%r" % darg for darg in dargs.iteritems()]
+    parts += ["%s=%r" % darg for darg in six.iteritems(dargs)]
     return ", ".join(parts)
 
 
diff --git a/server/subcommand.py b/server/subcommand.py
index 95053d1..234c81a 100644
--- a/server/subcommand.py
+++ b/server/subcommand.py
@@ -1,9 +1,15 @@
+# Lint as: python2, python3
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 __author__ = """Copyright Andy Whitcroft, Martin J. Bligh - 2006, 2007"""
 
-import sys, os, signal, time, cPickle, logging
+import sys, os, signal, time, six.moves.cPickle, logging
 
 from autotest_lib.client.common_lib import error, utils
 from autotest_lib.client.common_lib.cros import retry
+from six.moves import zip
 
 
 # entry points that use subcommand must set this to their logging manager
@@ -41,7 +47,7 @@
             if status != 0:
                 run_error = True
 
-        results.append(cPickle.load(task.result_pickle))
+        results.append(six.moves.cPickle.load(task.result_pickle))
         task.result_pickle.close()
 
     if return_results:
@@ -91,7 +97,7 @@
         if return_results:
             try:
                 result = function(arg)
-            except Exception, e:
+            except Exception as e:
                 return [e]
             return [result]
         else:
@@ -178,12 +184,12 @@
             for hook in self.fork_hooks:
                 hook(self)
             result = self.func(*self.args)
-            os.write(w, cPickle.dumps(result, cPickle.HIGHEST_PROTOCOL))
+            os.write(w, six.moves.cPickle.dumps(result, six.moves.cPickle.HIGHEST_PROTOCOL))
             exit_code = 0
-        except Exception, e:
+        except Exception as e:
             logging.exception('function failed')
             exit_code = 1
-            os.write(w, cPickle.dumps(e, cPickle.HIGHEST_PROTOCOL))
+            os.write(w, six.moves.cPickle.dumps(e, six.moves.cPickle.HIGHEST_PROTOCOL))
 
         os.close(w)
 
@@ -209,16 +215,16 @@
             raise RuntimeError("Unknown child exit status!")
 
         if self.returncode != 0:
-            print "subcommand failed pid %d" % self.pid
-            print "%s" % (self.func,)
-            print "rc=%d" % self.returncode
-            print
+            print("subcommand failed pid %d" % self.pid)
+            print("%s" % (self.func,))
+            print("rc=%d" % self.returncode)
+            print()
             if self.debug:
                 stderr_file = os.path.join(self.debug, 'autoserv.stderr')
                 if os.path.exists(stderr_file):
                     for line in open(stderr_file).readlines():
-                        print line,
-            print "\n--------------------------------------------\n"
+                        print(line, end=' ')
+            print("\n--------------------------------------------\n")
             raise error.AutoservSubcommandError(self.func, self.returncode)
 
 
@@ -254,10 +260,10 @@
 
             if result is None:
                 utils.nuke_pid(self.pid)
-                print "subcommand failed pid %d" % self.pid
-                print "%s" % (self.func,)
-                print "timeout after %ds" % timeout
-                print
+                print("subcommand failed pid %d" % self.pid)
+                print("%s" % (self.func,))
+                print("timeout after %ds" % timeout)
+                print()
                 result = self.wait()
 
             return result
diff --git a/server/subcommand_unittest.py b/server/subcommand_unittest.py
index 99efd7c..c775f9f 100755
--- a/server/subcommand_unittest.py
+++ b/server/subcommand_unittest.py
@@ -1,12 +1,19 @@
 #!/usr/bin/python2
 # Copyright 2009 Google Inc. Released under the GPL v2
 
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+
 import time, unittest
+import six
 
 import common
 from autotest_lib.client.common_lib import error
 from autotest_lib.client.common_lib.test_utils import mock
 from autotest_lib.server import subcommand
+from six.moves import range
+from six.moves import zip
 
 
 def _create_subcommand(func, args):
@@ -96,7 +103,7 @@
         self.god.stub_function(subcommand.os, 'fork')
         self.god.stub_function(subcommand.os, 'close')
         self.god.stub_function(subcommand.os, 'write')
-        self.god.stub_function(subcommand.cPickle, 'dumps')
+        self.god.stub_function(six.moves.cPickle, 'dumps')
         self.god.stub_function(subcommand.os, '_exit')
 
 
@@ -116,8 +123,8 @@
         subcommand.os.close.expect_call(10)
         fork_hook.expect_call(cmd)
         func.expect_call(1, 2).and_return(True)
-        subcommand.cPickle.dumps.expect_call(True,
-                subcommand.cPickle.HIGHEST_PROTOCOL).and_return('True')
+        six.moves.cPickle.dumps.expect_call(True,
+                six.moves.cPickle.HIGHEST_PROTOCOL).and_return('True')
         subcommand.os.write.expect_call(20, 'True')
         subcommand.os.close.expect_call(20)
         join_hook.expect_call(cmd)
@@ -140,8 +147,8 @@
         subcommand.os.close.expect_call(10)
         func.expect_call(1, 2).and_raises(error)
         subcommand.logging.exception.expect_call('function failed')
-        subcommand.cPickle.dumps.expect_call(error,
-                subcommand.cPickle.HIGHEST_PROTOCOL).and_return('error')
+        six.moves.cPickle.dumps.expect_call(error,
+                six.moves.cPickle.HIGHEST_PROTOCOL).and_return('error')
         subcommand.os.write.expect_call(20, 'error')
         subcommand.os.close.expect_call(20)
         subcommand.os._exit.expect_call(1)
@@ -247,7 +254,7 @@
 class parallel_test(unittest.TestCase):
     def setUp(self):
         self.god = mock.mock_god()
-        self.god.stub_function(subcommand.cPickle, 'load')
+        self.god.stub_function(six.moves.cPickle, 'load')
 
 
     def tearDown(self):
@@ -279,7 +286,7 @@
 
         for task in tasklist:
             task.fork_waitfor.expect_call(timeout=None).and_return(0)
-            (subcommand.cPickle.load.expect_call(task.result_pickle)
+            (six.moves.cPickle.load.expect_call(task.result_pickle)
                     .and_return(6))
             task.result_pickle.close.expect_call()
 
@@ -292,7 +299,7 @@
 
         for task in tasklist:
             task.fork_waitfor.expect_call(timeout=None).and_return(1)
-            (subcommand.cPickle.load.expect_call(task.result_pickle)
+            (six.moves.cPickle.load.expect_call(task.result_pickle)
                     .and_return(6))
             task.result_pickle.close.expect_call()
 
@@ -312,7 +319,7 @@
         for task in tasklist:
             subcommand.time.time.expect_call().and_return(1)
             task.fork_waitfor.expect_call(timeout=timeout).and_return(None)
-            (subcommand.cPickle.load.expect_call(task.result_pickle)
+            (six.moves.cPickle.load.expect_call(task.result_pickle)
                     .and_return(6))
             task.result_pickle.close.expect_call()
 
@@ -325,13 +332,13 @@
         tasklist = self._setup_common()
 
         tasklist[0].fork_waitfor.expect_call(timeout=None).and_return(0)
-        (subcommand.cPickle.load.expect_call(tasklist[0].result_pickle)
+        (six.moves.cPickle.load.expect_call(tasklist[0].result_pickle)
                 .and_return(6))
         tasklist[0].result_pickle.close.expect_call()
 
         error = Exception('fail')
         tasklist[1].fork_waitfor.expect_call(timeout=None).and_return(1)
-        (subcommand.cPickle.load.expect_call(tasklist[1].result_pickle)
+        (six.moves.cPickle.load.expect_call(tasklist[1].result_pickle)
                 .and_return(error))
         tasklist[1].result_pickle.close.expect_call()
 
@@ -385,7 +392,7 @@
 
     def test_default_subdirs_constructor(self):
         func = self.god.create_mock_function('func')
-        args = range(4)
+        args = list(range(4))
         for arg in args:
             subcommand.subcommand.expect_call(
                     func, [arg], str(arg)).and_return(arg)
@@ -397,7 +404,7 @@
 
     def test_nolog_skips_subdirs(self):
         func = self.god.create_mock_function('func')
-        args = range(3)
+        args = list(range(3))
         for arg in args:
             subcommand.subcommand.expect_call(
                     func, [arg], None).and_return(arg)
@@ -409,7 +416,7 @@
 
     def test_custom_subdirs_constructor(self):
         func = self.god.create_mock_function('func')
-        args = range(7)
+        args = list(range(7))
         subdirs = ['subdir%s' % arg for arg in args]
         for arg, subdir in zip(args, subdirs):
             subcommand.subcommand.expect_call(