Small case about whether the MySQL detection agent for PY is available

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.