onlyou13

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
# _*_ coding:utf-8 _*_

import urllib2
import re


class TestProxy(object):
    def __init__(self, timeout):
        self.url = 'http://www.baidu.com'
        self.timeout = timeout
        self.regex = re.compile(r'baidu.com')

    def TestIt(self, ip, port):
        server = 'http://' + ip + ':' + port

        opener = urllib2.build_opener(urllib2.ProxyHandler({'http': server}))
        urllib2.install_opener(opener)
        try:
            response = urllib2.urlopen(self.url, timeout=self.timeout)
        except:
            print '%s connect failed' % server
            return False
        else:
            try:
                str = response.read()
            except:
                print '%s connect failed' % server
                return False
            print str
            if self.regex.search(str):
                print '%s connect success .......' % server
                print ip + ':' + port
        return True


if __name__ == '__main__':
    proxylist = [("113.118.97.119", "9797"), ("221.222.245.208","9797"), ("61.155.164.110", "3128")]

    Tp = TestProxy(5)
    for item in proxylist:
        Tp.TestIt(item[0], item[1])

 

posted on 2017-12-21 14:11  onlyou13  阅读(1224)  评论(0编辑  收藏  举报