【发布时间】:2020-05-05 06:20:05
【问题描述】:
我正在尝试使用 Python 请求登录网站。不幸的是,它在打印其内容时总是显示此错误。
b'<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>
供参考我的代码
from requests import Session
import requests
INDEX_URL = 'https://phpzag.com/demo/ajax_login_script_with_php_jquery/index.php'
URL = 'https://phpzag.com/demo/ajax_login_script_with_php_jquery/welcome.php'
LOGIN_URL = 'https://phpzag.com/demo/ajax_login_script_with_php_jquery/login.php' # Or whatever the login request url is
payload = {'user_email': 'test@phpzag.com','password':'test'}
s = requests.Session()
user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'}
t=s.post(LOGIN_URL, data=payload, headers=user_agent)
r=s.get('https://phpzag.com/demo/ajax_login_script_with_php_jquery/welcome.php',headers=user_agent,cookies=t.cookies.get_dict())
print(r.content)
我可以知道缺少什么以及如何从中获取欢迎页面的 HTML 代码
更新
我正在尝试在登录身份验证后进行 API 调用。但是,我无法成功登录身份验证。因此我无法获得 API 调用的响应。根据我的想法,由于多因素身份验证,它失败了。我需要知道如何实现它?
例如:www.abc.com 是网站的 URL。登录是通过 JS 表单提交完成的,因此在 ajax 部分中指定了 URL。成功后,还有另一个第三方身份验证方(okta)也将验证凭据并最终到达主页。那么我需要为我的任务调用真正的 API。
但它不起作用。
import requests
import sys
class Login:
def sendRequestWithAuthentication(self,loginDetails,requestDetails):
user_agent = {'User-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'}
action_url=loginDetails['action_url'] if 'action_url' in loginDetails.keys() else None
pay_load=loginDetails['payload'] if 'payload' in loginDetails.keys() else None
session_requests = requests.session()
if action_url and pay_load:
act_resp=session_requests.post(action_url, data=pay_load, headers=user_agent,verify=False,files=[ ])
print(act_resp)
auth_cookies=act_resp.cookies.get_dict()
url,method,request_payload = requestDetails['url'],requestDetails['method'],requestDetails['payload']
querystring=requestDetails['querystring']
response=session_requests.get(url,headers=user_agent,cookies=auth_cookies,data=request_payload,params=querystring)
print(response)
return response.json()
在上述动作中,URL 是在 ajax 部分中给出的 API,而在第二个请求中,URL 是该 GET 的 API 地址。
简而言之,我可以知道如何在 python 请求中实现多因素身份验证
我的疑惑
- 我们是否需要将登录表单页面中的 cookie 包含在登录请求中
- 如何在 python 请求中实现多因素身份验证(这里我们不需要任何 pin 或通过 RSA 完成的东西。)是否需要用于登录的证书,因为它现在无法验证 SSL 证书李>
给出一个实现这种场景的虚拟示例 api
【问题讨论】: