Book Reviews


[Day Permalink] Thursday, May 13, 2004

[Item Permalink] Using Python for scripting web services -- Comment()
Today I wrote a short Python script for accessing our intranet calendar for the appointments and vacations of my group. This was rather easy to do, using the Python packages urrlib, string, re and codecs. Even https (SSL-encrypted connections) worked with the first try. Here is the Python code I used for this:
import urllib
class myopener(urllib.FancyURLopener):
    def prompt_user_passwd(self,host,realm):
        username = 'USERNAME'
        keyname = '"SERVER (' + username + ')"'
        sshpasskey = '/Applications/Utilities/SSHPassKey.app/' +
            'Contents/MacOS/SSHPassKey'
        f = popen(sshpasskey + ' ' + keyname)
        passwd = f.read()
        f.close()
        return (username,passwd)

sess = myopener()

address = "https://server/path" f = sess.open(address) l = f.read() sess.close()

After this, the contents of the web page are in the string l.

For output, I used the codecs module for translating the ISO-Latin-1 character set to Mac-Roman. Here is that part of the script:

from codecs import lookup
c1 = lookup('latin1')[1]
c2 = lookup('macroman')[0]
def lat2mac(text):
    (l,t) = c1(text)
    (l,t) = c2(l)
    return l
This could perhaps be simpified and improved?

I added the Python script to the Mail scrips forder on Mac OS X. Thus, now I can generate a new mail which contains the future appointments and vacations of my group just by pressing Ctrl-z.