简体   繁体   中英

Proxy tester with threading

I have written a simple proxy checker in Python. I have a one question - how to get speed up it? I trying to use queue and threading, but it didn't work. If it is possible use threading lib

Code:

import socks

red = "\033[1;31m"
green = "\033[1;32m"
yellow = "\033[1;33m"
blue = "\033[1;34m"
defaultcolor = "\033[0m"

linesinfile = len(open('proxy.txt', 'rU').readlines())

def proxygff():
    global fp
    with open('proxy.txt') as fp:
        pff = fp.readline()
        global cnt
        cnt = 1
        while pff:
            global PROXYIP, PROXYPORT
            PROXYIP, PROXYPORT = pff.split(":")
            pff = fp.readline()
            cnt = cnt + 1
            checkmain()


def savetofile(proxy):
    with open("proxywork.txt", 'a') as file:
        file.write(proxy)


def checkmain():
    try:
        socks.setdefaultproxy(socks.SOCKS5, str(PROXYIP), int(PROXYPORT))
        s = socks.socksocket()
        s.settimeout(1)
        s.connect(("google.com", 80))
        print blue + "Proxy working! " + green + str(cnt) + defaultcolor + "/" + green + str(linesinfile)
        proxy = PROXYIP + ":" + PROXYPORT
        savetofile(proxy)
        s.close()
    except:
        print red + "Proxy not working! " + green + str(cnt) + defaultcolor + "/" + green + str(linesinfile)


proxygff()

Written in Python 2.7

Python's multithreading will help you only when you creating a connection to google.com and waiting a response from them. Python interpreter runs only one thread at time due to GIL . You can do the same things (avoid IO waitings) with asyncio module (Python >= 3.4). And you have to do rewrite your code because you set up default proxy globally socks.setdefaultproxy for all opening sockets.

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