在正确登录后,python无法获得wordpress Dashboard

时间:2014-08-11 14:33:30

标签: python wordpress login

我想创建一个脚本,在我使用我的组合登录后返回wordpress Dashboard,它看起来像这样:(截图)

desired appearance - dashboard

所以我创建了这个脚本

import urllib, urllib2, os, sys, requests , re
site = 'http://example.com/blog/'
username = 'username'
password = 'password'
url = site + '/wp-login.php'
req = urllib2.Request(url)
headers = {
"User-Agent" : "Mozilla/5.0 (Windows; Windows NT 6.1; WOW64; rv:2.0b2) Gecko/20100720 Firefox/4.0b2",
"Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language" : "es-es,es;q=0.8,en-us;q=0.5,en;q=0.3",
"Accept-Charset" : "ISO-8859-1,utf-8;q=0.7,*;q=0.7"}
data = [
("log",username), 
("pwd",password), 
("testcookie",1), 
("submit","Log In"), 
("redirect_to",'http://example.com/blog/wp-admin/'), 
("rememberme","forever")]

req = urllib2.Request(url, urllib.urlencode(dict(data)), dict(headers))
response = urllib2.urlopen(req)
the_page=response.read() 
with open('result.html', 'w+') as file:
     file.write(the_page)
response.close()
print 'done'

而不是获取我的wordpress仪表板,我收到登录页面:(截图)

login page

我测试了错误组合的脚本,它返回登录页面,错误如下:(截图)

login page with error

任何想法如何取代仪表板?非常感谢任何帮助 ps:dict(response.headers)正确登录是:

{'x-powered-by': 'PHP/5.3.28', 'transfer-encoding': 'chunked', 'set-cookie': 'wordpress_test_cookie=
WP+Cookie+check; path=/blog/, wordpress_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013
 15:27:13 GMT; path=/blog/wp-admin, wordpress_sec_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 1
1-Aug-2013 15:27:13 GMT; path=/blog/wp-admin, wordpress_38b05fc993d7b123a9db72281bf1c40e=+; expires=
Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/wp-content/plugins, wordpress_sec_38b05fc993d7b123a9db7228
1bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/wp-content/plugins, wordpress_logged_i
n_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, wordpress_
logged_in_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, wo
rdpress_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, word
press_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, wordpr
ess_sec_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, word
press_sec_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/, wo
rdpressuser_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/,
wordpresspass_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blog/
, wordpressuser_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/blo
g/, wordpresspass_38b05fc993d7b123a9db72281bf1c40e=+; expires=Sun, 11-Aug-2013 15:27:13 GMT; path=/b
log/', 'expires': 'Wed, 11 Jan 1984 05:00:00 GMT', 'server': 'Apache', 'last-modified': 'Mon, 11 Aug
 2014 15:27:13 GMT', 'connection': 'close', 'pragma': 'no-cache', 'cache-control': 'no-cache, must-r
evalidate, max-age=0', 'date': 'Mon, 11 Aug 2014 15:27:13 GMT', 'x-frame-options': 'SAMEORIGIN', 'co
ntent-type': 'text/html; charset=UTF-8'}

1 个答案:

答案 0 :(得分:1)

我玩了一段时间,我找到了适合我的东西。

import urllib, urllib2, os, sys, re, cookielib

class MyCookiePolicy(cookielib.DefaultCookiePolicy):
    def set_ok(self, cookie, request):
    return True

class RedirHandler(urllib2.HTTPRedirectHandler):
    def redirect_request(self, oldreq, fp, code, msg, hdrs, newurl):
    global req
    #print (req,fp,code,msg,hdrs,newurl)
    jar.extract_cookies(fp,oldreq)
    req=urllib2.Request(newurl, None, hdrs)
    jar.add_cookie_header(req)
    return req

jar=cookielib.MozillaCookieJar("kukis.txt",True)#,MyCookiePolicy)
#jar.load()

direct=urllib2.build_opener(RedirHandler)

site = 'https://example.com'
username = 'username'
password = 'password'
url = site + '/login'
req = urllib2.Request(url)
jar.add_cookie_header(req)
response = direct.open(req)
jar.extract_cookies(response,req)
headers = {
"User-Agent" : "Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0",
"Host" : "example.com",
"Referer" : url,
"Connection" : "keep-alive",
"Cache-Control" : "max-age=0",
"Accept-Encoding" : "gzip, deflate",
"Accept" : "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language" : "pl,en-us;q=0.7,en;q=0.3",
"Accept-Charset" : "utf-8,ISO-8859-2;q=0.8,*;q=0.7"}
data = [
("login",username),
("passwd",password),
]

req = urllib2.Request(url, urllib.urlencode(dict(data)), dict(headers))
jar.add_cookie_header(req)
print ('Sent headers are: {')
for it in req.header_items():
    print ('%20r: %r'%it)
print ('}')
response = direct.open(req)
jar.extract_cookies(response,req)
print ('url is %s'%response.geturl())
print ('Received headers are: {')
for it in response.info().items():
   print ('%20r: %r'%it)
print ('}')
jar.save()
fn='result.html'
with open(fn, 'w+b') as fp:
    buff=response.read(1024)
    while buff!='':
    fp.write(buff)
    buff=response.read(1024)
response.close()
print ('done')