import socket,threading,time,http.client,urllib.parse
msgs=[]
def sub():
    s=socket.create_connection(("10.0.0.25",6379),timeout=10); f=s.makefile("rb")
    req=b"*2\r\n$9\r\nSUBSCRIBE\r\n$11\r\nold_oschina\r\n"; s.sendall(req)
    s.settimeout(0.5); t=time.time()
    def readresp():
        line=f.readline()
        if not line: return None
        t=line[:1]; rest=line[1:].rstrip(b"\r\n")
        if t in (b"+",b"-"): return rest
        if t==b":": return int(rest)
        if t==b"$":
            n=int(rest)
            if n<0: return None
            d=f.read(n); f.read(2); return d
        if t==b"*":
            n=int(rest)
            if n<0: return None
            return [readresp() for _ in range(n)]
    while time.time()-t<10:
        try: r=readresp()
        except Exception: continue
        if isinstance(r,list) and len(r)>=3 and r[0]==b"message":
            msgs.append(r[2])
    s.close()
th=threading.Thread(target=sub); th.start(); time.sleep(1)
body=urllib.parse.urlencode({"email":"chengxinxin@oschina.cn","pwd":"12345qwert,"})
c=http.client.HTTPConnection("10.0.0.13",8081,timeout=8)
c.request("POST","/action/user/login",body=body,headers={"Host":"www.oschina.net","User-Agent":"Mozilla/5.0","Content-Type":"application/x-www-form-urlencoded"})
r=c.getresponse(); r.read(100); c.close(); print("LOGIN",r.status)
time.sleep(3); th.join()
print("MSGS",len(msgs))
for i,m in enumerate(msgs):
    if True:
        print("MSG",i,"LEN",len(m),"HEX",m.hex())
        open("/tmp/l1_msg_%d.bin"%i,"wb").write(m)
