G
G
Goodron2018-07-07 16:59:45
Python
Goodron, 2018-07-07 16:59:45

How to make the inscription about the end of the program output only after the end of the work, and not in the process?

Wrote a very primitive but multi-threaded port scanner that checks if a port is open or not.
The problem is this:
5b40c6e5deb8c423732584.png
That is, the program first displays a completion message, and then displays the results of its work.
Here is the code:

import socket
import sys
import threading
import requests, shutil, os

host = input('IP or host name: ')

print('--------------------------------------')
print('#########')
print('#Scaning#')
print('#########' + '\n')

def PortCheck(port):
    # Запускаем проверку порта
    s = socket.socket()
    s.settimeout(1)
    try:
        s.connect((host, port))
    except socket.error:
        pass
    else:
        s.close
        print(str(port) + ' is open' + '\n')

ports = [20, 21, 22, 23, 25, 42, 43, 53, 67, 69, 80, 110,
         115, 123, 137, 138, 139, 143, 161, 179, 443, 445,
         514, 515, 993, 995, 1080, 1194, 1433, 1702, 1723,
         3128, 3268, 3306, 3389, 5432, 5060, 5900, 5938, 8080, 10000, 20000]

for port in ports:
    threading.Thread(target=PortCheck, args = [port]).start()

print('--------------------------------------')
input('Process ended. Press Enter.' + '\n')

How to make it so that first a list of open ports is displayed, and then an inscription about the end of the program?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2018-07-07
@AlexDarkStalker

for port in ports:
    threading.Thread(target=PortCheck, args = [port]).start()

for t in threading.enumerate():          # Получаем все потоки
    if t != threading.current_thread():  # Если это не текущий поток
        t.join()                         # Ожидаем его завершения

print('--------------------------------------')
input('Process ended. Press Enter.' + '\n')

I
Igor, 2018-07-07
@hostmaster

also atexit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question