使用请求登录 Wordpress - Python3 [英] Login Wordpress with requests - Python3
问题描述
import requests
with requests.Session() as s:
headers1 = {'Cookie':'wordpress_test_cookie=WP Cookie check'}
datas={'log':'admin','pwd':'admin','wp-submit':'Log In','redirect_to':'/wordpress/wp-admin/','testcookie':'1'}
s.post("http://ip/wordpress/wp-admin",headers=headers1,data=datas)
re = s.get("http://ip/wordpress/wp-admin").text
print (re)
使用此代码,我应该能够登录我的 wordpress,但无法正常工作.使用网络代理我发现,当单击登录按钮时,我的浏览器会向网络服务器发送一个会话 cookie.使用 Python,我不知道如何完成该任务,我的假设是:我需要找到一种在发送 post 请求(登录表单)时发送 cookie 的方法.
With this code I should be able to login my wordpress, but doesn't work. Using a web proxy I found that when clicking the login button, my browser sends a session cookie to the webserver. With Python, I don't know how to do that task and my hypothesis is: I need to find a way to send a cookie when sending the post request (login form).
推荐答案
你的代码没问题,但是你应该把post数据提交到/wp-login.php
,而不是/wp-admin/
Your code is ok, but you should submit the post data to /wp-login.php
, not /wp-admin/
wp_login = 'http://ip/wordpress/wp-login.php'
wp_admin = 'http://ip/wordpress/wp-admin/'
username = 'admin'
password = 'admin'
with requests.Session() as s:
headers1 = { 'Cookie':'wordpress_test_cookie=WP Cookie check' }
datas={
'log':username, 'pwd':password, 'wp-submit':'Log In',
'redirect_to':wp_admin, 'testcookie':'1'
}
s.post(wp_login, headers=headers1, data=datas)
resp = s.get(wp_admin)
print(resp.text)
如果它仍然不起作用,请尝试在标题中使用Referer"和User-Agent"
If it still doesn't work try with 'Referer' and 'User-Agent' in the headers
这篇关于使用请求登录 Wordpress - Python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!