2017-08-18 36 views
0

我想向設備的Web界面發送POST消息,以便我可以使用cookie來捲曲設備上的文件(設備的API非常有限)。Python - 發送帖子請求登錄表單

問題:我的POST請求似乎不工作,因爲代碼返回登錄源頁面內容,並且沒有cookie。我認爲我使用的表單數據不正確,但不確定有什麼問題?

我想我會看到狀態碼302如果POST請求工作?我有下面鏈接的Chrome開發工具的POST請求。

任何幫助將不勝感激。該設備的登錄Web界面的

Python代碼

import requests 
import urllib3 

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) 
headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 
     'Content-Type':'application/x-www-form-urlencoded', 
     'Origin' : 'https://172.110.35.61', 
     'Referer': 'https://172.110.35.61/', 
     'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',} 

login_url = 'https://172.110.35.61/' 
payload = {'_z': '0', 'page':'login', 'username': 'TestAccount', 'passwd':'stack!!', 'submit':' Log In'} 

my_request = requests.post(login_url, data=payload, verify=False) 
print my_request.text 
print my_request.cookies.values() 

源代碼

<div id="login"> 
    <form action="/" name="login" method="post"> 
    <input type=hidden name="_z" value="0"> 
    <input type="hidden" name="page" value="login"> 
    <table> 
    <tr> 
    <td align="right"> 
     Username: 
    </td> 
    <td> 
     <input class="username" type="text" name="username"maxlength="18" size="18" style="width: 15em;" value=""> 
    </td> 
    </tr> 
    <tr> 
    <td align="right"> 
     Password: 
    </td> 
    <td> 
     <input class="passwd" type="password" name="passwd" maxlength="18" size="18" style="width: 15em;" value=""> 
    </td> 
    </tr> 

    <tr><td colspan="2"></td></tr> 

    <tr> 
    <td> 
     <input class="button" type="submit" name="submit" value=" Log In "> 
    </td> 
    </tr> 
    </table> 
    </form> 

    </div> 
    <script type=text/javascript> 
      document.login.username.focus(); 
     document.cookie="noscript=0;"; 
    </script> 

所以我看到了從動作:<form action="/" name="login" method="post">

這裏是POST請求時我使用表格數據登錄: POST Request

+0

php如何與此相關 – Akintunde007

+0

如果您查看POST請求,您會看到它有一個PHPSESSID作爲cookie。 – SergioRyan

+0

所以我能夠通過在我的帖子中添加此標誌(allow_redirects = False)來查看302重定向。這使我認爲我正確地使用表單進行身份驗證,但我仍然沒有看到任何cookie? 我需要做一個get(url),以便我可以得到一個cookie並將該cookie傳遞給我的文章?我沒有看到從我的帖子返回的cookie,所以我不認爲它返回,這是否意味着我必須提供一個? – SergioRyan

回答

0

我設法解決了這個問題,如果我在郵件頭中傳遞了32個字符的PHPSESSID,我能夠對該探針進行身份驗證。我還使用requests.session來成功驗證身份後能夠調整數據。

php_session = 'PHPSESSID={}; noscript=0'.format('1'*32)