Skip to content

Instantly share code, notes, and snippets.

@nolochemical
Created October 21, 2012 15:45
Show Gist options
  • Save nolochemical/3927309 to your computer and use it in GitHub Desktop.
Save nolochemical/3927309 to your computer and use it in GitHub Desktop.
Tiny Python Pubnub UDP Server
#!/usr/bin/python2
# http://wiki.python.org/moin/UdpCommunication
import socket
import re
from Pubnub import Pubnub
## Initiate Class
pubnub = Pubnub( 'demo', 'demo', None, False )
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
SENTINEL = True
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
print "Started UDP Service."
while SENTINEL:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
p = re.compile('stop:udp:service')
m = re.search(p, data)
if(m.group() == "stop:udp:service"):
print "Stopping UDP Service."
info = pubnub.publish({
'channel' : 'udp_comms', # your choice
'message' : {
'text' : "Endding comms"
}
})
SENTINEL = False;
## Publish Example
info = pubnub.publish({
'channel' : 'hello_world',
'message' : {
'text' : data
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment