66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
# Copyright (c) 2011 Andreas Balogh
|
|
# See LICENSE for details.
|
|
|
|
""" prc_publish.eventlet
|
|
|
|
Price Publisher
|
|
"""
|
|
|
|
# imports
|
|
|
|
from argparse import ArgumentParser
|
|
import eventlet
|
|
import logging
|
|
import os
|
|
import socket
|
|
import sys
|
|
import cPickle as pickle
|
|
|
|
LOG = logging.getLogger()
|
|
|
|
# definitions
|
|
|
|
def main(argv=None):
|
|
if argv is None:
|
|
argv = sys.argv
|
|
LOG.info("starting '%s %s'", os.path.basename(argv[0]), " ".join(argv[1:]))
|
|
# parse options and arguments
|
|
parser = ArgumentParser(description="Price Publisher")
|
|
parser.add_argument("-f", "--file", dest="filename",
|
|
help="read configuration from %(dest)s")
|
|
parser.add_argument("-p", "--port", default=8001, type=int,
|
|
help="server port [default: %(default)s")
|
|
args = parser.parse_args()
|
|
print args
|
|
# application
|
|
address = ('localhost', 8010)
|
|
eventlet.spawn_n(subscriber, address)
|
|
while True:
|
|
eventlet.sleep(1)
|
|
|
|
def subscriber(address):
|
|
while True:
|
|
while True:
|
|
try:
|
|
LOG.info("connecting to %s", address)
|
|
cx = eventlet.connect(address)
|
|
break
|
|
except socket.error, e:
|
|
LOG.error("%i %s", e.errno, e)
|
|
eventlet.sleep(1)
|
|
while True:
|
|
s = cx.recv(4096)
|
|
if len(s) == 0:
|
|
LOG.error("connection broken")
|
|
break
|
|
item = pickle.loads(s)
|
|
LOG.info("%s", item)
|
|
cx.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(level=logging.DEBUG,
|
|
format='%(asctime)s.%(msecs)03i %(levelname).4s %(funcName)10s: %(message)s',
|
|
datefmt='%H:%M:%S')
|
|
main()
|