正确登录后python无法获取wordpress仪表板 [英] python failed to get wordpress Dashboard after a correct login

查看:66
本文介绍了正确登录后python无法获取wordpress仪表板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个脚本,用我的组合登录后返回wordpress仪表板,它看起来像这样:(截屏)

i want to create a script that return the wordpress Dashboard after i login in with my combination it looks like this : (screenshot)

所以我创建了这个脚本

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仪表板,而是收到登录页面:(截图)

and instead of getting my wordpress dashboard i receive the login page : (screenshot)

并且我以错误的组合测试了脚本,它返回了登录页面,并显示了这样的错误:(截图)

and i tested the script with bad combination and it returns the login page with error like this : (screenshot)

您知道如何获取仪表板吗?非常感谢您的帮助 ps:正确登录的dict(response.headers)是:

any idea how to get the dashboard instead ? and thanks a lot for any help ps : dict(response.headers) for correct login is :

{'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'}

推荐答案

我玩了一段时间,然后发现了一些对我有用的东西.

I played with it some time, and I figured out something that works for me.

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

这篇关于正确登录后python无法获取wordpress仪表板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆