How would one log into a phpBB3 forum through a Python script using urllib, urllib2 and ClientCookie?

(ClientCookie is a module for (automatic) cookie-handling:

# I encode the data I'll be sending:
data = urllib.urlencode({'username': 'mandark', 'password': 'deedee'})

# And I send it and read the page:
page = ClientCookie.urlopen('', data)
output =

The script doesn't log in, but rather seems to get redirected back to the same login page asking it for a username and password. What am I doing wrong?

Any help would be greatly appreciated! Thanks!

Asked by: John694 | Posted: 28-01-2022

Answer 1

Have you tried fetching the login page first?

I would suggest using Tamper Data to have a peek at exactly what's being sent when you request the login page and then log in normally using a web browser from a fresh start, with no initial cookies in place, so that your script can replicate it exactly.

That's the approach I used when writing the following, extracted from a script which needs to login to an Invision Power Board forum, using cookielib and urllib2 - you may find it useful as a reference.

import cookielib
import logging
import sys
import urllib
import urllib2

cookies = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies))
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv: Gecko/20080201 Firefox/',
    'Accept': 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
    'Accept-Language': 'en-gb,en;q=0.5',
    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',

# Fetch the login page to set initial cookies
urllib2.urlopen(urllib2.Request('', None, headers))

# Login so we can access the Off Topic forum
login_headers = headers.copy()
    'Referer': '',
    'Content-Type': 'application/x-www-form-urlencoded',
html = urllib2.urlopen(urllib2.Request('',
                                           'referer': '',
                                           'UserName': RLLMUK_USERNAME,
                                           'PassWord': RLLMUK_PASSWORD,
if 'The following errors were found' in html:
    logging.error('RLLMUK login failed')

Answered by: Max423 | Posted: 01-03-2022

Answer 2

I'd recommend taking a look at the mechanize library; it's designed for precisely this type of task. It's also far easier than doing it by hand.

Answered by: Fiona826 | Posted: 01-03-2022

Similar questions

django - python: Can't use urlopen!! from urllib, urllib2, clientcookie urlopen ssl error

I made some code for access particular site instead user. It's very similar to auto login program. My program receive userid and password from user and try access url with data and login, return login result. Here's code. from urllib import urlencode from urllib2 import Request from ClientCookie import urlopen, install_opener, build_opener httpheaders = {'User-Agent' : 'Mozilla/4.0 (compatible; MSI...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python