aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevendra2013-02-24 23:57:51 +0530
committerDevendra2013-02-24 23:57:51 +0530
commit8b1a2eb372f2206f24029cc7e4694ae472599d95 (patch)
tree54894f79624e3c7fe9ede1dd89ac724944fff76e
parent51b1dec8b04258f7bff59fb5bb4f69cbab52260c (diff)
downloadpubnub-python-8b1a2eb372f2206f24029cc7e4694ae472599d95.tar.bz2
saving work on async clients consolidation
-rw-r--r--PubnubCoreAsync.py107
-rw-r--r--python-tornado/Pubnub.py16
-rw-r--r--python-twisted/Pubnub.py61
3 files changed, 66 insertions, 118 deletions
diff --git a/PubnubCoreAsync.py b/PubnubCoreAsync.py
index 0ea03e9..4eb62e9 100644
--- a/PubnubCoreAsync.py
+++ b/PubnubCoreAsync.py
@@ -49,7 +49,8 @@ class PubnubCoreAsync(object):
secret_key = False,
cipher_key = False,
ssl_on = False,
- origin = 'pubsub.pubnub.com'
+ origin = 'pubsub.pubnub.com',
+ uuid = None
) :
"""
#**
@@ -76,6 +77,12 @@ class PubnubCoreAsync(object):
self.ssl = ssl_on
self.subscriptions = {}
self.timetoken = 0
+ self.uuid = uuid or str(self.uuid())
+ self.headers = {
+ 'V' : '3.1',
+ 'User-Agent' : 'Python-*',
+ 'Accept-Encoding' : 'gzip'
+ }
if self.ssl :
self.origin = 'https://' + self.origin
@@ -160,7 +167,7 @@ class PubnubCoreAsync(object):
signature = '0'
## Send Message
- return self._request([
+ return self._request({ "urlcomponents" : [
'publish',
self.publish_key,
self.subscribe_key,
@@ -168,7 +175,7 @@ class PubnubCoreAsync(object):
channel,
'0',
message
- ], publish_response )
+ ] }, publish_response )
@@ -234,7 +241,6 @@ class PubnubCoreAsync(object):
return "Already Connected"
self.subscriptions[channel]['connected'] = 1
- print self.subscriptions
## SUBSCRIPTION RECURSION
def substabizel():
## STOP CONNECTION?
@@ -242,6 +248,7 @@ class PubnubCoreAsync(object):
return
def sub_callback(response):
+ print response
response = json.loads(response)
## STOP CONNECTION?
if not self.subscriptions[channel]['connected']:
@@ -292,13 +299,13 @@ class PubnubCoreAsync(object):
## CONNECT TO PUBNUB SUBSCRIBE SERVERS
try :
- self._request( [
+ self._request( { "urlcomponents" : [
'subscribe',
self.subscribe_key,
channel,
'0',
str(self.timetoken)
- ], sub_callback )
+ ], "urlparams" : {"uuid":self.uuid} }, sub_callback )
except :
self.timeout( 1, substabizel )
return
@@ -347,13 +354,13 @@ class PubnubCoreAsync(object):
## Get History
pc = PubnubCrypto()
- return self._request( [
+ return self._request( {"urlcomponents" : [
'history',
self.subscribe_key,
channel,
'0',
str(limit)
- ], args['callback'] )
+ ]}, args['callback'] )
def time( self, args ) :
"""
@@ -376,10 +383,10 @@ class PubnubCoreAsync(object):
if not response: return 0
args['callback'](response[0])
- self._request( [
+ self._request( { "urlcomponents" : [
'time',
'0'
- ], complete )
+ ]}, complete )
def uuid(self) :
"""
@@ -397,86 +404,16 @@ class PubnubCoreAsync(object):
"""
return uuid.uuid1()
- def _request( self, request, callback ) :
- global pnconn_pool
-
+ def getUrl(self,request):
## Build URL
url = self.origin + '/' + "/".join([
"".join([ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and
hex(ord(ch)).replace( '0x', '%' ).upper() or
ch for ch in list(bit)
- ]) for bit in request])
-
- requestType = request[0]
- agent = Agent(
- reactor,
- self.ssl and None or pnconn_pool,
- connectTimeout=30
- )
- request = agent.request( 'GET', url, Headers({
- 'V' : ['3.1'],
- 'User-Agent' : ['Python-Twisted'],
- 'Accept-Encoding' : ['gzip']
- }), None )
-
- self.resulting_is = str()
- def received(response):
- headerlist = list(response.headers.getAllRawHeaders())
- for item in headerlist:
- if( item[0] == "Content-Encoding"):
- if type(item[1]) == type(list()):
- for subitem in item[1]:
- self.resulting_is = subitem
- elif type(item[1]) == type(str()):
- self.resulting_is = item[1]
-
- finished = Deferred()
- response.deliverBody(PubNubResponse(finished))
- return finished
-
- def complete(data):
- if ( type(data) == type(str()) ):
- if self.resulting_is:
- d = zlib.decompressobj(16+zlib.MAX_WBITS)
-
- try : data = d.decompress(data) # try/catch here, pass through if except
- except : data = data
-
- try : obj = json.loads(data)
- except : obj = None
-
- pc = PubnubCrypto()
- out = []
- if self.cipher_key :
- if requestType == "history" :
- if type(obj) == type(list()):
- for item in obj:
- if type(item) == type(list()):
- for subitem in item:
- encryptItem = pc.decrypt(self.cipher_key, subitem )
- out.append(encryptItem)
- elif type(item) == type(dict()):
- outdict = {}
- for k, subitem in item.iteritems():
- encryptItem = pc.decrypt(self.cipher_key, subitem )
- outdict[k] = encryptItem
- out.append(outdict)
- else :
- encryptItem = pc.decrypt(self.cipher_key, item )
- out.append(encryptItem)
- callback(out)
- elif type( obj ) == type(dict()):
- for k, item in obj.iteritems():
- encryptItem = pc.decrypt(self.cipher_key, item )
- out.append(encryptItem)
- callback(out)
- else :
- callback(obj)
- else :
- callback(obj)
-
- request.addCallback(received)
- request.addBoth(complete)
+ ]) for bit in request["urlcomponents"]])
+ if (request.has_key("urlparams")):
+ url = url + '?' + "&".join([ x + "=" + y for x,y in request["urlparams"].items()])
+ return url
def _request( self, request, callback, timeout=30 ) :
pass
diff --git a/python-tornado/Pubnub.py b/python-tornado/Pubnub.py
index e9251f8..a7d8a01 100644
--- a/python-tornado/Pubnub.py
+++ b/python-tornado/Pubnub.py
@@ -9,7 +9,6 @@
## PubNub 3.1 Real-time Push Cloud API
## -----------------------------------
import sys
-sys.path.append('../')
from PubnubCoreAsync import PubnubCoreAsync
import json
import time
@@ -55,24 +54,15 @@ class Pubnub(PubnubCoreAsync):
ssl_on,
origin,
)
+ self.headers['User-Agent'] = 'Python-Tornado'
def _request( self, request, callback ) :
- ## Build URL
- url = self.origin + '/' + "/".join([
- "".join([ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and
- hex(ord(ch)).replace( '0x', '%' ).upper() or
- ch for ch in list(bit)
- ]) for bit in request])
+ url = self.getUrl(request)
print url
## Send Request Expecting JSON Response
http = tornado.httpclient.AsyncHTTPClient(max_clients=1000)
- request = tornado.httpclient.HTTPRequest( url, 'GET', dict({
- 'V' : '3.1',
- 'User-Agent' : 'Python-Tornado',
- 'Accept-Encoding' : 'gzip'
- }) )
+ request = tornado.httpclient.HTTPRequest( url, 'GET', self.headers )
def responseCallback(response):
- print response._get_body()
callback(response._get_body())
http.fetch(
diff --git a/python-twisted/Pubnub.py b/python-twisted/Pubnub.py
index f7d5c52..10875dd 100644
--- a/python-twisted/Pubnub.py
+++ b/python-twisted/Pubnub.py
@@ -29,12 +29,13 @@ from twisted.web.client import getPage
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 Agent, ContentDecoderAgent, RedirectAgent, GzipDecoder
from twisted.web.client import HTTPConnectionPool
from twisted.web.http_headers import Headers
from PubnubCrypto import PubnubCrypto
import gzip
import zlib
+from twisted.internet.ssl import ClientContextFactory
pnconn_pool = HTTPConnectionPool(reactor)
pnconn_pool.maxPersistentPerHost = 100
@@ -64,37 +65,57 @@ class Pubnub(PubnubCoreAsync):
origin,
)
- def _request( self, request, callback, timeout=30 ) :
+ def _request( self, request, callback ) :
global pnconn_pool
## Build URL
+ '''
url = self.origin + '/' + "/".join([
"".join([ ' ~`!@#$%^&*()+=[]\\{}|;\':",./<>?'.find(ch) > -1 and
hex(ord(ch)).replace( '0x', '%' ).upper() or
ch for ch in list(bit)
]) for bit in request])
-
- requestType = request[0]
- agent = Agent(
+ '''
+ url = self.getUrl(request)
+ cf = WebClientContextFactory()
+ agent = ContentDecoderAgent(RedirectAgent(Agent(
reactor,
- self.ssl and None or pnconn_pool,
- connectTimeout=timeout
- )
+ contextFactory = cf,
+ pool = self.ssl and None or pnconn_pool
+ )), [('gzip', GzipDecoder)])
print url
- gp = getPage( url, headers={
- 'V' : ['3.4'],
+ request = agent.request( 'GET', url, Headers({
+ 'V' : ['3.1'],
'User-Agent' : ['Python-Twisted'],
'Accept-Encoding' : ['gzip']
- } );
-
- gp.addCallback(callback)
- gp.addErrback(callback)
+ }), None )
+
+ def received(response):
+ #print response
+ finished = Deferred()
+ response.deliverBody(PubNubResponse(finished))
+ return finished
+
+ def complete(data):
+ #print data
+ #try : obj = json.loads(data)
+ #except : obj = None
+
+ #print obj
+ callback(data)
+
+ request.addCallback(received)
+ request.addBoth(complete)
+
+class WebClientContextFactory(ClientContextFactory):
+ def getContext(self, hostname, port):
+ return ClientContextFactory.getContext(self)
+class PubNubResponse(Protocol):
+ def __init__( self, finished ):
+ self.finished = finished
-#class PubNubResponse(Protocol):
-# def __init__( self, finished ):
-# self.finished = finished
-#
-# def dataReceived( self, bytes ):
-# self.finished.callback(bytes)
+ def dataReceived( self, bytes ):
+ #print bytes
+ self.finished.callback(bytes)