gpt4 book ai didi

正确登录后python无法获取wordpress Dashboard

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:40 24 4
gpt4 key购买 nike

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

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

最佳答案

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

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 Dashboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com