From 09d6ea1fa20f87dc648740ffb2a70f67e7e4efa9 Mon Sep 17 00:00:00 2001 From: Devendra Date: Wed, 25 Sep 2013 13:51:19 +0530 Subject: combining into Pubnub.py, added common async unit test method --- python-tornado/unassembled/Platform.py | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 python-tornado/unassembled/Platform.py (limited to 'python-tornado/unassembled') diff --git a/python-tornado/unassembled/Platform.py b/python-tornado/unassembled/Platform.py new file mode 100644 index 0000000..618927e --- /dev/null +++ b/python-tornado/unassembled/Platform.py @@ -0,0 +1,58 @@ +import tornado.httpclient + +try: + from hashlib import sha256 + digestmod = sha256 +except ImportError: + import Crypto.Hash.SHA256 as digestmod + sha256 = digestmod.new + +import hmac +import tornado.ioloop +from PubnubCrypto import PubnubCrypto + +ioloop = tornado.ioloop.IOLoop.instance() + +class Pubnub(PubnubCoreAsync): + + def stop(self): ioloop.stop() + def start(self): ioloop.start() + def timeout( self, delay, callback): + ioloop.add_timeout( time.time()+float(delay), callback ) + + def __init__( + self, + publish_key, + subscribe_key, + secret_key = False, + cipher_key = False, + ssl_on = False, + origin = 'pubsub.pubnub.com' + ) : + super(Pubnub, self).__init__( + publish_key=publish_key, + subscribe_key=subscribe_key, + secret_key=secret_key, + cipher_key=cipher_key, + ssl_on=ssl_on, + origin=origin, + ) + self.headers = {} + self.headers['User-Agent'] = 'Python-Tornado' + self.headers['Accept-Encoding'] = self.accept_encoding + self.headers['V'] = self.version + self.http = tornado.httpclient.AsyncHTTPClient(max_clients=1000) + + def _request( self, request, callback ) : + url = self.getUrl(request) + ## Send Request Expecting JSON Response + #print self.headers + request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers, connect_timeout=310, request_timeout=310 ) + def responseCallback(response): + callback(eval(response._get_body())) + + self.http.fetch( + request, + callback=responseCallback, + ) + -- cgit v1.2.3 From 7ec3bbe414abe58072fda63f4f9c801f77b292e1 Mon Sep 17 00:00:00 2001 From: Devendra Date: Sun, 6 Oct 2013 14:39:59 +0530 Subject: changing timeout from 200 to 310 s, changed exception handling to not silently eat execeptions in callback --- python-tornado/unassembled/Platform.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'python-tornado/unassembled') diff --git a/python-tornado/unassembled/Platform.py b/python-tornado/unassembled/Platform.py index 618927e..4df04b3 100644 --- a/python-tornado/unassembled/Platform.py +++ b/python-tornado/unassembled/Platform.py @@ -9,6 +9,7 @@ except ImportError: import hmac import tornado.ioloop +from tornado.stack_context import ExceptionStackContext from PubnubCrypto import PubnubCrypto ioloop = tornado.ioloop.IOLoop.instance() @@ -47,9 +48,17 @@ class Pubnub(PubnubCoreAsync): url = self.getUrl(request) ## Send Request Expecting JSON Response #print self.headers - request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers, connect_timeout=310, request_timeout=310 ) + + request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers, connect_timeout=10, request_timeout=310 ) + def responseCallback(response): - callback(eval(response._get_body())) + def handle_exc(*args): + return True + if response.error is not None: + with ExceptionStackContext(handle_exc): + response.rethrow() + else: + callback(eval(response._get_body())) self.http.fetch( request, -- cgit v1.2.3 From 52c51378b4647911a64c8d4b1c759756d4930d4b Mon Sep 17 00:00:00 2001 From: Devendra Date: Sun, 6 Oct 2013 17:38:16 +0530 Subject: included changes of pull request # 4 --- python-tornado/unassembled/Platform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'python-tornado/unassembled') diff --git a/python-tornado/unassembled/Platform.py b/python-tornado/unassembled/Platform.py index 4df04b3..7565fd2 100644 --- a/python-tornado/unassembled/Platform.py +++ b/python-tornado/unassembled/Platform.py @@ -57,7 +57,7 @@ class Pubnub(PubnubCoreAsync): if response.error is not None: with ExceptionStackContext(handle_exc): response.rethrow() - else: + elif callback: callback(eval(response._get_body())) self.http.fetch( -- cgit v1.2.3