title: Mechanize and Python not handling cookies properly
body: <p>I have a Python script using mechanize browser which logs into a self hosted Wordpress blog, navigates to a different page after the automatic redirect to the dashboard to automate several builtin functions.</p>
  
  <p>This script actually works 100% on most of my blogs but goes into a permanent loop with one of them.</p>
  
  <p>The difference is that the only one which fails has a plugin called Wassup running.  This plugin sets a session cookie for all visitors and this is what I think is causing the issue.</p>
  
  <p>When the script goes to the new page the Wordpress code doesn't get the proper cookie set, decides that the browser isn't logged in and redirects to the login page.  The script logs in again and attempts the same function and round we go again.</p>
  
  <p>I tried using Twill which does login correctly and handles the cookies correctly but Twill, by default, outputs everything to the command line.  This is not the behaviour I want as I am doing page manipulation at this point and I need access to the raw html.</p>
  
  <p>This is the setup code</p>
  
  <pre><code># Browser
  self.br = mechanize.Browser()
  
  # Cookie Jar 
  policy = mechanize.DefaultCookiePolicy(rfc2965=True) 
  cj = mechanize.LWPCookieJar(policy=policy) 
  self.br.set_cookiejar(cj)
  </code></pre>
  
  <p>After successful login I call this function</p>
  
  <pre><code>def open(self):
      if 'http://' in str(self.burl):
          site = str(self.burl) + '/wp-admin/plugin-install.php' 
          self.burl = self.burl[7:]
      else:
          site = "http://" + str(self.burl) + '/wp-admin/plugin-install.php' 
      try:
          r = self.br.open(site, timeout=1000)
          html = r.read()
          return html
      except HTTPError, e:
          return str(e.code)
  </code></pre>
  
  <p>I'm thinking that I will need to save the cookies to a file and then shuffle the order so the Wordpress session cookie gets returned before the Wassup one.</p>
  
  <p>Any other suggestions?</p>