The starting point of a general beginner project is the automation of the landing function, and the technical difficulty of the project is different, poptest in the test development training more attention to technical difficulties, mastering the technology to achieve ideas, improve the hands-on ability. This is the case with Sina Weibo, a system that everyone can access.
Simulation login function is relatively simple, many people in the learning of automation will be landing as their first study case, a bit like the development of the language of the first code is helloworld! The same. Login simple analog login, direct POST request is OK, it is easy to implement. Here with Sina Weibo, is the technology to achieve complex, involving a lot of details. The difficulty is that when you log in to Sina Weibo, client JS encrypts the sent request, such as the username and password in the login.
The first step: analyzing the content of the Protocol
can be analyzed using the Protocol tool, where we can use the Firefox browser to track, enter the user name, will be pre-login, the URL is:/http login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinassocontroller.prelogincallback&su= Zw5nbgfuzhnldsu0mde2my5jb20%3d&rsakt=mod&checkpin=1&client=ssologin.js (v1.4.18) &_= 1443156845536, via response (Sinassocontroller.prelogincallback ({"Retcode": 0, "servertime": 1443156842, "pcid": " gz-e88b75a929252baec7c12c741985eaa45627 "," nonce ":" 2l4iz3 "," PubKey ":" Eb2a38568661887fa180bddb5cabd5f21c7bfd59c090cb2d245a87ac253062882729293e5506350508e7f9aa3bb77f4333231490f915f6d63c55fe2f0 8a49b353f444ad3993cacc02db784abbb8e42a9b1bbfffb38be18d78e87a0e41b9b8f73a928ee0ccee1f6739884b9777e4fe9e88a1bbe495927ac4a79 9b3181d6442443 "," rsakv ":" 1330428213 "," Showpin ": 0," Exectime ": 16}), here the analysis will find four variables, servertime, nonce, PubKey and rsakv.
Sina Weibo user name encryption is currently using BASE64 encryption algorithm, and Sina Weibo password encryption algorithm using RSA2, which is the focus of the simulation landing, you need to create an RSA public key, two parameters of the public key Sina Weibo gave a fixed value, the first parameter is the first step in the login PubKey, The second parameter is the ' 10001 ' in the JS encrypted file. These two values need to be converted from 16 to 10, convert 10001 to decimal 65537, and then add Servertime and nonce to encrypt again.
Step Two: Mock request
Request URL: http://login.sina.com.cn/sso/login.php?client=ssologin.js (v1.4.18), the form data is as follows:
Main Submission Data:
SU:BASE64 User name after encryption
SERVERTIME/NONCE/RSAKV before the pre-landing to get the
SP is the password for the encrypted transfer
Username after BASE64 calculation: username = base64.encodestring (urllib.quote (username)) [:-1];
Password has been encrypted three times, and the value of Servertime and Nonce has been added to interfere with the SHA1.
That is: two times SHA1 encryption, the result is added servertime and nonce value, and then SHA1 to calculate once.
Step three: Handle request response
After submission: Organize the parameters and post the request. There was no sign-in success after this. , because it's not a microblog profile, but a redirect code:
The value of Retcode after successful login is 0. Then request this URL so that you can log in to Weibo successfully.
Remember to build the cache in advance
Note: Technical implementation:
Cookielib: Used to store cookies.
URLLIB2: Send request for Web page data, cooperate with Cookielib, use cookie to access.
JSON: Working with Data interchange formats
Code:
#-*-Coding:utf-8-*-
########################
#author: Andrewseu
#date: 2015/9/23
#login Weibo
########################
Import Sys
Import Urllib
Import Urllib2
Import Cookielib
Import Base64
Import re
Import JSON
Import RSA
Import Binascii
#import requests
#from BS4 Import BeautifulSoup
#新浪微博的模拟登陆
Class Weibologin:
def enablecookies (self):
#获取一个保存cookies的对象
CJ = Cookielib. Cookiejar ()
#将一个保存cookies对象和一个HTTP的cookie的处理器绑定
Cookie_support = Urllib2. Httpcookieprocessor (CJ)
#创建一个opener, set a URL for handler to handle HTTP open
Opener = Urllib2.build_opener (Cookie_support, Urllib2. HttpHandler)
#安装opener, the installed opener object is used after Urlopen () is called
Urllib2.install_opener (opener)
#预登陆获得 Servertime, Nonce, PubKey, RSAKV
def getserverdata (self):
url = ' http://login.sina.com.cn/sso/prelogin.php? entry=weibo&callback=sinassocontroller.prelogincallback&su=zw5nbgfuzhnldsu0mde2my5jb20%3d&rsakt= Mod&checkpin=1&client=ssologin.js (v1.4.18) &_=1442991685270 '
data = 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, RSAKV
Except
print ' Get severtime error! '
Return None
#获取加密的密码
def GETP, assword (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 passwd
#获取加密的用户名
def getusername (self, username):
Username_ = Urllib.quote (username)
Username = base64.encodestring (username_) [:-1]
return username
#获取需要提交的表单数据
def getformdata (SELF,USERNAME,PASSWORD,SERVERTIME,NONCE,PUBKEY,RSAKV):
UserName = Self.getusername (userName)
PSW = Self.getpassword (Password,servertime,nonce,pubkey)
Form_data = {
' Entry ': ' Weibo ',
' Gateway ': ' 1 ',
' From ': ',
' SaveState ': ' 7 ',
' Useticket ': ' 1 ',
' Pagerefer ': ' Http://weibo.com/p/1005052679342531/home?from=page_100505&mod=TAB&pids=plc_main ',
' VSNF ': ' 1 ',
' su ': userName,
' Service ': ' Miniblog ',
' Servertime ': Servertime,
' Nonce ': nonce,
' Pwencode ': ' RSA2 ',
' RSAKV ': rsakv,
' SP ':p SW,
' SR ': ' 1366*768 ',
' Encoding ': ' UTF-8 ',
' Prelt ': ' 115 ',
' URL ': ' Http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack ',
' ReturnType ': ' META '
}
FormData = Urllib.urlencode (form_data)
Return FormData
#登陆函数
def login (SELF,USERNAME,PSW):
Self.enablecookies ()
url = ' http://login.sina.com.cn/sso/login.php?client=ssologin.js (v1.4.18) '
SERVERTIME,NONCE,PUBKEY,RSAKV = Self.getserverdata ()
FormData = Self.getformdata (USERNAME,PSW,SERVERTIME,NONCE,PUBKEY,RSAKV)
headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) gecko/20100101 firefox/41.0 '}
req = Urllib2. Request (
url = URL,
data = FormData,
headers = Headers
)
result = Urllib2.urlopen (req)
Text = Result.read ()
Print text
#还没完!!! There is a relocation URL, which is included in the script to get the real login
p = re.compile (' location\.replace[\ ' "] (. ∗?) [\‘"]‘)
Try
Login_url = P.search (text). Group (1)
Print Login_url
#由于之前的绑定, the cookie information is written directly
Urllib2.urlopen (Login_url)
Print "Login success!"
Except
print ' Login error! '
return 0
#访问主页, write the home page to a file
url = ' http://weibo.com/u/2679342531/home?topnav=1&wvr=6 '
Request = Urllib2. Request (URL)
Response = Urllib2.urlopen (Request)
Text = Response.read ()
Fp_raw = open ("e://weibo.html", "w+")
Fp_raw.write (text)
Fp_raw.close ()
#print text
Wblogin = Weibologin ()
print ' Sina Weibo demo login: '
Username = raw_input (U ' username: ')
Password = raw_input (u ' password: ')
Wblogin.login (Username,password)
Test development Python Training: Analog login Sina Weibo-technical article