在python脚本中从多个线程获取顺序输出

2024-05-29 05:45:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个多线程脚本,它针对参数中给定的IP地址运行mtr,一个类似traceroute的网络诊断工具,并使用MaxMind GeoIP数据库在mtr的输出中查找IP的位置。在

使用我的脚本,我得到混合输出,因为所有线程都在同一时间写入同一个STDOUT。 我不想使用锁,因为它会减慢脚本的执行速度。在

以下是我的剧本_最终.py)公司名称:

from subprocess import Popen, PIPE, STDOUT
import threading
from Queue import Queue
import geoip2.database
import geoip2.errors
import argparse
import logging
import re
from termcolor import colored
import time


queue = Queue()
FORMAT = '[%(levelname)s] (%(threadName)-7s) %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT,)

parser = argparse.ArgumentParser(prefix_chars="-+")
parser.add_argument("-d", "--dest", help="Destination IP Addresses", action="append", dest="ipd", default=[])
args = parser.parse_args()


def check_geo_country(ip):
    try:
        reader_country = geoip2.database.Reader('xxx')
        response_country = reader_country.country(ip)
        mycountry = response_country.country.name
        reader_country.close()
        return mycountry
    except (geoip2.errors.AddressNotFoundError, ValueError):
        pass


def check_geo_city(ip):
    try:
        reader_city = geoip2.database.Reader('xxx')
        response_city = reader_city.city(ip)
        mycity = response_city.city.name
        reader_city.close()
        return mycity
    except (geoip2.errors.AddressNotFoundError, ValueError):
        pass


def yoyo(q):
    while True:
        myip = q.get()
        logging.info('Started')
        cmd = "mtr --no-dns -rwc 10 %s" % myip
        op = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=True).communicate()[0]
        for line in op.split("\n"):
            ip_mtr = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
            if ip_mtr:
                ip_final = ''.join(ip_mtr)
                ctry = check_geo_country(ip_final)
                cty = check_geo_city(ip_final)
                country = colored(ctry, 'yellow', attrs=['bold'])
                city = colored(cty, 'magenta', attrs=['bold'])
                print "%s----{ %s }----{ %s }" % (line, country, city)
            else:
                print "%s" % line
        q.task_done()


num_of_threads = len(args.ipd)

for num in range(num_of_threads):
    t = threading.Thread(target=yoyo, args=(queue,))
    t.setDaemon(True)
    t.start()

for arguments in args.ipd:
    queue.put(arguments)

queue.join()
print "Done"

我使用的命令是:-

^{pr2}$

我得到的输出是:

[INFO] (Thread-3) Started
[INFO] (Thread-2) Started
[INFO] (Thread-1) Started
[INFO] (Thread-4) Started

Start: Tue Apr 26 13:37:23 2016
HOST: L-ubuntu       Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- 10.37.144.254  40.0%    10  106.8  63.6   5.9 125.4  53.2----{ None }----{ None }
  2.|-- 182.75.202.145  0.0%    10   43.3  14.7   3.4  54.2  18.5----{ India }----{ None }
  3.|-- 182.79.245.185  0.0%    10  172.4 129.7  95.3 188.6  31.7----{ India }----{ None }
  4.|-- 103.4.97.138    0.0%    10   82.8 109.4  80.7 272.5  58.8----{ Singapore }----{ None }
  5.|-- 173.252.65.188  0.0%    10  173.9 101.7  78.2 173.9  29.6----{ United States }----{ Menlo Park }
Start: Tue Apr 26 13:37:23 2016
HOST: L-ubuntu       Loss%   Snt   Last   Avg  Best  Wrst StDev
Start: Tue Apr 26 13:37:23 2016
HOST: L-ubuntu       Loss%   Snt   Last   Avg  Best  Wrst StDev
Start: Tue Apr 26 13:37:23 2016
HOST: L-ubuntu       Loss%   Snt   Last   Avg  Best  Wrst StDev
  1.|-- 10.37.144.254  50.0%    10    6.1  11.9   2.8  20.5   7.3----{ None }----{ None }
  6.|-- 173.252.66.16  20.0%    10  108.5  91.9  80.6 108.5   9.0----{ United States }----{ Menlo Park }
  1.|-- 10.37.144.254  50.0%    10    5.3  16.7   2.8  34.5  12.9----{ None }----{ None }
  1.|-- 10.37.144.254  50.0%    10    5.3  10.2   3.2  18.5   6.1----{ None }----{ None }
  2.|-- 182.75.202.145  0.0%    10   39.2  44.5   4.8 279.5  84.0----{ India }----{ None }
  2.|-- 182.75.202.145  0.0%    10   34.7  38.7   3.0 274.8  83.8----{ India }----{ None }
  2.|-- 182.75.202.145  0.0%    10   35.4  39.3   3.5 270.8  82.1----{ India }----{ None }
  7.|-- 31.13.79.220    0.0%    10  117.4 114.3  90.4 163.5  27.2----{ Ireland }----{ None }

  3.|-- 182.79.224.77   0.0%    10   14.9  30.4   8.5 167.1  48.5----{ India }----{ None }
  3.|-- 182.79.245.66   0.0%    10  101.0 127.9  93.4 270.8  53.5----{ India }----{ None }
  3.|-- 182.79.255.105  0.0%    10   14.7  29.9   8.6 165.8  48.2----{ India }----{ None }
  4.|-- 27.111.228.131 20.0%    10  347.9 287.3 257.4 347.9  29.5----{ Singapore }----{ Ayer Raja New Town }
  4.|-- 72.14.205.93    0.0%    10  132.4  76.1  42.6 132.4  30.5----{ United States }----{ Mountain View }
  4.|-- 72.14.205.93    0.0%    10  132.6  73.4  44.8 132.6  29.7----{ United States }----{ Mountain View }
  5.|-- 203.84.209.77   0.0%    10  139.6 128.3 106.8 181.4  22.4----{ Hong Kong }----{ None }
  5.|-- 66.249.95.106   0.0%    10   61.6  72.2  51.4 136.3  26.5----{ United States }----{ Mountain View }
  6.|-- 106.10.128.7    0.0%    10  183.2 112.8  89.5 183.2  36.4----{ Singapore }----{ None }
   5.|-- 66.249.95.106   0.0%    10   58.6  79.0  51.8 136.2  30.2----{ United States }----{ Mountain View }
  6.|-- 209.85.142.129  0.0%    10  167.1  86.0  58.1 167.1  31.9----{ United States }----{ Mountain View }
  7.|-- 106.10.128.17   0.0%    10  117.4 120.3  88.8 286.2  60.0----{ Singapore }----{ None }
  6.|-- 209.85.142.129  0.0%    10  155.3  86.4  62.5 155.3  27.5----{ United States }----{ Mountain View }
  7.|-- 72.14.239.25    0.0%    10  101.8  95.8  62.6 266.7  61.2----{ United States }----{ Mountain View }
  8.|-- 106.10.128.71   0.0%    10  101.7 135.5  89.9 242.2  52.2----{ Singapore }----{ None }
  7.|-- 72.14.239.25    0.0%    10  103.0  93.3  63.5 257.8  59.1----{ United States }----{ Mountain View }
  8.|-- 209.85.143.5    0.0%    10   84.6 113.1  69.1 200.2  47.8----{ United States }----{ Mountain View }
  9.|-- 106.10.138.240  0.0%    10  111.8 109.9  92.7 126.9  14.2----{ Singapore }----{ None }
   8.|-- 209.85.143.5    0.0%    10   83.6 104.7  70.3 196.2  47.0----{ United States }----{ Mountain View }

  9.|-- 216.58.199.164  0.0%    10   59.0  65.8  57.7  84.4   8.9----{ United States }----{ Mountain View }

   9.|-- 216.58.199.174  0.0%    10   55.0  63.7  53.9  79.8   9.5----{ United States }----{ Mountain View }

Done

这个输出是来自所有线程的混合mtr行。我希望按顺序输出,而不出现任何混乱。请帮助引导我解决这个问题。我是python新手,正在努力学习。在


Tags: importipnoneviewcityargscountryreader

热门问题