Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 78 lines (62 sloc) 2.46 kb
a9b392c Stephen L. Blum Upgraded to PubNub 3.0 Python Real-time Cloud Push API.
stephenlb authored
1 ## www.pubnub.com - PubNub Real-time push service in the cloud.
2 # coding=utf8
3
4 ## PubNub Real-time Push APIs and Notifications Framework
5 ## Copyright (c) 2010 Stephen Blum
6 ## http://www.pubnub.com/
7
8 ## -----------------------------------
9 ## PubNub 3.0 Real-time Push Cloud API
10 ## -----------------------------------
11
12 from Pubnub import Pubnub
13 import sys
14
15 publish_key = len(sys.argv) > 1 and sys.argv[1] or 'demo'
16 subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'demo'
17 secret_key = len(sys.argv) > 3 and sys.argv[3] or None
18 ssl_on = len(sys.argv) > 4 and bool(sys.argv[4]) or False
19
20
21 ## -----------------------------------------------------------------------
22 ## Initiat Class
23 ## -----------------------------------------------------------------------
24
25 pubnub = Pubnub( publish_key, subscribe_key, secret_key, ssl_on )
26 crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':",./<>?abcd'
27
28 ## ---------------------------------------------------------------------------
29 ## Unit Test Function
30 ## ---------------------------------------------------------------------------
31 def test( trial, name ) :
32 if trial :
33 print( 'PASS: ' + name )
34 else :
35 print( 'FAIL: ' + name )
36
37 ## -----------------------------------------------------------------------
38 ## Publish Example
39 ## -----------------------------------------------------------------------
40 pubish_success = pubnub.publish({
41 'channel' : crazy,
42 'message' : crazy
43 })
44 test( pubish_success[0] == 1, 'Publish First Message Success' )
45
46 ## -----------------------------------------------------------------------
47 ## History Example
48 ## -----------------------------------------------------------------------
49 history = pubnub.history({
50 'channel' : crazy,
51 'limit' : 1
52 })
53 test(
54 history[0].encode('utf-8') == crazy,
55 'History Message: ' + history[0]
56 )
57 test( len(history) == 1, 'History Message Count' )
58
59 ## -----------------------------------------------------------------------
60 ## PubNub Server Time Example
61 ## -----------------------------------------------------------------------
62 timestamp = pubnub.time()
63 test( timestamp > 0, 'PubNub Server Time: ' + str(timestamp) )
64
65 ## -----------------------------------------------------------------------
66 ## Subscribe Example
67 ## -----------------------------------------------------------------------
68 def receive(message) :
69 print(message)
70 return True
71
72 pubnub.subscribe({
73 'channel' : crazy,
74 'callback' : receive
75 })
76
77
Something went wrong with that request. Please try again.