简体   繁体   中英

python3 threading output help. Is this correct output?

This is the source code to my program, but to me the output looks kind of funny but it works so I am still happy with it. But if I am not using threads right than I would like to know so I can get it working the proper way.

import os
import time
import threading
import urllib.request


max_threads = 10


RED   = '\033[31m'
GREEN = '\033[32m'
ESC   = '\033[0m'


def check(proxy):
    proxy_support = urllib.request.ProxyHandler({'https':proxy})
    opener = urllib.request.build_opener(proxy_support)
    urllib.request.install_opener(opener)
    print(end='\r' + time.strftime('[%H:%M:%S]')+" ~ Trying => " +proxy)
    try:
        urllib.request.urlopen("https://www.google.com", timeout=5)
        time.sleep(1)
        print(end='\r'+time.strftime('[%H:%M:%S]')+" ~"+GREEN+" Good [!] "+ESC +proxy)
        time.sleep(1)        
        with open("CheckedProxies.txt", "a") as appe:
            appe.write(proxy.replace("\n","") + "\n")
    except:
        time.sleep(1)
        print(end='\r'+time.strftime('[%H:%M:%S]')+" ~"+RED+" Bad [!] "+ESC +proxy)
        time.sleep(1)
        pass




try:
    proxies = open("/home/zion/Desktop/proxies.txt", "r").readlines()
except:
    print("File Empty Exiting!")
    exit()

if proxies == "":
    print("File Empty, Enter Proxies In proxies.txt File")

newtxt = open("CheckedProxies.txt","w")
print("Loading "+ str(len(proxies)) +" Proxies From Text File[!]")
time.sleep(3)
for proxy in proxies:
    threading.Thread(target=check, args=(proxy,)).start()
    while threading.activeCount() >= max_threads:
        time.sleep(1)


os.exit()

and here is the output from my program....

[02:28:02] ~ Trying => 1.0.135.34:8080
[02:28:02] ~ Trying => 1.10.236.214:8080
[02:28:02] ~ Trying => 103.122.255.18:8080
[02:28:02] ~ Trying => 101.231.104.82:80
[02:28:02] ~ Trying => 102.176.160.109:8080
[02:28:02] ~ Trying => 1.179.144.181:8080
[02:28:02] ~ Trying => 103.10.228.221:8080
[02:28:02] ~ Trying => 101.255.40.38:47638
[02:28:02] ~ Trying => 101.108.110.95:3128
[02:28:03] ~ Bad [!] 1.0.135.34:8080
[02:28:03] ~ Bad [!] 101.255.40.38:47638
[02:28:03] ~ Bad [!] 103.10.228.221:8080
[02:28:03] ~ Bad [!] 1.10.236.214:8080
[02:28:03] ~ Bad [!] 101.231.104.82:80
[02:28:05] ~ Trying => 103.215.200.125:8080
[02:28:05] ~ Trying => 101.108.102.231:8080

I thought it would be more like this

[02:28:02] ~ Trying => 127.0.0.1:8080
[02:28:03] ~ Bad [!] 127.0.0.1:80
[02:28:02] ~ Trying => 127.0.0.1:8080
[02:28:03] ~ Bad [!] 127.0.0.1:80
[02:28:02] ~ Trying => 127.0.0.1:47638
[02:28:03] ~ Bad [!] 127.0.0.1:80
[02:28:02] ~ Trying => 127.0.0.1:3128

I don't know why you expect synchronous execution when using multiple threads.

To make this code better:

  • use logging instead of printing strftime
  • use thread pool instead of while loop with sleep
  • use requests rather than urllib
  • use f-string formatting rather than concatenation
  • open files using with to make sure they are properly closed
import logging
from concurrent.futures.thread import ThreadPoolExecutor
from typing import Tuple

import requests


def is_proxy_ok(proxy: str) -> bool:
    try:
        logging.info(f"Trying {proxy}")
        proxies = {"https": proxy, "http": proxy}
        response = requests.get("https://www.google.com", proxies=proxies, timeout=5)
        response.raise_for_status()
        logging.info(f"OK {proxy}")
        return True
    except requests.RequestException:
        logging.info(f"Bad {proxy}")
        return False


def check_proxy(proxy: str) -> Tuple[bool, str]:
    return is_proxy_ok(proxy), proxy


if __name__ == "__main__":
    logging.basicConfig(format="%(asctime)-15s - %(message)s", level=logging.INFO)
    with open("/home/zion/Desktop/proxies.txt") as proxy_file:
        proxies = [line.strip() for line in proxy_file]
    print(f"Loading {len(proxies)} Proxies From Text File[!]")
    working_proxies = []
    with ThreadPoolExecutor(max_workers=25) as executor:
        for is_working, proxy in executor.map(check_proxy, proxies):
            if is_working:
                working_proxies.append(proxy)
    with open("working_proxies.txt", "w") as out_file:
        print("\n".join(working_proxies), file=out_file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM