Skip to content

Instantly share code, notes, and snippets.

@konrad
Created June 5, 2010 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konrad/426697 to your computer and use it in GitHub Desktop.
Save konrad/426697 to your computer and use it in GitHub Desktop.
A little example how to send messages to a OneSociaWeb server in Python.
#!/usr/bin/python
"""
FUNCTION: A little example how to send messages to a OneSociaWeb server
(http://onesocialweb.org). Heavily inspired by Tyler
Gillies' ruby script with the same purpose.
Copyright (c) 2010, Konrad Foerstner <konrad@foerstner.org>
Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted, provided that the
above copyright notice and this permission notice appear in all
copies.
THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
"""
__author__ = "Konrad Foerstner <konrad@foerstner.org>"
__copyright__ = "2010 by Konrad Foerstner <konrad@foerstner.org>"
__license__ = "ISC license"
import datetime
import time
import socket
def main():
# Add account details e.g. for bob@flow.betavine.net
username = "" # e.g. bob
password = "" # something special
host = "" # e.g. flow.betavine.net
osw_sender = OSWSender(host)
osw_sender.connect()
osw_sender.login(username, password)
osw_sender.sent_message("Just another message - title",
"Just another message - content", username, host)
osw_sender.disconnect()
class OSWSender(object):
def __init__(self, host, port=5222):
self.host = host
self.port = port
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((host, port))
def connect(self):
stream = ("<?xml version='1.0'?><stream:stream to='%s' "
"xmlns='jabber:client' xmlns:stream="
"'http://etherx.jabber.org/streams' version='1.0'>"
% (self.host))
self.socket.send(stream)
print self.socket.recv(512)
def login(self, username, password):
auth = ("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' "
"mechanism='PLAIN'/>")
self.socket.send(auth)
print self.socket.recv(512)
login=("<iq type='set' id='auth2'><query xmlns='jabber:iq:auth'>"
"<username>%s</username> <password>%s</password>"
"<resource>raw</resource></query></iq>" % (username, password))
self.socket.send(login)
print self.socket.recv(512)
def sent_message(self, message_title, message_content, username, host):
# Todo - fix time zone information
time = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S ") + "+00:00"
message = (
"<iq type='set' from='%s@%s' id='osw2'>"
"<pubsub xmlns='http://jabber.org/protocol/pubsub'>"
"<publish node='urn:xmpp:microblog:0'>"
"<item>"
"<entry xmlns='http://www.w3.org/2005/Atom'"
"xmlns:activity='http://activitystrea.ms/spec/1.0/' "
"xmlns:osw='http://onesocialweb.org/spec/1.0/'>"
"<published>%s</published>"
"<title>%s</title>"
"<activity:verb>"
"http://activitystrea.ms/schema/1.0/post</activity:verb>"
"<activity:object>"
"<activity:object-type>"
"http://onesocialweb.org/spec/1.0/object"
"/status</activity:object-type>"
"<content type='text/plain'>%s</content>"
"</activity:object>"
"<osw:acl-rule>"
"<osw:acl-action permission="
"'http://onesocialweb.org/spec/1.0/acl/permission/grant'>"
"http://onesocialweb.org/spec/1.0/acl/action/view</osw:acl-action>"
"<osw:acl-subject type="
"'http://onesocialweb.org/spec/1.0/acl/subject/everyone'/>"
"</osw:acl-rule>"
"</entry>"
"</item>"
"</publish>"
"</pubsub>"
"</iq>" % (username, host, time, message_title, message_content))
self.socket.send(message)
print self.socket.recv(512)
def disconnect(self):
self.socket.close()
if __name__ == '__main__': main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment