def getStockIPList(ip_list=[], n=0): ''' 根据ping排序返回可用的ip列表 :param n: 最多返回的ip数量, 当可用ip数量小于n,返回所有可用的ip :return: ''' import pickle import os filename = '/tmp/stockiplist.pickle' if os.path.isfile(filename): with open(filename, 'rb') as filehandle: # read the data as binary data stream results = pickle.load(filehandle) print('loading stock ip list.') else: data_stock = [(ping(x['ip'], x['port'], 'stock'), x) for x in ip_list] results = [] for data, x in data_stock: # 删除ping不通的数据 if data < datetime.timedelta(0, 9, 0): results.append((data, x)) # 按照ping值从小大大排序 results = [x[1] for x in sorted(results, key=lambda x: x[0])] with open(filename, 'wb') as filehandle: # store the data as binary data stream pickle.dump(results, filehandle) print('saving stock ip list.') if n == 0: return results else: return results[:n]
def test_select_best_ip(self): best_ip = select_best_ip() ip = best_ip['stock']['ip'] port = best_ip['stock']['port'] self.assertTrue(isinstance(ip, str), '未获取到ip') self.assertTrue(isinstance(port, int), '未获取到端口号') self.assertTrue(ping(ip, port, 'stock') < datetime.timedelta(0, 1, 0), '地址ping不通: {} {} {}'.format(ip, port, ping(ip, port, 'stock'))) # ip = best_ip['future']['ip'] # port = best_ip['future']['port'] # self.assertTrue(ping(ip, port, 'stock') < datetime.timedelta(0, 1, 0), '地址ping不通: {} {} {}'.format(ip, port, ping(ip, port, 'stock'))) code = '000001' days = 300 start = datetime.datetime.now().date() - datetime.timedelta(days) end = datetime.datetime.now().date() - datetime.timedelta(10) data = QA_fetch_get_stock_day(code, start_date=start, end_date=end) print(data) self.assertTrue(len(data) > (end - start).days / 2, '返回数据个数不匹配,数据长度:{},天数(包含节假日):{}'.format(len(data), (end - start).days / 2)) default_ip = {'stock': {'ip': None, 'port': None}, 'future': {'ip': None, 'port': None}} qasetting = QA_Setting() qasetting.set_config( section='IPLIST', option='default', default_value=default_ip) best_ip = select_best_ip() ip = best_ip['stock']['ip'] port = best_ip['stock']['port'] self.assertTrue(isinstance(ip, str) or ip is None, '未获取到ip') self.assertTrue(isinstance(port, int) or port is None, '未获取到端口号') ip = best_ip['future']['ip'] port = best_ip['future']['port'] self.assertTrue(isinstance(ip, str) or ip is None, '未获取到ip') self.assertTrue(isinstance(port, int) or port is None, '未获取到端口号') data = QA_fetch_get_stock_day(code, start, end) self.assertTrue(len(data) > (end - start).days / 2, '返回数据个数不匹配,数据长度:{},天数(包含节假日):{}'.format(len(data), (end - start).days / 2))
def test_select_best_ip(self): best_ip = select_best_ip() ip = best_ip['stock']['ip'] port = best_ip['stock']['port'] self.assertTrue(isinstance(ip, str), '未获取到ip') self.assertTrue(isinstance(port, int), '未获取到端口号') self.assertTrue( ping(ip, port, 'stock') < datetime.timedelta(0, 1, 0), '地址ping不通: {} {} {}'.format(ip, port, ping(ip, port, 'stock'))) type = 'future' ip = best_ip[type]['ip'] port = best_ip[type]['port'] self.assertTrue( ping(ip, port, type) < datetime.timedelta(0, 1, 0), '地址ping不通: {} {} {}'.format(ip, port, ping(ip, port, 'stock'))) code = '000001' days = 300 start = datetime.datetime.now().date() - datetime.timedelta(days) end = datetime.datetime.now().date() - datetime.timedelta(10) data = QA_fetch_get_stock_day(code, start_date=start, end_date=end) # print(data) self.assertTrue( len(data) > (end - start).days / 2, '返回数据个数不匹配,数据长度:{},天数(包含节假日):{}'.format(len(data), (end - start).days / 2)) # 恢复初始化ip,重新测试ip default_ip = { 'stock': { 'ip': None, 'port': None }, 'future': { 'ip': None, 'port': None } } qasetting = QA_Setting() qasetting.set_config(section='IPLIST', option='default', default_value=default_ip) filenames = ['stock_ip_list', 'stock_ip_list_MP'] for filename in filenames: # 删除保存ip的pickle文件 filename = '{}{}{}.pickle'.format( qasetting.get_config(section='LOG', option='path', default_value=""), os.sep, filename) if os.path.isfile(filename): os.remove(filename) best_ip = select_best_ip() ip = best_ip['stock']['ip'] port = best_ip['stock']['port'] self.assertTrue(isinstance(ip, str), '未获取到ip') self.assertTrue(isinstance(port, int), '未获取到端口号') ip = best_ip['future']['ip'] port = best_ip['future']['port'] self.assertTrue(isinstance(ip, str), '未获取到ip') self.assertTrue(isinstance(port, int), '未获取到端口号') data = QA_fetch_get_stock_day(code, start, end) self.assertTrue( len(data) > (end - start).days / 2, '返回数据个数不匹配,数据长度:{},天数(包含节假日):{}'.format(len(data), (end - start).days / 2))
def myPing(ip, port, type_): return ping(ip, port.type_), {'ip': ip, 'port': port}