
import argparse
import msgpack
import requests
import sys

from datetime import datetime

import strongfellowbtc.zmq
import strongfellowbtc.constants as constants

def post_incoming_transactions(args=None):
    if args is None:
        args = sys.argv[1:]
    parser = argparse.ArgumentParser()
    parser.add_argument('--host', required=True)
    parser.add_argument('--network', required=True)
    parser.add_argument('--txport', type=int, default=str(constants.RAW_TX_PORT))

    params = parser.parse_args(args)
    
    with strongfellowbtc.zmq.socket(port=params.txport, topic='rawtx') as socket:
        while True:
            topic, tx = socket.recv_multipart()
            delta = datetime.now() - datetime(1970, 1, 1)
            ms = long(delta.total_seconds() * 1000)
            data = msgpack.packb({
                't': ms, # milliseconds since epoch
                'x': tx, # the transaction
                'h': params.host, # short name of the host
                'n': params.network # main, testnet, segnet, etc.
            })
            r = requests.post('http://localhost:8080/transactions', data=data)
            print r.status_code


from __future__ import absolute_import

import argparse
import logging
import requests
import sys

import strongfellowbtc.zmq
from strongfellowbtc.logging import configure_logging
from strongfellowbtc.protocol import ds256
from strongfellowbtc.hex import big_endian_hex

def post_incoming_transactions(args=None):
    if args is None:
        args = sys.argv[1:]
    parser = argparse.ArgumentParser()
    parser.add_argument('--txport', type=int, default=str(constants.RAW_TX_PORT))

    params = parser.parse_args(args)
    
    with strongfellowbtc.zmq.socket(port=params.txport, topic='rawtx') as socket:
        while True:
            logging.info('receiving transactions from zeromq')
            topic, tx = socket.recv_multipart()
            h = big_endian_hex(ds256(tx))
            url = 'http://localhost:8080/transactions/%s' % h
            logging.info('received tx, posting to %s', url)
            r = requests.post(url, data=tx)
            logging.info('posted tx %s with status code %d', h, r.status_code)

simplify poster, lets do the logic on the backend