def __ip_query(self, hint: str): cp.about_t('Start a sub progress of SSR', hint) # sub progress self._sub_progress = subprocess.Popen( self._cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=os.setsid, ) # Group PID gpid = os.getpgid(self._sub_progress.pid) cp.wr(cp.Fore.LIGHTYELLOW_EX + '(G)PID {}:{} '.format(gpid, self.local_port)) # wait, during the progress launching. for i in range(0, 5): cp.wr(cp.Fore.LIGHTBLUE_EX + '.') cp.fi() time.sleep(1) cp.success(' Next.') # Request for IP ip = None try: cp.about_t('Try to request for the IP address') ip = ip_query.ip_query(requests_proxies=proxy_fn.requests_proxies( host=self.local_address, port=self.local_port, )) if ip: cp.success('{} {}'.format(ip['ip'], ip['country'])) else: cp.fx() except Exception as e: # ConnectionError? cp.fx() cp.error(e) finally: cp.about_t('Kill SSR sub progress', 'PID {pid}'.format(pid=gpid)) os.killpg(gpid, 9) cp.success('Done.') if ip: self._exit_ip = ip return ip return None
def get_sms(self, mobile=None, item_id=None): """ Get the SMS :param mobile: :param item_id: :return: """ mobile = mobile or self._mobile item_id = item_id or self._item_id if self._cli: cp.watching('Getting SMS') # get sms query = { 'action': 'getsms', 'token': self._token, 'itemid': item_id, 'mobile': mobile, 'release': 1, } started_at = time.time() while True: now = time.time() # try to get sms r = self._req_text(query=query) # success if r.split('|')[0] == 'success': txt = r.split('|')[1] if self._cli: cp.wr(cp.Fore.LIGHTBLACK_EX + ' {}'.format(txt)) cp.fx() return txt # error if '3001' != r: self._raise(r) # timed out -> release mobile if (now - started_at) > self._sms_wait_max: cp.wr(' ') if self._cli: cp.error(' TIMED OUT.') self.release_mobile() return False time.sleep(self._sms_interval) if self._cli: cp.step()
def _raise(s): """ Raise exception :param str s: Error string :return: """ cp.fx() errors = { '1001': '参数 token 不能为空', '1002': '参数 action 不能为空', '1003': '参数 action 错误', '1004': 'token 失效', '1005': '用户名或密码错误', '1006': '用户名不能为空', '1007': '密码不能为空', '1008': '账户余额不足', '1009': '账户被禁用', '1010': '参数错误', '1011': '账户待审核', '1012': '登录数达到上限', '2001': '参数 itemid 不能为空', '2002': '项目不存在', '2003': '项目未启用', '2004': '暂时没有可用的号码', '2005': '获取号码数量已达到上限', '2006': '参数 mobile 不能为空', '2007': '号码已被释放', '2008': '号码已离线', '2009': '发送内容不能为空', '2010': '号码正在使用中', '3001': '尚未收到短信', '3002': '等待发送', '3003': '正在发送', '3004': '发送失败', '3005': '订单不存在', '3006': '专属通道不存在', '3007': '专属通道未启用', '3008': '专属通道密码与项目不匹配', '9001': '系统错误', '9002': '系统异常', '9003': '系统繁忙', } if s in errors: raise ApiException('Error #.%s %s' % (s, errors[s])) else: raise ApiException('Error #.%s Unknown' % s)
def random_a_proxy_dict_from_file(path_to_file: str): """ Random a proxy dict from a certain file, with verification. :param str path_to_file: /path/to/file :return: dict or None """ lines = file_fn.read_to_list(path_to_file) if lines: while len(lines) > 0: cp.getting('Random a proxy') # random _line = random.choice(lines) proxy_dict = line2dict(_line) # verify if proxy_dict: cp.wr('{host}:{port} '.format(host=proxy_dict['host'], port=proxy_dict['port'], )) cp.step(with_spaces=1) try: ip = ip_query(requests_proxies=dict2requests_proxies(proxy_dict)) if ip: cp.value(ip['ip'], inline=True) if ip['country']: cp.value(' ({})'.format(ip['country']), inline=True) cp.fx() return proxy_dict except Exception as e: cp.error('[proxy_fn] {}'.format(e)) lines.remove(_line) return None