这是困扰我一段时间的事情。我想编写一个程序,以便我可以自动登录到PowerSchool门户,将来,该门户可能使我能够执行诸如分析日程表和成绩之类的事情。第一步是认证,这已成为我的问题。
import sys
import os
import requests
import lxml
import json
from bs4 import BeautifulSoup
def login(username, password):
with requests.Session() as s:
url = 'https://sisavrsb.ednet.ns.ca/guardian/home.html#sign-in-content'
r = s.get(url)
soup = BeautifulSoup(r.text, "lxml")
token = soup.select_one("[name='pstoken']")['value']
contextdata = soup.select_one("[name='contextData']")['value']
headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'keep-alive',
#'Content-Length': '423',
#'Content-Type': 'application/x-www-form-urlencoded',
#'Cookie': 'JSESSIONID=0B1666C446234245CECC2983F1D6CA8A; PowerSchool_Cookie_K=2069644430.1.329063952.2221457792',
'DNT': '1',
#'Host': 'sisavrsb.ednet.ns.ca',
'Referer': 'https://sisavrsb.ednet.ns.ca/public/',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0'
}
print(contextdata)
data = json.dumps({
'account': username,
#'contextData': '30A7205567365DDB643E707E25B32D43578D70A04D9F407113CF640632082056',
'contextData' : contextdata,
'credentialType': 'User Id and Password Credential',
#'dbpw': '61a2474517a2f79ae0da0781b9bdf57d',
#'dbpw' : password,
'pcasServerUrl': '\/',
'pstoken': token,
'pw': password,
'returnUrl': '',
'serviceName': 'PS Parent Portal',
'serviceTicket': '',
'translator_ldappassword': '',
'translator_password': '',
'translator_username': '',
'translatorpw': ''
})
p = s.post(url, headers=headers, data=data, allow_redirects=True)
soup = BeautifulSoup(p.text, "lxml")
if p.status_code == 302:
print('Success!')
else:
print('Authentication error', p.status_code)
print('cookies', requests.utils.dict_from_cookiejar(s.cookies))
print(p.history)
print(p.headers)
def main():
login('xxxxx', 'xxxxx')
if __name__ == '__main__':
main()
在这一点上,我已经尝试了几乎所有东西,从机械化到(过时的)PowerSchool API。我已尽力使用
requests.Session()
复制标头和数据,以使cookie正常工作。经过数小时的摆弄之后,我终于明白了,以便p.history()不为空。现在它包含“ <Response [302]>
”,这对我来说是非常模糊的,但总比没有好。这是我的输出
Authentication error 200
cookies {'JSESSIONID': 'B847F853CC373DC7EAA8800FA02EEC00', 'PowerSchool_Cookie_K': '2069644430.1.329063608.2225303936'}
[<Response [302]>]
{'Server': 'Apache-Coyote/1.1', 'Cache-control': 'no-store, no-cache, must-revalidate, post-check=0, check=0', 'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT', 'Content-Type': 'text/html;charset=UTF-8', 'Content-Length': '8238', 'Date': 'Thu, 08 Feb 2018 01:01:05 GMT'}
我离开了网站链接,以便您可以测试POST请求并查看标题等。我没有解决该问题的想法,但我真的很想让它工作。显然302在历史记录中,这对于POST代码来说是一个好兆头,但我仍然无法通过登录。如果我再执行一次
requests.get()
并打印输出,它将再次成为登录页面。机械化(引发500个内部服务器错误):
import mechanize
import cookielib
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
# Debugging
br.set_debug_http(True)
br.set_debug_redirects(True)
br.set_debug_responses(True)
br.set_handle_refresh(False)
# Fake User-Agent header
br.addheaders = [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36')]
br.open('https://sisavrsb.ednet.ns.ca/public/home.html')
#
br.select_form(name='LoginForm')
br.form['account'] = 'xxxxx'
br.form['pw'] = 'xxxxxx'
br.method = 'POST'
response = br.submit()
print response.read()
编辑:RoboBrowser也给了我500答复。想知道这是因为我缺少了什么,还是仅仅是他们的问题。
最佳答案
不知道这是否适合您,但最近我做了类似的事情。
所有逻辑都在/admin/javascript/md5.js
文件中完成(该文件似乎是他们修改并添加了自己的功能的库)。
这是我在自己的脚本中使用的python
from bs4 import BeautifulSoup
import requests, base64, hashlib, hmac
POWERSCHOOL_BASE_URL = "https://powerschool.eips.ca/"
def initLoginPage(httpSession: requests.Session) -> [str, str]:
response = httpSession.get(POWERSCHOOL_BASE_URL + "public/home.html")
html_response = BeautifulSoup(response.content, "lxml")
contextData = html_response.find('input', id='contextData').attrs['value']
pstoken = html_response.find('input', attrs={'name': 'pstoken'}).attrs['value']
return contextData, pstoken
def getPassword(contextData: str, password: str) -> str:
return hmac.new(contextData.encode('UTF-8'), msg=base64.b64encode(hashlib.md5(password.encode('UTF-8')).digest()).strip(b'='), digestmod=hashlib.md5).hexdigest()
def login(httpSession: requests.Session, username: str, pw: str, pstoken: str) -> requests.Response:
post_data = {
'account': username,
'pw': pw,
'pstoken': pstoken,
}
return httpSession.post(POWERSCHOOL_BASE_URL + "guardian/home.html", data=post_data)
旁注:整个PowerSchool系统确实非常混乱且不安全(以防仅用密码散列单行代码的怪兽还不够)。