Archive

Archive for the ‘Programming’ Category

How to use cookies with CherryPy

May 27th, 2010 eddsn No comments

Introduction

CherryPy uses the Cookie module from Python and in particular the SimpleCookie object type to handle cookies.

Sending a cookie to a browser is accomplished by using cherrypy.response.cookie and receiving a cookie from the browser by cherrypy.request.cookie.

Example

This is demonstrated in the following example code where we use a login and logout procedure :

import cherrypy

class Root(object):
  @cherrypy.expose
  def index(self):
    return """
              <form id="
login" action="/doLogin/" method="post">
              <label>
              Username:
              <input name="
username" type="text" />
              </label>
              <label>
              Password:
              <input name="
password" type="password" />
              </label>
              <input type="
submit" value="Login" />
              </form>
           "
""

  @cherrypy.expose
  def doLogin(self, username, password):
    # Set cookie to send
    cookie = cherrypy.response.cookie

    cookie[‘user’] = username
    cookie[‘user’][‘path’] = ‘/’
    cookie[‘user’][‘max-age’] = 3600

    cookie[‘pass’] = password
    cookie[‘pass’][‘path’] = ‘/’
    cookie[‘pass’][‘max-age’] = 3600

    return ‘Cookie set. You can now <a href="/doLogout/">logout</a>.’

  @cherrypy.expose
  def doLogout(self):
    # Request cookie that is already set
    reqcookie = cherrypy.request.cookie

    # Response cookie that overwrites the old one and expires
    rescookie = cherrypy.response.cookie
    for name in reqcookie.keys():
      rescookie[name] = name
      rescookie[name][‘path’] = ‘/’
      rescookie[name][‘max-age’] = 0 # or: rescookie[name]['expires'] = 0

    return ‘Logged out succesfully. You can now <a href="/">login</a> again.’

cherrypy.quickstart(Root())
 

Download source code

Creating a cookie

It is important to note that

  • cookie[name]
  • cookie[name]['path']
  • cookie[name]['max-age']

are a bare minimum of attributes that you have to set in order to get this working. If you do not set one of these three attributes, the cookie will simply not be set.

Deleting a cookie

Instead of cookie[name]['max-age']=0 you can also use cookie[name]['expires']=0, which results in the same effect of deleting the cookie.

Categories: Cherrypy, Programming, Python, Tutorials Tags:

“Unable to find vcvarsall.bat” error when trying to install rdflib

May 19th, 2010 eddsn 7 comments

Some things just don’t work out like you expect them to do. During my quest with a new web application that I’m about to develop, the very first and basic thing went wrong. The setup of the rdflib python library (version 2.4.2) gave me the following error (on Windows):

“error: Setup script exited with error: Unable to find vcvarsall.bat”

After a lot of useless spent hours on trying to come up with some solution, the answer was found. There is a good solution on the project page of the library too, but I found a slightly quicker solution:

  1. First of all download MinGW. You need g++ compiler and MingW make in setup.
  2. If you installed MinGW for example to “C:\MinGW” then add “C:\MinGW\bin” to your PATH in Windows.
  3. Now start your Command Prompt and go the directory where you have your setup.py residing.
  4. Last and most important step: 
    setup.py install build --compiler=mingw32

Note: This is all about rdflib version 2.4.2! Version 3.x for example has some major differences with 2.4.2 and so I’m not aware whether the problem there exists and even if it does, i’m not sure if it can be solved by the above solution.

Categories: Fixes, Python, RDF Tags:

Large file sizes after compiling with Lazarus

December 26th, 2009 eddsn No comments

In the previous post when I was building the WLM Uninstaller tool, my choice was to write the app in Pascal (Lazarus).

When you compile a project in Lazarus, you might end up with large executables. In my case, it was about 100 lines of code but compiled to a 12MB executable!

Luckily, I found out how to significantly compress the file. In Lazarus there is a file called strip.exe that, as the name suggests, strips the file from any unnecessary mess. The file on my installation is located in C:\lazarus\fpc\2.2.2\bin\i386-win32. You can simply use it as follows: “strip file.exe”. The file I compiled went from 12MB to 1.77MB!

A further decrease can be accomplished by using a packer like UPX ( “upx -9 -o outputfile.exe inputfile.exe” ). In my case this was a further jump from 1.77MB to 668kB (5,4% of the original size!).

Categories: Lazarus, Pascal, Programming Tags:

Windows Live Messenger Uninstaller v0.01

December 26th, 2009 eddsn 113 comments

This is the first public version of the Windows Live Messenger Uninstaller (v0.01).

What the tool does is basically remove a broken Windows Live Messenger installation. It really uses only the windows installer (msiexec.exe) to do the job combined with the CLSID of the installation that uniquely identifies the WLM version.
The symptoms might be that when you want to reinstall the application it might say that it is already installed, while actually it is not. When trying to remove it with the usual Configuration – Add/Remove software steps, it might not even appear in the list, while the setup of the WLM says it is really installed.

I decided to build this tool since there are a lot of people strugling with this problem [1][2][3][4]. All these existing solutions presented by people on these sites use the single command with a CLSID key (eg. msiexec /x {B1403D7D-C725-4858-AACC-7E5FA2D72859}), but since the key is different for each WLM version such a solution might only be useful if you have that exact version. It can also be done manually by changing the registry keys, but I thought this would speed up the work significantly since there might be a lot of keys to search in (The location we are talking about here is: [HKEY_CLASSES_ROOT\Installer\Products\])

I heard of people formatting the entire hard drive because of not being able to resolve this, so I really hope this gets around and avoids a lot of headaches.

You can download this first version of the tool below.

Download binary | Download source code (pascal)

MD5 hash binary: 7525a442da4b4515c8166debfc4a4d01
MD5 hash source code: 28efa8f882c5354c6c4845393724e99a

Requirements
The tool is tested on Windows 7, but should also work on XP and Vista.

Usage
The tool is very simple to use. If it finds an installation it will give you the uninstall command that you can use to manually remove it. You can also press RUN and it will run the command automatically for you.

Note: This software comes with absolute no warranty! By using this program you agree that I am not responsible for anything caused by this software whatsoever.

Future releases
If you have any comments, suggestions or bugs, please put them here in the comments. If it is useful I might add/fix it in the next release.

Currently I’m planning to build a more general remover for the Windows Live series, since there are also other components that could get broken.

References
[1] http://ask-leo.com/how_do_i_uninstall_windows_messenger.html#comments
[2] http://ayumilove.wordpress.com/2009/03/14/how-to-install-remove-windows-live-messenger-2009/
[3] http://www.mydigitallife.info/2006/02/11/remove-and-uninstall-windows-messenger/
[4] http://www.vistax64.com/live-messenger/185883-cannot-uninstall-windows-live-messenger-beta-2009-a.html

Untrue descriptions on other sites linking to this page
The link to this topic is already published on several sites, but unfortunately the presented description of the tool is misleading. For it says that I’m claiming to have a better tool than all the other existing solutions, which is of course not true. This is only another alternative that may solve the problem.
In my description I was only referring to the existing solutions presented on the topics and comments on these that I used as references (see above [1][2][3][4]) and not on any other existing tool that probably uses other methods.

Softpedia certification

The tool can now also be downloaded from Softpedia.