无视界-个人小站
python版discuz 的uc_client客户端.


#!/usr/bin/python  
# -*- coding: UTF-8 -*-  
# $Id: uc_client.py 6903 2011-10-21 16:36:21$  
# author : 漠北     QQ4620498  
# /upload/2021/11'':  
            key=self.UC_KEY;  
        ckey_length = 4;  
        key = md5(key);  
        keya = md5(key[0:16]);  
        keyb = md5(key[16:32]);  

        #print key,keya,keyb,key[16:32]  
        #keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';  
        if ckey_length>0:  
            if operation == 'DECODE':  
                keyc=string[0: ckey_length];  
            else:  
                keyc=md5(str(time.time()))[0-ckey_length:];  

        cryptkey = keya+md5(keya+keyc);  

        key_length = len(cryptkey);  
        if operation=='DECODE':  
             string=base64.decodestring(string[ckey_length:]+==)  
        else:  
            #sprintf('%010d', $expiry ? $expiry + time() : 0)  
            if expiry==0:  
                e=%010d%0;  
            else:  
                e=%010d%(expiry+int(time.time()));  
            e=e+(md5(string+keyb))[0:16];  
            string=e+string;  
        string_length = len(string);      
        result = '';  
        box={};  
        for i in range(0, 256):  
            box[i]=i;  

        rndkey = {};  

        for i in range(0,256):  
            s=i % key_length;  
            rndkey[i] = ord(cryptkey[s:s+1]);  

        j=0;  

        for i in range(0,256):  
            j=(j+box[i]+rndkey[i]) % 256;  
            tmp = box[i];  
            box[i]=box[j];  
            box[j]=tmp;  

        a=j=0;  
        for i in range(string_length):  
            a = ( a + 1 ) % 256;  
            j = ( j + box[a] ) % 256;  
            tmp = box[a];  
            box[a] = box[j];  
            box[j] = tmp;  

            result+= chr(ord(string[i:i+1]) ^ box[(box[a] + box[j]) % 256])#chr(ord(string[i:i+1])) ^ box[(box[a] + box[j]) % 256];  

        if operation == 'DECODE':  
            if (int(result[0:10]) == 0 or int(result[0:10])-int(time.time()) >0) and result[10:26] == (md5(result[26:]+keyb))[0:16]:  
                return result[26:]  
            else:  
                return '';  
        else:  
            result=base64.encodestring(result).replace(==,)#.replace(
,)  
            return keyc+result  
    def uc_api_post(self,module,action, kwargs):  
        s=sep=;  
        for key in kwargs.keys():  
            k=str(key);  
            v=kwargs[k];  
            if type(v) is types.DictType:  
                s2=sep2=;  
                for k2 in v.keys():  
                    v2=v[k2];  

                    k2=urllib.quote(k2);  
                    v2=urllib.quote(v2);  
                    s2+=sep2+k+k2+=.urllib.quote(stripslashes(str(v2)));  
                    sep2=&;  
                s+=sep+s2;  
            else:  
                t=sep+k+=+urllib.quote(stripslashes(str(v)));  
                s+=t  
            sep='&';  

        postdata = self.uc_api_requestdata(module, action, s);  
        return self.uc_fopen2(self.UC_API+'/index.php', 8096, postdata, '', True, self.UC_IP, 20);  
    def uc_fopen2(self,address, limit=1024, post='', cookie='', bysocket=False, ip='', timeout=15, block=True):  
        headers = {  
               Connection:Clost,  
        Cache-Control: no-cache,  
        Cookie: cookie,  
        Accept-Language:zh-cn,  
        User-Agent:Python Cient  
               };  

        #try:  
        url = httplib.urlsplit(address)  
        conn = httplib.HTTPConnection(url.netloc)  
        if post!='':  
            headers['Content-Type']=application/x-www-form-urlencoded;  
            conn.request(method=POST,url=url.path,body=post,headers=headers);  
        else:  
            conn.request(method=GET,url=url.path,headers=headers);  

        f = conn.getresponse()  
        data='';  
        while True:  
            t = f.read(limit)  
            if not len(t):  
                break  

            data+=t;  
        f.close()  
        conn.close()  
        return data;  
    def uc_unserialize(self,result):  
        x=re.findall(<item[^>*]*>(.*?)</item>,result)  
        r=[];  
        for i in x:  
            if i.find(<![CDATA[)!=-1:  
                r.append(i.replace(<![CDATA[,)[:-3])  
            else:  
                r.append(i)  
        return r;  
    def uc_api_input(self,data):  
        return urllib.quote(self.uc_authcode(data+'&agent='+md5(Python Cient)+&time=+str(int(time.time())), 'ENCODE', self.UC_KEY))  
    def uc_api_requestdata(self,module, action, a='', extra=''):  

        input = self.uc_api_input(a);  
        post = m=+module+&a=+action+&inajax=2&release=+self.UC_CLIENT_RELEASE+&input=+input+&appid=+str(self.UC_APPID)+extra;  
        return post;  
    def uc_user_register(self,username, password, email, questionid = '', answer = '', regip = ''):  
        return self.uc_api_post('user','register',{'username':username,'password':password,'email':email,'questionid':questionid,'answer':answer,'regip':regip});  
    def uc_user_login(self,username, password, isuid = 0, checkques = 0, questionid = '', answer = ''):  
    isuid = int(isuid);  
        return self.uc_api_post('user', 'login', {'username':username, 'password':password, 'isuid':isuid, 'checkques':checkques, 'questionid':questionid, 'answer':answer})  
    #return self.uc_unserialize();  
    def uc_get_user(self,username, isuid=0) :  
    return self.uc_unserialize(self.uc_api_post('user', 'get_user', {'username':username, 'isuid':isuid}));  
    def uc_user_synlogin(self,uid) :  
        uid = int(uid);  
        o=self.uc_api_post('user', 'synlogin', {'uid':uid});  
        if o!=:  
            url=o[o.find(/upload/2021/11 __main__:  
    u=ucClient(http://bbs.yourdomain.com/uc_server,key=64位的UCKEY,appid=4)  
    user=u.uc_user_login('xxxxxsx','123456');  
    print user  

将此python版uc_client奉献给战斗在python的web开发领域的同仁们