关于py的mysql检测代理是否可用的小案例
Posted kings0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于py的mysql检测代理是否可用的小案例相关的知识,希望对你有一定的参考价值。
最近帮朋友做个小工具。想了想。还是python写起来快点。顺便还可以复习下python
具体是从数据库获取到所有需要检测的ip。然后多线程进行扫描。将可用ip和不可用ip区分出来
import socket import pymysql import threading from time import ctime,sleep host="127.0.0.1" user="test" passwd="123456" dbname="testdb" threadCnt=10 #子线程空闲时的休息时间 wait_sec=5 #数据库获取待检测ip的间隔时间 wait_load=60 db=pymysql.connect(host,user,passwd,dbname,port=3306) cursor=db.cursor() iplist=[] iplock=threading.Lock() execlock=threading.Lock() socket.setdefaulttimeout(3) def getip(): iplock.acquire() ip="" if len(iplist) > 0: ip = iplist[0] iplist.remove(ip) iplock.release() return ip def execsql(sql): execlock.acquire() cursor.execute(sql) db.commit() execlock.release() def ping(): while True : ip=getip() if not ip: print("not ip,wait....") sleep(wait_sec) continue print("check ip:"+ip) fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) result = fd.connect_ex((ip, 1080)) if result == 0: print("success "+ip) sql="UPDATE wx_proxy SET valid=2 WHERE ip=""+ip+""" execsql(sql) else: print("error "+ip) sql = "UPDATE wx_proxy SET valid=-1 WHERE ip="" + ip + """ execsql(sql) fd.close() def loadip(): if len(iplist)>0: print("checking....wait") return cursor.execute("SELECT ip FROM wx_proxy WHERE valid!=-1 ORDER BY id DESC") data = cursor.fetchall() for item in data: iplist.append(item[0]) print("add iplist") global timer timer = threading.Timer(wait_load, loadip) timer.start() def main(): global timer timer = threading.Timer(1, loadip) timer.start() threads=[] for i in range(threadCnt): th = threading.Thread(target=ping) threads.append(th) th.setDaemon(True) th.start() for thread in threads: th.join() if __name__=="__main__": main()
以上是关于关于py的mysql检测代理是否可用的小案例的主要内容,如果未能解决你的问题,请参考以下文章