<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>geek scrap &#187; wsgi</title>
	<atom:link href="http://geekscrap.com/tags/wsgi/feed/" rel="self" type="application/rss+xml" />
	<link>http://geekscrap.com</link>
	<description>there is at least one way to do it</description>
	<lastBuildDate>Tue, 12 Apr 2011 10:14:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Integrate WordPress and Django</title>
		<link>http://geekscrap.com/2010/02/integrate-wordpress-and-django/</link>
		<comments>http://geekscrap.com/2010/02/integrate-wordpress-and-django/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 08:00:21 +0000</pubDate>
		<dc:creator>geekscrap</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wsgi]]></category>
		<category><![CDATA[xml-rpc]]></category>

		<guid isPermaLink="false">http://geekscrap.com/?p=675</guid>
		<description><![CDATA[Last year WordPress got an award as best Open Source CMS software and the reason is clear: it&#8217;s easy to setup, low on resources, very customizable and full of useful extensions. So unless you have very specific deployment requirements and if your blog is not part of your core technology, you may get the best [...]]]></description>
			<content:encoded><![CDATA[<p>Last year WordPress got an <a href="http://www.packtpub.com/award">award</a> as best Open Source CMS software and the reason is clear: it&#8217;s easy to setup, low on resources, very customizable and full of useful extensions. So unless you have very specific deployment requirements and if your blog is not part of your core technology, you may get the best of both worlds by using WordPress for blogging and use a web framework for everything else. Not reinventing the wheel is very important in post-agile world, after all.</p>
<p>Being a Django monkey, I&#8217;d like to share some tips on how to make WordPress and Django live together:<br />
<span id="more-675"></span></p>
<h2>URL mapping</h2>
<p>To make WordPress and Django co-exist, they should map to different parts of the url space: you can simply configure them to respond on different virtual hosts, or just map WordPress to a specific path. For instance, if your webserver is Apache and you&#8217;re serving Django through mod_wsgi, you can use a config snippet like this:</p>
<pre>&lt;VirtualHost *:80&gt;
ServerName somehost.com
ServerAdmin hostmaster@somehost.com
ErrorLog /path/to/somehost.com/log/error.log
CustomLog /path/to/somehost.com/log/access.log combined
DirectoryIndex index.html
DocumentRoot /var/empty
WSGIScriptAlias / /path/to/somehost.com/parts/wsgi/wsgi
WSGIDaemonProcess somehost.com user=www group=www threads=25
WSGIProcessGroup somehost.com
Alias /blog/ /path/to/somehost.com/wordpress/
Alias /media/ /path/to/somehost.com/project/media/

&lt;Directory /path/to/somehost.com/wordpress/&gt;
Order allow,deny
Allow from all
&lt;/Directory&gt;

&lt;Directory /path/to/somehost.com/media/&gt;
Order allow,deny
Allow from all
&lt;/Directory&gt;
&lt;/VirtualHost&gt;</pre>
<h2>Accessing data</h2>
<p>To share data between your WordPress blog and your Django instance, you have <span style="text-decoration: line-through;">two</span> three main options:</p>
<ol>
<li>Save your WordPress tables and your Django models in the same database and configure your Django models accordingly.<br />
<strong>Pros</strong>: this approach is very straightforward and you don&#8217;t need to learn WordPress API. You can use Django admin interface to edit your WordPress database.<br />
<strong>Cons</strong>: sharing a database between two applications can have an impact on maintenance issues. For example, to rehash your Django application, you can&#8217;t just drop database, re-create it and populate with <em>syncdb</em>. WordPress database schema can change in new releases and make your ORM mapping obsolete.</li>
<li>Use Python&#8217;s xmlrpclib in Django to access WordPress XML-RPC interface.<br />
<strong>Pros</strong>: API changes won&#8217;t possibly break compatibility with previous releases. XML-RPC adds more logic, which means more consistency checks and more behind-the-curtain processing.<br />
<strong>Cons</strong>: XML-RPC calls are not as performant as direct access to MySQL.</li>
<li><strong>UPDATE</strong>: <a rel="nofollow" href="http://dougma.com/">Doug</a> pointed me to another method: adding a <a rel="nofollow" href="http://code.djangoproject.com/ticket/2440">PHP template loader</a> to Django. An useful <a rel="nofollow" href="http://showmedo.com/videotutorials/video?name=pythonNapleonePyConTech2&amp;fromSeriesID=54">video tutorial</a> is also available.</li>
</ol>
<p>To setup method 1, you can use <a rel="nofollow" href="http://code.google.com/p/django-wordpress-admin/source/browse/trunk/wordpress/models.py">models.py</a> from <a rel="nofollow" href="http://code.google.com/p/django-wordpress-admin/">django-wordpress-admin</a> project, which was built around <a rel="nofollow" href="http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb">inspectdb</a> output with some custom manager enhancements. On GitHub there&#8217;s another project named <a href="http://github.com/sunlightlabs/django-wordpress">django-wordpress</a>, with the same approach and a step-by-step tutorial is published at <a href="http://uswaretech.com/blog/2010/01/wordpress-and-django-best-buddies/">uswaretech.com</a>.</p>
<p>If you found Django-Wordpress integration tips useful, please drop a note in the comment box below to say how you used it, or just stay in touch with <a rel="nofollow" href="http://twitter.com/geekscrap">Twitter</a> or <a href="http://geekscrap.com/feed/">RSS Feed</a> for more Django stories.</p>
]]></content:encoded>
			<wfw:commentRss>http://geekscrap.com/2010/02/integrate-wordpress-and-django/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Integrate Tornado in Django</title>
		<link>http://geekscrap.com/2010/02/integrate-tornado-in-django/</link>
		<comments>http://geekscrap.com/2010/02/integrate-tornado-in-django/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:32:00 +0000</pubDate>
		<dc:creator>geekscrap</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[How-tos]]></category>
		<category><![CDATA[buildout]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[supervisord]]></category>
		<category><![CDATA[tornado]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://geekscrap.com/?p=588</guid>
		<description><![CDATA[Tornado is a nice python WSGI-compliant web server developed by guys at FriendFeed. It&#8217;s primarily thought as application server for python web frameworks and according to FriendFeed benchmarks, it&#8217;s blazing fast thanks to its non-blocking connections. UPDATE: For more performance info, James Abley pointed me to a very complete benchmark of available Python asynchronous webservers. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.tornadoweb.org/">Tornado</a> is a nice python WSGI-compliant web server developed by guys at FriendFeed. It&#8217;s primarily thought as application server for python web frameworks and according to FriendFeed benchmarks, it&#8217;s blazing fast thanks to its non-blocking connections.</p>
<p><strong>UPDATE</strong>: For more performance info, James Abley pointed me to a <a rel="nofollow" href="http://nichol.as/asynchronous-servers-in-python">very complete benchmark</a> of available Python asynchronous webservers. It looks like Tornado is a real monster of concurrency.</p>
<p>There are already some how-to&#8217;s on the web on plugging Django web framework into Tornado webserver. A quick recap:</p>
<ol>
<li>A <a rel="nofollow" href="http://www.jeremybowers.com/blog/4/tornado-web-framework-production-django-and-nginx/">tutorial</a> on Tornado, Django and nginx by Jeremy Bowers.</li>
<li>How to <a rel="nofollow" href="http://lincolnloop.com/blog/2009/sep/15/using-django-inside-tornado-web-server/">import django framework</a> inside a Tornado project by Lincoln Loop.</li>
<li>A <a rel="nofollow" href="http://www.djangosnippets.org/snippets/1748/">snippet</a> by lawgon.</li>
</ol>
<p>My approach is slightly different as I wanted to run Tornado using Django management command-line interface.</p>
<p><span id="more-588"></span>The 3 easy steps are:</p>
<ol>
<li>Add Tornado module to your django setup. If you use buildout, add Tornado git checkout to buildout.cfg using <em>minitage.recipe.fetch</em> recipe, like this:
<pre lang="ini">[buildout]
...
parts =
...
    tornado
    django
...

[tornado]
recipe = minitage.recipe.fetch
urls = git://github.com/facebook/tornado.git | git | | ${buildout:parts-directory}/tornado

[django]
recipe = minitage.recipe.scripts
initialization =
    import os
    os.environ['DJANGO_SETTINGS_MODULE']='project.settings.development'
scripts =
    django
eggs =
    Django
    ...
entry-points=
    django=django.core.management:execute_from_command_line
extra-paths =
    ${buildout:directory}
    ${tornado:location}
...</pre>
</li>
<li>Next, create a command-line extension hierarchy in your project&#8217;s main app:
<pre>$ mkdir project/myapp/management
$ touch project/myapp/management/__init__.py
$ mkdir project/myapp/management/commands
$ touch project/myapp/management/commands/__init__.py</pre>
</li>
<li>Last, add a runtornado.py script in project/myapp/management/commands/ folder with the following content:
<pre lang="python">from django.core.management.base import BaseCommand, CommandError
from optparse import make_option
import os
import sys

class Command(BaseCommand):
    option_list = BaseCommand.option_list + ()
    help = "Starts a Tornado Web."
    args = '[optional port number, or ipaddr:port]'

    def handle(self, addrport='', *args, **options):
        import django
        from django.core.handlers.wsgi import WSGIHandler
        from tornado import httpserver, wsgi, ioloop

	sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
	sys.stderr = os.fdopen(sys.stderr.fileno(), 'w', 0)

        if args:
            raise CommandError('Usage is runserver %s' % self.args)
        if not addrport:
            addr = ''
            port = '8000'
        else:
            try:
                addr, port = addrport.split(':')
            except ValueError:
                addr, port = '', addrport
        if not addr:
            addr = '127.0.0.1'

        if not port.isdigit():
            raise CommandError("%r is not a valid port number." % port)

        quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

        def inner_run():
            from django.conf import settings
            print "Validating models..."
            self.validate(display_num_errors=True)
            print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
            print "Server is running at http://%s:%s/" % (addr, port)
            print "Quit the server with %s." % quit_command
            application = WSGIHandler()
            container = wsgi.WSGIContainer(application)
            http_server = httpserver.HTTPServer(container)
            http_server.listen(int(port), address=addr)
            ioloop.IOLoop.instance().start()

        inner_run()</pre>
</li>
</ol>
<p>To run your tornado webserver, you just need to call your usual management program like manage.py with runtornado command, with the same syntax as runserver. In my case, I just run production server using <a rel="nofollow" href="http://supervisord.org/">supervisord</a>, with a command like this:</p>
<pre>$ ./bin/django runtornado --settings=project.settings.production 8000</pre>
<p>If you found this quick how-to useful, remember to follow me on <a rel="nofollow" href="http://twitter.com/geekscrap">Twitter</a> or subscribe to my feed for more django tips.</p>
]]></content:encoded>
			<wfw:commentRss>http://geekscrap.com/2010/02/integrate-tornado-in-django/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
