import re
import time
import requests as rq
from requests.exceptions import ConnectionError, ReadTimeout
from adslproxy.db import RedisClient
from adslproxy.config import *
import platform
from random import choice

if platform.python_version().startswith('2.'):
    import commands as subprocess
elif platform.python_version().startswith('3.'):
    import subprocess
else:
    raise ValueError('python version must be 2 or 3')


class Sender():
    def __init__(self):
        self.proxy = None
        self.headers = [
            {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36'},
            {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'},
            {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'}
        ]
        
    def get_ip(self, ifname=ADSL_IFNAME):
        (status, output) = subprocess.getstatusoutput('ifconfig')
        if status == 0:
            pattern = re.compile(ifname + '.*?inet.*?(\d+\.\d+\.\d+\.\d+).*?netmask', re.S)
            result = re.search(pattern, output)
            if result:
                ip = result.group(1)                    
                return ip                    

    def test_proxy(self, proxy):
        try:
            if proxy != self.proxy:
                print('start texting')
                headers = choice(self.headers)
                proxies = {'http':'http://'+proxy}
                html = rq.get(TEST_URL,proxies=proxies,headers=headers,timeout = 20)
                if html.status_code == 200:
                    if self.get_yanzhengma(html.text):
                        self.proxy = '{}\t{}'.format(proxy,headers['User-Agent'])
                        return True
                else:
                    self.proxy = proxy
                    return False
            else:
                self.proxy = proxy
                return False
        except:
            return False

    def remove_proxy(self):
        self.redis = RedisClient()
        self.redis.remove(CLIENT_NAME)
        print('Successfully Removed Proxy')

    def set_proxy(self, proxy):
        self.redis = RedisClient()
        if self.redis.set(CLIENT_NAME, proxy):
            print('Successfully Set Proxy', proxy)
    
    def get_yanzhengma(self,html):
        n = re.findall('Robot Check',html)
        if not n:
            return True
        else:
            return False

    def adsl(self):
        while True:
            print('ADSL Start, Please wait')           
            (status, output) = subprocess.getstatusoutput(ADSL_BASH)
            if status == 0:
                print('ADSL Successfully')
                ip = self.get_ip()
                if ip:
                    proxy = '{ip}:{port}'.format(ip=ip, port=PROXY_PORT)
                    print("new proxy ",proxy)
                    if self.test_proxy(proxy):
                        print('Valid Proxy')
                        self.set_proxy(self.proxy)
                        print('Sleeping',ADSL_CYCLE + 15)
                        time.sleep(ADSL_CYCLE)
                        self.remove_proxy()
                        time.sleep(15)
                    else:
                        print('Invalid Proxy')
                else:
                    print('Get IP Failed, Re Dialing')
                    time.sleep(ADSL_ERROR_CYCLE)
            else:
                print('ADSL Failed, Please Check')
                time.sleep(ADSL_ERROR_CYCLE)


def run():
    sender = Sender()
    sender.adsl()


if __name__ == '__main__':
    run()

@@ -489,16 +489,44 @@
  = None%0A
+        self.proxies = None%0A
         
@@ -1742,16 +1742,60 @@
 == 200:%0A
+                    print(html.status_code)%0A
         
@@ -1870,25 +1870,27 @@
    self.prox
-y
+ies
  = '%7B%7D%5Ct%7B%7D'.
@@ -1925,16 +1925,103 @@
 gent'%5D)%0A
+                        print(self.proxies)%0A                        self.proxy = proxy%0A
         
@@ -3292,25 +3292,27 @@
 xy(self.prox
-y
+ies
 )%0A          
@@ -3356,13 +3356,8 @@
 YCLE
- + 15
 )%0A  
@@ -3449,47 +3449,8 @@
 y()%0A
-                        time.sleep(15)%0A
     

Update sender.py