登录到phpBB论坛。 Cookie已更改 [英] Login to a phpBB forum. Cookie changed
问题描述
我正在尝试创建一个在后台运行的小脚本,并检查是否有论坛(使用phpBB3)接收到新消息。由于许多子论坛都需要登录用户的权限才能查看,因此我创建了一个脚本,该脚本将向服务器发送后请求以首先登录。
I am trying to create a small script that is to run in the background and check for incomming new messages on a forum (with phpBB3). As many of the subforums need permissions from a logged in user to be viewed I created a script that would send a post-request to the server to login first.
该脚本当前已成功登录,并收到欢迎消息,告诉我我已登录。但是,当我访问新页面时,该脚本似乎已经忘记了
The script currently gets a successfull login, and is greeted with a welcome message telling me I am logged in. But when I access a new page the script seems to have forgotten that I logged in and my session-cookie is changed.
这是我创建的一些小测试代码,该错误提示我:
Here is some small test-code I have created that gives me the error:
import urllib, urllib2, cookielib, re, time
username = "username"
password = "password"
loginsite = "http://www.mydomain.com/ucp.php?mode=login"
ok_tekst = "You have been successfully logged in."
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password,
"autologin" : "on", 'login' : 'Login'})
resp = opener.open(loginsite, login_data)
if ok_tekst in resp.read():
print "Login successfull"
print "Cookies are:"
for cookie in cj:
print cookie
post_site = "http://www.mydomain.com/"
ok_tekst = "Logout [ " + username + " ]"
resp = opener.open(post_site)
readHTML = resp.read()
if ok_tekst not in readHTML:
print ""
print "Unsuccessfull, I am now logged out?!"
print "Cookies are now:"
for cookie in cj:
print cookie
这将产生以下输出,还显示不同cookie的值:
This produces the following output that also shows the value of the different cookies:
>> python test.py
Login successfull
Cookies are:
<Cookie phpbb3_pwsle_k=b3fe944b89bfbce2 for .mydomain.com/>
<Cookie phpbb3_pwsle_sid=318b3ace3d52409fb5e1eef87433fbdb for .mydomain.com/>
<Cookie phpbb3_pwsle_u=419 for .mydomain.com/>
Unsuccessfull, I am now logged out?!
Cookies are now:
<Cookie phpbb3_pwsle_k= for .mydomain.com/>
<Cookie phpbb3_pwsle_sid=719520cd3a16526d6da4fa2fbdfe40f4 for .mydomain.com/>
<Cookie phpbb3_pwsle_u=1 for .mydomain.com/>
推荐答案
Okey,所以我现在解决了自己的问题-好像我正在运行的论坛上启用了某些浏览器保护功能,如果我使用USER-AGENT(位于http-header中)设置为Python / URLLIB进行访问,则该会话将取消所有会话。通过更改标头以假装我是google-chrome修复了我的问题。
Okey, so I solved the problem my self now - and it seemed the forum I was running had some brower-protection enabled that would cancel any sessions if I visited with a USER-AGENT (in http-header) set to Python/URLLIB. By changing the header to pretend I was google-chrome fixed my problem.
这篇关于登录到phpBB论坛。 Cookie已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!