Skip to content

Commit

Permalink
Merge pull request #7 from jt6211/threatstream
Browse files Browse the repository at this point in the history
added ability to log channel output
  • Loading branch information
jatrost committed May 21, 2014
2 parents 17f9f43 + 51b348e commit 3b35705
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions examples/to_stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,43 @@
import logging
logging.basicConfig(level=logging.WARNING)
import hpfeeds
outstream = sys.stdout

def main():
if len(sys.argv) < 5:
print >> sys.stderr, "Usage: python %s <host> <port> <ident> <secret> <channel,channel2,channel3,...>"%(sys.argv[0])
print >> sys.stderr, "Usage: python %s <host> <port> <ident> <secret> <channel,channel2,channel3,...> [logfile]"%(sys.argv[0])
sys.exit(1)

HOST, PORT, IDENT, SECRET, CHANNELS = [arg.encode("utf-8") for arg in sys.argv[1:6]]
CHANNELS = CHANNELS.split(",")
PORT = int(PORT)

if len(sys.argv) > 6:
outstream = open(sys.argv[6], "a")

hpc = hpfeeds.new(HOST, PORT, IDENT, SECRET)
print >>sys.stderr, 'connected to', hpc.brokername

def on_message(identifier, channel, payload):
try:
payload = str(payload).strip()
print "ident=%s, channel=%s, payload='%s'"%(identifier, channel, payload)
print >>outstream, "ident=%s, channel=%s, payload=%s"%(identifier, channel, payload)
except Exception, e:
print "Error", e
print >> sys.stderr, "Error", e

def on_error(payload):
print >>sys.stderr, ' -> errormessage from server: {0}'.format(payload)
hpc.stop()
outstream.close()

hpc.subscribe(CHANNELS)
hpc.run(on_message, on_error)
hpc.close()
outstream.close()
return 0

if __name__ == '__main__':
try: sys.exit(main())
except KeyboardInterrupt:sys.exit(0)
except KeyboardInterrupt:
outstream.close()
sys.exit(0)

0 comments on commit 3b35705

Please sign in to comment.