def valid_one(self, proxy, method, url='https://baidu.com'): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' } proxies = { 'http': 'http://' + proxy['proxy'], 'https': 'http://' + proxy['proxy'] } try: start_time = time.time() resp = requests.get(url, headers=headers, proxies=proxies, timeout=8) delay = round(time.time() - start_time, 2) # round()方法返回浮点数x的四舍五入值 if resp.status_code == 200: proxy['delay'] = delay if method == 'insert': MongoDB().insert(proxy) elif method == 'check': MongoDB().update({'proxy': proxy['proxy']}, {'delay': proxy['delay']}) else: print("无效ip:{}".format(proxy)) if method == 'check': MongoDB().delete({'proxy': proxy['proxy']}) except (ProxyError, ConnectTimeout): print("无效ip:{}".format(proxy)) if method == 'check': MongoDB().delete({'proxy': proxy['proxy']})
def get_many(): # http://127.0.0.1:5000/many?count=2 # args = flask.request.args # 参数提交 proxies = MongoDB().get(1) result = [proxy['proxy'] for proxy in proxies] print(result) print(MongoDB().get_count()) # x = random.randint(1,MongoDB().get_count()-1) return json.dumps(result)
def check(): ''' 定时检测数据库中代理的可用性 :return: ''' while True: m = MongoDB() count = m.get_count() if not count == 0: print('开始检测数据库中代理可用性>>>>>>>>') proxies = m.get(count) Validate().valid_many(proxies, 'check') time.sleep(10 * 60)
def delete(): args = flask.request.args MongoDB().delete({'proxy': args['proxy']}) return '删除成功:{}'.format(args)
def get_many(): proxies = MongoDB().get(1) result = [proxy['proxy'] for proxy in proxies] print(result) print(MongoDB().get_count()) return json.dumps(result)
def get_one(): proxies = MongoDB().get(1) result = [proxy['proxy'] for proxy in proxies] x = random.randint(0, MongoDB().get_count() - 1) return json.dumps(dict(proxy=result[x]))