import pymysqlimport requests class GetRandomIP(): def getip(self): conn = pymysql.connect(host="localhost", port=3306, user="root", password="liuzhiyu", db="python77", charset="utf8") cursor = conn.cursor() sql=‘select type,ip,port from ipcontent ORDER BY RAND() LIMIT 1‘ cursor.execute(sql) rows = cursor.fetchall()[0] type_ipaddress_port = ‘{}://{}:{}‘.format(rows[0], rows[1], rows[2]) print(‘*‘*50,end=‘\n\n‘) print(‘从数据库中获取的IP为: {}‘.format(type_ipaddress_port)) self.yz_ip(rows[0],rows[1],type_ipaddress_port) cursor.close() conn.close() def yz_ip(self,type,ipaddress,type_ipaddress_port): print(‘验证ip: {} 是否有效:‘.format(type_ipaddress_port)) try: response=requests.get(‘http://www.baidu.com‘,proxies={type:type_ipaddress_port}) #proxies={‘HTTP‘:‘HTTP://106.56.102.139:8070‘} except Exception as e: print(‘ip: {} 无效,将从数据库中删除‘.format(ipaddress)) self.del_ip(ipaddress) else: print(‘状态码: {} (200代表有效)‘.format(response.status_code),end=‘\n\n‘) def del_ip(self,ip): conn = pymysql.connect(host="localhost", port=3306, user="root", password="liuzhiyu", db="python77", charset="utf8") cursor = conn.cursor() sql = ‘delete from ipcontent where ip=%s‘ cursor.execute(sql,(ip)) conn.commit() print(‘删除ip: {} 成功‘.format(ip),end=‘\n\n‘) cursor.close() conn.close() if __name__ == ‘__main__‘: gr = GetRandomIP() while 1: gr.getip()
原文地址:https://www.cnblogs.com/inorilzy/p/9574134.html
时间: 2024-10-03 14:45:33