def del_queue(name, port='8089', host='127.0.0.1'): data = {'name': name, 'opt': 'del', 'cli': 'true'} url = 'http://%s:%s/taskq?%s' % (host, port, urlencode(data)) try: resp = request.urlopen(url) return resp.read().decode() except (URLError): raise Exception('Connection refused. Please check host or port.')
def __call__(self, parser, namespace, values, option_string=None): url = 'http://127.0.0.1:%s/taskq?opt=list' % values # port try: resp = request.urlopen(url) for item in loads(resp.read().decode()): print('%-16s%4s%4s' % tuple(item)) except(URLError): print('Connection refused. Please check port.') exit(0)
def __call__(self, parser, namespace, values, option_string=None): port, name = values data = {'name': name, 'opt': 'cls', 'cli': 'true'} url = 'http://127.0.0.1:%s/taskq?%s' % (port, urlencode(data)) try: resp = request.urlopen(url) print(resp.read().decode()) except(URLError): print('Connection refused. Please check port.') exit(0)
def __call__(self, parser, namespace, values, option_string=None): port, name, mode = values if mode not in ['con', 'seq']: print('The mode must be either con or seq.') exit(1) if not name.strip(): print('The name is invalid.') exit(1) data = {'name': name, 'mode': mode, 'opt': 'add', 'cli': 'true'} url = 'http://127.0.0.1:%s/taskq?%s' % (port, urlencode(data)) try: resp = request.urlopen(url) print(resp.read().decode()) except(URLError): print('Connection refused. Please check port.') exit(0)
def urlopen(path, args, method, json=False): now = datetime.now(UTC(conf.utc)).strftime('%d/%b/%Y %H:%M:%S') try: headers = {'User-Agent': 'uCron v%s' % __version__} path += '/' if path.count('/') < 3 else '' if method.upper() == 'POST' or json: method = 'POST' data = args.encode('utf8') if args else b'' if json: headers['Content-Type'] = 'application/json' else: data = None path += '?' + args if args else '' resp = request.urlopen(request.Request(path, headers=headers), data) return '[%s] %s %s - %s' % (now, path, method, resp.code) except Exception as common_ex: return '[%s] %s %s - %s' % (now, path, method, common_ex)
def add_task(path, args='', method='GET', name='default_seq', port='8089', host='127.0.0.1', json=None): if args != '' and not isinstance(args, dict): raise Exception('TypeError: Argument args should be dict.') headers = {'Content-Type': 'application/json'} req = request.Request('http://%s:%s/add_task' % (host, port), headers=headers) data = { 'path': path, 'args': args, 'method': method, 'name': name, 'json': json } try: resp = request.urlopen(req, dumps(data).encode('utf8')) return resp.read().decode() except (URLError): raise Exception('Connection refused. Please check host or port.')