How to handle sessions in Google app engine

While developing a GAE app engine recently, I came across the need to handle session variables. While session was a thing that I use to take for granted in ASP.NET (Session[“xyz”]) or php ($_SESSION[‘xyz’]), storing and fetching session variables isn’t something that straightforward with webapp2, the web framework widely used for building GAE apps with python.

However, I found a pretty easy way to implement sessions in my existing app (which was almost half-way done!) by creating a simple BaseHandler class that is session-powered, and then deriving all my url handlers with this new class instead of the usual webapp2.RequestHandler. Once you do that, you can straightaway use the python dictionary self.session[‘my_variable’] (that pretty much resembles $_SESSION[‘my_var’] in php). Here is the implementation for BaseHandler:

class BaseHandler(webapp2.RequestHandler):
    def dispatch(self):
        # Get a session store for this request.
        self.session_store = sessions.get_store(request=self.request)
        try:
            # Dispatch the request.
            webapp2.RequestHandler.dispatch(self)
        finally:
            # Save all sessions.
            self.session_store.save_sessions(self.response)
    @webapp2.cached_property
    def session(self):
        # Returns a session using the default cookie key.
        sess = self.session_store.get_session()
        #add some default values:
        if not sess.get(‘theme’):
            sess[‘theme’]=’cosmo’#’slate’
        return sess

Here, you may initialize any particular session variables if you want and set default values for them (as I’ve done for the cosmo theme above).

Hopefully, this should save you some development time if you want to implement sessions in existing app engine code. As an example, here is how I make use of a session variable to pass on a user’s preferred theme to the template engine, so that the webpage can be displayed accordingly:

class MainPage(BaseHandler):
    def get(self):
        template = JINJA_ENVIRONMENT.get_template(‘stereo.html’)
        if self.request.get(‘theme’):
            theme=self.request.get(‘theme’)
            self.session[‘theme’]=theme
        else:
            theme=self.session[‘theme’]

A simple pythonic script to log internet bandwidth usage on Linux

We often want to log internet bandwidth on our laptops or PCs in order to track our usage over a period of time. This also comes handy to cross-check our usage with that of our ISP’s invoice. Now how to do it on linux? While the Windows folk have all kinds of utilities for this kind of thing, we lunix folk have to invent it ourselves.

Some time ago, I wrote this small python script that writes your bandwidth stats to a log file each time you disconnect your internet connection. This, I’ve tested on ubuntu 12.04 and debian wheezy:

Source file: Tata.py

#!/usr/bin/python
# author: Prahlad Yeri
import subprocess, os, datetime, sys
def execute(command):
    try:
        p=subprocess.Popen(command, shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        p.wait()
        result=p.communicate()
        #print str(result)
        if len(result[0])>0:
            return result[0].strip()
        else:
            return result[1].strip()
    except Exception as e:
        print ‘error occured:’ + errorstring
        return errorstring
#main
mypath = ‘/etc/NetworkManager/dispatcher.d/’
iface = sys.argv[1].strip()
status = sys.argv[2].strip()
log=open(mypath + ‘tata.log’,’a’)
#log.write(iface + ‘:’ + status + ‘\n’)
#log.close()
for part in sys.argv:
        log.write(‘argv:’ + part + ‘\n’)
if (iface == ‘ppp0’ or iface == ‘ttyUSB0’) and status == ‘down’:
        s = execute(‘iptables -L -v’)
        rules = s.splitlines()
        rx,tx = 0,0
        rxdrop, txdrop = 0,0
        inrule = False
        outrule = False
        for rule in rules:
                log.write(‘rule:’ + rule.strip() + ‘\n’)
                words = rule.strip().split()
                if rule.strip().startswith(“Chain OUTPUT”):
                        log.write(‘switch to outrule’)
                        outrule = True
                        inrule = False
                elif rule.strip().startswith(“Chain INPUT”):
                        log.write(‘switch to inrule’)
                        inrule = True
                        outrule = False
                elif rule.strip().startswith(“Chain FORWARD”):
                        log.write(‘switch to no rule’)
                        inrule=False
                        outrule=False
                elif len(words) >=3:
                        if words[6].strip() == “ppp0”:
                                #log.write(‘bytes=’ + words[1]
                                if inrule:
                                        if words[2].strip() == ‘DROP’:
                                                rxdrop += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                        else:
                                                rx += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                elif outrule:
                                        if words[2].strip() == ‘DROP’:
                                                txdrop += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
                                        else:
                                                tx += int(words[1].replace(‘K’,’000′).replace(‘M’,’000000′))
        #print ‘rx=’ + str(rx) + ‘ tx=’ + str(tx)
        #print ‘rxdrop=’ + str(rxdrop) + ‘ txdrop=’ + str(txdrop)
        execute(“iptables –zero”)
        header=”
        lines=[]
        if not os.path.exists(mypath + ‘usage.csv’):
                #create header
                header = ‘date,rx,tx,rx-dropped,tx-dropped,rx-tot,tx-tot\n’
        f = open(mypath + ‘usage.csv’,’a’)
        if len(header) > 0:
                lines.append(header)
        lines.append(str(datetime.datetime.now()).split(‘.’)[0] + ‘,’ + str(rx) + ‘,’ + str(tx) + ‘,’ + str(rxdrop) + ‘,’ + str(txdrop) + ‘,’ + str(rx+rxdrop) + ‘,’ + str(tx+txdrop) + ‘\n’)
        #rx = execute(‘cat /sys/class/net/ppp0/statistics/rx_bytes’)
        #tx = execute(‘cat /sys/class/net/ppp0/statistics/tx_bytes’)
        #strdate=str(datetime.datetime.now())
        #line = strdate + ‘,’ + rx + ‘,’ + tx + ‘\n’
        f.writelines(lines)
        f.close()
        log.close()
        sys.exit(0)

In the above script, replace ppp0 with the interface-name for the name of interface through which you access internet. For mobile broadbands it is typically ppp0, but you can issue the “ifconfig” command to list them all and make sure.

You have to place this script at /etc/NetworkManager/dispatcher.d on debian/ubuntu systems so that it runs each time you stop using the modem. In case of other systems, or in case you don’t use NetworkManager, refer to your distro documentation to check the folder/script which is executed when an interface is down.

A Python finds out which is the best performing Linux distro!

python

With more and more linux distributions competing among themselves for the userspace, we are currently living in the best of times as far as choice in open-source software is concerned. However, more the number of choices, more is the head-scratching to decide what to choose amongst them. Like many, I am also one of the victims of “distro-hopping”. No sooner a major distribution declares a new version, I’m itching to try one out in my virtual box or as a LiveCD version. Few days ago, this benchmarking idea occurred to me.

I’m a software developer and my requirements were clear. I wanted a distro that is good at performance. Since my desktop is cluttered with “heavy-duty” programs like Eclipse-ADT, Java, IDLE, etc., performance was my primary concern. I mostly write application software and android apps, so things like disk I/O, memory and CPU utilization matter.

With these things in my mind, I was seeking a simple way to do a quick benchmark of various distros to find out what suits my needs. Thats when this idea came to me – Almost all distros come preloaded with a general-purpose scripting language called python which could be summoned for the job. I took “time taken in milliseconds” to do 5 basic tasks to decide on the performance. These tasks were:

  1. Zip-test: A test to compress a large file to a zip file, and extract in back in python code.
  2. Random-test: Generate 5 million random numbers and round them to zero decimals.
  3. I/O test: Write a unicode string ‘spam and eggs’ 5 million times to a text file, and read them back.
  4. CSV test: Generate a CSV spreadsheet with 5 million rows in it, and read them back.
  5. Bandwidth test: Download the 800K akonadi RPM file from IIT Kanpur’s website.

(The last one was just for the sake of completeness. Internet bandwidth depends more on other parameters such as your ISP limits, time of the day, latency, etc. rather than the disto you are using).

The contestants in the arena were:

  1. openSUSE 12.2
  2. Debian 7 (GNOME version)
  3. Fedora 18
  4. Ubuntu 12.04 LTS
  5. Xubuntu 12.04

My previous experience with Ubuntu made be biased towards it, but the tests showed me how wrong I was. Also, from what I’d read in most blogs on the Internet, KDE was one of the most bloated distros ever. However, in my tests KDE on openSUSE topped the race in most parameters, though the credit should go to openSUSE for optimizing the KDE. The second spot saw Fedora and Debian fighting amongst them, while Ubuntu (that I was presently using) fared the worst! Here are the detailed results:

Test zt1

(compress)

zt2

(archive)

random io csv bandwidth
debian1 7622 2753 9142 5732 12784 118978
debian2 7724 2752 9161 5287 12112 92386
fedora18_1 8605 4168 6287 5972 12750 0
fedora18_2 7762 4164 6419 6160 12818 0
openSUSE1 9001 2313 5915 5904 12715 115999
openSUSE2 7253 2245 6035 5935 11492 71257
precise1 9649 5012 8807 6846 13552 78560
precise2 10555 4434 8890 7229 13455 48286
xubuntu1 13305 4827 8954 5839 14676 58538
xubuntu2 10826 4760 8934 7908 12802 52768

*Above figures represent time-taken in milliseconds

The bandwidth test could not be performed on fedora as the Live CD did not have the wget package installed, but I did not need that to get a general idea of things.

My Conclusion: openSUSE wins the race, though Debian-7 and Fedora-18 are also fine distros for performance.

Here is the entire python benchmarking script that you may customize as per your requirement (change initializing variables in the script and run by issuing “python benchmark.py” from the shell):

 

import sys,time,os
import zipfile,random,csv
import subprocess
from zipfile import ZipFile,ZIP_DEFLATED
from random import random

##Variables
#This should be initially present before running the program:
test_name=’debian1′
file_to_extract = “VirtualBox.exe” #”dotnetfx35.exe”
##

def timer(test):
t1=time.time()#.clock()
elapsed=0
#
if test == “ziptest1”:
ziptest1()
elif test == “ziptest2”:
ziptest2()
elif test == “randomtest”:
randomtest()
elif test==”iotest”:
iotest()
elif test==”csvtest”:
csvtest()
elif test==”bwtest”:
bwtest()

elapsed=time.time()-t1
timetaken=round(elapsed*1000,0)
print test,timetaken,”msecs.”
return timetaken

def ziptest1():
myzip=zipfile.ZipFile(‘t.zip’,’w’,ZIP_DEFLATED) #ZIP_STORED
myzip.write(file_to_extract)
myzip.close()
return

def ziptest2():
os.remove(file_to_extract)
myzip=zipfile.ZipFile(‘t.zip’,’r’)
myzip.extractall()
myzip.close()
return

def randomtest():
for i in range(1,5000000):
r = round(random()*100,0);
#print r
return

def iotest():
file=open(‘myfile1k.txt’,’w’)
for i in range(1,5000000):
file.write(u’spam and eggs’)
file.close()
#
file=open(‘myfile1k.txt’,’r’)
s=file.readline()
while (s!=”):
#print s
s=file.readline()
file.close()
return

def csvtest():
file=open(‘myfile.csv’,’w+b’)
#
writer= csv.writer(file,delimiter=’,’,quotechar=’|’)#,quoting=csv.QUOTE_MINIMAL)
for i in range(1,5000000):
writer.writerow([‘spam’,’eggs’,’spam1′])
#writer.close()
file=open(‘myfile.csv’,’r+b’)
reader=csv.reader(file,delimiter=’,’,quotechar=’|’)
for row in reader:
s= ‘,’.join(row)
#reader.close()
#
file.close()
return

def bwtest():
subprocess.call([“wget”,”http://mirror.cse.iitk.ac.in/fedora/releases/18/Fedora/i386/os/Packages/a/akonadi-1.8.1-1.fc18.i686.rpm”,”-O akonadi.rpm”])
#subprocess.call([“wget”,”http://www.google.com”,”-O”,”akonadi.rpm”])
return

if __name__ == “__main__”:
#print time.strftime(“%d-%m-%y %H:%M”, time.gmtime())
#print time.strftime(“%d-%m-%y %H:%M”, time.localtime())
zt1=timer(“ziptest1”)
zt2=timer(“ziptest2”)
random=timer(“randomtest”)
io=timer(“iotest”)
cst=timer(“csvtest”)
bw=timer(“bwtest”)

#Store results to csv file.
file=open(‘results.csv’,’ab’)
writer= csv.writer(file,delimiter=’,’,quotechar=’|’)
#writer.writerow([‘test’,’zt1′,’zt2′,’random’,’io’,’cst’,’bw’])
writer.writerow([test_name,zt1,zt2,random,io,cst,bw])
file.close()
print ‘\n\nWritten results.csv’
#
exit(0)