Created
May 16, 2015 12:47
-
-
Save elprup/1d8a0f9eb6c4070b6f5b to your computer and use it in GitHub Desktop.
weibo login simulator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf8 | |
import urllib | |
import urllib2 | |
import cookielib | |
import base64 | |
import re, sys, json | |
import binascii | |
import rsa | |
def http_get(url): | |
return urllib2.urlopen(url).read() | |
def http_post(url, postdict, headerdict): | |
postdata = urllib.urlencode(postdict) | |
req = urllib2.Request(url=url, data=postdata, headers=headerdict) | |
result = urllib2.urlopen(req) | |
return result.read() | |
def setup_urllib2(): | |
cookie_support = urllib2.HTTPCookieProcessor(cookielib.LWPCookieJar()) | |
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) | |
urllib2.install_opener(opener) | |
setup_urllib2() | |
def find(pattern,s): | |
p = re.compile(pattern) | |
return p.search(s).group(1) | |
class Weibo(): | |
def _prelogin(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)' % username | |
resp = http_get(url) | |
json_text = find('\((.*)\)', resp) | |
d = json.loads(json_text) | |
servertime, nonce, pubkey, rsakv = str(d['servertime']), str(d['nonce']), str(d['pubkey']), str(d['rsakv']) | |
return servertime, nonce, pubkey, rsakv | |
def _get_pwd(self, pwd, servertime, nonce, pubkey): | |
rsaPublickey = int(pubkey, 16) | |
key = rsa.PublicKey(rsaPublickey, 65537) | |
message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd) | |
return binascii.b2a_hex(rsa.encrypt(message, key)) | |
def _get_user(self, username): | |
return base64.encodestring(urllib.quote(username)) | |
def login(self, username, pwd): | |
servertime, nonce, pubkey, rsakv = self._prelogin(username) | |
su = self._get_user(username) | |
sp = self._get_pwd(pwd, servertime, nonce, pubkey) | |
postdict = { | |
'entry': 'weibo', | |
'gateway': '1', | |
'from': '', | |
'savestate': '7', | |
'userticket': '1', | |
'ssosimplelogin': '1', | |
'vsnf': '1', | |
'vsnval': '', | |
'su': su, | |
'service': 'miniblog', | |
'servertime': servertime, | |
'nonce': nonce, | |
'pwencode': 'rsa2', | |
'sp': sp, | |
'rsakv' : rsakv, | |
'encoding': 'UTF-8', | |
'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack', | |
'returntype': 'META' | |
} | |
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'} | |
url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.4)' | |
resp = http_post(url, postdict, headers) | |
final_url = find('location\.replace\([\'|"](.*?)[\'|"]\)', resp) | |
return http_get(final_url) | |
if __name__ == '__main__': | |
weibo = Weibo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment