Recently help a friend to make a small tool. Think about it. or Python to write it up faster. By the way, you can review python.
The specific is to obtain from the database to all need to detect the IP. Then the multithreading is scanned. Differentiate between available IP and unavailable IP
ImportSocketImportPymysqlImportThreading fromTimeImportCtime,sleephost="127.0.0.1"User="Test"passwd="123456"dbname="TestDB"threadcnt=10#break time when a child thread is idleWait_sec=5#database Gets the time interval for IP to be detectedWait_load=60DB=pymysql.connect (host,user,passwd,dbname,port=3306) Cursor=db.cursor () iplist=[]iplock=Threading. Lock () Execlock=Threading. Lock () socket.setdefaulttimeout (3)defGetIP (): Iplock.acquire () IP="" ifLen (IPList) >0:ip=iplist[0] Iplist.remove (IP) iplock.release ()returnIPdefexecsql (SQL): Execlock.acquire () cursor.execute (SQL) Db.commit () execlock.release ()defPing (): whileTrue:ip=GetIP ()if notIP: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)) ifresult = =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 ()defLoadip ():ifLen (IPList) >0:Print("checking....wait") returnCursor.execute ("SELECT IP from wx_proxy WHERE valid!=-1 ORDER by ID DESC") Data=Cursor.fetchall () forIteminchdata:iplist.append (item[0])Print("Add IPList") GlobalTimer Timer=Threading. Timer (Wait_load, Loadip) Timer.start ()defMain ():GlobalTimer Timer= Threading. Timer (1, Loadip) Timer.start () Threads=[] forIinchRange (threadcnt): th= Threading. Thread (target=ping) threads.append (TH) Th.setdaemon (True) Th.start () forThreadinchThreads:th.join ()if __name__=="__main__": Main ()
Small case about whether the MySQL detection agent for PY is available