diff options
| -rw-r--r-- | PubnubCoreAsync.py | 22 | ||||
| -rw-r--r-- | python-tornado/Pubnub.py | 22 | ||||
| -rw-r--r-- | python-twisted/Pubnub.py | 31 |
3 files changed, 28 insertions, 47 deletions
diff --git a/PubnubCoreAsync.py b/PubnubCoreAsync.py index 4eb62e9..b31fdfe 100644 --- a/PubnubCoreAsync.py +++ b/PubnubCoreAsync.py @@ -21,19 +21,7 @@ except ImportError: import Crypto.Hash.SHA256 as digestmod sha256 = digestmod.new import hmac -from twisted.internet import reactor -from twisted.internet.defer import Deferred -from twisted.internet.protocol import Protocol -from twisted.web.client import Agent -from twisted.web.client import HTTPConnectionPool -from twisted.web.http_headers import Headers from PubnubCrypto import PubnubCrypto -import gzip -import zlib - -pnconn_pool = HTTPConnectionPool(reactor) -pnconn_pool.maxPersistentPerHost = 100 -pnconn_pool.cachedConnectionTimeout = 310 class PubnubCoreAsync(object): @@ -78,12 +66,9 @@ class PubnubCoreAsync(object): self.subscriptions = {} self.timetoken = 0 self.uuid = uuid or str(self.uuid()) - self.headers = { - 'V' : '3.1', - 'User-Agent' : 'Python-*', - 'Accept-Encoding' : 'gzip' - } - + self.version = '3.4' + self.accept_encoding = 'gzip' + print self.ssl if self.ssl : self.origin = 'https://' + self.origin else : @@ -277,6 +262,7 @@ class PubnubCoreAsync(object): pc = PubnubCrypto() out = [] for message in response[0]: + print type(message) if self.cipher_key : if type( message ) == type(list()): for item in message: diff --git a/python-tornado/Pubnub.py b/python-tornado/Pubnub.py index a7d8a01..880e396 100644 --- a/python-tornado/Pubnub.py +++ b/python-tornado/Pubnub.py @@ -48,27 +48,29 @@ class Pubnub(PubnubCoreAsync): origin = 'pubsub.pubnub.com' ) : super(Pubnub, self).__init__( - publish_key, - subscribe_key, - secret_key, - ssl_on, - origin, + publish_key=publish_key, + subscribe_key=subscribe_key, + secret_key=secret_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) print url ## Send Request Expecting JSON Response - http = tornado.httpclient.AsyncHTTPClient(max_clients=1000) - request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers ) + #print self.headers + request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers, connect_timeout=310, request_timeout=310 ) def responseCallback(response): callback(response._get_body()) - http.fetch( + self.http.fetch( request, callback=responseCallback, - connect_timeout=310, - request_timeout=310 ) diff --git a/python-twisted/Pubnub.py b/python-twisted/Pubnub.py index 10875dd..c52bf6d 100644 --- a/python-twisted/Pubnub.py +++ b/python-twisted/Pubnub.py @@ -37,7 +37,7 @@ import gzip import zlib from twisted.internet.ssl import ClientContextFactory -pnconn_pool = HTTPConnectionPool(reactor) +pnconn_pool = HTTPConnectionPool(reactor, persistent=True) pnconn_pool.maxPersistentPerHost = 100 pnconn_pool.cachedConnectionTimeout = 310 @@ -58,12 +58,16 @@ class Pubnub(PubnubCoreAsync): origin = 'pubsub.pubnub.com' ) : super(Pubnub, self).__init__( - publish_key, - subscribe_key, - secret_key, - ssl_on, - origin, + publish_key=publish_key, + subscribe_key=subscribe_key, + secret_key=secret_key, + ssl_on=ssl_on, + origin=origin, ) + self.headers = {} + self.headers['User-Agent'] = ['Python-Twisted'] + #self.headers['Accept-Encoding'] = [self.accept_encoding] + self.headers['V'] = [self.version] def _request( self, request, callback ) : global pnconn_pool @@ -77,18 +81,13 @@ class Pubnub(PubnubCoreAsync): ]) for bit in request]) ''' url = self.getUrl(request) - cf = WebClientContextFactory() agent = ContentDecoderAgent(RedirectAgent(Agent( reactor, - contextFactory = cf, + contextFactory = WebClientContextFactory(), pool = self.ssl and None or pnconn_pool )), [('gzip', GzipDecoder)]) print url - request = agent.request( 'GET', url, Headers({ - 'V' : ['3.1'], - 'User-Agent' : ['Python-Twisted'], - 'Accept-Encoding' : ['gzip'] - }), None ) + request = agent.request( 'GET', url, Headers(self.headers), None ) def received(response): #print response @@ -97,11 +96,6 @@ class Pubnub(PubnubCoreAsync): return finished def complete(data): - #print data - #try : obj = json.loads(data) - #except : obj = None - - #print obj callback(data) request.addCallback(received) @@ -116,6 +110,5 @@ class PubNubResponse(Protocol): self.finished = finished def dataReceived( self, bytes ): - #print bytes self.finished.callback(bytes) |
