Tuesday, April 14, 2009

Update Google App Engine with Jython

Here is the background/software: JRE 1.5.0_15, Jython 2.5b3, AppEngine 1.2.0 and a authentication proxy for connecting to Internet.

Due to the issue of the HTTPS proxy handler in Python library urllib2, I can't use Jython to udpate my app to AppEngine. After searhed the web I came up to this solution - use Java class to get html from HTTPS, but still use urllib2 for HTTP proxy.

1. Edit the file google-appengine\google\appengine\tools\appengine_rpc.py, add the line in the header.
import ProxyHTTP

Then in the line 178, update as
      response_body = ProxyHTTP.getHTTPS(req.get_full_url() + '?' + req.get_data())
      #response = self.opener.open(req)
      #response_body = response.read()

And line 359, update as
    #opener.add_handler(urllib2.ProxyHandler())
    opener.add_handler(ProxyHTTP.HTTPProxyHandler())
2. Create the file google-appengine\google\appengine\tools\ProxyHTTP.py
from java.net import Authenticator
from java.net import PasswordAuthentication
from java.lang import System
from java.net import URL
from urllib2 import ProxyHandler

proxy_info = {
'user' : 'domain\\user',
'pass' : 'password',
'host' : 'host',
'port' : 'port'
}

class auth(Authenticator):
    def getPasswordAuthentication(self):
        return PasswordAuthentication(proxy_info['user'], proxy_info['pass'])

def getHTTPS(url):
    systemSettings = System.getProperties()
    systemSettings.put("https.proxyHost", proxy_info['host'])
    systemSettings.put("https.proxyPort", proxy_info['port'])
    Authenticator.setDefault(auth())
 
    u = URL(url)
    d = u.openConnection().getInputStream()
    c = d.read()
    r = []
    while c <> -1:
        r.append(chr(c))
        c = d.read()

    return ''.join(r)

def HTTPProxyHandler():
    return ProxyHandler({'http' : \
        'http://%(user)s:%(pass)s@%(host)s:%(port)s' % proxy_info})


def main():
    urlpath = r'https://www.gmail.com'
    print getHTTPS(urlpath)

if __name__ == '__main__':
    main()


That's all. But there are still some issues when using Jython for AppEngine. Such as:
  • Before upload files, there is a warning: "Problem with getpass. Passwords may be echoed".
  • When run the app locally, there is no PIL(I did not install it as it might have many problems).
  • When run the app locally with google framework, there is a ImportError: No module named operator.

No comments:

Post a Comment