urllib post and redirect = fail

LoD MoD lodmod.dod at gmail.com
Thu Dec 10 20:30:08 EST 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()
f.close()
print data

Here is the form source:
    <form id="loginData" name="loginData" method="post"
action="/console/j_security_check">
      <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>
      <div class="input-row">
        <label for="j_username">
        Username:</label>
        <span class="ctrl">
          <input class="textinput" type="text" name="j_username"
id="j_username">
        </span>

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

      <div class="button-row">
        <span class="ctrl">
          <input class="formButton" type="submit"
            onclick="form.submit();this.disabled=true;document.body.style.cursor
= 'wait'; this.className='formButton-disabled';"
            value='Log In'>
        </span>
        <input type="hidden" name="j_character_encoding" value="UTF-8">
      </div>
    </form>

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;
path=/
X-Powered-By: Servlet/2.5 JSP/2.1



More information about the Python-list mailing list