Created
February 12, 2012 15:19
-
-
Save mthurlin/1809025 to your computer and use it in GitHub Desktop.
bpclient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gevent | |
from gevent import socket | |
import re | |
import json | |
import traceback | |
import datetime | |
ip = "1.2.3.4" | |
def beaconPushClient(callback, apiKey, channels): | |
def listen(seqId): | |
s = socket.create_connection((ip, 80)) | |
s.sendall("GET /spheres/%s/users/guest_123?sequenceId=%d&authToken=t&channels=%s HTTP/1.0\r\n" % (apiKey, seqId, ",".join(channels))) | |
s.sendall("Host: server1.beaconpush.com\r\n") | |
s.sendall("\r\n") | |
buffer = "" | |
while True: | |
buf = s.recv(1024) | |
if not buf: | |
break | |
buffer += buf | |
if "\r\n\r\n" in buffer: | |
break | |
header, body = buffer.split("\r\n\r\n", 1) | |
length = int(re.findall("content-length:\s*(\d+)", header.lower())[0]) | |
length = length - len(body) | |
while length > 0: | |
buf = s.recv(length) | |
if not buf: | |
break | |
body += buf | |
length -= len(buf) | |
s.close() | |
return body | |
seqId = 0 | |
while True: | |
try: | |
body = listen(seqId) | |
data = json.loads(body) | |
seqId = data["seqId"] | |
for msg in data["messages"]: | |
gevent.spawn(callback, msg) | |
except Exception: | |
traceback.print_exc() | |
gevent.sleep(1) | |
def handle(msg): | |
now = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") | |
out = "%s, %d, %d, %d" % (now, msg["playing"], msg["users"], msg["rps"]) | |
beaconPushClient(handle, "abcdef", ["rupps"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment