urllib post and redirect = fail

LoD MoD lodmod.dod at gmail.com
Fri Dec 11 02:30:08 CET 2009

I'm trying to do a post to log into a simple admin console. All I get
is a simple you need to log in error. I already mapped the form id's
to make sure they are correct, but it still seems to deny.
I know mechanize would probably do a better job of this than urllib,
but their docs for python aren't available online right now. Any help
is appreciated.

import urllib, urllib2

# build opener with HTTPCookieProcessor
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener( o )

# assuming the site expects 'user' and 'pass' as query params
#p = urllib.urlencode( { 'j_character_encoding' : 'UTF-8',
'j_username': 'cooduser', 'j_password': 'password' } )
p = urllib.urlencode(dict(j_username='cooduser', j_password='password'))
# perform login with params
f = o.open( 'http://myserver:5000/login/LoginForm.jsp',  p )
data = f.read()
print data

Here is the form source:
    <form id="loginData" name="loginData" method="post"
      <div class="message-row">

          <p class="loginFailed"><img src="/console/images/message_error.gif"
          alt='Authentication Denied '>
          Authentication Denied</p>

          <p>The username or password has been refused by Server.
Please try again.</p>

      <div class="input-row">
        <label for="j_username">
        <span class="ctrl">
          <input class="textinput" type="text" name="j_username"

      <div class="input-row">
        <label for="j_password">
        <span class="ctrl">
          <input class="textinput" type="password" name="j_password"

      <div class="button-row">
        <span class="ctrl">
          <input class="formButton" type="submit"
= 'wait'; this.className='formButton-disabled';"
            value='Log In'>
        <input type="hidden" name="j_character_encoding" value="UTF-8">

The response code is 200.

Here is the header info from f.info() :
Cache-Control: no-cache
Connection: close
Date: Fri, 11 Dec 2009 01:29:40 GMT
Pragma: no-cache
Content-Length: 3534
Content-Type: text/html; charset=UTF-8
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: NCONSOLESESSION=QwcDLhgG5NrxJGBQJfpv1dynvpddjT4LJTHnGhYz9ZDl6R9Pzqvh!2016686010;
X-Powered-By: Servlet/2.5 JSP/2.1

More information about the Python-list mailing list