Skip to content
Browse files

add a really basic postgres example

  • Loading branch information...
1 parent d072862 commit 5bb865870a56f791ad41c2473518a0dc3767ba96 @rep committed Jan 16, 2012
Showing with 53 additions and 0 deletions.
  1. +53 −0 cli/basic_postgres.py
View
53 cli/basic_postgres.py
@@ -0,0 +1,53 @@
+
+import sys
+import datetime
+import json
+import logging
+logging.basicConfig(level=logging.CRITICAL)
+
+import psycopg2
+import hpfeeds
+
+HOST = 'hpfeeds.honeycloud.net'
+PORT = 10000
+CHANNELS = ['dionaea.capture', ]
+IDENT = ''
+SECRET = ''
+
+def main():
+ conn = psycopg2.connect("host=localhost dbname=hpfeeds user=username password=pw")
+ cur = conn.cursor()
+
+ try:
+ hpc = hpfeeds.new(HOST, PORT, IDENT, SECRET)
+ except hpfeeds.FeedException, e:
+ print >>sys.stderr, 'feed exception:', e
+ return 1
+
+ print >>sys.stderr, 'connected to', hpc.brokername
+
+ def on_message(identifier, channel, payload):
+ cur.execute("INSERT INTO rawlog (identifier, channel, payload) VALUES (%s, %s, %s)", (identifier, channel, payload))
+ conn.commit()
+
+ def on_error(payload):
+ print >>sys.stderr, ' -> errormessage from server: {0}'.format(payload)
+ hpc.stop()
+
+ hpc.subscribe(CHANNELS)
+ try:
+ hpc.run(on_message, on_error)
+ except hpfeeds.FeedException, e:
+ print >>sys.stderr, 'feed exception:', e
+ except KeyboardInterrupt:
+ pass
+ finally:
+ cur.close()
+ conn.close()
+ hpc.close()
+ return 0
+
+if __name__ == '__main__':
+ try: sys.exit(main())
+ except KeyboardInterrupt:sys.exit(0)
+

0 comments on commit 5bb8658

Please sign in to comment.
Something went wrong with that request. Please try again.