I use Requests (2.2.1) to login a url http://tx3.netease.com/logging.php?action=login
, but the login logic of this url is different from Django's csrf token mechanism, that is:
- When you GET this url, there is two import values
formhash
andsts
in html text, both of which will be used in a js functiondo_encrypt
(in filehttp://tx3.netease.com/forumdata/cache/rsa/rsa_min.js
). This is fine, I can easily grab them via re.
The key part of html text is:
<form method="post" name="login" id="loginform" class="s_clear" onsubmit="do_encrypt('ori_password','password');pwdclear = 1;" action="logging.php?action=login&loginsubmit=yes">
<input type="hidden" name="formhash" value="91e54489" />
<input type="hidden" name="referer" value="http://tx3.netease.com/" />
<input type="hidden" name="sts" id="sts" value="1409414053" />
<input type="hidden" name="password" id="password" />
...
<input type="password" id="ori_password" name="ori_password" onfocus="clearpwd()" onkeypress="detectCapsLock(event, this)" size="36" class="txt" tabindex="1" autocomplete="off" />
...
</form>
2. After entering email and original password ori_password
, clicking submit button will call do_encrypt
, which will use formhash
, sts
and ori_password
to set the real password password
for the post dict. Problem comes out -- There seems no way to get password
string directly. (For contrast, you can directly get csrfmiddlewaretoken
from session_client.cookies['csrftoken']
in Django case)
This is the code:
import requests
import json
import re
loginUrl = "http://tx3.netease.com/logging.php?action=login"
client = requests.session()
r = client.get(loginUrl)
r.encoding='gb18030'
stsPat = re.compile('<input type="hidden" name="sts" id="sts" value="(\d+?)" />')
formhashPat = re.compile('<input type="hidden" name="formhash" value="([\d\w]+?)" />')
sts = stsPat.search(r.text).groups()[0]
formhash = formhashPat.search(r.text).groups()[0]
loginData={
'username' : "smaller9@163.com",
'password' : ..., # Set by js function do_encrypt
'referer':'/',
'loginfield':'username',
'ori_password':'', # it's `111111`, but `do_encrypt` will set it to empty.
'loginsubmit':'true',
'sts':sts,
'formhash':formhash,
}
# r = client.post(url=loginUrl,data=loginData)