2017-11-10 4 views
1

楽しいため、私は学校の学生ポータルにログオンするためにPythonリクエストを使用しようとしています。これはこれまで私が思いついたことです。 302(成功したログイン)ではなく、200ステータスコード(ログインに失敗したときに取得するコード)を取得しているため、ヘッダーを明示的に表示しようとしています。ネットワーク要求を調べるためにChromeを使用し要求のあるサイトにログインできません

import sys 
import os 
import requests 

def login(username, password): 
    url = '(link)/home.html#sign-in-content' 
    values = { 
     'translator_username' : '', 
     'translator_password' : '', 
     'translator_ldappassword' : '', 
     'returnUrl' : '', 
     'serviceName' : 'PS Parent Portal', 
     'serviceTicket' : '', 
     'pcasServerUrl' : '\/', 
     'credentialType' : 'User Id and Password Credential', 
     'account' : username, 
     'pw' : password, 
     'translatorpw' : password 
    } 

    headers = { 
     'accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 
     'accept-encoding' : 'gzip, deflate, br', 
     'accept-language' : 'en-US,en;q=0.9', 
     'cache-control' : 'max-age=0', 
     'connection' : 'keep-alive', 
     'content-type' : 'application/x-www-form-urlencoded', 
     'host' : '(link)', 
     'origin' : '(link)', 
     'referer' : '(link)guardian/home.html', 
     'upgrade-insecure-requests' : '1' 
    } 

    with requests.Session() as s: 
     p = s.post(url, data=values) 
     if p.status_code == 302: 
      print(p.text) 
     print('Authentication error', p.status_code) 

     r = s.get('(link)guardian/home.html') 
     print(r.text) 

def main(): 
    login('myname', 'mypass') 

if __name__ == '__main__': 
    main() 

、これらのヘッダのすべてが長いクッキーの数、コンテンツの長さ、およびユーザエージェントに加えて、「リクエストヘッダ」の下にあります。次のように

形式は以下のとおりです。

pstoken:(token) 
contextData:(text) 
translator_username: 
translator_password: 
translator_ldappassword: 
returnUrl:(url)guardian/home.html 
serviceName:PS Parent Portal 
serviceTicket: 
pcasServerUrl:\/ 
credentialType:User Id and Password Credential 
account:f 
pw:(id) 
translatorpw: 

私は、ヘッダー/フォーム名と何かが足りないのですか?クッキーには問題がありますか?

私はp.requests.headersを見れば、これは送信されているものです。

{'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.89 Safari/537.36', 'accept-encoding': 'gzip, deflate, br', 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'connection': 'keep-alive', 'accept-language': 'en-US,en;q=0.9', 'cache-control': 'max-age=0', 'content-type': 'application/x-www-form-urlencoded', 'host': '(url)', 'origin': '(url)', 'referer': '(url)guardian/home.html', 'upgrade-insecure-requests': '1', 'Content-Length': '263'} 

p.textは私にPowerAPI、リクエスト、Mechanizeのでテストログインページ

のHTMLを与え、ロボブラウザー。すべて失敗します。

+0

を私達にあなたの要求とその応答の例を与えます。 – Sraw

+0

@Sraw私が修正した記事の一番下を参照してください。 –

+0

これはPowerSchoolのログインフォームに役立つ場合 –

答えて

0

あなたはどんな反応を期待していますか?あなたはあなたの応答を分析するために間違った方法を使用しています。あなたのコードで

with requests.Session() as s: 
    p = s.post(url, data=values) 
    if p.status_code == 302: 
     print(p.text) 
    print('Authentication error', p.status_code) 

    r = s.get('(link)guardian/home.html') 
    print(r.text) 

、あなたはAuthentication errorstatus_codeを無視してプリントアウトし、私はそれが、少なくとも次のようにすべきだと思う:

with requests.Session() as s: 
    p = s.post(url, data=values) 
    if p.status_code == 302: 
     print(p.text) 
     r = s.get('(link)guardian/home.html') 
     print(r.text) 
    else: 
     print('Authentication error', p.status_code) 
+0

それでも、私はまったく同じ応答を得て、コードは200です。 –

+0

@Chase本当にあなたのヘッダーを設定しましたか? 's.post(url、headers = headers、data = values)' – Sraw

+0

はい、私はそれを設定していたので、違いはありませんでした。私は現在Robobrowserを試しています。情報が送信されても​​コードにエラーは発生しませんが、認証を過ぎてページを開くようになり、HTMLをフェッチしてもまだログインしていることがわかります。 –

関連する問題