Python请求模块以验证HTTP登录是否成功 [英] Python requests module to verify if HTTP login is successful or not

查看:64
本文介绍了Python请求模块以验证HTTP登录是否成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试实现类似的方法此处.

I've been trying to implement similar thing here and here.

基本上,我只想登录一个网站并获得HTTP/S身份验证是否成功的验证.

Basically I just want to login to a website and get a verification if the HTTP/S authentication has been successful or not.

不幸的是,以下尝试均无效.我不知道登录尝试是否成功,因为得到相同的< Response [200]> 并不重要,密码是否正确.

Unfortunately, none of the attempts below work. I have no idea whether login attempt was successful or not as I'm getting the same <Response [200]> doesn't matter if the password right or wrong.

我还检查了HTTP流量.如果登录失败,则会在登录页面上显示以下消息( http://localhost/test/login.php)

I've also inspected the HTTP traffic. If login failed, the following message will be displayed on the login page (http://localhost/test/login.php)

<div class="message">Login failed</div>

如果登录成功,页面将被重定向到 index.php http://localhost/test/index.php

If login successful, page will be redirected to index.php http://localhost/test/index.php

登录页面上的HTML表单(( http://localhost/test/login.php )

<form action="login.php" method="post">
    <fieldset>
            <label for="user">Username</label> <input type="text" class="loginInput" size="20" name="username"><br> 
            <label for="pass">Password</label> <input type="password" class="loginInput" autocomplete="off" size="20" name="password"><br>
            <p class="submit"><input type="submit" value="Login" name="Login"></p>
    </fieldset>
</form>

以下代码1-5将生成< Response [200]> ,而代码6将显示登录页面与用户名/密码错误无关.

The following Code 1 - 5 will produce <Response [200]> while Code 6 will display the login page doesn't matter if username/password is wrong.

代码1

import requests
url = 'http://localhost/test/login.php'
payload = {'username': 'admin', 'password': 'wrongpwd'}
requests.post(url, data=payload)

代码2

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
url = 'http://localhost/test/login.php'
payload = { 'username': 'admin', 'password': 'wrongpwd' }
requests.post(url, data=payload, verify=False)

代码3

import requests
headers = {'User-Agent': 'Mozilla/5.0'}
url     = 'http://localhost/test/login.php'
payload = {'username':'admin','pass':'wrongpwd'}
session = requests.Session()
resp    = session.get(url,headers=headers)
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
resp    = session.post(url,headers=headers,data=payload,cookies=cookies)
session.get(url)

代码4

import requests
headers = {'User-Agent': 'Mozilla/5.0'}
url     = 'http://localhost/test/login.php'
payload = {'username':'admin','password':'wrongpwd'}
session = requests.Session()
session.post(url,headers=headers,data=payload)

代码5

import requests
from requests.auth import HTTPBasicAuth
url = 'http://localhost/test/login.php'
requests.get(url, auth=HTTPBasicAuth('admin', 'wrongpwd'))

代码6 (仅显示登录页面)

import requests
url = 'http://localhost/test/login.php'
values = {'username': 'admin', 'password': 'wrongpwd'}
r = requests.post(url, data=values)
print(r.content)

如果登录成功或失败,我应该怎么做?

What should I do to get verification if the login is successful or not?

所需的输出

Login successful

Login failed

如果我的问题不清楚,或者您需要进一步的解释,请告诉我.

If my question was not clear, or if you need further explanation, please let me know.

推荐答案

您的响应为200,因为您的脚本成功向目的地创建了请求

Your response would be 200 because your script successful create request to your destination

如果您要检测登录是否成功,可以使用 .text 函数和 re.search 来解析所需的响应,以在登录成功后解析html响应文本.

If you want to detect your login is success or not you can parse html response text after login success using .text function and re.search to parse response you want

示例:

import requests
url = 'http://localhost/test/login.php'
values = {'username': 'admin', 'password': 'wrongpwd'}
r = requests.post(url, data=values)
print(r.text) #this will print html content

# now, search text in html to detect welcome page after login success. Or login page text to detect in case login failed, depends on your need
s = "welcome"
result = re.search(s, r.text)
if result.group(0) == s:
 print("Login successful")
else:
 print("Login failed")

这篇关于Python请求模块以验证HTTP登录是否成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆