美文网首页
大象代理相关Demo

大象代理相关Demo

作者: 裴general | 来源:发表于2018-10-30 16:24 被阅读0次
    import time
    import redis
    import requests
    
    # url_200 大象代理api链接
    url_200 = ''
    # 本地redis
    rclient = redis.Redis(host='127.0.0.1', port='6379')
    
    
    def get_ips(url):
        # 获取ip
        print(url)
        res = requests.get(url, proxies={'http': 'http://10.0.0.50:9999'}).text
        ips = []
        for i in res.split('\r\n'):
            info = {
                'http': 'http://' + i,
                'https': 'http://' + i
            }
            ips.append(info)
        # 测试ip是否可用,并将其加入redis中
        print('******', len(ips))
        for i in ips:
            try:
                response = requests.get('http://47.106.77.82/', proxies=i, timeout=5)
                if response.status_code == 200:
                    print('添加成功')
                    rclient.sadd('zhongyi_ipools', i)
            except Exception as e:
                print(e)
    
    
    def run_ips():
        for i in rclient.smembers('zhongyi_ipools'):
            try:
                response = requests.get('http://www.baidu.com', proxies=eval(i), timeout=5)
                if response.status_code == 200:
                    print('pass-test: %s' % eval(i))
            except Exception as e:
                print(e)
                rclient.srem('zhoongyi_ipools', eval(i))
    
    
    def main():
        while True:
            print(rclient.scard('zhongyi_ipools'))
            if rclient.scard('zhongyi_ipools') < 200:
                print('------------------------------')
                get_ips(url_200)
            print('A round of exams finally')
            time.sleep(10)
    
    
    if __name__ == '__main__':
        main()
    

    相关文章

      网友评论

          本文标题:大象代理相关Demo

          本文链接:https://www.haomeiwen.com/subject/dvwgtqtx.html