批量获取及验证HTTP代理的Python脚本

yipeiwu_com5年前Python基础

HTTP暴力破解、撞库,有一些惯用的技巧,比如:

1. 在扫号人人网时,我遇到单个账号错误两次,强制要求输入验证码,而对方并未实施IP策略。

我采用维护10万(用户名,密码) 队列的方式来绕过验证码。具体的做法是,当某个用户名、密码组合遇到需要验证码,就把该破解序列挂起,放到队列尾部等待下次测试,继续破解其他账号密码。

这样就可以保证2/3的时间都在进行正常破解和扫号。

2. 在破解美团网某系统账号时,我遇到了单个IP访问有一定限制,请求频率不可过快。于是我挂了72个 HTTP代理来解决这个问题。 看似每个IP的请求都正常,但其实从整个程序上看,效率还是挺可观的。

本篇我发出自己抓HTTP的脚本片段,其实只有几行。匿名代理是从这里抓取的:http://www.xici.net.co/nn/

首先获取代理列表 :

from bs4 import BeautifulSoup
import urllib2


of = open('proxy.txt' , 'w')

for page in range(1, 160):
  html_doc = urllib2.urlopen('http://www.xici.net.co/nn/' + str(page) ).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[1].text.strip()
    port = tds[2].text.strip()
    protocol = tds[5].text.strip()
    if protocol == 'HTTP' or protocol == 'HTTPS':
      of.write('%s=%s:%s\n' % (protocol, ip, port) )
      print '%s=%s:%s' % (protocol, ip, port)

of.close()

接着验证代理是否可用,因为我是用于破解美团网系统的账号,因此用了美团的页面标记:

#encoding=gbk
import httplib
import time
import urllib
import threading

inFile = open('proxy.txt', 'r')
outFile = open('available.txt', 'w')

lock = threading.Lock()

def test():
  while True:
    lock.acquire()
    line = inFile.readline().strip()
    lock.release()
    if len(line) == 0: break
    protocol, proxy = line.split('=')
    headers = {'Content-Type': 'application/x-www-form-urlencoded',
      'Cookie': ''}
    try:
      conn = httplib.HTTPConnection(proxy, timeout=3.0)
      conn.request(method='POST', url='http://e.meituan.com/m/account/login', body='login=ttttttttttttttttttttttttttttttttttttt&password=bb&remember_username=1&auto_login=1', headers=headers )
      res = conn.getresponse()
      ret_headers = str( res.getheaders() ) 
      html_doc = res.read().decode('utf-8')
      print html_doc.encode('gbk')
      if ret_headers.find(u'/m/account/login/') > 0:
        lock.acquire()
        print 'add proxy', proxy
        outFile.write(proxy + '\n')
        lock.release()
      else:
        print '.',
    except Exception, e:
      print e

all_thread = []
for i in range(50):
  t = threading.Thread(target=test)
  all_thread.append(t)
  t.start()
  
for t in all_thread:
  t.join()

inFile.close()
outFile.close()

相关文章

Python http接口自动化测试框架实现方法示例

Python http接口自动化测试框架实现方法示例

本文实例讲述了Python http接口自动化测试框架实现方法。分享给大家供大家参考,具体如下: 一、测试需求描述 对服务后台一系列的http接口功能测试。 输入:根据接口描述构造不同的...

python中map()与zip()操作方法

对于map()它的原型是:map(function,sequence),就是对序列sequence中每个元素都执行函数function操作。 比如之前的a,b,c = map(int,r...

python PIL模块与随机生成中文验证码

python PIL模块与随机生成中文验证码

在这之前,你首先得了解Python中的PIL库。PIL是Python Imaging Library的简称,PIL是一个Python处理图片的库,提供了一系列模块和方法,比如:裁切,平移...

Django使用中间件解决前后端同源策略问题

Django使用中间件解决前后端同源策略问题

问题描述 前端时间在公司的时候,要使用angular开发一个网站,因为angular很适合前后端分离,所以就做了一个简单的图书管理系统来模拟前后端分离。 但是在开发过程中遇见了同源策略...

Python字典的基本用法实例分析【创建、增加、获取、修改、删除】

本文实例讲述了Python字典的基本用法。分享给大家供大家参考,具体如下: 字典是一系列的键值对 。 每个键都与一个值相关联, 我们可以使用键来访问与之相关联的值。 与键相关联的值可以任...