defget_pwd_rsa(pwd,servertime,nonce):""" Get rsa2 encrypted password, using RSA module from https://pypi.python.org/pypi/rsa/3.1.1, documents can be accessed at http://stuvel.eu/files/python-rsa-doc/index.html """#n, n parameter of RSA public key, which is published by WEIBO.COM#Hardcoded here but you can also find it from values return from prelogin status aboveweibo_rsa_n='EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443'#e, exponent parameter of RSA public key, WEIBO uses 0x10001, which is 65537 in Decimalweibo_rsa_e=65537message=str(servertime)+'\t'+str(nonce)+'\n'+str(pwd)#construct WEIBO RSA Publickey using n and e above, note that n is a hex stringkey=rsa.PublicKey(int(weibo_rsa_n,16),weibo_rsa_e)#get encrypted passwordencropy_pwd=rsa.encrypt(message,key)#trun back encrypted password binaries to hex stringreturnbinascii.b2a_hex(encropy_pwd)
3.验证用户登录是否成功,并且保存Cookies
12345678910111213
data=urllib2.urlopen(login_url).read()#Verify login feedback, check whether result is TRUEpatt_feedback='feedBackUrlCallBack\((.*)\)'p=re.compile(patt_feedback,re.MULTILINE)feedback=p.search(data).group(1)feedback_json=json.loads(feedback)iffeedback_json['result']:cookie_jar2.save(cookie_file,ignore_discard=True,ignore_expires=True)#Save Cookiesreturn1else:return0