Exemplo n.º 1
0
def send_update(channel_id, data):
    serialized = StringIO()
    simplejson.dump(data, serialized)

    serialized.seek(0, 2)
    length = serialized.tell()
    serialized.seek(0)

    req = urllib.Request(publisher_url(channel_id))
    req.add_data(serialized)
    req.add_unredirected_header('Content-type', 'application/json; charset=utf-8')
    req.add_unredirected_header('Content-length', '%d' % length)
    urllib.urlopen(req)
Exemplo n.º 2
0
def send_update(channel_id, data, already_serialized=False, ignore_errors=False):
    if already_serialized:
        serialized = data
        length = len(data)
    else:
        serialized = StringIO()
        simplejson.dump(data, serialized)

        serialized.seek(0, 2)
        length = serialized.tell()
        serialized.seek(0)

    request = urllib.Request(publisher_url(channel_id))
    request.add_data(serialized)
    request.add_unredirected_header('Content-type', 'application/json; charset=utf-8')
    request.add_unredirected_header('Content-length', '%d' % length)

    signals.pre_send_update.send(sender=sys.modules[__name__], channel_id=channel_id, data=data, already_serialized=already_serialized, request=request)

    try:
        response = urllib.urlopen(request)
    except Exception as e:
        signals.post_send_update.send(sender=sys.modules[__name__], channel_id=channel_id, data=data, already_serialized=already_serialized, request=request, response=e)

        if ignore_errors or getattr(settings, 'PUSH_SERVER_IGNORE_ERRORS', False):
            return

        raise

    signals.post_send_update.send(sender=sys.modules[__name__], channel_id=channel_id, data=data, already_serialized=already_serialized, request=request, response=response)