How can I serve a WSGI Python app using Mongrel2? - python

This is an attempt to discover a canonical answer for something that seems to be hard to Google for right now. I am writing a web app in Pyramid, and I have the luxury of deciding what my server stack will look like. I've heard very good things about Mongrel2, and would like to try using it to serve my app. However, I can't figure out from the Mongrel2 manual and Google searches, how to connect Mongrel2 to a WSGI app. Do I have to roll my own, or are there existing solutions to this problem?

In pursuit of a canonical answer, here are the WSGI handlers that I've found while looking for a way to get Mongrel2 to serve a WSGI app. Right now, it looks like wsgid is the best bet for Django users, but terminally flawed for others, and the alternatives are flawed.
The wsgid project is not yet at 1.0, but there are a lot of things that are usable before 1.0. It seems to have an active community around it, which is a recommendation.
The m2wsgi handler appears to mostly be one guy's project, but it looks to be alive, and "I started writing foo because I tried bar and baz and they didn't quite solve my problem" is a good place to start, and its feature-list looks good.
The Mongrel2-WSGI-Handler project on GitHub hasn't had a commit in over a year, so while it might be functional, I'm a little wary of it.
The mongrel2_wsgi project on BitBucket also hasn't had a commit in over a year, so it doesn't look like a great candidate either.
Brubeck is an entire web framework built on Mongrel2 and Python, which looks interesting.
Discussions and posts elsewhere about this topic:
LibreList: "State of WSGI handlers?"
Hacker News: "Brubeck: a new Python web framework running on Mongrel2"
Shedding Bikes: "Mongrel2 Python Library Working"
Spike Labs: "Deploying graphite on Mongrel2"
Dalton Matos: "Deploying Your Django Application With Mongrel2 and wsgid"

There is this post I wrote that talks about how to setup a mongrel2 with wsgid to run your WSGI app. It's here: http://daltonmatos.wordpress.com/2011/11/06/deploying-your-django-application-with-mongrel2-and-wsgid/

You need to use a Mongrel 2 handler that can serve a WSGI application. Basically something that translates the messages from a mongrel2 zeromq socket into a wsgi environ. There are several of these, I've used m2wsgi successfully in the past, but I think wsgid looks very promising as well.

Specifically about wsgid, you will probably need to tell it to load a generic WSGI application. You can know more about how to do it in the docs: Loading a generic WSGI Application, but implementing an AppLoader would not be that difficult.
If you need any other help you can send an email to the mailing list, will be a pleasure to help.

Related

Good resources to start python for web development?

I'm really interested in learning Python for web development. Can anyone point me in the right direction? I've been looking at stuff on Google, but haven't really found anything that shows proper documentation and how to get started. Any recommended frameworks? Tutorials?
I've been doing PHP for 5 years now, so I just want to try something new.
Django is probably the best starting point. It's got great documentation and an easy tutorial (at http://djangoproject.com/) and a free online book too (http://www.djangobook.com/).
Web Server Gateway Interface
About
http://www.wsgi.org/en/latest/index.html
http://en.wikipedia.org/wiki/Web_Server_Gateway_Interface
Tutorials
http://webpython.codepoint.net/wsgi_tutorial
http://lucumr.pocoo.org/2007/5/21/getting-started-with-wsgi/
http://archimedeanco.com/wsgi-tutorial/
There are three major parts to python web frameworks, in my experience. From the front to back:
Views/Templates: Application frameworks don't function as independent scripts - instead, you map paths to python functions or objects which return html. To generate the html you probably need templates (aka views). Check out Cheetah.
Application framework/Server: There are plenty. CherryPy is my favorite, and is good for understanding how a python application server works because a) it's simple and b) unlike django and others, it is just the application server and doesn't include a templating engine or a database abstraction layer.
Database layer: I've actually never used it, but everyone seems to like SQLAlchemy. I prefer, in simple applications, executing SQL directly using a tool like psycopg2 (for postgres).
You can try Django. It's easy to learn, and it works with GAE (though the default version is 0.96, a little bit old, but you can change it). And there's a video about rapid development (by Guido Van Rossum) that goes through the basics of setting up a Django project in App Engine.

app-engine-patch is dead. Now what is the best way to use Django on Google App Engine?

The app-engine-patch authors have officially marked this wonderful project as dead on their website. Over the last year a lot of people have asked what the best way to run Django on Google App Engine was, and time after time people have pointed to app-engine-patch being the way to go. Now that this project is dead, I would love to revisit this discussion and see what you all would recommend.
The old app-engine-patch website refers to a different project called djangoappengine that they are working on. It focuses on using Native Django on App Engine but by doing so appears to limit your ability to use all of App Engine's features (such as its built in User model). What do you all think about this as a replacement solution?
Does the Google App Engine Helper For Django project now look more inviting now that app-engine-patch has died?
Or is app-engine-patch still so great that you would recommend still using it despite it being dead?
I currently have a project in its middle stages relying on app-engine-patch and have loved using it so far, but am scared to continue using a project that is dead. I would really appreciate any comments you have about what you think the best long term solution is now. Thanks!
App engine patch is probably a safer bet for a given moment. Though not actively supported at the moment, it's still great, as it's been tested more thoroughly. If you're ready to take some risks - go and give the new djangoappengine+django-nonrel (native django support for non relational databases, primarily google datastore yest) option a try. That is the option we'll all use in a little while, so probably it's worth to try it out and not to look back. For instance, app engine patch only supports ~300 entities in the admin (i.e. doesn't take advantage of the 1.3.1 functionality), while with the djangoappengine+django-nonrel that is not an issues anymore. www.allbuttonspressed.com, a site of the guys behind app-engine-patch, is all built this new way, you can grab the source code of the site's skeleton # http://bitbucket.org/wkornewald/allbuttonspressed/.
good luck!
It looks like you can just import it using use_library...
http://code.google.com/appengine/docs/python/tools/libraries.html

Chat application using django

If i devlop a chat application using django will it have some performance problem?
Can i do server push in django?
I want to have PM and room discussions as well.
I released a Django app on Pypi and Github that provides a multi-user web chat.
It's based on Gevent: works well in multithreaded environments, but not in pre-forked ones such as gunicorn running more than 1 worker.
I'm just writing the documentation. The repo is: https://github.com/qubird/django-chatrooms
How about using tornado? I tried demo chat application of Tornado.
And also Tornado claims to have a better performance than django.
Let me know your thoughts.
grono.net has chat and PMs (although there are no rooms) and is built on django. Performance is pretty well, so I believe you should be able achieve the same performance. It depends, on how much connections you are expecting. grono.net is pretty big and it uses some caching and server distribution to perform well. But it all is doable on Django.
I think for a chat application you can use other technologies, such as AMQP(RabbitMQ, etc), Comet, etc.
But, for develop user profile, PMs, and other you can use Django.
Do not forget that performance still depends on server configuration (web server software, cache, db)
Basically Django is not the best way to do it.
However, if you are really stick to it and don't want to use to much solutions or/and want to keep it simple you can try with it:
http://popcnt.org/2008/01/django-evserver-asynchronous-server-for.html
Whih is asynchronous django server.
Also Twisted is worth checking out. I think that you described their tutorial scenario.

Writing a website in Python

I'm pretty proficient in PHP, but want to try something new.
I'm also know a bit of Python, enough to do the basics of the basics, but haven't used in a web design type situation.
I've just written this, which works:
#!/usr/bin/python
def main():
print "Content-type: text/html"
print
print "<html><head>"
print "<title>Hello World from Python</title>"
print "</head><body>"
print "Hello World!"
print "</body></html>"
if __name__ == "__main__":
main()
Thing is, that this seems pretty cumbersome. Without using something huge like django, what's the best way to write scripts that can process get and post?
Your question was about basic CGI scripting, looking at your example, but it seems like everyone has chosen to answer it with "use my favorite framework". Let's try a different approach.
If you're looking for a direct replacement for what you wrote above (ie. CGI scripting), then you're probably looking for the cgi module. It's a part of the Python standard library. Complimentary functionality is available in urllib and urllib2. You might also be interested in BaseHTTPServer and SimpleHTTPServer, also part of the standard library.
Getting into more interesting territory, wsgiref gives you the basics of a WSGI interface, at which point you probably want to start thinking about more "frameworky" (is that a word?) things like web.py, Django, Pylons, CherryPy, etc, as others have mentioned.
As far as full frameworks go I believe Django is relatively small.
If you really want lightweight, though, check out web.py, CherryPy, Pylons and web2py.
I think the crowd favorite from the above is Pylons, but I am a Django man so I can't say much else.
For more on lightweight Python frameworks, check out this question.
There are a couple of web frameworks available in python, that will relieve you from most of the work
Django
Pylons (and the new TurboGears, based on it).
Web2py
CherryPy (and the old TurboGears, based on it)
I do not feel Django as "big" as you say; however, I think that Pylons and CherryPy may be a better answer to your question. CherryPy seems simpler,. but seems also a bit "passé", while Pylons is under active development.
For Pylons, there is also an interesting Pylons book, available online.
In Python, the way to produce a website is to use a framework. Most of the popular (and actively maintained/supported) frameworks have already been mentioned.
In general, I do not view Djano or Turbogears as "huge", I view them as "complete." Each will let you build a database backed, dynamic website. The preference for one over the other is more about style than it is about features.
Zope on the other hand, does feel "big". Zope is also "enterprise" class in terms of the features that are included out of the box. One very nice thing is that you can use the ZODB (Zope Object Database) without using the rest of Zope.
It would certainly help if we knew what kinds of websites you were interested in developing, as that might help to narrow the suggestions.
In web2py the previous code would be
in controller default.py:
def main():
return dict(message="Hello World")
in view default/main.html:
<html><head>
<title>Hello World from Python</title>
</head><body>
{{=message}}
</body></html>
nothing else, no installation, no configuration, you can edit the two files above directly on the web via the admin interface. web2py is based on wsgi but works also with cgi, mod_python, mod_proxy and fastcgi if mod_wsgi is not available.
I really love django and it doesn't seem to me that is huge. It is very powerful but not huge.
If you want to start playing with http and python, the simplest thing is the BaseHttpServer provided in the standard library. see http://docs.python.org/library/basehttpserver.html for details
I agree with Paolo - Django is pretty small and the way to go - but if you are not down with that I would add to TurboGears to the list
If you are looking for a framework take a look at this list: Python Web Frameworks
If you need small script(-s) or one time job script, might be plain CGI module is going to be enough - CGI Scripts and cgi module itself.
I would recommend you to stick to some framework if you want to create something more then static pages and simple forms. Django is I believe most popular and most supported.
What is "huge" is a matter of taste, but Django is a "full stack" framework, that includes everything from an ORM, to templates to well, loads of things. So it's not small (although smaller than Grok and Zope3, other full-stack python web frameworks).
But there are also plenty of really small and minimalistic web frameworks, that do nothing than provide a framework for the web part. Many have been mentioned above. To the list I must add BFG and Bobo. Both utterly minimal, but still useful and flexible.
http://bfg.repoze.org/
http://bobo.digicool.com/

Python web server?

I want to develop a tool for my project using python. The requirements are:
Embed a web server to let the user get some static files, but the traffic is not very high.
User can configure the tool using http, I don't want a GUI page, I just need a RPC interface, like XML-RPC? or others?
Besides the web server, the tool need some background job to do, so these jobs need to be done with the web server.
So, Which python web server is best choice? I am looking at CherryPy, If you have other recommendation, please write it here.
what about the internal python webserver ?
just type "python web server" in google, and host the 1st result...
Well, I used web frameworks like TurboGears, my current projects are based on Pylons. The last ist fairly easy to learn and both come with CherryPy.
To do some background job you could implement that in pylons too.
Just go to your config/environment.py and see that example:
(I implemented a queue here)
from faxserver.lib.myQueue import start_queue
...
def load_environment(global_conf, app_conf):
...
start_queue()
It depends on your need if you simply use CherryPy or start to use something more complete like Pylons.
Use the WSGI Reference Implementation wsgiref already provided with Python
Use REST protocols with JSON (not XML-RPC). It's simpler and faster than XML.
Background jobs are started with subprocess.
Why dont you use open source build tools (continuous integration tools) like Cruise. Most of them come with a web server/xml interface and sometimes with fancy reports as well.
This sounds like a fun project. So, why don't write your own HTTP server? Its not so complicated after all, HTTP is a well-known and easy to implement protocol and you'll gain a lot of new knowledge!
Check documentation or manual pages (whatever you prefer) of socket(), bind(), listen(), accept() and so on.

Categories

Resources