Ejemplo n.º 1
0
class FreeProxyTest(object):

    headers = {
        'Accept':
        'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
        'Accept-Language':
        'en',
        'upgrade-insecure-requests':
        '1',
        'user-agent':
        'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
    }

    def __init__(self):
        self.pm = ProxyManager()

    def getAll(self):
        # 获取redis数据库useful proxy
        return self.pm.getAll()

    def testWeb(self, proxy=None):
        proxies = None
        if proxy:
            proxies = {'https': 'http://' + proxy}

        url = 'https://www.zhipin.com/'
        try:
            resp = requests.get(url,
                                proxies=proxies,
                                timeout=10,
                                headers=self.headers)
        except Exception as e:
            print(e)
            if proxy:
                self.pm.delete(proxy)
            return
        print('status_code:', resp.status_code)
        print('text:\n', resp.text)
        print('-' * 100)

    def testHttpbinGet(self, proxy=None):
        proxies = None
        if proxy:
            proxies = {'https': 'http://' + proxy}
        url = 'https://httpbin.org/get'
        try:
            resp = requests.get(url, proxies=proxies, timeout=10)
        except Exception as e:
            print(e)
            return
        print('status_code:', resp.status_code)
        print('text:\n', resp.text)
        print('-' * 100)

    def getProxy(self, proxy=None):
        if not proxy:
            url = 'http://127.0.0.1:5010/get/'
            resp = requests.get(url)
            if resp.status_code != 200 or resp.text == None:
                raise Exception('fail get proxy')
            proxy = resp.text
            print(proxy)

        self.proxies = {'https': 'http://' + proxy}
Ejemplo n.º 2
0
def getAll():
    proxies = ProxyManager.getAll()
    return proxies