本文主要參考:http://www.cnblogs.com/mouse-coder/archive/2013/03/03/2941265.html
作者:敲代碼的耗子
原SHA1加密方式可參考本人之前的部落格:http://blog.csdn.net/monsion/article/details/7981366
敲代碼的耗子 同學已經介紹的及其詳細,不再多說,附上python原始碼,攻參考。代碼寫的比較爛,勿噴啊。
首先是weiboLogin.py檔案,實現一個類。
#! /usr/bin/env python# -*- coding: utf-8 -*-import sysimport urllibimport urllib2import cookielibimport base64import reimport jsonimport hashlibimport rsaimport binasciiclass weiboLogin:cj = cookielib.LWPCookieJar()cookie_support = urllib2.HTTPCookieProcessor(cj)opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)urllib2.install_opener(opener)postdata = {'entry': 'weibo','gateway': '1','from': '','savestate': '7','userticket': '1','ssosimplelogin': '1','vsnf': '1','vsnval': '','su': '','service': 'miniblog','servertime': '','nonce': '','pwencode': 'rsa2','sp': '','encoding': 'UTF-8','prelt': '115','rsakv': '','url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack','returntype': 'META'}def get_servertime(self,username):url = 'http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=%s&rsakt=mod&client=ssologin.js(v1.4.4)' % usernamedata = urllib2.urlopen(url).read()p = re.compile('\((.*)\)')try:json_data = p.search(data).group(1)data = json.loads(json_data)servertime = str(data['servertime'])nonce = data['nonce']pubkey = data['pubkey']rsakv = data['rsakv']return servertime, nonce, pubkey, rsakvexcept:print 'Get severtime error!'return Nonedef get_pwd(self, password, servertime, nonce, pubkey):rsaPublickey = int(pubkey, 16)key = rsa.PublicKey(rsaPublickey, 65537) #建立公開金鑰message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #拼接明文js加密檔案中得到passwd = rsa.encrypt(message, key) #加密passwd = binascii.b2a_hex(passwd) #將加密資訊轉換為16進位。return passwddef get_user(self, username):username_ = urllib.quote(username)username = base64.encodestring(username_)[:-1]return usernamedef get_account(self,filename):f=file(filename)flag = 0for line in f:if flag == 0:username = line.strip()flag +=1else:pwd = line.strip()f.close()return username,pwddef login(self,filename):username,pwd = self.get_account(filename)url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.4)'try:servertime, nonce, pubkey, rsakv = self.get_servertime(username)print servertimeprint nonceprint pubkeyprint rsakvexcept:print 'get servertime error!'returnweiboLogin.postdata['servertime'] = servertimeweiboLogin.postdata['nonce'] = nonceweiboLogin.postdata['rsakv'] = rsakvweiboLogin.postdata['su'] = self.get_user(username)weiboLogin.postdata['sp'] = self.get_pwd(pwd, servertime, nonce, pubkey)weiboLogin.postdata = urllib.urlencode(weiboLogin.postdata)headers = {'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0 Chrome/20.0.1132.57 Safari/536.11'}req = urllib2.Request(url = url,data = weiboLogin.postdata,headers = headers)result = urllib2.urlopen(req)text = result.read()print textp = re.compile('location\.replace\(\"(.*)\"\)')#此處和之前略有區別,小心。try:login_url = p.search(text).group(1)#print login_urlurllib2.urlopen(login_url)print "Login success!"return 1except:print 'Login error!'return 0
然後是main.py檔案
# -*- coding: utf-8 -*-import weiboLoginimport urllibimport urllib2import timefilename = './config/account'#儲存微博帳號的使用者名稱和密碼,第一行為使用者名稱,第二行為密碼WBLogin = weiboLogin.weiboLogin()if WBLogin.login(filename)==1:print 'Login success!'else:print 'Login error!'exit()