for loop in thread runs once in Python 3

J.K

I've written a Python script to fetch certificates of a list of IP address to match a domain:

#! /usr/bin/env python3

import ssl
import socket
import argparse
from threading import Thread, Lock
from itertools import islice

class scanThread(Thread):
    def __init__(self,iplist, q, hostname, port):
        Thread.__init__(self)
        self.iplist = iplist
        self.hostname = hostname
        self.port = port
        self.queue = q

    def dummy(self,ip):
        print("Running dummy")

    def checkCert(self, ip):
        print('Processing IP: %s' % ip )
        ctx = ssl.create_default_context()
        s = ctx.wrap_socket(socket.socket(), server_hostname=self.hostname)
        try:
            s.connect((ip, self.port))
            cert = s.getpeercert()
            if cert['subjectAltName'][0][1].find(hostname) != -1:
                return ip
        except (ssl.CertificateError, ssl.SSLError):
            print('Ignore: %s' % ip)
        finally:
            s.close()
            return

    def run(self):
        for ip in self.iplist:
            returnIP = self.checkCert(ip)
            if returnIP:
                self.queue.append(ip)

def main(l, hostname, port):
    iplist = []
    threads = []
    hostPool = []
    with open(l,'r') as f:
        #while True:
        iplist.extend([f.readline().strip() for x in islice(f, 10000)])
        #print(iplist)
        t = scanThread(iplist, hostPool, hostname, port)
        t.start()
        threads.append(t)
        iplist.clear()

    for t in threads:
        t.join()

    for h in hostPool:
        print(h)




if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("hostname",help="root hostname")
    parser.add_argument("-l","--list",required=True, help="IP list for scanning")
    parser.add_argument("-p","--port", nargs='?', const=443, default=443, type=int, help="port to scan")
    arg = parser.parse_args()
    main(arg.list,arg.hostname, arg.port)

I just comment out while loop in main function, thus the script creates one thread and scans 10,000 IPs.

Taking 'google.com' for example, it has numerous IP addresses worldwide:

./google.py -l 443.txt google.com

Sample output:

Processing IP: 13.76.139.89
Ignore: 13.76.139.89

After some tests, I'm pretty sure that the for ... in loop in scanThread.run() executed one time. Did I do something inappropriate in this snippet code?

Arun Ghosh

This might be because you are clearing the list in the main function.

    t = scanThread(iplist, hostPool, hostname, port)
    t.start()
    threads.append(t)
    iplist.clear() // here you are clearing.

Can you try:

class scanThread(Thread):
    def __init__(self,iplist, q, hostname, port):
        Thread.__init__(self)
        self.iplist = list(iplist)

self.iplist = list(iplist) this is make a copy of the list, rather than using the list which is passed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

for loop runs only once (python)

From Dev

for loop runs only once in angularjs

From Dev

Foreach loop only runs once

From Dev

For loop in Javascript runs only once

From Dev

Java - For loop only runs once

From Dev

PHP for loop only runs once

From Dev

why the while loop runs once?

From Dev

Run 3 variables at once in a python for loop.

From Dev

Python function only runs once

From Dev

Tensorflow while loop runs only once

From Dev

java for loop that runs list executes only once

From Dev

python watchdog runs more than once

From Dev

python watchdog runs more than once

From Dev

Iterating tab delimited file fails (loop runs only once)

From Dev

My Tkinter button command only runs through the loop once

From Dev

Control a loop in thread in python

From Dev

Why does this loop in python runs progressively slower?

From Dev

Making a Queue for a function so it only runs once at a time in python

From Dev

How to run a thread more than once in python

From Dev

How to run a thread more than once in python

From Dev

How to make a delayed thread that runs only once modifying some UI elements in JavaFX?

From Dev

Kotlin coroutines - async withTimeout which stops blocking the thread once it runs out of time

From Dev

python3: do a for loop once more when looped over the whole file

From Dev

Java: for loop executes once even after interruption of current thread

From Dev

Java while(true) loop executes only once inside thread

From Dev

Thread's while (true) loop only running once

From Dev

Python:For loop only works once in python

From Dev

iPython runs on Python 3 instead of Python 2

From Dev

setInterval just runs once

Related Related

HotTag

Archive