Reputation:
i'm creating a script that connect to wordpress and detect if the login is incorrect , i didn't prefer "if contain" methode so i tried to use this :
when you access this url in a wordpress site : http://www.example.com/blog/wp-admin/profile.php
if the combination is correct you access the page normaly , if not the url redricted to this :
http://www.example.com/blog/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fblog%2Fwp-admin%2Fprofile.php&reauth=1
so i tried this code for verifying the http response (300 , 301 ..) but it always gives 200 response ..
import urllib, urllib2, os, sys, requests , re
from time import sleep
from threading import Thread
url='http://www.example.com/blog/wp-login.php'
headers = [
("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5")]
data = [
("log","user"),
("pwd","password"),
("testcookie",1),
("submit","Log In"),
("redirect_to","http://www.example.com/blog/wp-login.php"),
("rememberme","forever")]
#this is the first methode :
req = urllib2.Request(url, urllib.urlencode(dict(data)), dict(headers))
response = urllib2.urlopen(req)
the_page=response.read()
print the_page
#this is the seconde methode :
get3 = requests.get('http://www.example.com/blog/wp-admin/profile.php')
print get3.text
any idea to detect the change of url from : http://www.example.com/blog/wp-admin/profile.php
to :
http://www.example.com/blog/wp-login.php?redirect_to=http%3A%2F%2Fwww.example.com%2Fblog%2Fwp-admin%2Fprofile.php&reauth=1
or anything help me to finish the script that will be so helpful thanks a lot . :)
Upvotes: 0
Views: 613
Reputation: 4493
There is a 3rd party module called Requests that is simpler than the built in python urllib stuff. Check here: http://docs.python-requests.org/en/latest/user/quickstart/#redirection-and-history
This below is from the link. Notice that the first url is requested with get, but the redirected url is available from r.url (one is http, the redirect is https)
>>> r = requests.get('http://github.com')
>>> r.url
'https://github.com/'
>>> r.status_code
200
>>> r.history
[<Response [301]>]
try this code with your url (obviously not 'example.com') and see what it produces
Upvotes: 3