def _sendOneshot(self, oneshot): body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) for s,p,o in oneshot)).encode('utf8') fetch(method='POST', url='http://bang6:9071/oneShot', headers={'Content-Type': ['text/n3']}, postdata=body, timeout=5)
def twitter_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given API path, e.g., "/statuses/user_timeline/btaylor" The path should not include the format (we automatically append ".json" and parse the JSON output). If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the Twitter methods are documented at http://apiwiki.twitter.com/Twitter-API-Documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage: class MainHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.twitter_request( "/statuses/update", post_args={"status": "Testing Tornado Web Server"}, access_token=user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ # Add the OAuth resource request signature if we have credentials url = "http://twitter.com" + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) self._oauth_consumer_token() method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib.urlencode(args) if post_args is not None: d = httpclient.fetch(url, method="POST", postdata=urllib.urlencode(post_args)) d.addCallback(self.async_callback(self._on_twitter_request, callback)) else: d = httpclient.fetch(url) d.addCallback(self.async_callback(self._on_twitter_request, callback))
def _oneShotPostActions(self, deviceGraph, inferred): """ Inferred graph may contain some one-shot statements. We'll send statement objects to anyone on web sockets, and also generate POST requests as described in the graph. one-shot statement ?s ?p ?o with this in the graph: ?osp a :OneShotPost ?osp :subject ?s ?osp :predicate ?p this will cause a post to ?o """ # nothing in this actually makes them one-shot yet. they'll # just fire as often as we get in here, which is not desirable log.info("_oneShotPostActions") def err(e): log.warn("post %s failed", postTarget) for osp in deviceGraph.subjects(RDF.type, ROOM['OneShotPost']): s = deviceGraph.value(osp, ROOM['subject']) p = deviceGraph.value(osp, ROOM['predicate']) if s is None or p is None: continue for postTarget in inferred.objects(s, p): log.info("post target %r", postTarget) # this packet ought to have 'oneShot' in it somewhere self.sendToLiveClients({"s":s, "p":p, "o":postTarget}) log.info(" POST %s", postTarget) fetch(postTarget, method="POST", timeout=2).addErrback(err) self._postMpdCommands(inferred)
def get_authenticated_user(self, callback): """Gets the OAuth authorized user and access token on callback. This method should be called from the handler for your registered OAuth Callback URL to complete the registration process. We call callback with the authenticated user, which in addition to standard attributes like 'name' includes the 'access_key' attribute, which contains the OAuth access you can use to make authorized requests to this service on behalf of the user. """ request_key = self.get_argument("oauth_token") request_cookie = self.get_cookie("_oauth_request_token") if not request_cookie: log.err("Missing OAuth request token cookie") callback(None) return cookie_key, cookie_secret = request_cookie.split("|") if cookie_key != request_key: log.err("Request token does not match cookie") callback(None) return token = dict(key=cookie_key, secret=cookie_secret) httpclient.fetch(self._oauth_access_token_url(token)) d.addCallback(self.async_callback(self._on_access_token, callback))
def _sendOneshot(self, oneshot): body = (' '.join('%s %s %s .' % (s.n3(), p.n3(), o.n3()) for s,p,o in oneshot)).encode('utf8') bang6 = 'fcb8:4119:fb46:96f8:8b07:1260:0f50:fcfa' fetch(method='POST', url='http://[%s]:9071/oneShot' % bang6, headers={'Content-Type': ['text/n3']}, postdata=body, timeout=5)
def authenticate_redirect(self): """Just like authorize_redirect(), but auto-redirects if authorized. This is generally the right interface to use if you are using Twitter for single-sign on. """ httpclient.fetch(self._oauth_request_token_url()).addCallback( self.async_callback( self._on_request_token, self._OAUTH_AUTHENTICATE_URL, None))
def authenticate_redirect(self): """Just like authorize_redirect(), but auto-redirects if authorized. This is generally the right interface to use if you are using Twitter for single-sign on. """ httpclient.fetch(self._oauth_request_token_url()).addCallback( self.async_callback(self._on_request_token, self._OAUTH_AUTHENTICATE_URL, None))
def twitter_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given API path, e.g., "/statuses/user_timeline/btaylor" The path should not include the format (we automatically append ".json" and parse the JSON output). If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the Twitter methods are documented at http://apiwiki.twitter.com/Twitter-API-Documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage: class MainHandler(tornado.web.RequestHandler, tornado.auth.TwitterMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.twitter_request( "/statuses/update", post_args={"status": "Testing Tornado Web Server"}, access_token=user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ # Add the OAuth resource request signature if we have credentials url = "http://twitter.com" + path + ".json" if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) consumer_token = self._oauth_consumer_token() method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib.urlencode(args) if post_args is not None: d = httpclient.fetch(url, method="POST", postdata=urllib.urlencode(post_args)) d.addCallback(self.async_callback(self._on_twitter_request, callback)) else: d = httpclient.fetch(url) d.addCallback(self.async_callback(self._on_twitter_request, callback))
def friendfeed_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/bret/friends" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the FriendFeed methods are documented at http://friendfeed.com/api/documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage: class MainHandler(tornado.web.RequestHandler, tornado.auth.FriendFeedMixin): @tornado.web.authenticated @tornado.web.asynchronous def get(self): self.friendfeed_request( "/entry", post_args={"body": "Testing Tornado Web Server"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ # Add the OAuth resource request signature if we have credentials url = "http://friendfeed-api.com/v2" + path if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) self._oauth_consumer_token() method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib.urlencode(args) if post_args is not None: d = httpclient.fetch(url, method="POST", postdata=urllib.urlencode(post_args)) d.addCallback(self.async_callback(self._on_friendfeed_request, callback)) else: httpclient.fetch(url).addCallback(self.async_callback( self._on_friendfeed_request, callback))
def _put(self, url, payload, agent=None): assert isinstance(payload, bytes) def err(e): log.warn(" put %s failed (%r)", url, e) log.info(" PUT %s payload=%s agent=%s", url, payload, agent) headers = {} if agent is not None: headers['x-foaf-agent'] = [str(agent)] fetch(url, method="PUT", postdata=payload, timeout=2, headers=headers).addErrback(err)
def facebook_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/btaylor/picture" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. An introduction to the Facebook Graph API can be found at http://developers.facebook.com/docs/api Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(cyclone.web.RequestHandler, cyclone.auth.FacebookGraphMixin): @cyclone.web.authenticated @cyclone.web.asynchronous def get(self): self.facebook_request( "/me/feed", post_args={"message": "Posting from my cyclone app!"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ url = "https://graph.facebook.com" + path all_args = {} if access_token: all_args["access_token"] = access_token all_args.update(args) if all_args: url += "?" + urllib_parse.urlencode(all_args) callback = self.async_callback(self._on_facebook_request, callback) if post_args is not None: httpclient.fetch(url, method="POST", postdata=urllib_parse.urlencode( post_args)).addCallback(callback) else: httpclient.fetch(url).addCallback(callback)
def friendfeed_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/bret/friends" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. All the FriendFeed methods are documented at http://friendfeed.com/api/documentation. Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(cyclone.web.RequestHandler, cyclone.auth.FriendFeedMixin): @cyclone.web.authenticated @cyclone.web.asynchronous def get(self): self.friendfeed_request( "/entry", post_args={"body": "Testing cyclone Web Server"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ # Add the OAuth resource request signature if we have credentials url = "http://friendfeed-api.com/v2" + path if access_token: all_args = {} all_args.update(args) all_args.update(post_args or {}) method = "POST" if post_args is not None else "GET" oauth = self._oauth_request_parameters( url, access_token, all_args, method=method) args.update(oauth) if args: url += "?" + urllib_parse.urlencode(args) if post_args is not None: d = httpclient.fetch(url, method="POST", postdata=urllib_parse.urlencode(post_args)) d.addCallback(self.async_callback(self._on_friendfeed_request, callback)) else: httpclient.fetch(url).addCallback(self.async_callback( self._on_friendfeed_request, callback))
def get_authenticated_user(self, redirect_uri, client_id, client_secret, code, callback, extra_fields=None): """Handles the login for the Facebook user, returning a user object. Example usage:: class FacebookGraphLoginHandler(LoginHandler, cyclone.auth.FacebookGraphMixin): @cyclone.web.asynchronous def get(self): if self.get_argument("code", False): self.get_authenticated_user( redirect_uri='/auth/facebookgraph/', client_id=self.settings["facebook_api_key"], client_secret=self.settings["facebook_secret"], code=self.get_argument("code"), callback=self.async_callback( self._on_login)) return self.authorize_redirect( redirect_uri='/auth/facebookgraph/', client_id=self.settings["facebook_api_key"], extra_params={"scope": "read_stream,offline_access"}) def _on_login(self, user): logging.error(user) self.finish() """ args = { "redirect_uri": redirect_uri, "code": code, "client_id": client_id, "client_secret": client_secret, } fields = set([ 'id', 'name', 'first_name', 'last_name', 'locale', 'picture', 'link' ]) if extra_fields: fields.update(extra_fields) httpclient.fetch(self._oauth_request_token_url(**args))\ .addCallback(self.async_callback( self._on_access_token, redirect_uri, client_id, client_secret, callback, fields))
def delete_object(self, bucket_name, object_name): bucket = self.riak_client.bucket(bucket_name) obj = yield bucket.get_binary(object_name) if obj.exists(): _object = json.loads(obj.get_data()) if 'ObjectPath' in object_name: httpclient.fetch('http://127.0.0.1:8098/%s' % _object['ObjectPath'], method="DELETE") log.msg('Deleted object %s from bucket %s on %s' % (object_name, bucket_name, _object['ObjectPath'])) else: log.msg('Deleted object %s from bucket %s' % (object_name, bucket_name)) obj.delete() defer.returnValue(True) defer.returnValue(False)
def get_authenticated_user(self, callback): """Fetches the authenticated user data upon redirect. This method should be called by the handler that receives the redirect from the authenticate_redirect() or authorize_redirect() methods. """ # Verify the OpenID response via direct request to the OP args = dict((k, v[-1]) for k, v in self.request.arguments.iteritems()) args["openid.mode"] = u"check_authentication" url = self._OPENID_ENDPOINT + "?" + urllib.urlencode(args) callback = self.async_callback(self._on_authentication_verified, callback) httpclient.fetch(url).addBoth(callback)
def get_authenticated_user(self, callback): """Fetches the authenticated user data upon redirect. This method should be called by the handler that receives the redirect from the authenticate_redirect() or authorize_redirect() methods. """ # Verify the OpenID response via direct request to the OP args = dict((k, v[-1]) for k, v in self.request.arguments.items()) args["openid.mode"] = u"check_authentication" url = self._OPENID_ENDPOINT callback = self.async_callback(self._on_authentication_verified, callback) httpclient.fetch(url, method="POST", postdata=urllib.urlencode(args)).addBoth(callback)
def facebook_request(self, path, callback, access_token=None, post_args=None, **args): """Fetches the given relative API path, e.g., "/btaylor/picture" If the request is a POST, post_args should be provided. Query string arguments should be given as keyword arguments. An introduction to the Facebook Graph API can be found at http://developers.facebook.com/docs/api Many methods require an OAuth access token which you can obtain through authorize_redirect() and get_authenticated_user(). The user returned through that process includes an 'access_token' attribute that can be used to make authenticated requests via this method. Example usage:: class MainHandler(cyclone.web.RequestHandler, cyclone.auth.FacebookGraphMixin): @cyclone.web.authenticated @cyclone.web.asynchronous def get(self): self.facebook_request( "/me/feed", post_args={"message": "Posting from my cyclone app!"}, access_token=self.current_user["access_token"], callback=self.async_callback(self._on_post)) def _on_post(self, new_entry): if not new_entry: # Call failed; perhaps missing permission? self.authorize_redirect() return self.finish("Posted a message!") """ url = "https://graph.facebook.com" + path all_args = {} if access_token: all_args["access_token"] = access_token all_args.update(args) if all_args: url += "?" + urllib.urlencode(all_args) callback = self.async_callback(self._on_facebook_request, callback) if post_args is not None: httpclient.fetch(url, method="POST", postdata=urllib.urlencode(post_args)).addCallback(callback) else: httpclient.fetch(url).addCallback(callback)
def get(self): try: cpOrderId = self.get_argument("cpOrderId") orderStatus = self.get_argument("orderStatus") payFee = self.get_argument("payFee") payTime = self.get_argument("payTime") signature = self.get_argument("signature") cpUserInfo = self.get_argument("cpUserInfo") except Exception: raise web.HTTPError(400, "Argument error") params = self.request.arguments.copy() params.pop('signature') for x, y in params.items(): params[x] = y[0] params = yield self.format_params(params, False) m = hashlib.md5() m.update(params) _sign = m.hexdigest() if signature != _sign: url = "%s/xmpay/notify/" % cpUserInfo params = dict(cpOrderId=cpOrderId, orderStatus=orderStatus, payFee=payFee, payTime=payTime) yield httpclient.fetch(httputil.url_concat(url, params)) ret = dict(error_code=200) else: ret = dict(error_code=1525, errMsg=E.errmsg(E.ERR_SIGN)) reb = zlib.compress(escape.json_encode(ret)) self.write(ret)
def event_toughcloud_mail_account_expire(self, userinfo): """ toughcloud mail api notify event """ if not userinfo: return api_secret = self.get_param_value("toughcloud_license") service_mail = self.get_param_value("toughcloud_service_mail") if not service_mail: return api_token = yield tools.get_sys_token() params = dict(token=api_token.strip(), mailto=userinfo.email, tplname=self.MAIL_TPLNAME, customer=utils.safestr(userinfo.realname), username=userinfo.account_number, product=utils.safestr(userinfo.product_name), expire=userinfo.expire_date, service_call=self.get_param_value( "toughcloud_service_call", ''), service_mail=service_mail, nonce=str(int(time.time()))) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) try: resp = yield httpclient.fetch(self.MAIL_APIURL, postdata=urlencode(params)) logger.info(resp.body) except Exception as err: logger.exception(err)
def post(self): dom = parseString(self.request.body) root = dom.firstChild childs = root.childNodes for child in childs: if child.nodeType == child.TEXT_NODE: pass else: if child.nodeName == 'contentId': contentId = child.childNodes[0].data elif child.nodeName == 'consumeCode': consumeCode = child.childNodes[0].data elif child.nodeName == 'cpid': cpid = child.childNodes[0].data elif child.nodeName == 'hRet': hRet = child.childNodes[0].data elif child.nodeName == 'cpparam': print child.childNodes[0].data.encode("utf-8") orderId, zoneid = child.childNodes[0].data.split('%2C') else:pass res = yield self.sql.runQuery("SELECT domain FROM core_zone WHERE zoneid=%s", (zoneid, )) if res: domain, = res[0] url = "%s/cmpay/notify/" % domain params = dict(orderId=orderId, contentId=contentId, consumeCode=consumeCode, cpid=cpid, hRet=hRet) yield httpclient.fetch(httputil.url_concat(url, params)) self.write('''<?xml version="1.0" encoding="UTF-8"?> <request><hRet>0</hRet><message>Successful</message> </request>''')
def getPresentMacAddrs_multirouter(self): rows = [] for router in self.routers: log.debug("GET %s", router) try: resp = yield fetch(router.url, headers=router.headers, timeout=2) except socket.error: log.warn("get on %s failed" % router) continue data = resp.body if 'Wireless -- Authenticated Stations' in data: # zyxel 'Station Info' page rows.extend(self._parseZyxel(data, router.name)) else: # tomato page rows.extend(self._parseTomato(data, router.name)) for r in rows: try: r['name'] = self.knownMacAddr[r['mac']] except KeyError: pass returnValue(rows)
def loadCiscoData(): config = json.load(open("/my/proj/homeauto/service/tomatoWifi/priv-uva.json")) headers = {'Authorization': ['Basic %s' % config['userPass'].encode('base64').strip()]} print headers resp = yield fetch('http://10.2.0.2/', headers=headers) print resp.body returnValue([])
def post(self): logging.info("/shafa/notify/ start") params = self.request.arguments.copy() logging.info(params) try: data = self.get_argument("data") except Exception: logging.error("/shafa/notify/ 获得的传入参数有误") self.write("获得的传入参数有误") return data_json = json.loads(data) price = data_json['price'] quantity = data_json['quantity'] custom_data = data_json['custom_data'] time = data_json['time'] custom_data = data_json['custom_data'] logging.info("/shafa/notify/ 1 custom_data:"+custom_data) custom_data_json = json.loads(custom_data) ptorder = custom_data_json['ptorder'] app_order_id,hurl = ptorder.split('_') url = "%s/shafa/notify/" % hurl print "shafa notify url:",url params = dict(app_order_id=app_order_id, price=price, quantity=quantity, time=time) respose = yield httpclient.fetch(httputil.url_concat(url, params)) self.write("SUCCESS") logging.info("/shafa/notify/ end")
def process(self, *args, **kwargs): next_interval = self.get_notify_interval() user_total = 0 online_total = 0 with make_db(self.db) as db: try: user_total = db.query(models.TrAccount).count() online_total = db.query(models.TrOnline).count() except Exception as err: pass try: api_url = "https://www.toughcloud.net/api/v1/ping" api_token = yield tools.get_sys_token() params = dict( token=api_token, app="toughradius", ver=__version__, release=self.config.system.get('release', "standard"), unum=user_total, onum=online_total, dist=' '.join(pf.linux_distribution()), ) param_str = urlencode(params) resp = yield httpclient.fetch(api_url + "?" + param_str, followRedirect=True) logger.info("toughcloud ping resp code: %s" % resp.code) except Exception as err: logger.error(err) defer.returnValue(next_interval)
def process(self, *args, **kwargs): next_interval = self.get_notify_interval() user_total = 0 online_total = 0 with make_db(self.db) as db: try: user_total = db.query(models.TrAccount).count() online_total = db.query(models.TrOnline).count() except Exception as err: pass try: api_url = "https://www.toughcloud.net/api/v1/ping" api_token = yield tools.get_sys_token() params = dict( token=api_token, app="toughradius", ver=__version__, release=self.config.system.get('release',"standard"), unum=user_total, onum=online_total, dist=' '.join(pf.linux_distribution()), ) param_str = urlencode(params) resp = yield httpclient.fetch(api_url+"?"+param_str,followRedirect=True) logger.info("toughcloud ping resp code: %s"%resp.code) except Exception as err: logger.error(err) defer.returnValue(next_interval)
def post(self): headers = {"Content-type": ["application/x-www-form-urlencoded"]} username = self.get_argument("username") params = [username] sign = mksign(self.settings.config.defaults.secret, params) post_data={"userName":username,"sign":sign} body = urllib.urlencode(post_data) radius_list = self.db.query(models.TraRadius).all() packets = [] for radius in radius_list: try: resp = yield httpclient.fetch("http://%s:1815/api/queryPacketRecord" %radius.ip_addr, postdata=body, headers=headers) resp_msg = json.loads(resp.body) packets.extend(resp_msg.get('data') or []) log.msg(u'%s recieve a response ' %radius.ip_addr) except Exception, e: import traceback log.msg(u'%s cannot recieve a response, check this radius client please' %radius.ip_addr) traceback.print_exc()
def index(req): url = req.get_argument("q", None) if url: if os.path.exists(req.settings.cache.getpath(url)): print 'true???' req.redirect("/mustashify?q=" + url) defer.returnValue(None) else: req.render("index.html", err={}) defer.returnValue(None) if not url.startswith("http"): url = "http://" + url try: tmp = urlparse.urlparse(url) assert tmp.scheme assert tmp.netloc except: req.render("index.html", err={"url": True}) defer.returnValue(None) try: response = yield fetch(url, followRedirect=1, maxRedirects=3) assert response.code == 200, "Bad response code %s" % response.code except Exception, e: #log.err() req.render("index.html", err={"fetch": True}) defer.returnValue(None)
def ping(self): conn = self.db() try: nonce = str(time.time()) action = "ping" headers = {"Content-Type": ["application/json"]} for oss in conn.query(models.TraOssServer): try: sign = self.mksign(oss.secret, params=[action, nonce]) reqdata = json.dumps(dict(action=action, nonce=nonce, sign=sign)) resp = yield httpclient.fetch(oss.admin_url, postdata=reqdata, headers=headers) if resp.code != 200: self.syslog.error("ping oss <%s> error,http status = %s" % (oss.admin_url, resp.code)) continue jsonresp = json.loads(resp.body) if jsonresp['code'] == 0: oss.last_check = utils.get_currtime() except Exception as err: defer.returnValue("ping oss <%s> error, %s" % (oss.admin_url, str(err))) conn.commit() defer.returnValue("ping oss success") except Exception as err: defer.returnValue("ping oss error, %s" % str(err)) finally: conn.close()
def post(self): logging.info("/cmpay/notify/ start") dom = parseString(self.request.body) root = dom.firstChild childs = root.childNodes for child in childs: if child.nodeType == child.TEXT_NODE: pass else: if child.nodeName == 'contentId': contentId = child.childNodes[0].data elif child.nodeName == 'consumeCode': consumeCode = child.childNodes[0].data elif child.nodeName == 'cpid': cpid = child.childNodes[0].data elif child.nodeName == 'hRet': hRet = child.childNodes[0].data elif child.nodeName == 'cpparam': print child.childNodes[0].data.encode("utf-8") orderId, zoneid = child.childNodes[0].data.split('%2C') else:pass res = yield self.sql.runQuery("SELECT domain FROM core_zone WHERE zoneid=%s", (zoneid, )) if res: domain, = res[0] url = "%s/cmpay/notify/" % domain params = dict(orderId=orderId, contentId=contentId, consumeCode=consumeCode, cpid=cpid, hRet=hRet) yield httpclient.fetch(httputil.url_concat(url, params)) self.write('''<?xml version="1.0" encoding="UTF-8"?> <request><hRet>0</hRet><message>Successful</message> </request>''') logging.info("/cmpay/notify/ end")
def mustashify(req): url = req.get_argument("q") try: response = yield fetch(url, followRedirect=1, maxRedirects=3) except Exception, e: #log.err() raise web.HTTPError(404)
def get(self): logging.info("/xmpay/notify/ start") try: cpOrderId = self.get_argument("cpOrderId") orderStatus = self.get_argument("orderStatus") payFee = self.get_argument("payFee") payTime = self.get_argument("payTime") signature = self.get_argument("signature") cpUserInfo = self.get_argument("cpUserInfo") except Exception: raise web.HTTPError(400, "Argument error") params = self.request.arguments.copy() params.pop('signature') for x, y in params.items(): params[x] = y[0] params = yield self.format_params(params, False) m = hashlib.md5() m.update(params) _sign = m.hexdigest() if signature != _sign: url = "%s/xmpay/notify/" % cpUserInfo params = dict(cpOrderId=cpOrderId, orderStatus=orderStatus, payFee=payFee, payTime=payTime) yield httpclient.fetch(httputil.url_concat(url, params)) ret = dict(error_code=200) else: ret = dict(error_code=1525, errMsg=E.errmsg(E.ERR_SIGN)) reb = zlib.compress(escape.json_encode(ret)) self.write(ret) logging.info("XmNotifyHandler /xmpay/notify/ end.")
def setColor(lightName, rgb): """takes 10-bit r,g,b returns even if the server is down """ log.debug("setColor(%r,%r)", lightName, rgb) serv = lightResource[lightName] h = hexFromRgb(rgb) log.debug("put %r to %r", h, serv) t1 = time.time() d = fetch(url=serv, method='PUT', postdata=h, headers={'content-type': ['text/plain']}) def err(e): log.warn("http client error on %s: %s" % (serv, e)) raise e d.addErrback(err) def done(ret): log.debug('put took %.1fms', 1000 * (time.time() - t1)) d.addCallback(done) return d
def add_portal(self): sign = self.mksign(params=[ self.server.portal_name, self.server.portal_addr, self.server.admin_listen, self.server.api_secret, self.server.port, self.server.listen, self.server.ac1[0] ]) reqdata = json.dumps(dict( name=self.server.portal_name, ip_addr=self.server.portal_addr, admin_port=self.server.admin_listen, secret=self.server.api_secret, http_port=self.server.port, listen_port=self.server.listen, ac_server=self.server.ac1[0], sign=sign )) if self.server.debug: self.syslog.debug("register portal request: %s" % reqdata) d = httpclient.fetch("%s/portal/add" % self.server.apiurl, postdata=reqdata, headers={"Content-Type": ["application/json"]}) d.addCallback(self.on_add_portal)
def get_template_attrs(self,ssid): cache_key = '{0}{1}'.format('get_template_attrs', ssid) _resp = portal_cache.get(cache_key) if _resp: if self.settings.debug: self.syslog.debug("query template request hit cache; key=%s" % cache_key) defer.returnValue(_resp) return sign = self.mksign(params=[ssid]) reqdata = json.dumps(dict(ssid=ssid, sign=sign)) apiurl = "%s/tpl/query" % self.settings.apiurl if self.settings.debug: self.syslog.debug("query template request (%s): %s" % (apiurl,reqdata)) resp = yield httpclient.fetch(apiurl,postdata=reqdata, headers={"Content-Type": ["application/json"]}) jsonresp = json.loads(resp.body) if jsonresp['code'] == 1: self.syslog.error("query template attrs error, %s" % jsonresp['msg']) defer.returnValue({'tpl_name':'default'}) return if jsonresp['code'] == 0: self.syslog.info("query template attrs success") portal_cache.set(cache_key, jsonresp['attrs'], expire=60) defer.returnValue(jsonresp['attrs'])
def get(self): logging.info("/lgpay/notify/ start") try: serial_no = self.get_argument("serial_no") transaction_id = self.get_argument("transaction_id") result_code = self.get_argument("result_code") fee = self.get_argument("fee") except Exception: raise web.HTTPError(400, "Argument error") r,zoneid = serial_no.split('_') logging.info("/lgpay/notify/ zoneid:",zoneid) res = yield self.sql.runQuery("SELECT domain FROM core_zone WHERE zoneid=%s", (zoneid, )) if res: domain, = res[0] if int(result_code) == 120: url = "%s/lgpay/notify/" % domain logging.info("/lgpay/notify/ url:",url) params = dict(serial_no=serial_no, transaction_id=transaction_id, fee=fee) yield httpclient.fetch(httputil.url_concat(url, params)) ret = dict(serial_no=serial_no) reb = zlib.compress(escape.json_encode(ret)) self.write(ret) logging.info("/lgpay/lgpay/ end")
def more(self): trs = [T.tr['']] response = yield fetch("http://bang:8007/commands", headers={"X-Foaf-Agent": [str(self.user)]}) if not response: raise ValueError( '-H "X-Foaf-Agent: %s" http://bang:8007/commands failed' % str(self.user)) cmds = jsonlib.loads(response.body) belowZero = [] for (cmd, score) in cmds: cmd = URIRef(cmd) if score < 0: belowZero.append((cmd, score)) continue if len(trs[-1].children) >= 1 + columns: trs.append(T.tr['']) trs[-1].children.append(T.td["\n", self._buttonForm(cmd, score)]) trs.append(T.tr[T.td(colspan=columns)]) for (cmd, score) in belowZero: trs[-1].children[-1][self._buttonForm(cmd, score)] returnValue(T.table[trs])
def event_toughcloud_sms_account_open(self, userinfo): """ toughCloud sms api open notify event """ if not userinfo: return if not userinfo.get('phone'): logger.error('user phone is None exit') return api_secret = self.get_param_value("toughcloud_license") api_token = yield tools.get_sys_token() params = dict( token=api_token.strip(), action='sms', tplname=self.SMS_TPLNAME, phone=userinfo.get('phone'), customer=utils.safestr(userinfo.get('realname')), username=userinfo.get('account_number'), product=utils.safestr(userinfo.get('product_name')), password=userinfo.get('password'), expire=userinfo.get('expire_date'), nonce=str(int(time.time())) ) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) try: resp = yield httpclient.fetch(self.SMS_APIURL, postdata=urlencode(params)) logger.info(resp.body) logger.info('open account send short message success') except Exception as err: logger.exception(err)
def event_toughcloud_mail_account_open(self, userinfo): """ toughCloud mail api open notify without password event """ if not userinfo: return if not userinfo.get('email'): logger.error('user email is None exit') return try: api_secret = self.get_param_value("toughcloud_license") service_mail = self.get_param_value("toughcloud_service_mail") if not service_mail: return api_token = yield tools.get_sys_token() params = dict( token=api_token.strip(), action='email', mailto=userinfo.get('email'), tplname=self.MAIL_TPLNAME, customer=utils.safestr(userinfo.get('realname')), username=userinfo.get('account_number'), product=utils.safestr(userinfo.get('product_name')), expire=userinfo.get('expire_date'), service_call=self.get_param_value("toughcloud_service_call", ''), service_mail=service_mail, nonce=str(int(time.time())) ) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) resp = yield httpclient.fetch(self.MAIL_APIURL, postdata=urlencode(params)) logger.info(resp.body) logger.info('open account send email without password success') except Exception as err: logger.exception(err)
def pollMusicTime(self): def cb(response): if response.code != 200: raise ValueError("%s %s", response.code, response.body) position = json.loads(response.body) # this is meant to be the time when the server gave me its # report, and I don't know if that's closer to the # beginning of my request or the end of it (or some # fraction of the way through) self.positionFetchTime = time.time() self.position = position self.onChange(position) reactor.callLater(self.period, self.pollMusicTime) def eb(err): log.warn("talking to ascoltami: %s", err.getErrorMessage()) reactor.callLater(2, self.pollMusicTime) d = fetch(networking.musicPlayer.path("time")) d.addCallback(cb) d.addErrback(eb) # note this includes errors in cb()
def get_authenticated_user(self, callback): """Gets the OAuth authorized user and access token on callback. This method should be called from the handler for your registered OAuth Callback URL to complete the registration process. We call callback with the authenticated user, which in addition to standard attributes like 'name' includes the 'access_key' attribute, which contains the OAuth access you can use to make authorized requests to this service on behalf of the user. """ request_key = escape.utf8(self.get_argument("oauth_token")) oauth_verifier = self.get_argument("oauth_verifier", None) request_cookie = self.get_cookie("_oauth_request_token") if not request_cookie: log.msg("Missing OAuth request token cookie") callback(None) return self.clear_cookie("_oauth_request_token") cookie_key, cookie_secret = [ base64.b64decode(escape.utf8(i)) for i in request_cookie.split("|") ] if cookie_key != request_key: log.msg("Request token does not match cookie") callback(None) return token = dict(key=cookie_key, secret=cookie_secret) if oauth_verifier: token["verifier"] = oauth_verifier d = httpclient.fetch(self._oauth_access_token_url(token)) d.addCallback(self.async_callback(self._on_access_token, callback))
def process(self, *args, **kwargs): next_interval = self.get_next_interval() user_total = 0 online_total = 0 with make_db(self.db) as db: try: user_total = db.query(models.TrAccount).count() online_total = db.query(models.TrOnline).count() except Exception as err: pass try: api_url = 'http://www.taurusxcloud.net/api/v1/ping' api_token = yield tools.get_sys_token() params = dict(token=api_token, app='taurusxee', ver=__version__, release=os.environ.get('LICENSE_TYPE', 'taurusxee'), unum=user_total, onum=online_total, dist=pf.linux_distribution()) param_str = urlencode(params) resp = yield httpclient.fetch(api_url + '?' + param_str, followRedirect=True) logger.info('taurusxcloud ping resp code: %s' % resp.code) except Exception as err: logger.error(err) defer.returnValue(next_interval)
def event_toughcloud_sms_account_open(self, userinfo): """ toughCloud sms api open notify event """ if not userinfo: return if not userinfo.get('phone'): logger.error('user phone is None exit') return api_secret = self.get_param_value("toughcloud_license") api_token = yield tools.get_sys_token() params = dict(token=api_token.strip(), action='sms', tplname=self.SMS_TPLNAME, phone=userinfo.get('phone'), customer=utils.safestr(userinfo.get('realname')), username=userinfo.get('account_number'), product=utils.safestr(userinfo.get('product_name')), password=userinfo.get('password'), expire=userinfo.get('expire_date'), nonce=str(int(time.time()))) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) try: resp = yield httpclient.fetch(self.SMS_APIURL, postdata=urlencode(params)) logger.info(resp.body) logger.info('open account send short message success') except Exception as err: logger.exception(err)
def event_toughcloud_mail_account_expire(self, userinfo): """ toughcloud mail api notify event """ if not userinfo: return api_secret = self.get_param_value("toughcloud_license") service_mail=self.get_param_value("toughcloud_service_mail") if not service_mail: return api_token = yield tools.get_sys_token() params = dict( token=api_token.strip(), mailto=userinfo.email, tplname=self.MAIL_TPLNAME, customer=utils.safestr(userinfo.realname), username=userinfo.account_number, product=utils.safestr(userinfo.product_name), expire=userinfo.expire_date, service_call=self.get_param_value("toughcloud_service_call",''), service_mail=service_mail, nonce = str(int(time.time())) ) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) try: resp = yield httpclient.fetch(self.MAIL_APIURL, postdata=urlencode(params)) logger.info(resp.body) except Exception as err: logger.exception(err)
def index(req): url = req.get_argument("q", None) if url: if os.path.exists(req.settings.cache.getpath(url)): print 'true???' req.redirect("/mustashify?q="+url) defer.returnValue(None) else: req.render("index.html", err={}) defer.returnValue(None) if not url.startswith("http"): url = "http://"+url try: tmp = urlparse.urlparse(url) assert tmp.scheme assert tmp.netloc except: req.render("index.html", err={"url":True}) defer.returnValue(None) try: response = yield fetch(url, followRedirect=1, maxRedirects=3) assert response.code == 200, "Bad response code %s" % response.code except Exception, e: #log.err() req.render("index.html", err={"fetch":True}) defer.returnValue(None)
def more(self): trs = [T.tr['']] response = yield fetch("http://bang:8007/commands", headers={"X-Foaf-Agent":[str(self.user)]}) if not response: raise ValueError('-H "X-Foaf-Agent: %s" http://bang:8007/commands failed' % str(self.user)) cmds = jsonlib.loads(response.body) belowZero = [] for (cmd, score) in cmds: cmd = URIRef(cmd) if score < 0: belowZero.append((cmd, score)) continue if len(trs[-1].children) >= 1 + columns: trs.append(T.tr['']) trs[-1].children.append(T.td["\n", self._buttonForm(cmd, score)]) trs.append(T.tr[T.td(colspan=columns)]) for (cmd, score) in belowZero: trs[-1].children[-1][self._buttonForm(cmd, score)] returnValue(T.table[trs])
def event_toughcloud_mail_account_open(self, userinfo): """ toughCloud mail api open notify without password event """ if not userinfo: return if not userinfo.get('email'): logger.error('user email is None exit') return try: api_secret = self.get_param_value("toughcloud_license") service_mail = self.get_param_value("toughcloud_service_mail") if not service_mail: return api_token = yield tools.get_sys_token() params = dict(token=api_token.strip(), action='email', mailto=userinfo.get('email'), tplname=self.MAIL_TPLNAME, customer=utils.safestr(userinfo.get('realname')), username=userinfo.get('account_number'), product=utils.safestr(userinfo.get('product_name')), expire=userinfo.get('expire_date'), service_call=self.get_param_value( "toughcloud_service_call", ''), service_mail=service_mail, nonce=str(int(time.time()))) params['sign'] = apiutils.make_sign(api_secret.strip(), params.values()) resp = yield httpclient.fetch(self.MAIL_APIURL, postdata=urlencode(params)) logger.info(resp.body) logger.info('open account send email without password success') except Exception as err: logger.exception(err)
def poll(self): ret = None startTime = time.time() try: url = (f'http://{deviceIp}/cgi-bin/cgi_manager').encode('ascii') resp = yield fetch( url, method=b'POST', headers={b'Authorization': [b'Basic %s' % auth]}, postdata=(f'''<LocalCommand> <Name>get_usage_data</Name> <MacId>0x{macId}</MacId> </LocalCommand> <LocalCommand> <Name>get_price_blocks</Name> <MacId>0x{macId}</MacId> </LocalCommand>''').encode('ascii'), timeout=10) ret = json.loads(resp.body) log.debug(ret) if ret['demand_units'] != 'kW': raise ValueError if ret['summation_units'] != 'kWh': raise ValueError pts = [ dict(measurement='housePowerW', fields=dict(value=float(ret['demand']) * 1000), tags=dict(house='berkeley'), time=int(startTime))] sd = float(ret['summation_delivered']) if sd > 0: # Sometimes nan pts.append(dict(measurement='housePowerSumDeliveredKwh', fields=dict(value=float()), tags=dict(house='berkeley'), time=int(startTime))) if 'price' in ret: pts.append(dict( measurement='price', fields=dict(price=float(ret['price']), price_units=float(ret['price_units'])), tags=dict(house='berkeley'), time=int(startTime), )) self.influx.write_points(pts, time_precision='s') self.graph.patchObject(context=ROOM['powerEagle'], subject=ROOM['housePower'], predicate=ROOM['instantDemandWatts'], newObject=Literal(float(ret['demand']) * 1000)) except Exception as e: traceback.print_exc() log.error("failed: %r", e) log.error(repr(ret)) os.abort() now = time.time() goal = startTime + periodSec - .2 reactor.callLater(max(1, goal - now), self.poll)
def changed(uri): """call this to rescan an image""" # errors? url = 'http://%s:%s/update' % networking.imageSet() log.info('post %r for index update', url) return fetch(url, method='POST', postdata=urllib.urlencode([('uri', str(uri))]))
def getSimilarDocs(self, doc_id, limit=10, skip=0): search_url = 'http://%s:%s/%s/%s/%s/_mlt?mlt_fields=content' % ( self.config.host, self.config.port, self.config.index, self.config.document_type, str(doc_id)) response = yield httpclient.fetch(search_url, method='POST') jsondata = json.loads(response.body) jsondata = jsondata["hits"]["hits"] jsondata = [x["_source"] for x in jsondata][:3] defer.returnValue(jsondata)
def authorize_redirect(self, callback_uri=None): """Redirects the user to obtain OAuth authorization for this service. Twitter and FriendFeed both require that you register a Callback URL with your application. You should call this method to log the user in, and then call get_authenticated_user() in the handler you registered as your Callback URL to complete the authorization process. This method sets a cookie called _oauth_request_token which is subsequently used (and cleared) in get_authenticated_user for security purposes. """ if callback_uri and getattr(self, "_OAUTH_NO_CALLBACKS", False): raise Exception("This service does not support oauth_callback") callback = self.async_callback(self._on_request_token, self._OAUTH_AUTHORIZE_URL, callback_uri) httpclient.fetch(self._oauth_request_token_url()).addCallback(callback)
def request(self, path, method="GET", postdata=None, headers={}): url = "http://localhost:%s%s" % (self.port, path) if isinstance(postdata, dict): postdata = json.dumps(postdata) response = yield httpclient.fetch(url, method=method, postdata=postdata, headers=headers) defer.returnValue(response)
def post_request(self, requestURL, requestArgs): headers = {'Content-Type': ['application/json']} url = base_url + requestURL data = json.dumps(requestArgs) response = yield httpclient.fetch(url, method='POST', headers=headers, postdata=data) response = json.loads(response.body) defer.returnValue(response)
def getDocument(self, doc_id): qry = {"query": {"bool": {"must": [{"term": {"doc.did": doc_id}}]}}} search_url = 'http://%s:%s/%s/%s/_search' % ( self.config.host, self.config.port, self.config.index, self.config.document_type) response = yield httpclient.fetch(search_url, method='POST', postdata=json.dumps(qry)) jsondata = json.loads(response.body) jsondata = jsondata["hits"]["hits"][0]["_source"] defer.returnValue(jsondata)
def send_sms(self, phone, tplid, args = [], kwargs = {}): smsapiurl = '{0}/taurusxee/sendsms'.format(self.url) kwargs['apikey'] = self.apiuser kwargs['phone'] = phone kwargs['tplname'] = tplid kwargs['timestamp'] = long(time.time()) kwargs['sign'] = apiutils.make_sign(self.apikey, kwargs.values()) postdata = urlencode(kwargs) resp = yield httpclient.fetch(smsapiurl, postdata=postdata) ret = json.loads(resp.body) defer.returnValue(ret['code'] == 0)
def _req(method, url, body, headers): d = fetch(url=url, method=method, postdata=body, headers=dict((k, [v]) for k, v in headers.items())) @d.addErrback def err(e): log.warn("http client error on %s: %s" % (url, e)) raise e return d
def addData(source): try: trig = (yield fetch(source)).body except AttributeError: print vars() raise try: g.addN(parseTrig(trig)) except Exception: import traceback print "fetching %s:" % source traceback.print_exc()
def send_sms(self, phone, tplid, args = [], kwargs = {}): smspwd = md5(utils.safestr('%s%s' % (self.apiuser, self.apikey))).hexdigest() _params = dict(ac='send', uid=utils.safestr(apikey), pwd=smspwd, mobile=utils.safestr(phone), content=utils.safestr(json.dumps(kwargs, ensure_ascii=False))) sms_api_url = '%s?%s' % (self.url, urlencode(_params)) resp = yield httpclient.fetch(sms_api_url, followRedirect=True) if '200' != str(resp.status_code): defer.returnValue(True) else: ret = json.loads(resp.body) if self.debug: logger.info(ret) defer.returnValue(ret['stat'] == 0)
def process(self, *args, **kwargs): next_interval = self.get_notify_interval() try: api_url = "https://www.toughcloud.net/api/v1/ping" api_token = yield tools.get_sys_token() param_str = urlencode({'token': api_token}) resp = yield httpclient.fetch(api_url + "?" + param_str, followRedirect=True) logger.info("toughcloud ping resp code: %s" % resp.code) if resp.code == 200: self.cache.set(toughcloud_ping_key, resp.body, expire=3600) except Exception as err: logger.error(err) defer.returnValue(next_interval)