简体   繁体   中英

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)

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

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)

If it still doesn't work try with 'Referer' and 'User-Agent' in the headers

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