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.

Friday, April 10, 2009

My friend came back from HK

He came back in the Easter but will go to visit his ancestor's grave. He lived here before he graduated from university. Then he settled down in HK.

Automation on L-Note

Refer to this page: http://www-12.lotus.com/ldd/doc/lotusscript/lotusscript.nsf/ 1efb1287fc7c27388525642e0074f2b6/1f82ab14864680138525642e007687cb?OpenDocument

I want to create a JS script to list all the mails in the Inbox. As I can't use com object in Python on my workstation. But it failed to get value of "from" while VBS can do the job. I don't have enough time to investigate the root cause, just leave it here for later reference.
'VB script

Set objNotesSession = CreateObject("Lotus.NotesSession")
objNotesSession.Initialize(pwd)
Set objNotesMailFile = objNotesSession.GetDatabase(server, database file)
Set view = objNotesMailFile.GetView("($Inbox)")
Set doc = view.GetFirstDocument
While Not ( doc Is Nothing )
  WScript.Echo doc.GetItemValue( "From" )(0)
  Set doc = view.GetNextDocument( doc )
Wend
//JavaScript

var s = new ActiveXObject("Lotus.NotesSession");
s.Initialize(pwd);
var db = s.GetDatabase(server, database file);
var v = db.GetView("($Inbox)")
var doc = v.GetFirstDocument();
while (doc) {
  WScript.Echo( doc.LastAccessed );
  doc = v.GetNextDocument(doc);
}