我用Python编写了一个简单的代理检查器。我有一个问题-如何加快速度?我试图使用队列和线程,但是没有用。 如果可能的话,使用线程库
代码:
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()
用Python 2.7编写
答案 0 :(得分:0)
仅当您创建与google.com
的连接并等待它们的响应时,Python的多线程才会帮助您。由于GIL,Python解释器一次只运行一个线程。您可以使用asyncio模块(Python> = 3.4)执行相同的操作(避免IO等待)。而且您必须重写代码,因为您为所有打开的套接字全局设置了socks.setdefaultproxy
默认代理。