码迷,mamicode.com
首页 > 数据库 > 详细

关于py的mysql检测代理是否可用的小案例

时间:2018-08-27 01:01:44      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:cut   子线程   nbsp   global   execl   read   col   sql   fetchall   

最近帮朋友做个小工具。想了想。还是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检测代理是否可用的小案例

标签:cut   子线程   nbsp   global   execl   read   col   sql   fetchall   

原文地址:https://www.cnblogs.com/kings0/p/9539661.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!