python脚本–获取可用http代理
在安全测试中,竟然会遇到IP被拦截的情况,在这种情况下,我们除了换IP继续测试之外好像没什么其他方法了,代理在这个时候就非常有用了。
如何获取可用的IP列表呢,互联网上有很多公开的http代理网站, 这里我以 "http://www.xicidaili.com/nn/" 为例,网站上每天都公布很多可用的IP,但等到我们想用的时候可能已经无法使用了。
我们一个一个去测试是不是有点低效呢?今天就在这里给大家拼凑了一段代码,让大家快速找到可用的http代理服务器。
=============分割线=============
以下是代码片段: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author: IcySun # 脚本',this.id)">脚本功能:获取可用http代理 import urllib2 import socket from bs4 import BeautifulSoup def IsOpen(ip,port): s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: s.connect((ip,int(port))) s.shutdown(2) return True except: return False header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0'} url = 'http://www.xicidaili.com/nn/' hurl = urllib2.Request(url,headers=header) html_doc = urllib2.urlopen(hurl).read() soup = BeautifulSoup(html_doc) trs = soup.find('table', id='ip_list').find_all('tr') for tr in trs[1:]: tds = tr.find_all('td') ip = tds[2].text.strip() port = tds[3].text.strip() protocol = tds[6].text.strip() if protocol == 'HTTP' or protocol == 'HTTPS': print ip if IsOpen(ip, port): print ip + ":" + port + " is OK" with open("ava.txt","a") as ava: ava.write(ip + ":" + port + "/n") |
执行结果如下:


我从里面随机找了个IP测试OK。

编译好的工具下载地址:链接:http://pan.baidu.com/s/1gem9q4z 密码:4iph