Archive for the ‘Programming’ Category
Authenticating in GAE
Having successfully created a desktop application in IronPython, I wanted to go a step further – integrate desktop and web environment. Data can be captured in desktop and passed on to web; and analysis can be done on the web.
To start with it, I need to authenticate the incoming request. Hmm…when I started reading about authentication I read so many jargons that scared me – openid, openauth, authsub, basicauth. It took me a while to understand the basics of each (still I don’t claim that I’ve understood them; probably whatever I’ve understood is wrong too)
To some extent, I understood ‘Basic Authentication’ and this is how I went about implementing it.
First of all, I am going with Google Account Authentication, assuming all the users will have a Google account; getting one is not a big deal.
IronPython Desktop Application will pass userid, password to AppEngine Application, which in turn will validate with Google Accounts Authentication service. If authenticated, AppEngine application will proceed; else it will pass the error back to the desktop application.
Having discussed the concepts, here are the code snippets. I’m sharing with a hope that it will be useful to someone; or if this is wrong, someone will raise an alarm; or if there is a better way to do it, someone will comment.
AppEngine
Here the expectation is that the incoming request will have a header (‘HTTP_AUTHORIZATION’) with userid and password in Base64 format.
import urllib
import base64
from google.appengine.api import urlfetch
from django.http import HttpResponse
def authenticate(request):
response_vals={}
resp = HttpResponse()
try:
(method, encoded) = request.META['HTTP_AUTHORIZATION'].split()
response_vals['method'] = method
response_vals['encoded'] = encoded
if method.lower() == 'basic':
(username, password) = base64.b64decode(encoded).split(':')
request_body = urllib.urlencode({'Email': username,
'Passwd': password,
'accountType': 'HOSTED_OR_GOOGLE',
'service': 'ah',
'source': 'test'})
auth_response = urlfetch.fetch('https://www.google.com/accounts/ClientLogin',
method=urlfetch.POST,
headers={'Content-type':'application/x-www-form-urlencoded',
'Content-Length':
str(len(request_body))},
payload=request_body)
resp.status_code = auth_response.status_code
except:
resp.status_code = 401
return resp
return resp
Desktop – Python
Encode the user id, password and call the particular URL (/auth?). And handle the response.
import urllib2
import base64
url = 'http://localhost:8000/api/auth/'
username = 'mysecs@gmail.com'
password = 'mysecspwd'
USER_AGENT = 'mySecsWndClient'
request = urllib2.Request(url)
base64string = base64.encodestring('%s:%s' % (username,password))
authheader = "Basic %s" % base64string
request.add_header('USER_AGENT', USER_AGENT)
request.add_header('AUTHORIZATION', authheader)
try:
response = urllib2.urlopen(request).read()
print response
except urllib2.HTTPError, e:
print e.code
print e.read()
except urllib2.URLError, e:
if hasattr(e, 'reason'):
print e.reason
elif hasattr(e,'code'):
print e.code
Desktop – IronPython
Since I’ve written mySecs in IronPython, I’ve to translate the above into IronPython. As ipy is a relatively new entrant, there is not much of documentation on how to do it. Here is what I’ve done.
from System.IO import StreamReader
from System.Net import HttpWebRequest, NetworkCredential, WebException
url = 'http://localhost:8000/api/auth/'
username = 'mysecs@gmail.com'
password = 'mysecspwd'
USER_AGENT = 'mySecsWndClient'
req = HttpWebRequest.Create(url)
req.UserAgent = USER_AGENT
auth_string = '%s:%s' % (username,password)
authheader = "Basic %s" % System.Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(auth_string))
req.Headers.Add('AUTHORIZATION', authheader)
try:
rsp = req.GetResponse()
response = StreamReader(rsp.GetResponseStream()).ReadToEnd()
print response
rsp.Close()
except (Exception, WebException), e:
print 'Error: %s' % e
Applies to: GAE 1.1.1; Django 0.96 with helper; IronPython 1.1
Reference:
AuthSub Authentication for Web Applications
Python for mobile programming?
To tell you the truth, I like the Python language. A lot.
It is simple to learn; easy to read and plenty of tools are available that makes the developer very productive. Converting an idea into implementation is faster than many other languages that I learnt over the years.
Not only excellent GUI and Web frameworks are available in Python; but the language itself is getting ported into other VMs too – like .NET and Java VM.
I choose Django for web development and IronPython for Windows desktop development. So far they have been excellent choices.
Continuing on the love of Python, I looked for a Python framework/tool for mobile development. I was (disappointed and) surprised that there are not many options. In fact only Nokia has released a mobile SDK in Python. I don’t have a Nokia and so I can’t use the application that I develop. That won’t motivate me much. Would it?
As I continued my search, I came across Jython. I was ‘wow’ing since I assumed that I can now start writing mobile (J2ME) applications in Python. Installing Jython is easy. However it is currently used only for desktop and server programming; not for mobile programing.
I spent almost a week trying to write a J2ME program in Jython. Porting a simple ‘HelloWorld’ to Jython is easy. Issue starts immediately after that – preverifying the application and deploying into the device is either not possible or it is just overly complicated.
So after a week of R&D and banging my head against the wall, I’m giving up on mobile development in Python. May be I’ll visit Jython (or any other Python related language) sometime in the future and may be then there will be lot more players than just Noika.
IronPython Learnings
Recently I started developing Windows Desktop Applications in IronPython – a Python language port for Windows.NET. I blogged about the application already here.
In the post, I’m sharing what I learnt in the process with a hope that it will be helpful to someone else.
Visual Studio as ipy development environment
You can download IronPython and start to develop using your favorite text editor. However having a Visual Studio environment helps. Fundamentally it involves:
- Download and install ironpython
- Download and install Visual Studio SDK
- Run IronPython Integration Sample from ‘C:\Program Files\Visual Studio 2005 SDK\2007.02\VisualStudioIntegration\Samples\IronPythonIntegration’
Designing Windows Forms
With Visual Studio Integration, designing of Windows Forms is a simple drag and drop operation. Of course code generated by VS may not be as elegant as some Python purists like; but it works well.
Calling unmanaged code from IronPython
If you are into IronPython, you’ll definitely come across VoidSpace, who has a huge collection of IronPython articles.
I needed to query the active window for my application. VoidSpace discusses a solution in his pages. Better to name the namespace as a ‘UnmanagedCode’ as VoidSpace discusses in the solution. I named it as the name of the application and that created a lot of trouble and was difficult to troubleshoot.
Logging in IronPython
IPY doesn’t include standard python libraries (at least as of 1.1). So I used a NSpring .NET logging library.
(I found out that any .NET library can be used with IronPython. I did same with SQLite Library and SourceGrid, a grid library for .NET)
Finding time difference
One can find the time difference between two time variable (=System.DateTime.Now) using
timeSpent = currentEndTime.Subtract(currentStartTime)
If you want to know the differences in seconds then use
timeSpentinSec = timeSpent.TotalSeconds.ToString()
timeSpent.Seconds() will provide only the seconds part of the timespan. TotalSeconds() provide the time difference in seconds.
Don’t forget to stop timer
I used Timer() to poll activewindow on a regular interval. However, I forgot to stop that assuming that once the form is closed all instances will be cleared. Apparently not and so the application stayed in memory even after exit. Lesson learnt: stop all threads before exiting the application.
A time tracking tool
While I was waiting for Django 1.0 to be released, I wanted to quickly learn to develop desktop applications. I preferred it to be on Python so that I can continue to learn the language. I did an evaluation of different options and settled on IronPython.
As a consultant, there is always a need to submit timesheets. By the end of the day/week, it is difficult to remember the tasks done during that time-span (day/week). If somehow a tool can capture applications that were worked on, then it will help.
So I developed an application for the same.
This application polls the active window at an interval of 5 seconds and captures the application details (application name and the application title) and the time spent on each of these tasks.
It can be minimized into systray so that it can continue to capture details without any disturbance (first icon).
One can download the details on the grid as a csv file too.
Currently thats all in there. As I continue to use (and probably get some feedback) I will continue to enhance it.
The project is hosted in Google. One can download the executable and the source.
S-I-P goes online
In an earlier post, I mentioned about developing a photo-blog in Django. I’m happy to announce that over the last week, I hosted in WebFaction and it is online now.
I realized that hosting Django applications are not that easy – WordPress installation is easy – transfer files; setup some parameters and in about 5 minutes you are up and ready to go. That is the case even for a newbie. However it took me about a week to get this first installation up and running. I was even close to giving up out of frustration.
Good news is that I didn’t give up.
Surely WebFaction, has made Django hosting easier. They have a single-click install of Django – be it with mod_python or with mod_wsgi. You can even choose the trunk version or the official release. Without these it would’ve been a nightmare for newbie like me to host Django applications.
My learning on Django hosting will be another post. This one is about the photo-blog.
As the images are hosted in Flickr, first step is to add Flickr API Key.
Then you start to add photos by providing the Flickr Photo id:
I’m using Beej’s Flickr Python API to connect to Flickr and get information. With the photo id, it will get the EXIF information, photo title, description and tags and store it locally. Once the information is obtained, if needed, it can be changed as well. If the information has to be obtained from Flickr again, just delete the title and it will fire the query again.
That’s all to be done to add a photo!
Coming to the homepage, I’ve designed a very simple homepage. (It shows up messed up in IE; it is ok in Firefox). Most of the part works – browse via tags, comments, about page etc. In the days to come, I want to incorporate the below features:
- Tags as a drop down in browse page
- Email notification of comments
- Comments moderation
- Caching
- Contact form
At this moment, I’m not interested in voting or favorites. But might consider these as add-on features at a later time.
As always the code is hosted in Google hosting. Feel free to browse, comment, code review.