Browse Source

Upgrade to use Python3 interpreter

lennylxx 7 years ago
parent
commit
d101fc3bd3
1 changed files with 19 additions and 20 deletions
  1. 19 20
      update_hosts.py

+ 19 - 20
update_hosts.py

@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 import os
@@ -153,13 +153,13 @@ class watcher_thread(threading.Thread):
         wn = int(config['threadnum'])
         if wn > total_num:
             wn = total_num
-        print "There are %d threads working..." % wn
-        print "Press 'Enter' to exit.\n"
+        print("There are %d threads working..." % wn)
+        print("Press 'Enter' to exit.\n")
 
         while True:
             if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
-                raw_input()
-                print 'Waiting threads to exit...'
+                input()
+                print("Waiting threads to exit...")
                 global running
                 with thread_lock:
                     running = False
@@ -168,11 +168,10 @@ class watcher_thread(threading.Thread):
             dn = done_num
             outbuf = "Total: %d lines, Done: %d lines, Ratio: %d %%.\r"\
                      % (total_num, dn, dn * 100 / total_num)
-            print outbuf,
-            sys.stdout.flush()
+            print(outbuf, end='', flush=True)
 
             if dn == total_num:
-                print outbuf
+                print(outbuf)
                 break
 
             time.sleep(1)
@@ -186,7 +185,7 @@ def query_domain(domain, tcp):
 
     proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
     out, _ = proc.communicate()
-    outarr = out.splitlines()
+    outarr = out.decode('utf-8').splitlines()
 
     cname = ip = ''
     for v in outarr:
@@ -222,7 +221,7 @@ def validate_ip_addr(ip_addr):
             return False
 
 def print_help():
-    print '''usage: update_hosts [OPTIONS] FILE
+    print('''usage: update_hosts [OPTIONS] FILE
 A simple multi-threading tool used for updating hosts file.
 
 Options:
@@ -232,7 +231,7 @@ Options:
   -t QUERY_TYPE          dig command query type, default: aaaa
   -c, --cname            write canonical name into hosts file
   -n THREAD_NUM          set the number of worker threads, default: 10
-'''
+''')
 
 def get_config():
     shortopts = 'hs:o:t:n:c'
@@ -241,7 +240,7 @@ def get_config():
     try:
         optlist, args = getopt.gnu_getopt(sys.argv[1:], shortopts, longopts)
     except getopt.GetoptError as e:
-        print e, '\n'
+        print(e)
         print_help()
         sys.exit(1)
 
@@ -262,7 +261,7 @@ def get_config():
             sys.exit(0)
 
     if len(args) != 1:
-        print "You must specify the input hosts file (only one)."
+        print("You must specify the input hosts file (only one).")
         sys.exit(1)
 
     config['infile'] = args[0]
@@ -274,8 +273,8 @@ def main():
 
     dig_path = '/usr/bin/dig'
     if not os.path.isfile(dig_path) or not os.access(dig_path, os.X_OK):
-        print "It seems you don't have 'dig' command installed properly "\
-              "on your system."
+        print("It seems you don't have 'dig' command installed properly "\
+              "on your system.")
         sys.exit(2)
 
     global hosts
@@ -283,7 +282,7 @@ def main():
         with open(config['infile'], 'r') as infile:
             hosts = infile.readlines()
     except IOError as e:
-        print e
+        print(e)
         sys.exit(e.errno)
 
     if os.path.exists(config['outfile']):
@@ -292,10 +291,10 @@ def main():
     try:
         outfile = open(config['outfile'], 'w')
     except IOError as e:
-        print e
+        print(e)
         sys.exit(e.errno)
 
-    print "Input: %s    Output: %s\n" % (config['infile'], config['outfile'])
+    print("Input: %s    Output: %s\n" % (config['infile'], config['outfile']))
 
     threads = []
 
@@ -306,7 +305,7 @@ def main():
     worker_num = config['threadnum']
     lines_num = len(hosts)
 
-    lines_per_thread = lines_num / worker_num
+    lines_per_thread = lines_num // worker_num
     lines_remain = lines_num % worker_num
 
     start_pt = 0
@@ -335,7 +334,7 @@ def main():
     try:
         outfile.writelines(hosts)
     except IOError as e:
-        print e
+        print(e)
         sys.exit(e.errno)
 
     sys.exit(0)