Regular and POST on the same connection

please feel free, I am new to Python!

I am trying to login to a site that uses PHP. The form contains two hidden fields, the value of one and the name of the other are generated on page load.

My code below successfully hits the page and with regex manages to return values ​​- great!

The problem I am facing is that I then create my request to be used for POST (this contains the two values ​​obtained earlier) and reopens the url. This generates new markers / values ​​and my originals are useless.

Can someone shed some light on how I can connect to a site, use a regex to get the values, and then POST on the same connection.

Hopefully I am convinced if not let me know.

Thanks in advance for your help.

import urllib2,urllib,re,cookielib

url='http://www.example.com/index.php'

req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3 Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
link=response.read()
response.close()
token1=re.compile('<input type="hidden" name="return" value="(.+?)" />').findall(link)
token2=re.compile('<input type="hidden" name="(.+?)" value="1" />').findall(link)
print token1[0]
print token2[0]

username = 'username'
password = 'password'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'password' : password, 'return' : token1[0], token2[0] : '1', 'Submit' : 'Log in', 'option' : 'com_users', 'task' : 'user.login'})
opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')

      

FORM:

<form action="/index.php/welcome2" method="post" id="login-form" >
     <fieldset class="userdata">
          <p id="form-login-username">
               <label for="modlgn-username">User Name</label>
               <input id="modlgn-username" type="text" name="username" class="inputbox"  size="18" />
          </p>
          <p id="form-login-password">
               <label for="modlgn-passwd">Password</label>
               <input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18"  />
          </p>
          <p id="form-login-remember">
               <label for="modlgn-remember">Remember Me</label>
               <input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
         </p>
         <input type="submit" name="Submit" class="button" value="Log in" />
         <input type="hidden" name="option" value="com_users" />
         <input type="hidden" name="task" value="user.login" />
         <input type="hidden" name="return" value="aW5kZXgucGhwP0l0ZW1pZD0xMjc=" />
         <input type="hidden" name="c813c34837e4e48e8e3268c0a42912a2" value="1" />
    </fieldset>
<ul>
<li>
<a href="/index.php/my-account/my-details?view=reset">
Forgot your password?</a>
</li>
<li>
<a href="/index.php/my-account/my-details?view=remind">
Forgot your username?</a>
</li>
<li>
<a href="/index.php/register">
Create an account</a>
</li>
</ul>
</form>

      

+3


source to share


1 answer


When you write ...

opener.open('http://www.example.com/index.php', login_data)
resp = opener.open('http://www.example.com/index.php')

      

Why not just that?



resp = opener.open('http://www.example.com/index.php', login_data)

      

I have never used this Python library, but my first reaction is that it will give you the response text in one request, with which you can get a new token, right?

Form based update: It looks like your problem is that you are submitting login information index.php

instead index.php/welcome

.

0


source







All Articles