<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Peter's Blog - Nodes for cherrypy</title>
    <link>http://www.petersblog.org/</link>
    <description>Nodes containing the tag cherrypy</description>
    <item>
      <title>TurboGears, cherrypy, mod_python, cookie woes</title>
      <link>http://www.petersblog.org/node/view/1106</link>
      <description>&lt;p&gt;
Had a problem in my &lt;a href="/tag/turbogears"&gt;Turbogears&lt;/a&gt; app in that I was trying to set a cookie. It worked nicely under development, running on the cherrypy server but it didn't work in production when running under mod_python/apache. Eventually I established that it was because my http response was trying to send two cookies (my cookie and the session cookie) and only the last one was actually being sent to the browser. 
&lt;/p&gt;
&lt;p&gt;
I pinned it down to the mpcp script that I am using to interface mod_python with cherrypy. It has this code: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;elif&lt;/span&gt; tv &lt;span class="Keyword"&gt;is&lt;/span&gt; &lt;span class="Support"&gt;str&lt;/span&gt;:
    req.headers_out[header] &lt;span class="Keyword"&gt;=&lt;/span&gt; value
&lt;/pre&gt;
&lt;p&gt;
which is only allowing one 'Set-Cookie' header to be defined, later ones will overwrite the original. 
&lt;/p&gt;
&lt;p&gt;
Changing the code to the following fixes the problem: 
&lt;/p&gt;
&lt;pre class="lazy"&gt;&lt;span class="Keyword"&gt;elif&lt;/span&gt; tv &lt;span class="Keyword"&gt;is&lt;/span&gt; &lt;span class="Support"&gt;str&lt;/span&gt;:
    &lt;span class="Keyword"&gt;if&lt;/span&gt; &lt;span class="MetaFunctionCallPy"&gt;header.lower&lt;span class="MetaFunctionCallPy"&gt;(&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;)&lt;/span&gt;&lt;/span&gt; &lt;span class="Keyword"&gt;!=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;set-cookie&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;:
        req.headers_out[header] &lt;span class="Keyword"&gt;=&lt;/span&gt; value
    &lt;span class="Keyword"&gt;else&lt;/span&gt;:
        &lt;span class="MetaFunctionCallPy"&gt;req.headers_out.add&lt;span class="MetaFunctionCallPy"&gt;(&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Set-Cookie&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, value&lt;/span&gt;&lt;span class="MetaFunctionCallPy"&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/cherrypy"&gt;cherrypy&lt;/a&gt; &lt;a href="/tag/mod_python"&gt;mod_python&lt;/a&gt; &lt;a href="/tag/python"&gt;python&lt;/a&gt; &lt;a href="/tag/turbogears"&gt;turbogears&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1106</guid>
      <category domain="http://www.technorati.com/tag">cherrypy</category>
      <category domain="http://www.technorati.com/tag">mod_python</category>
      <category domain="http://www.technorati.com/tag">python</category>
      <category domain="http://www.technorati.com/tag">turbogears</category>
    </item>
    <item>
      <title>Sessions in TurboGears with mod_python</title>
      <link>http://www.petersblog.org/node/view/1095</link>
      <description>&lt;p&gt;
Sessions in &lt;a href="/tag/turbogears"&gt;TurboGears&lt;/a&gt; are easy enough: &lt;a href="http://www.turbogears.org/docs/sessions.html"&gt;the documents&lt;/a&gt; are very clear. 
&lt;/p&gt;
&lt;p&gt;
This ran pretty much first time. During development, using cherrypy's internal server the sessions were all lost whenever I rebooted the server, which was frustrating as it meant I had to log into my web app every time. 
&lt;/p&gt;
&lt;p&gt;
When I moved the app to the production server (mod_python) my sessions appeared to die after about 30 seconds. I think this is because apache reboots or something to kill any zombie/trojan activities. 
&lt;/p&gt;
&lt;p&gt;
I studied the cherrypy docs which are typically vague and incomplete. In combination with looking in the cherrypy source code, it seems that cherrypy defaults to storing the sessions in ram so they are deleted whenever the process dies. The fix boils down to putting the following in the config file: 
&lt;/p&gt;
&lt;div class="verbatim-block"&gt;&lt;pre&gt;sessionFilter.on = True
sessionFilter.storageType='File'
sessionFilter.storagePath='/var/www/wherever/sessions'
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
This stores the sessions in files on the server. This may well work on windows too, I haven't tried it yet. 
&lt;/p&gt;
&lt;p&gt;
From the source it seems cherrypy only supports sessions in a postgresql database, not mysql &lt;img alt="sad" src="/images/smileys/sad.png" /&gt; Still, it is possible that files are ultimately more efficient for something simple like this. 
&lt;/p&gt;
&lt;p&gt;
I must say I like the way TurboGears has two setup files, one for development and one for production. This makes it very easy for me to develop under windows and deploy on linux. I can use sqlite on windows and &lt;a href="/tag/mysql"&gt;mysql&lt;/a&gt; on linux. I have needed to create separate startup files as well (on windows to use cherrypy with a debugger, on linux to get it working with mod_python) but once this is done the code is pretty much identical (apart from using samba.winbind under linux to validate users). 
&lt;/p&gt;&lt;p&gt;Related Posts: &lt;a href="/tag/cherrypy"&gt;cherrypy&lt;/a&gt; &lt;a href="/tag/python"&gt;python&lt;/a&gt; &lt;a href="/tag/turbogears"&gt;turbogears&lt;/a&gt;&lt;/p&gt;</description>
      <guid>http://www.petersblog.org/node/view/1095</guid>
      <category domain="http://www.technorati.com/tag">cherrypy</category>
      <category domain="http://www.technorati.com/tag">python</category>
      <category domain="http://www.technorati.com/tag">turbogears</category>
    </item>
  </channel>
</rss>
