问题
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
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/>
回答1:
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.
来源:https://stackoverflow.com/questions/11834895/login-to-a-phpbb-forum-cookie-changed