def _send_notify(pushbulletapi, message): http_handler = HTTPSConnection("api.pushbullet.com") if not pushbulletapi: pushbulletapi = autosub.PUSHBULLETAPI mydata = {'type': 'note'.encode('utf-8'), 'title': 'Auto-Sub'.encode('utf-8'), 'body': message.encode('utf-8') } try: http_handler.request('POST', '/v2/pushes', body=json.dumps(mydata), headers={'Content-Type': 'application/json', 'Authorization': 'Bearer %s' % pushbulletapi}) except: log.error("Pushbullet: notification failed.") return False response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info("Pushbullet: notification sent.") return True elif request_status == 401: log.error("Pushbullet: No valid access token provided") return False elif request_status == 403: log.error("Pushbullet: The access token is not valid for that request") return False else: log.error("Pushbullet: notification failed with error") return False
def send(hook_url, message, **kwargs): headers = { "Content-type": "application/json" } http_key = kwargs.pop("http_key", None) if not http_key and hasattr(settings, 'HTTP_KEY'): http_key = settings.HTTP_KEY headers["Authorization"] = "key={}".format(http_key) kwargs["msg"] = message up = urlparse(hook_url) if up.scheme == 'https': http = HTTPSConnection(up.netloc) else: http = HTTPConnection(up.netloc) http.request( "POST", up.path, headers=headers, body=dumps(kwargs)) response = http.getresponse() if response.status != 200: raise HTTPError(response.reason) return True
def _probe_geonode_wms(self, raw_url): url = urlsplit(raw_url) if url.scheme == 'https': conn = HTTPSConnection(url.hostname, url.port) else: conn = HTTPConnection(url.hostname, url.port) conn.request('GET', '/api/ows_endpoints/', '', {}) response = conn.getresponse() content = response.read() status = response.status content_type = response.getheader("Content-Type", "text/plain") # NEW-style OWS Enabled GeoNode if status == 200 and 'application/json' == content_type: try: _json_obj = json.loads(content) if 'data' in _json_obj: data = _json_obj['data'] for ows_endpoint in data: if 'OGC:OWS' == ows_endpoint['type']: return ows_endpoint['url'] + '?' + url.query except BaseException: pass # OLD-style not OWS Enabled GeoNode _url = "%s://%s/geoserver/ows" % (url.scheme, url.netloc) return _url
def send_request(self, message, use_ssl=False, issoap=False, parse=False): """Send the message to the SOAP server. Arguments: message: message string use_ssl: if set secure HTTPS connection will be used issoap: if not set the client will add a SOAP header to the message. parse: if true an object of etree Element with be returned """ if use_ssl: conn = HTTPSConnection(self.host, self.port, self.key, self.cert) else: conn = HTTPConnection(self.host, self.port) conn.connect() headers = {'SOAPAction': '', 'Content-Type': 'text/xml'} if issoap == False: message = SoapClient.soapify_message(message) conn.request('POST', self.uri, message, headers) resp = conn.getresponse() response = resp.read() conn.close() tree = etree.fromstring(response) message = tree.xpath('soap:Body/nmwg:message', \ namespaces={'soap': \ 'http://schemas.xmlsoap.org/soap/envelope/', \ 'nmwg': 'http://ggf.org/ns/nmwg/base/2.0/'}) if parse is True: return message[0] else: return etree.tostring(message[0], pretty_print=True)
def send(token, title, **kwargs): """ Site: https://boxcar.io/ API: http://help.boxcar.io/knowledgebase/topics/48115-boxcar-api Desc: Best app for system administrators """ headers = { "Content-type": "application/x-www-form-urlencoded", "User-Agent": "DBMail/%s" % get_version(), } data = { "user_credentials": token, "notification[title]": from_unicode(title), "notification[sound]": "notifier-2" } for k, v in kwargs.items(): data['notification[%s]' % k] = from_unicode(v) http = HTTPSConnection(kwargs.pop("api_url", "new.boxcar.io")) http.request( "POST", "/api/notifications", headers=headers, body=urlencode(data)) response = http.getresponse() if response.status != 201: raise BoxcarError(response.reason) return True
def request(action, param=None, **params): """ 发送请求数据 """ if param: params.update(param) params = signature(params) logger.info("%s : params:%s", action, params) if PROXY: conn = HTTPSConnection(PROXY) conn.set_tunnel(API_SITE, 443) else: conn = HTTPSConnection(API_SITE) conn.request(API_METHOD, '/api/' + action + '/', urllib.urlencode(params), {"Content-type": "application/x-www-form-urlencoded"}) response = conn.getresponse() result = response.read() conn.close() if response.status < 200 or response.status >= 300: raise Exception(result) else: data = json.loads(result.decode('utf8')) logger.debug('%s : result:%s', action, data) if data.get('code') != 0: raise Exception("api error:", data.get('message')) data = data.get('data') if data is None: raise Exception('response data is none') return data
def test_no_content_length(self): # "The presence of a message-body in a request is signaled by the # inclusion of a Content-Length or Transfer-Encoding header field in # the request's message-headers." # # Send a message with neither header and no body. Even though # the request is of method POST, this should be OK because we set # request.process_request_body to False for our handler. if self.scheme == "https": c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) else: c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c.request("POST", "/no_body") response = c.getresponse() self.body = response.fp.read() self.status = str(response.status) self.assertStatus(200) self.assertBody('Hello world!') # Now send a message that has no Content-Length, but does send a body. # Verify that CP times out the socket and responds # with 411 Length Required. if self.scheme == "https": c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) else: c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c.request("POST", "/") response = c.getresponse() self.body = response.fp.read() self.status = str(response.status) self.assertStatus(411)
def get_bug_information(bug_ids): bug_information = [] http_connection = HTTPSConnection('bugs.launchpad.net') for bug_id in bug_ids: http_connection.request('GET', '/bugs/' + bug_id + '/+text') http_response = http_connection.getresponse() if http_response.status != 200: print "Error occured when fetching data" continue data = http_response.read() data = data.split('\n') bug = {} bug_title = bug_task = bug_status = bug_importance = None for line in data: if line.startswith('title:'): bug_title = line.split(' ', 1)[1] elif line.startswith('task:'): bug_task = line.split(' ', 1)[1] elif line.startswith('status:'): bug_status = line.split(' ', 1)[1] elif line.startswith('importance:'): bug_importance = line.split(' ', 1)[1] elif line.startswith('Content-Type'): break bug['id'] = bug_id bug['title'] = bug_title bug['task'] = bug_task bug['status'] = bug_status bug['importance'] = bug_importance bug_information.append(bug) http_connection.close() return bug_information
def notify(self, message, event): if not mylar.PUSHOVER_ENABLED: return http_handler = HTTPSConnection("api.pushover.net:443") data = { "token": mylar.PUSHOVER_APIKEY, "user": mylar.PUSHOVER_USERKEY, "message": message.encode("utf-8"), "title": event, "priority": mylar.PUSHOVER_PRIORITY, } http_handler.request( "POST", "/1/messages.json", body=urlencode(data), headers={"Content-type": "application/x-www-form-urlencoded"}, ) response = http_handler.getresponse() request_status = response.status if request_status == 200: logger.info(u"Pushover notifications sent.") return True elif request_status == 401: logger.info(u"Pushover auth failed: %s" % response.reason) return False else: logger.info(u"Pushover notification failed.") return False
def request(param=None, **params): """ 发送请求数据 """ if param: params.update(param) params = signature(params) logger.debug("params:%s", params) if PROXY: conn = HTTPSConnection(PROXY) conn.set_tunnel(API_SITE, 443) else: conn = HTTPSConnection(API_SITE) conn.request(API_METHOD, '/', urllib.urlencode(params), {"Content-type": "application/x-www-form-urlencoded"}) response = conn.getresponse() data = response.read() conn.close() if response.status < 200 or response.status >= 300: logger.warn('%s : error:%s', params['Action'], data) raise Exception(data) else: data = json.loads(data.decode('utf8')) logger.debug('%s : result:%s', params['Action'], data) return data
class CMSWeb (object): def __init__ (self): self.URL_CMSWEB_BASE='cmsweb.cern.ch' self.URL_PHEDEX_BLOCKREPLICAS='/phedex/datasvc/json/prod/blockreplicas' self.URL_DBS_DATASETS='/dbs/prod/global/DBSReader/datasets' self.URL_DBS_FILES='/dbs/prod/global/DBSReader/files' self.URL_DBS_RUNS='/dbs/prod/global/DBSReader/runs' self.URL_DBS_BLOCKS='/dbs/prod/global/DBSReader/blocks' self.conn = HTTPSConnection(self.URL_CMSWEB_BASE, cert_file='/tmp/x509up_u{0}'.format(getuid()), timeout=30) self.reply_cache = {} self.last_result = "" self.errors = 0 def __del__(self): self.conn.close () def get_cmsweb_data(self, url): self.last_result = url if url in self.reply_cache: return True, self.reply_cache[url] msg ="" try: self.conn.request('GET', url) msg = self.conn.getresponse() if msg.status!=200: self.errors = self.errors + 1 print 'Result: {0} {1}: {2}'.format(msg.status, msg.reason, url) return False, {} self.reply_cache[url]=json.loads(msg.read()) return True, self.reply_cache[url] except Exception, e: print "Error:", e, url self.errors = self.errors + 1 return False, {}
def _load_token(cls): """ Загружает новый токен. @return: string """ logger.debug("loading token") logger.warning("looks like you do not have token in your settings_local.py yet") logger.warning("please get an auth code from %s", cls._get_auth_url()) logger.warning("it is better to set TOKEN variable on settings_local.py but you can just put in it here") logger.warning("see README for more information") auth_code = raw_input() logger.debug("auth code is %s", auth_code) connection = HTTPSConnection("oauth.yandex.ru") body = urlencode( { "grant_type": "authorization_code", "client_id": CLIENT_ID, "client_secret": CLIENT_SECRET, "code": auth_code, } ) connection.request("POST", "/token", body) response = connection.getresponse().read() result = json.loads(response) token = result["access_token"] logger.debug("token loaded: %s", token) return token
def pushover(message, token, user, title="JCVI: Job Monitor", \ priority=0, timestamp=None): """ pushover.net python API <https://pushover.net/faq#library-python> """ assert -1 <= priority <= 2, \ "Priority should be an int() between -1 and 2" if timestamp == None: from time import time timestamp = int(time()) retry, expire = (300, 3600) if priority == 2 \ else (None, None) conn = HTTPSConnection("api.pushover.net:443") conn.request("POST", "/1/messages.json", urlencode({ "token": token, "user": user, "message": message, "title": title, "priority": priority, "timestamp": timestamp, "retry": retry, "expire": expire, }), { "Content-type": "application/x-www-form-urlencoded" }) conn.getresponse()
def request(self, method, endpoint, headers=None, **params): url = "https://api.pingdom.com/api/{version}/{endpoint}".format( **dict( username=self.username, password=self.password, version=self.version, endpoint=endpoint ) ) if headers is None: headers = dict() if 'Account-Email' not in headers: headers['Account-Email'] = self.account if 'App-Key' not in headers: headers['App-Key'] = self.apikey if 'Content-Type' not in headers: headers['Content-Type'] = 'application/json' if 'Authorization' not in headers: headers['Authorization'] = 'Basic %s' % base64.encodestring( "{username}:{password}".format(username=self.username, password=self.password) ) conn = HTTPSConnection('api.pingdom.com', context=ctx) conn.request( method, "/api/{version}/{endpoint}".format(version=self.version, endpoint=endpoint), headers=headers ) response = conn.getresponse() body = response.read() return json.loads(body)
def _sendPushalot(self, pushalot_authorizationtoken=None, event=None, message=None, force=False): if not sickrage.srCore.srConfig.USE_PUSHALOT and not force: return False sickrage.srCore.srLogger.debug("Pushalot event: " + event) sickrage.srCore.srLogger.debug("Pushalot message: " + message) sickrage.srCore.srLogger.debug("Pushalot api: " + pushalot_authorizationtoken) http_handler = HTTPSConnection("pushalot.com") data = {'AuthorizationToken': pushalot_authorizationtoken, 'Title': event.encode('utf-8'), 'Body': message.encode('utf-8')} try: http_handler.request("POST", "/api/sendmessage", headers={'Content-type': "application/x-www-form-urlencoded"}, body=urlencode(data)) except (SSLError, HTTPException, socket.error): sickrage.srCore.srLogger.error("Pushalot notification failed.") return False response = http_handler.getresponse() request_status = response.status if request_status == 200: sickrage.srCore.srLogger.debug("Pushalot notifications sent.") return True elif request_status == 410: sickrage.srCore.srLogger.error("Pushalot auth failed: %s" % response.reason) return False else: sickrage.srCore.srLogger.error("Pushalot notification failed.") return False
def _send_notify(pushalotapi, message): http_handler = HTTPSConnection("pushalot.com") if not pushalotapi: pushalotapi = autosub.PUSHALOTAPI data = {'AuthorizationToken': pushalotapi, 'Title': "Auto-Sub", 'Body': message.encode('utf-8') } try: http_handler.request("POST", "/api/sendmessage", headers = {'Content-type': "application/x-www-form-urlencoded"}, body = urlencode(data)) except: log.error("Pushalot: notification failed.") return False response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info("Pushalot: notification sent.") return True elif request_status == 410: log.error("Pushalot: Auth failed %s" % response.reason) return False else: log.error("Pushalot: notification failed.") return False
def _get_signin_token(self, creds): """ GET the generated Signin Token from the federation endpoint :param creds: credentials to pass to the federation endpoint :type creds: dict :return: signin token returned by the federation endpoint :rtype: str """ host = 'signin.aws.amazon.com' req_path = 'https://signin.aws.amazon.com/federation' \ '?Action=getSigninToken' \ '&Session=%s' % quote_plus(json.dumps(creds)) logger.debug('HTTPS GET request to %s: %s', host, req_path) conn = HTTPSConnection(host, 443) conn.request('GET', req_path) resp = conn.getresponse() logger.debug('Response: HTTP %s %s', resp.status, resp.reason) logger.debug('Headers: %s', resp.getheaders()) body = resp.read() logger.debug('Body: %s', body.strip()) if resp.status != 200: logger.critical('AWS Federation endpoint responded HTTP %s %s: %s', resp.status, resp.reason, body) raise RuntimeError('Error obtaining console signin credentials.') try: b = json.loads(body)['SigninToken'] except Exception: logger.critical( 'AWS Federation endpoint returned an invalid response: %s', body ) raise RuntimeError('Invalid response from AWS Federation endpoint.') return b
def notify(self, message, event): if not mylar.PROWL_ENABLED: return http_handler = HTTPSConnection("api.prowlapp.com") data = { "apikey": mylar.PROWL_KEYS, "application": "Mylar", "event": event, "description": message.encode("utf-8"), "priority": mylar.PROWL_PRIORITY, } http_handler.request( "POST", "/publicapi/add", headers={"Content-type": "application/x-www-form-urlencoded"}, body=urlencode(data), ) response = http_handler.getresponse() request_status = response.status if request_status == 200: logger.info(u"Prowl notifications sent.") return True elif request_status == 401: logger.info(u"Prowl auth failed: %s" % response.reason) return False else: logger.info(u"Prowl notification failed.") return False
def signin(request): result = {'nav_dreamspark': 'active'} if request.method == 'POST': form = SigninForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] domain = form.cleaned_data['domain'] password = form.cleaned_data['password'] user = authenticate(username=username, domain=domain, password=password) if user: try: onthehub = HTTPSConnection('e5.onthehub.com') onthehub.request('GET', '/WebStore/Security/AuthenticateUser.aspx?account=' + DREAMSPARK_ACCOUNT + '&username='******'&key=' + DREAMSPARK_KEY + '&academic_statuses=' + ('students' if domain == '@nou.com.cn' else 'faculty,staff') + '&email=' + user.email + '&last_name=' + user.last_name + '&first_name=' + user.first_name) response = onthehub.getresponse() if response.status == 200: return HttpResponseRedirect(response.read()) else: result['error'] = '与 DreamSpark 通信异常,请稍后重试' except: result['error'] = '与 DreamSpark 通信超时,请稍后重试' else: result['error'] = '邮箱地址或密码错误,请重新输入' result.update(csrf(request)) return render_to_response('dreamspark/signin.html', result)
def notify(self, message, event): if not headphones.PUSHBULLET_ENABLED: return http_handler = HTTPSConnection("api.pushbullet.com") data = {'device_iden': headphones.PUSHBULLET_DEVICEID, 'type': "note", 'title': "Headphones", 'body': message.encode("utf-8") } http_handler.request("POST", "/api/pushes", headers = {'Content-type': "application/x-www-form-urlencoded", 'Authorization' : 'Basic %s' % base64.b64encode(headphones.PUSHBULLET_APIKEY + ":") }, body = urlencode(data)) response = http_handler.getresponse() request_status = response.status logger.debug(u"PushBullet response status: %r" % request_status) logger.debug(u"PushBullet response headers: %r" % response.getheaders()) logger.debug(u"PushBullet response body: %r" % response.read()) if request_status == 200: logger.info(u"PushBullet notifications sent.") return True elif request_status >= 400 and request_status < 500: logger.info(u"PushBullet request failed: %s" % response.reason) return False else: logger.info(u"PushBullet notification failed serverside.") return False
def notify(self, message = '', data = {}, listener = None): http_handler = HTTPSConnection("api.pushover.net:443") data = { 'user': self.conf('user_key'), 'token': self.app_token, 'message': toUnicode(message), 'priority': self.conf('priority') } http_handler.request('POST', "/1/messages.json", headers = {'Content-type': 'application/x-www-form-urlencoded'}, body = tryUrlencode(data) ) response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info('Pushover notifications sent.') return True elif request_status == 401: log.error('Pushover auth failed: %s', response.reason) return False else: log.error('Pushover notification failed.') return False
def notify(self, message, event): if not headphones.CONFIG.PROWL_ENABLED: return http_handler = HTTPSConnection("api.prowlapp.com") data = {'apikey': headphones.CONFIG.PROWL_KEYS, 'application': 'Headphones', 'event': event, 'description': message.encode("utf-8"), 'priority': headphones.CONFIG.PROWL_PRIORITY} http_handler.request("POST", "/publicapi/add", headers={ 'Content-type': "application/x-www-form-urlencoded"}, body=urlencode(data)) response = http_handler.getresponse() request_status = response.status if request_status == 200: logger.info(u"Prowl notifications sent.") return True elif request_status == 401: logger.info(u"Prowl auth failed: %s" % response.reason) return False else: logger.info(u"Prowl notification failed.") return False
def notify(self, message="", data={}, listener=None): if self.isDisabled(): return http_handler = HTTPSConnection("api.prowlapp.com") data = { "apikey": self.conf("api_key"), "application": self.default_title, "description": toUnicode(message), "priority": self.conf("priority"), } http_handler.request( "POST", "/publicapi/add", headers={"Content-type": "application/x-www-form-urlencoded"}, body=tryUrlencode(data), ) response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info("Prowl notifications sent.") return True elif request_status == 401: log.error("Prowl auth failed: %s", response.reason) return False else: log.error("Prowl notification failed.") return False
def notify(self, message, event): if not headphones.PUSHOVER_ENABLED: return http_handler = HTTPSConnection("api.pushover.net") data = {'token': self.application_token, 'user': headphones.PUSHOVER_KEYS, 'title': event, 'message': message.encode("utf-8"), 'priority': headphones.PUSHOVER_PRIORITY } http_handler.request("POST", "/1/messages.json", headers = {'Content-type': "application/x-www-form-urlencoded"}, body = urlencode(data)) response = http_handler.getresponse() request_status = response.status logger.debug(u"Pushover response status: %r" % request_status) logger.debug(u"Pushover response headers: %r" % response.getheaders()) logger.debug(u"Pushover response body: %r" % response.read()) if request_status == 200: logger.info(u"Pushover notifications sent.") return True elif request_status >= 400 and request_status < 500: logger.info(u"Pushover request failed: %s" % response.reason) return False else: logger.info(u"Pushover notification failed.") return False
def _getDailymotionAPI(self, vid): self.logger.debug("Retrieving video information from the Dailymotion API.") con = HTTPSConnection("api.dailymotion.com", timeout=10) params = {"fields", "title,duration,allow_embed"} data = None try: try: con.request("GET", "/video/%s?fields=title,duration,allow_embed" % (vid)) data = con.getresponse().read() except SSLError as e: # There is a bug in OpenSSL 1.0.1 which affects Python 2.7 on systems that rely on it. # Attempt to use curl as a fallback. # Curl must be installed for this to work. # This is the worst hack I have ever coded. # Since vid is a valid video id there is no risk of any attack. self.logger.warning("SSL Error, attempting to use curl as a fallback.") try: data = subprocess.check_output(["curl", "-k", "-s", "-m 10", "https://api.dailymotion.com/video/%s?fields=title,duration,allow_embed" % (vid)]) except Exception as e: self.logger.warning("Curl fallback failed.") data = "SSL Failure" raise e # Do this last and separately to avoid catching it elsewhere. data = json.loads(data) except Exception as e: # Many things can go wrong with an HTTP request or during JSON parsing self.logger.warning("Error retrieving Dailymotion API information.") self.logger.debug(e) finally: con.close() return data
def notify(self, message = '', data = {}): if self.isDisabled(): return http_handler = HTTPSConnection('api.prowlapp.com') data = { 'apikey': self.conf('api_key'), 'application': self.default_title, 'description': toUnicode(message), 'priority': self.conf('priority'), } http_handler.request('POST', '/publicapi/add', headers = {'Content-type': 'application/x-www-form-urlencoded'}, body = urlencode(data) ) response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info('Prowl notifications sent.') return True elif request_status == 401: log.error('Prowl auth failed: %s' % response.reason) return False else: log.error('Prowl notification failed.') return False
def notify(self, message, event, module=None): if not mylar.PUSHOVER_ENABLED: return if module is None: module = '' module += '[NOTIFIER]' http_handler = HTTPSConnection("api.pushover.net:443") data = {'token': mylar.PUSHOVER_APIKEY, 'user': mylar.PUSHOVER_USERKEY, 'message': message.encode("utf-8"), 'title': event, 'priority': mylar.PUSHOVER_PRIORITY } http_handler.request("POST", "/1/messages.json", body = urlencode(data), headers = {'Content-type': "application/x-www-form-urlencoded"} ) response = http_handler.getresponse() request_status = response.status if request_status == 200: logger.info(module + ' Pushover notifications sent.') return True elif request_status == 401: logger.info(module + 'Pushover auth failed: %s' % response.reason) return False else: logger.info(module + ' Pushover notification failed.') return False
def notify(self, message, event, module=None): if not mylar.PROWL_ENABLED: return if module is None: module = '' module += '[NOTIFIER]' http_handler = HTTPSConnection("api.prowlapp.com") data = {'apikey': mylar.PROWL_KEYS, 'application': 'Mylar', 'event': event, 'description': message.encode("utf-8"), 'priority': mylar.PROWL_PRIORITY } http_handler.request("POST", "/publicapi/add", headers = {'Content-type': "application/x-www-form-urlencoded"}, body = urlencode(data)) response = http_handler.getresponse() request_status = response.status if request_status == 200: logger.info(module + ' Prowl notifications sent.') return True elif request_status == 401: logger.info(module + ' Prowl auth failed: %s' % response.reason) return False else: logger.info(module + ' Prowl notification failed.') return False
def doLogin(self): """ Perform login, raise exception if something wrong """ self.api_url = None self.auth_token = None uri = urlparse.urlparse(self.auth_url) headers = dict() headers['x-auth-user'] = self.api_user headers['x-auth-key'] = self.api_key conn = HTTPSConnection(uri.hostname, uri.port) conn.request('GET', self.auth_url, '', headers) response = conn.getresponse() buff = response.read() # 401 == Auth failed if response.status == 401: raise RackSpaceAuthenticationFailedException() # TODO: check codes and return more informative exceptions if response.status != 204: raise RackSpaceException() for header in response.getheaders(): if header[0].lower() == "x-server-management-url": self.api_url = header[1] if header[0].lower() == "x-auth-token": self.auth_token = header[1] conn.close() if self.auth_token is None or self.api_url is None: # Invalid response raise RackSpaceException()
def getRemoteDBModifiedTS(): """ Performs a HEAD request to get the Last-Modified date-time of a database dump file and parses it into a UNIX timestamp. """ debug_msg = 'Unable to get timestamp of remote database dump - {0}' logging.info("Getting timestamp of database dump at '%s'", HOST + PATH) try: # Removing the scheme from the URL conn = HTTPSConnection(HOST[8:], timeout=TIMEOUT) conn.request('HEAD', PATH) except gaierror as e: logging.debug( debug_msg.format( "Cannot connect to '%s', error: %s"), HOST + PATH, e) exit(1) rsp = conn.getresponse() if rsp.status != 200: logging.debug( debug_msg.format('Server responded with: %d %s'), rsp.status, rsp.reason) exit(1) last_modified = rsp.getheader('last-modified', None) if last_modified is None: logging.debug(debug_msg.format( 'Response doesnt include Last-Modified Header')) exit(1) last_m_dt = datetime.strptime( last_modified.split(', ')[1], '%d %b %Y %H:%M:%S %Z') return timegm(last_m_dt.timetuple())
def request(action, param=None, **params): """ 发送请求数据 """ if param: params.update(param) params.update({'login_token': '***', 'format': 'json'}) info("%s : params:%s", action, params) params['user_token'] = "%s,%s" % (ID, TOKEN) if PROXY: conn = HTTPSConnection(PROXY) conn.set_tunnel(API_SITE, 443) else: conn = HTTPSConnection(API_SITE) conn.request( API_METHOD, '/' + action, urlencode(params), { "Content-type": "application/x-www-form-urlencoded", "User-Agent": "DDNS Client/1.0" }) response = conn.getresponse() res = response.read() conn.close() if response.status < 200 or response.status >= 300: raise Exception(res) else: data = jsondecode(res.decode('utf8')) debug('%s : result:%s', action, data) if not data: raise Exception("empty response") elif data.get("status", {}).get("code") == "1": return data else: raise Exception(data.get('status', {}))
def getBestServer(servers): """Perform a speedtest.net latency request to determine which speedtest.net server has the lowest latency """ results = {} for server in servers: cum = [] url = '%s/latency.txt' % os.path.dirname(server['url']) urlparts = urlparse(url) for i in range(0, 3): try: if urlparts[0] == 'https': h = HTTPSConnection(urlparts[1]) else: h = HTTPConnection(urlparts[1]) headers = {'User-Agent': user_agent} start = timeit.default_timer() h.request("GET", urlparts[2], headers=headers) r = h.getresponse() total = (timeit.default_timer() - start) except (HTTPError, URLError, socket.error): cum.append(3600) continue text = r.read(9) if int(r.status) == 200 and text == 'test=test'.encode(): cum.append(total) else: cum.append(3600) h.close() avg = round((sum(cum) / 6) * 1000, 3) results[avg] = server fastest = sorted(results.keys())[0] best = results[fastest] best['latency'] = fastest return best
def _notify(self, event, message, prowl_api=None, prowl_priority=None, force=False): # suppress notifications if the notifier is disabled but the notify options are checked if not sickbeard.USE_PROWL and not force: return False # fill in omitted parameters if not prowl_api: prowl_api = sickbeard.PROWL_API if not prowl_priority: prowl_priority = sickbeard.PROWL_PRIORITY logger.log(u"PROWL: Sending notice with details: event=\"%s\", message=\"%s\", priority=%s, api=%s" % (event, message, prowl_priority, prowl_api), logger.DEBUG) try: http_handler = HTTPSConnection("api.prowlapp.com") data = {'apikey': prowl_api, 'application': "SickBeard", 'event': event, 'description': message.encode('utf-8'), 'priority': prowl_priority } http_handler.request("POST", "/publicapi/add", headers={'Content-type': "application/x-www-form-urlencoded"}, body=urlencode(data) ) response = http_handler.getresponse() request_status = response.status except Exception, e: logger.log(u"PROWL: Notification failed: " + ex(e), logger.ERROR) return False
def request(action, param=None, **params): """ 发送请求数据 """ if param: params.update(param) params = dict((k, params[k]) for k in params if params[k] is not None) params.update({API.TOKEN_PARAM: '***', 'format': 'json'}) info("%s/%s : %s", API.SITE, action, params) params[API.TOKEN_PARAM] = "%s,%s" % (Config.ID, Config.TOKEN) if Config.PROXY: conn = HTTPSConnection(Config.PROXY) conn.set_tunnel(API.SITE, 443) else: conn = HTTPSConnection(API.SITE) conn.request(API.METHOD, '/' + action, urlencode(params), { "Content-type": "application/x-www-form-urlencoded", "User-Agent": "DDNS/%s ([email protected])" % environ.get("DDNS_VERSION", "1.0.0") }) response = conn.getresponse() res = response.read().decode('utf8') conn.close() if response.status < 200 or response.status >= 300: warning('%s : error[%d]:%s', action, response.status, res) raise Exception(res) else: data = jsondecode(res) debug('%s : result:%s', action, data) if not data: raise Exception("empty response") elif data.get("status", {}).get("code") == "1": return data else: raise Exception(data.get('status', {}))
def run(self): # uuid.getnode() is based on the mac address, so should be stable across multiple sessions on the same machine: anonymousId = md5(uuid.UUID(int=uuid.getnode()).bytes).hexdigest() while True: try: event_name, event_properties = self._queue.get() if self._is_analytics_disabled() or self._is_unittest(): continue if event_properties is None: event_properties = {} conn = HTTPSConnection("api.segment.io") body = { "anonymousId": anonymousId, "event": event_name, "properties": event_properties, } headers = { "Content-Type": "application/json", "Authorization": "Basic RFl3Q0E1TENRTW5Uek95ajk5MTJKRjFORTkzMU5yb0o6", } conn.request("POST", "/v1/track", body=dumps(body), headers=headers) response = conn.getresponse() response.read() # Throttle a bit sleep(0.2) except Exception as e: pass # Never fail due to analytics requests erroring out
def _daemon(self): while True: url, return_func = self._queue.get() conn = HTTPSConnection(self._host) conn.request("GET", url, headers={"X-Riot-Token": self._api_key}) res = conn.getresponse() # Get wait time from response headers wait_time = self._calculate_wait_time(res) status = res.status response = res.read() if response: response = loads(response) conn.close() return_func(response, status) self._queue.task_done() # Wait if necessary if wait_time != 0: print "[{}] Reaching rate limit, better pause for {} seconds".format( self._host, wait_time) sleep(wait_time) print "[{}] Continuing...".format(self._host)
def notify(self, message = '', data = None, listener = None): if not data: data = {} http_handler = HTTPSConnection("api.pushover.net:443") api_data = { 'user': self.conf('user_key'), 'token': self.app_token, 'message': toUnicode(message), 'priority': self.conf('priority'), } if data and data.get('library'): api_data.update({ 'url': toUnicode('http://www.imdb.com/title/%s/' % data['library']['identifier']), 'url_title': toUnicode('%s on IMDb' % getTitle(data['library'])), }) http_handler.request('POST', "/1/messages.json", headers = {'Content-type': 'application/x-www-form-urlencoded'}, body = tryUrlencode(api_data) ) response = http_handler.getresponse() request_status = response.status if request_status == 200: log.info('Pushover notifications sent.') return True elif request_status == 401: log.error('Pushover auth failed: %s', response.reason) return False else: log.error('Pushover notification failed.') return False
def httpPost(body): host = Config.get('influxdb', 'influxHost') port = Config.get('influxdb', 'port') wachtwoord = Config.get('influxdb', 'wachtwoord') username = Config.get('influxdb', 'username') dbname = Config.get('influxdb', 'dbname') context = ssl._create_unverified_context() conn = HTTPSConnection(host, port, context=context) headers = { 'Content-type': 'application/x-www-form-urlencoded', 'Accept': 'text/plain' } #except Exception as e: # logging.info('Ooops! something went wrong with creating HTTP object! {}'.format(e)) conn.set_debuglevel(7) try: #dbname='db_name' conn.request( 'POST', '/write?db={db}&u={user}&p={password}'.format(db=dbname, user=username, password=wachtwoord), body, headers) except Exception as e: logging.info('Ooops! something went wronh with POSTing {}'.format(e)) pass else: response = conn.getresponse() logging.info('Updated Influx. HTTP response {}'.format( response.status)) finally: conn.close() #logging.debug("Reason: {}\n Response:{}".format(response.reason, response.read) ) conn.close()
def getSentiment(message): documents = {'documents': [{'id': '0', 'language': 'en', 'text': message}]} try: conn = HTTPSConnection('westus.api.cognitive.microsoft.com') print('1') conn.connect() print('2') conn.request("POST", '/text/analytics/v2.0/sentiment', json.dumps(documents), headers) print('3') response = conn.getresponse() print('4') return json.dumps(json.loads(response.read())) data = json.loads(response.read().decode()) print('5') score = data['documents'][0]['score'] print('6') conn.close() print('7') #return score; except Exception as e: #Error occurred, just assume 0.5 sentiment print(e)
def notify(self, message, event): if not headphones.PUSHALOT_ENABLED: return pushalot_authorizationtoken = headphones.PUSHALOT_APIKEY logger.debug(u"Pushalot event: " + event) logger.debug(u"Pushalot message: " + message) logger.debug(u"Pushalot api: " + pushalot_authorizationtoken) http_handler = HTTPSConnection("pushalot.com") data = {'AuthorizationToken': pushalot_authorizationtoken, 'Title': event.encode('utf-8'), 'Body': message.encode("utf-8") } http_handler.request("POST", "/api/sendmessage", headers = {'Content-type': "application/x-www-form-urlencoded"}, body = urlencode(data)) response = http_handler.getresponse() request_status = response.status logger.debug(u"Pushalot response status: %r" % request_status) logger.debug(u"Pushalot response headers: %r" % response.getheaders()) logger.debug(u"Pushalot response body: %r" % response.read()) if request_status == 200: logger.info(u"Pushalot notifications sent.") return True elif request_status == 410: logger.info(u"Pushalot auth failed: %s" % response.reason) return False else: logger.info(u"Pushalot notification failed.") return False
class Telegram(object): def __init__(self, token): url = 'https://api.telegram.org/bot' self.__url = urlsplit('{0}{1}/'.format(url, token)) self.__web = HTTPSConnection(self.__url.netloc) # def __send_req(self, target, method='GET'): self.__web.connect() self.__web.request(method, self.__url.path + target) # def get_botname(self): 'Return: json data.' self.__send_req('getMe') data = Request(self.__web) return data.data # def get_resp(self): 'Return: request object.' self.__send_req('getUpdates') return Request(self.__web) # def __call__(self): 'Call get_resp()' return self.get_resp() # def send_message(self, chat_id, message): 'Return: request data.' data = urlencode({'chat_id': chat_id, 'text': message}) self.__send_req('sendMessage?' + data, 'POST') data = Request(self.__web) return data.data
def post(self, path, body, headers=None): url = urlparse(self.base_url + path) conn = HTTPSConnection( url.hostname, url.port, timeout=5, context=ssl._create_unverified_context(), ) headers = headers or {} if isinstance(body, (dict, list)): body = json.dumps(body) headers['Content-Type'] = 'application/json' conn.request('POST', url.path, body, headers) response = conn.getresponse() response.headers = dict(response.getheaders()) response.body = response.read() if hasattr(response.body, 'decode'): response.body = response.body.decode('utf-8') if '/json' in response.headers.get('content-type', '') or \ '/json' in response.headers.get('Content-Type', ''): response.json = json.loads(response.body) if response.status >= 400: raise self.Error(response) return response
def redirect(key, secret, request): consumer = OAuthConsumer(str(key), str(secret)) oauth_request = OAuthRequest.from_consumer_and_token( consumer, http_url='https://twitter.com/oauth/request_token') oauth_request.sign_request(SIGNATURE_METHOD(), consumer, None) url = oauth_request.to_url() connection = HTTPSConnection('twitter.com') connection.request(oauth_request.http_method, url) response = connection.getresponse() resp = response.read() token = OAuthToken.from_string(resp) request.session['unauth_token'] = token auth_url = 'https://twitter.com/oauth/authorize' if isinstance(auth_url, (list, tuple)): params = auth_url[1] auth_url = auth_url[0] else: params = {} oauth_request = OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=auth_url, parameters=params) oauth_request.sign_request(SIGNATURE_METHOD(), consumer, token) if request.is_ajax(): return HttpResponse('<script>document.location = \'%s\';</script>' % oauth_request.to_url()) else: return HttpResponseRedirect(oauth_request.to_url())
def _enrich_layer_metadata(self, geonode_layer): url = urlsplit(self.url) if url.scheme == 'https': conn = HTTPSConnection(url.hostname, url.port) else: conn = HTTPConnection(url.hostname, url.port) workspace, layername = geonode_layer.name.split( ":") if ":" in geonode_layer.name else (None, geonode_layer.name) conn.request('GET', '/api/layers/?name=%s' % layername, '', {}) response = conn.getresponse() content = response.read() status = response.status content_type = response.getheader("Content-Type", "text/plain") if status == 200 and 'application/json' == content_type: try: _json_obj = json.loads(content) if _json_obj['meta']['total_count'] == 1: _layer = _json_obj['objects'][0] if _layer: pass # TODO except BaseException: pass
def probobuf_request(host, method, url, body, headers, msg=None): h = HTTPSConnection(host) r = h.request(method, url, body, headers) res = h.getresponse() length = res.getheader("content-length") if length == None: length = 0 else: length = int(length) data = res.read() while len(data) < length: d = res.read() data += d h.close() if msg == None: return data res = msg() res.ParseFromString(data) return res
def reconnect(self): r = HTTPSConnection('google.fr', context=ssl._create_unverified_context()) r.request('GET', '/') try: resp = r.getresponse() except Exception as e: print e.message magic = resp.getheader('location').split('?')[-1] page = "/%s" % resp.getheader('location').split('/')[-1] r = HTTPSConnection(self.gateway, context=ssl._create_unverified_context()) try: print r.request('GET', page) r.getresponse() except Exception as e: print e.message req = urlencode({ 'magic': magic, 'answer': '1', '4Tredir': 'https://google.fr/' }) r.request('POST', page, req) try: r.getresponse() except Exception as e: print e req = urlencode({ 'magic': magic, 'username': self.username, 'password': self.password, '4Tredir': 'https://google.fr/' }) r.request('POST', page, req) try: r.getresponse() except Exception as e: print e
def getsid(): """Connetti al cpanel per ottenere l'id di sessione""" # La procedura e' necessaria per ottenere i cookie, # che chiameremo PSI e SID # PSI corrisponde al PHPSESSID= , presente nella risposta di /x-login, # mentre SID corrisponde al cookie che viene rilasciato nel body della pagina /godns global sid, psi # Prepara l'header ed effettua una POST a cp.tophost.it/x-login, # fornisci le credenziali e ottieni PHPSESSID= dall'header, # che verra' inserito su "psi" headersauth = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' } auth = urllib.urlencode([('user', '' + userid), ('pass', '' + passwd)]) conn = HTTPSConnection('cp.tophost.it') conn.request('POST', '/x-login', auth, headersauth) r = conn.getresponse() psi = r.getheader('Set-Cookie').split(';')[0].replace('PHPSESSID=', '') conn.close # Prepara l'header ed effettua una GET a /x-httpsstatus, # necessaria per determinare il cookie "nodo" # che si trovera' nell'header headers_httpstatus = { 'Cookie': 'PHPSESSID=' + psi + '; logged_cp=' + userid } conn = HTTPSConnection('cp.tophost.it') conn.request('GET', '/x-httpsstatus', headers=headers_httpstatus) x_httpsstatus = conn.getresponse() node = x_httpsstatus.getheader('set-cookie').split(';')[0] conn.close() # Prepara l'header ed effettua una GET a /godns, # necessaria per determinare il cookie "sid" # che sara' presente nella pagina come dato hidden headers_godns = { 'Cookie': 'PHPSESSID=' + psi + '; logged_cp=' + userid + '; ' + node } conn = HTTPSConnection('cp.tophost.it') conn.request('GET', '/godns', headers=headers_godns) godns = conn.getresponse() page = godns.read() bound = '<input type=hidden name=sid value=' s = page.find(bound) e = page.find('>', s) sid = page[s + len(bound):e].replace('"', '') conn.close() return sid, psi
def probobuf_request(host, method, url, body, headers, msg=None, log=None): conn = HTTPSConnection(host) sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address) conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1) request = conn.request(method, url, body, headers) response = conn.getresponse() length = response.getheader("content-length") if length is None: length = 0 else: length = int(length) data = response.read() while len(data) < length: d = response.read() data += d conn.close() if msg == None: if log: log.write( "probobuf request with host {}, method {}, url {}, body {}:\n". format(host, method, url, body)) log.write(" headers were {}\n".format(headers)) log.write(" response was {}\n".format(data)) return data res = msg() res.ParseFromString(data) if log: log.write( "probobuf request with host {}, method {}, url {}, body {}:\n". format(host, method, url, body)) log.write(" headers were {}\n".format(headers)) log.write(" response was {}\n".format(repr(res))) return res
def plist_request(host, method, url, body, headers): conn = HTTPSConnection(host) request = conn.request(method, url, body, headers) response = conn.getresponse() data = response.read() try: plist_data = plistlib.readPlistFromString(data) except: plist_data = None if response.status != 200: if plist_data is None: print "Request %s returned code %d" % (url, response.status) else: print "{}: {}".format(plist_data.title, plist_data.message) return return plist_data
def push_to_sysinf(put_url, info): """ Attempt to post the given URL to the central sysinf server. First, try directly. then try through local Squid proxy, then throught Squid on 'appserver.gwn', then give up. """ headers = {'Content-Type': 'application/json'} sysinf_success = False info['sysinf_access'] = 'direct' json_info = dumps(info) try: # Try to send DIRECTLY to the new sysinf server sysinf = HTTPSConnection('sysinf.getwellnetwork.com', timeout=15) sysinf.request('PUT', put_url, json_info, headers) sysinf.getresponse() sysinf.close() sysinf_success = True except Exception as e: pass if not sysinf_success: try: # Try to send via LOCAL Squid port (if site enforces upstream proxy) to the new sysinf server info['sysinf_access'] = 'local-proxy' json_info = dumps(info) sysinf = HTTPSConnection('localhost', 3128, timeout=15) sysinf.set_tunnel('sysinf.getwellnetwork.com', 443) sysinf.request('PUT', put_url, json_info, headers) sysinf.getresponse() sysinf.close() sysinf_success = True except Exception as e: pass if not sysinf_success: try: # Try to send VIA APPSERVER to the new sysinf server info['sysinf_access'] = 'proxy' json_info = dumps(info) sysinf = HTTPSConnection('appserver.gwn', 3128, timeout=15) sysinf.set_tunnel('sysinf.getwellnetwork.com', 443) sysinf.request('PUT', put_url, json_info, headers) sysinf.getresponse() sysinf.close() sysinf_success = True except Exception as e: pass
def probobuf_request(host, method, url, body, headers, msg=None): print "DEBUG", method, host, url h = HTTPSConnection(host) #headers["Accept"] = "application/vnd.com.apple.mbs+protobuf" r = h.request(method, url, body, headers) res = h.getresponse() if res.status != 200: print "DEBUG STATUS = %d" % res.status length = res.getheader("content-length") if length == None: length = 0 else: length = int(length) data = res.read() while len(data) < length: d = res.read() data += d h.close() if msg == None: return data res = msg() res.ParseFromString(data) return res
def plist_request(host, method, url, body, headers, log=None): conn = HTTPSConnection(host) sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address) conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1) request = conn.request(method, url, body, headers) response = conn.getresponse() data = response.read() try: plist_data = plistlib.readPlistFromString(data) except: plist_data = None if response.status != 200: if plist_data is None: print "Request %s returned code %d" % (url, response.status) else: print "{}: {}".format(plist_data.title, plist_data.message) if log: log.write( "plist request with host {}, method {}, url {}, body {}:\n". format(host, method, url, body)) log.write(" headers were {}\n".format(headers)) log.write(" response status was {}\n".format(response.status)) return if log: log.write( "plist request with host {}, method {}, url {}, body {}:\n".format( host, method, url, body)) log.write(" headers were {}\n".format(headers)) log.write(" response:\n") log.write("{}\n".format(json.dumps(plist_data, indent=4))) return plist_data
def plist_request(host, method, url, body, headers): conn = HTTPSConnection(host) sock = socket.create_connection((conn.host, conn.port), conn.timeout, conn.source_address) conn.sock = ssl.wrap_socket(sock, conn.key_file, conn.cert_file, ssl_version=ssl.PROTOCOL_TLSv1) request = conn.request(method, url, body, headers) response = conn.getresponse() data = response.read() try: plist_data = plistlib.readPlistFromString(data) except: plist_data = None if response.status != 200: if plist_data is None: print "Request %s returned code %d" % (url, response.status) else: print "{}: {}".format(plist_data.title, plist_data.message) return return plist_data
def proxy(request): PROXY_ALLOWED_HOSTS = getattr(settings, 'PROXY_ALLOWED_HOSTS', ()) host = None if 'geonode.geoserver' in settings.INSTALLED_APPS: from geonode.geoserver.helpers import ogc_server_settings hostname = ( ogc_server_settings.hostname, ) if ogc_server_settings else () PROXY_ALLOWED_HOSTS += hostname host = ogc_server_settings.netloc if 'url' not in request.GET: return HttpResponse( "The proxy service requires a URL-encoded URL as a parameter.", status=400, content_type="text/plain") raw_url = request.GET['url'] url = urlsplit(raw_url) locator = str(url.path) if url.query != "": locator += '?' + url.query if url.fragment != "": locator += '#' + url.fragment if not settings.DEBUG: if not validate_host(url.hostname, PROXY_ALLOWED_HOSTS): return HttpResponse( "DEBUG is set to False but the host of the path provided to the proxy service" " is not in the PROXY_ALLOWED_HOSTS setting.", status=403, content_type="text/plain") headers = {} if settings.SESSION_COOKIE_NAME in request.COOKIES and is_safe_url( url=raw_url, host=host): headers["Cookie"] = request.META["HTTP_COOKIE"] if request.method in ("POST", "PUT") and "CONTENT_TYPE" in request.META: headers["Content-Type"] = request.META["CONTENT_TYPE"] if url.scheme == 'https': conn = HTTPSConnection(url.hostname, url.port) else: conn = HTTPConnection(url.hostname, url.port) conn.request(request.method, locator, request.body, headers) result = conn.getresponse() # If we get a redirect, let's add a useful message. if result.status in (301, 302, 303, 307): response = HttpResponse( ('This proxy does not support redirects. The server in "%s" ' 'asked for a redirect to "%s"' % (url, result.getheader('Location'))), status=result.status, content_type=result.getheader("Content-Type", "text/plain")) response['Location'] = result.getheader('Location') else: response = HttpResponse(result.read(), status=result.status, content_type=result.getheader( "Content-Type", "text/plain")) return response
def _enrich_layer_metadata(self, geonode_layer): url = urlsplit(self.url) if url.scheme == 'https': conn = HTTPSConnection(url.hostname, url.port) else: conn = HTTPConnection(url.hostname, url.port) workspace, layername = geonode_layer.name.split( ":") if ":" in geonode_layer.name else (None, geonode_layer.name) conn.request('GET', '/api/layers/?name=%s' % layername, '', {}) response = conn.getresponse() content = response.read() status = response.status content_type = response.getheader("Content-Type", "text/plain") if status == 200 and 'application/json' == content_type: try: _json_obj = json.loads(content) if _json_obj['meta']['total_count'] == 1: _layer = _json_obj['objects'][0] if _layer: r_fields = {} # Update plain fields for field in GeoNodeServiceHandler.LAYER_FIELDS: if field in _layer and _layer[field]: r_fields[field] = _layer[field] if r_fields: Layer.objects.filter( id=geonode_layer.id).update( **r_fields) geonode_layer.refresh_from_db() # Update Thumbnail if "thumbnail_url" in _layer and _layer["thumbnail_url"]: thumbnail_remote_url = _layer["thumbnail_url"] _url = urlsplit(thumbnail_remote_url) if not _url.scheme: thumbnail_remote_url = "{}{}".format( geonode_layer.remote_service.service_url, _url.path) resp, image = http_client.request( thumbnail_remote_url) if 'ServiceException' in image or \ resp.status < 200 or resp.status > 299: msg = 'Unable to obtain thumbnail: %s' % image logger.debug(msg) # Replace error message with None. image = None if image is not None: thumbnail_name = 'layer-%s-thumb.png' % geonode_layer.uuid geonode_layer.save_thumbnail( thumbnail_name, image=image) else: self._create_layer_thumbnail(geonode_layer) # Add Keywords if "keywords" in _layer and _layer["keywords"]: keywords = _layer["keywords"] if keywords: geonode_layer.keywords.clear() geonode_layer.keywords.add(*keywords) # Add Regions if "regions" in _layer and _layer["regions"]: (regions_resolved, regions_unresolved) = resolve_regions( _layer["regions"]) if regions_resolved: geonode_layer.regions.clear() geonode_layer.regions.add(*regions_resolved) # Add Topic Category if "category__gn_description" in _layer and _layer["category__gn_description"]: try: categories = TopicCategory.objects.filter( Q(gn_description__iexact=_layer["category__gn_description"])) if categories: geonode_layer.category = categories[0] except BaseException: traceback.print_exc() except BaseException: traceback.print_exc() finally: geonode_layer.save()
class Client: httpClient = None def __init__(self , token): self.token = token self.httpClient = HTTPSConnection(vs.HTTP_URL, vs.HTTP_PORT) def __del__( self ): if self.httpClient is not None: self.httpClient.close() def encodepath(self, path): start = 0 n = len(path) re = '' i = path.find('=', start) while i != -1 : re += path[start:i+1] start = i+1 i = path.find('&', start) if(i>=0): for j in range(start, i): if(path[j] > '~'): if ct.PY3: re += urllib.parse.quote(path[j]) else: re += urllib.quote(path[j]) else: re += path[j] re += '&' start = i+1 else: for j in range(start, n): if(path[j] > '~'): if ct.PY3: re += urllib.parse.quote(path[j]) else: re += urllib.quote(path[j]) else: re += path[j] start = n i = path.find('=', start) return re def init(self, token): self.token = token def getData(self, path): result = None path='/data/v1' + path path=self.encodepath(path) try: self.httpClient.request('GET', path, headers = {"Authorization": "Bearer " + self.token}) response = self.httpClient.getresponse() if response.status == vs.HTTP_OK: result = response.read() else: result = response.read() if(path.find('.csv?') != -1): result=result.decode('GBK').encode('utf-8') return response.status, result except Exception as e: raise e return -1, result
# Request an access token from Google: try: conn = HTTPSConnection('www.google.com') except: print('Could not connect to Google') sys.exit() params = urlencode({ 'accountType': 'GOOGLE', 'service': 'mail', 'Email': JID(opts.jid).bare, 'Passwd': opts.password }) headers = {'Content-Type': 'application/x-www-form-urlencoded'} try: conn.request('POST', '/accounts/ClientLogin', params, headers) resp = conn.getresponse().read() data = {} for line in resp.split(): k, v = line.split(b'=', 1) data[k] = v except Exception as e: print('Could not retrieve login data') sys.exit() if b'SID' not in data: print('Required data not found') sys.exit() params = urlencode({ 'SID': data[b'SID'],
def fetch_response(self, request): """Executes request and fetchs service response""" connection = HTTPSConnection(self.SERVER_URL) connection.request(request.method.upper(), request.to_url()) return connection.getresponse().read()
def request(self, method, url, body=None, headers={}): self.request_length = 0 HTTPSConnection.request(self, method, url, body, headers)
class OAuth: def __init__(self, client_id, client_secret): self.client_id = client_id self.client_secret = client_secret self.device_code = '' self.verification_url = '' self.set_connection() # this setup is isolated because it eventually generates a BadStatusLine # exception, after which we always get httplib.CannotSendRequest errors. # When this happens, we try re-creating the exception. def set_connection(self): # HTTPConnection.debuglevel = 1 if sslContext: self.conn = HTTPSConnection('accounts.google.com', context=sslContext) else: self.conn = HTTPSConnection('accounts.google.com') def get_user_code(self): try: self.conn.request( "POST", "/o/oauth2/device/code", urlencode({ 'client_id': self.client_id, 'scope': 'https://www.googleapis.com/auth/youtube' }), {"Content-type": "application/x-www-form-urlencoded"}) response = self.conn.getresponse() if (response.status == 200): data = loads(response.read()) self.device_code = data['device_code'] self.verification_url = data['verification_url'] self.retry_interval = data['interval'] return data['user_code'] else: print(response.status) print(response.read()) except: pass return '' def get_new_token(self): try: self.conn.request( "POST", "/o/oauth2/token", urlencode({ 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': self.device_code, 'grant_type': 'http://oauth.net/grant_type/device/1.0' }), {"Content-type": "application/x-www-form-urlencoded"}) response = self.conn.getresponse() if (response.status == 200): data = loads(response.read()) if 'access_token' in data: self.conn.close() return data['refresh_token'], 1 except: pass return None, self.retry_interval + 2 def get_access_token(self, refresh_token): try: self.conn.request( "POST", "/o/oauth2/token", urlencode({ 'client_id': self.client_id, 'client_secret': self.client_secret, 'refresh_token': refresh_token, 'grant_type': 'refresh_token' }), {"Content-type": "application/x-www-form-urlencoded"}) response = self.conn.getresponse() if (response.status == 200): data = loads(response.read()) self.conn.close() return data['access_token'] else: print("Unexpected response %d" % response.status) print(response.read()) self.conn.close() except: pass return None