满眼喜欢990 2024-05-08 17:28 采纳率: 0%
浏览 0

python2.7低版本多线程问题请教

求解python2.7版本的一些问题,关于多线程活跃主机扫描的,代码跑起来就停止不了了,不知道为什么

img

#!/usr/bin/env python
#coding:utf-8
# Author: Zeroh

import os
import platform
import Queue
import threading
from IPy import IP
import pymysql
 
import time
 
printLock = threading.Semaphore(1)  #lock Screen print
TimeOut = 5 
 
def get_os():
    '''
    get os 类型
    '''
    os = platform.system()
    if os == "Windows":
        return "n"
    else:
        return "c"
 
 
class find_ip():
    '''
    给出当前的127.0.0 ,然后扫描整个段所有地址
    '''
    def __init__(self,cidr,threads_num):
        self.threads_num = threads_num
        self.cidr = IP(cidr)
    #build ip queue
        self.IPs = Queue.Queue()
        for ip in self.cidr:
            ip = str(ip)
            self.IPs.put(ip)
    
    def request(self):
        with threading.Lock():  # 注意这里的改动
            while self.IPs.qsize() > 0:
                ip = self.IPs.get()
                try:
                    cmd = ["ping", "-n" if get_os() == "n" else "-c", "1", ip]  # 注意这里的改动
                    output = os.popen(" ".join(cmd)).readlines()
                    flag = False
                    for line in output:
                        if line.upper().find("TTL") >= 0:
                            flag = True
                            break
                    if flag:
                        print("ip: %s is ok ***" % ip)  # 注意这里的改动
                except Exception as e:  # 注意这里的改动
                    printLock.acquire()
                    print("Exception occurred: %s" % e)  # 打印异常信息
                    printLock.release()
 
    def run(self):
        for i in range(self.threads_num):
            t = threading.Thread(target=self.request)
            t.start()    
    
 
def main(cidr):
    # print cidr
    print "start time %s" % time.ctime()
    options={'threads_num': 200}
    s = find_ip(cidr,threads_num=options['threads_num'])
    s.run()
    print "end time %s" % time.ctime()
    return
 
# main('127.0.0.1')
main('192.168.242.0/24')

  • 写回答

4条回答 默认 最新

  • 专家-皆知 2024-05-08 17:28
    关注

    引自免费微信小程序:皆我百晓生

    请提供具体的代码和错误信息,以便分析和解决问题。

    评论

报告相同问题?

问题事件

  • 修改了问题 5月8日
  • 创建了问题 5月8日

悬赏问题

  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑