python-examples: ddf5dba1e293a6181e4fbeb6cf7f326261b7372f
1: # Blogging server in 17 lines (not counting comments)
2: # Note, you can only post blog entries from an account that is
3: # in the server accounts roster
4:
5: import xmpp, time
6:
7: # This function is run automatically when an incoming message arrives
8: def xmpp_message(con, event):
9: # Handle messages but not protocol overhead like logon/off
10: if event.getType() == 'chat' and event.getBody() is not None:
11: changed_site = [] # Start with a clean HTML file
12: # Add the current HTML file to it
13: for line in open('blog.html', 'r').readlines():
14: changed_site.append(line)
15: # Add a timestamp and the new post above the previous posts, starting after the following comment
16: if line.find('<!--Blog Posts Start Here-->') > -1:
17: changed_site.append(' <h2>' + time.strftime('%Y-%m-%d %H:%M:%S') + '</h2>\n <p>' + event.getBody() + '</p>\n\n')
18: # Replace the old HTML file with the new one
19: open('blog.html','w').writelines(changed_site)
20:
21: # This logs onto XMPP as 'username@server' with password 'password'
22: client = xmpp.Client('server',debug=[])
23: client.connect()
24: client.auth('username','password',resource='microblog')
25: # Declare which function we want to run on incoming messages
26: client.RegisterHandler('message',xmpp_message)
27: # Tell the world that we are online
28: client.sendInitPresence()
29:
30: # Look for messages every second
31: while True:
32: client.Process(1)
33: time.sleep(1)
Generated by git2html.