Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4327018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:28:46+00:00 2026-05-21T09:28:46+00:00

I have used mechanize and deployed an app on GAE and it works fine.

  • 0

I have used mechanize and deployed an app on GAE and it works fine. But, for an app that I am making, I am trying to automate login to gmail through mechanize. It doesn’t work in the development environment on local machine as well as after deploying on appengine.

I have been able to use the same script to run it on my server through mod_python using PSP.

I found a lot of solutions here, but none of them seem to work for me. Here is a snippet of my code:

<snip>
br = mechanize.Browser()
response = br.open("http://www.gmail.com")
loginForm = br.forms().next()
loginForm["Email"] = self.request.get('user')
loginForm["Passwd"] = self.request.get('password')
response = br.open(loginForm.click())
response2 = br.open("http://mail.google.com/mail/h/")
result = response2.read()
<snip>

When I look at the result, all I get is the login page when used with appengine. But with mod_python hosted on my own server, I get the page with the user’s inbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:28:46+00:00Added an answer on May 21, 2026 at 9:28 am

    The problem is most likely due to how Google crippled the urllib2 module on GAE.

    Internally it now uses the urlfetch module (which is something that Google wrote) and they have completely removed the HTTPCookieProcessor() functionality – meaning, cookies are NOT persisted from request to request which is the critical piece when automatically logging into sites programmatically.

    There is a way around this, but not using mechanize. You have to roll your own Cookie processor – here is the basic approach I took (not perfect, but it gets the job done):

    import urllib, urllib2, Cookie
    from google.appengine.api import urlfetch
    from urlparse import urljoin
    import logging
    
    class GAEOpener(object):
        def __init__(self):
            self.cookie = Cookie.SimpleCookie()
            self.last_response = None
    
        def open(self, url, data = None):
            base_url = url
            if data is None:
                method = urlfetch.GET
            else:
                method = urlfetch.POST
            while url is not None:
                self.last_response = urlfetch.fetch(url = url,
                    payload = data,
                    method = method,
                    headers = self._get_headers(self.cookie),
                    allow_truncated = False,
                    follow_redirects = False,
                    deadline = 10
                    )
                data = None # Next request will be a get, so no need to send the data again. 
                method = urlfetch.GET
                self.cookie.load(self.last_response.headers.get('set-cookie', '')) # Load the cookies from the response
                url = urljoin(base_url, self.last_response.headers.get('location'))
                if url == base_url:
                    url = None
            return self.last_response
    
        def _get_headers(self, cookie):
            headers = {
                'Host' : '<ENTER HOST NAME HERE>',
                'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)',
                'Cookie' : self._make_cookie_header(cookie)
                 }
            return headers
    
        def _make_cookie_header(self, cookie):
            cookie_header = ""
            for value in cookie.values():
                cookie_header += "%s=%s; " % (value.key, value.value)
            return cookie_header
    
        def get_cookie_header(self):
            return self._make_cookie_header(self.cookie)
    

    You can use it like you would urllib2.urlopen, except the method you would use is just “open”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used a plugin that uses prototype js, it's working fine but the
We have used Shibboleth to authenticate users. It works great. The issue is that
I have used auto complete textbox previously. That auto complete works only when i
I have used mechanize in Python with great success. However, I am trying to
I have used the silverlight control in CRM 2011.It also published on form but
I have used NSSets many times in my apps, but I have never created
I have used geocoder in my ruby on rails application for a while but
I have some tasks on a site in php using nginx that I'm trying
I have been trying to write a script that may help me to comment
I have never used multiple inheritance but while reading about it recently I started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.