简体   繁体   中英

Python - Send Post Request to login form

I am trying to send a POST message to a device's web interface so I can use the cookie to curl files on the device (The device's API is very limited).

Issue: My POST request does not seem to be working because the code returns the login source page content and there is no cookie. I think the form data I am using is incorrect but not sure what is wrong?

I think I would see the Status Code 302 if the POST request worked? I have the POST request from Chrome Dev Tools linked below.

Any help would be greatly appreciated.

Python Code

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()

Source code of the device's login web interface

    <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>

So I see the from action: <form action="/" name="login" method="post">

Here is the POST request when I login with the form data: POST Request

I managed to solve this problem, I was able to authenticate to the probe if I passed a PHPSESSID with a 32 chars in the post header. I also used requests.session to be able to curl the data once I successfully authenticated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM