wsgi interview questions
Top wsgi frequently asked interview questions
I have this HTML code with multiple input
s with the same name:
<input type="hidden" value="42" name="authors" />
<input type="hidden" value="13" name="authors" />
<input type="hidden" value="33" name="authors" />
The order of the values is important. Does the HTML spec define that user agents have to preserve this order, and if yes, do the common (market share > 1%) browsers follow this definition?
Bonus points if someone knows if WSGI and especially Django preserve the order server-side :-)
Thanks!
Source: (StackOverflow)
Could anyone please explain pros/cons when using WSGI VS uWSGI with Nginx.
Currently i am building up a production server for the Django website which i have prepared but unable to decide whether should i go with WSGI or uWSGI. Could you please explain in detail what differentiates each configuration? Which configuration should scale the best?
Thanks in advance
Source: (StackOverflow)
I'm building an app with Flask, but I don't know much about WSGI and it's HTTP base, Werkzeug. When I start serving a Flask application with gunicorn and 4 worker processes, does this mean that I can handle 4 concurrent requests?
I do mean concurrent requests, and not requests per second or anything else.
Thanks!
Source: (StackOverflow)
How can I be certain that my application is running on development server or not? I suppose I could check value of settings.DEBUG
and assume if DEBUG
is True
then it's running on development server, but I'd prefer to know for sure than relying on convention.
Source: (StackOverflow)
I can't get wsgi to import my settings file for my project 'mofin'.
The list of errors from the apache error log are as follows
mod_wsgi (pid=4001): Exception occurred within WSGI script '/var/www/wsgi-scripts/django.wsgi'.
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 228, in __call__
self.load_middleware()
File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 31, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 28, in __getattr__
self._import_settings()
File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 59, in _import_settings
self._target = Settings(settings_module)
File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 94, in __init__
raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'mofin.settings' (Is it on sys.path? Does it have syntax errors?): No module named mofin.settings
I got the "hello world!" wsgi app listed here(http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide) to work fine.
The settings.py file loads fine with python manage.py (runserver|shell|syncdb|test store)
as does the application.
Here is my wsgi file:
import os
import sys
sys.path.append('/home/django/mofin/trunk')
sys.path.append('/home/django/mofin/trunk/mofin')
print >> sys.stderr, sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'mofin.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
the sys.path as printed in the error log is
['/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0', '/home/django/mofin/trunk', '/home/django/mofin/trunk/mofin']
if I open an interactive shell with manage.py, sys.path is
['/home/django/mofin/trunk/mofin', '/usr/lib/python25.zip', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0']
My django settings file looks like this:
# Django settings for mofin project.
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Dan xxxx', 'xxxx@yyyyyyyyyy.com'),
)
MANAGERS = ADMINS
DATABASE_ENGINE = 'mysql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mofin' # Or path to database file if using sqlite3.
DATABASE_USER = 'aaaaaa' # Not used with sqlite3.
DATABASE_PASSWORD = 'bbbbbb' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/London'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-GB'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/django/media/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://mofin.mywebsite.co.uk/media/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin_media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
ROOT_URLCONF = 'mofin.urls'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'mofin.store'
)
Source: (StackOverflow)
I'm just wondering what the differences and advantages are for the different CGI's out there. Which one would be best for python scripts, and how would I tell the script what to use?
Source: (StackOverflow)
Every time I read either WSGI or CGI I cringe. I've tried reading on it before but nothing really has stuck.
What is it really in plain English?
Does it just pipe requests to a terminal and redirect the output?
Source: (StackOverflow)
I have a Bluehost account where I can run Python scripts as CGI. I guess it's the simplest CGI, because to run I have to define the following in .htaccess
:
Options +ExecCGI
AddType text/html py
AddHandler cgi-script .py
Now, whenever I look up web programming with Python, I hear a lot about WSGI and how most frameworks use it. But I just don't understand how it all fits together, especially when my web server is given (Apache running at a host's machine) and not something I can really play with (except defining .htaccess
commands).
How are WSGI, CGI, and the frameworks all connected? What do I need to know, install, and do if I want to run a web framework (say web.py or CherryPy) on my basic CGI configuration? How to install WSGI support?
Source: (StackOverflow)
So, while ago I asked similar question:
How to get whole request POST body in Python (Flask)
And I got an answer that actually flask.request.data
is the raw POST BODY. But that seems to work only if the request has few additional headers:
headers = {
'Content-type': 'binary/octet-stream',
'Content-length': len(postBody),
'Content-transfer-encoding': 'binary',
}
If those headers are not present, the flask.request.data
will be empty:
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['POST'])
def parse_request():
data = flask.request.data # but data will be empty unless the request has the proper content-type header...
So now I found the request is actually application/x-www-form-urlencoded
(which is default mimetype) then I could take the data like this:
app = Flask(__name__)
@app.route('/', methods=['POST'])
def parse_request():
data = flask.request.data # but data will be empty unless the request has the proper content-type header...
if not data:
data = request.form.keys()[0]
But I'm not sure that I could count on it...
So, is there a way to be able to obtain the raw post body of any post request, regardless of the headers?
Source: (StackOverflow)
I've pretty much tried every Python web framework that exists, and it took me a long time to realize there wasn't a silver bullet framework, each had its own advantages and disadvantages. I started out with Snakelets and heartily enjoyed being able to control almost everything at a lower level without much fuss, but then I discovered TurboGears and I have been using it (1.x) ever since. Tools like Catwalk and the web console are invaluable to me.
But with TurboGears 2 coming out which brings WSGI support, and after reading up on the religious debates between the Django and WSGI camps, I'm really torn between "doing it the right way", e.g., learning WSGI, spending valuable time writing functionality that already exists in Django and other full-stack frameworks, as opposed to using Django or some high-level framework that does everything for me. The downsides with the latter that I can see are pretty obvious:
- I'm not learning anything in the process
- If I ever need to do anything lower level it's going to be a pain
- The overhead required for just a basic site which uses authentication is insane. (IMO)
So, I guess my question is, which is the better choice, or is it just a matter of opinion, and should I suck it up and use Django if it achieves what I want with minimal fuss (I want authentication and a CRUD interface to my database)? I tried Werkzeug, Glashammer, and friends, but AuthKit and Repoze scared me off, as well as the number of steps involved to just setup basic authentication. I looked at Pylons, but the documentation seems lacking, and when referencing simple features like authentication or a CRUD interface, various wiki pages and documentation seemed to contradict each other, with different hacks for versions and such.
Thanks to S. Lott for pointing out that I wasn't clear enough. My question is: which of the following is worthwhile in the long run, but not painful in the short (e.g., some sort of middle ground, anyone?) - Learn WSGI, or stick with a "batteries-included" framework? If the latter, I would appreciate a suggestion as to whether I should give Django another try, stick with TurboGears 1.x, or venture into some other framework.
Also, I have tried CherryPy, but couldn't seem to find a good enough CRUD application that I could plop in and use right away.
Source: (StackOverflow)
Deploying a WSGI application. There are many ways to skin this cat. I am currently using apache2 with mod-wsgi, but I can see some potential problems with this.
So how can it be done?
- Apache Mod-wsgi (the other mod-wsgi's seem to not be worth it)
- Pure Python web server eg paste, cherrypy, Spawning, Twisted.web
- as 2 but with reverse proxy from nginx, apache2 etc, with good static file handling
- Conversion to other protocol such as FCGI with a bridge (eg Flup) and running in a conventional web server.
More?
I want to know how you do it, and why it is the best way to do it. I would absolutely love you to bore me with details about the whats and the whys, application specific stuff, etc. I will upvote any non-insane answer.
Source: (StackOverflow)
I am a big fan of Flask - in part because it is simple and in part because has a lot of extensions. However, Flask is meant to be used in a WSGI environment, and WSGI is not a non-blocking, so (I believe) it doesn't scale as well as Tornado for certain kinds of applications.
Since each one has an URL dispatcher which will call a function, and both will use Python files (in Django you dont launch the python file but in flask or tornado you do) do does it make sense to have two seperate parts to your website - one part running the non-blocking jobs with Tornado, and the other part written with Flask?
If this is a good idea, how would you go about sharing cookies / sessions between Flask and Tornado? Will I run into issues, since Flask will use it own system and Tornado will use its own system?
Source: (StackOverflow)
I'm trying to get two (or more) Django applications set up at subdirectories under the same domain, e.g.:
http://example.com/site1/
http://example.com/site2/
I know that normally this works fine by setting up an apache virtualhost like this:
<VirtualHost *:80>
...
WSGIScriptAlias /site1 /path/to/site1.wsgi
WSGIScriptAlias /site2 /path/to/site2.wsgi
</VirtualHost>
Now, I've verified that each site works individually. But when I try to run both side-by-side, apache sends me to whichever site the worker process loaded first. Example:
- Restart apache configured to serve 6 threads
- Load example.com/site1/, get the correct page
- Load example.com/site2/, get the correct page
- Repeat 2 and 3 2 more times.
- Refresh example.com/site1/ repeatedly, watch it cycle from site to site.
Effectively, for any given number of worker processes, it cycles through the total number of them sending the request to whichever one it hit first regardless of the WSGIScriptAlias directive. No matter what I do (setting WSGIProcessGroup, daemon mode vs. embedded mode, or directives) it continues to exhibit this behavior.
If anyone can point out what I'm doing wrong here, that would be phenomenal!
Source: (StackOverflow)
I am trying to set up SSL on a Django site I maintain and having a bit of trouble setting up my VirtualHost with SSL. I followed the instructions here but every time I try to restart apache, it tells me it cannot restart because of multiple virtualhosts usign the same wsgi config:
/etc/init.d/apache2 reload
Syntax error on line 33 of /etc/apache2/sites-enabled/www.mydomain.com:
Name duplicates previous WSGI daemon definition.
...fail!
I understand what is happening, just not how to fix it. Any suggestions are appreciated, thanks! Here is what my VirutalHosts file looks like:
<VirtualHost *:80>
ServerAdmin my@email.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /sites/mydomain
# WSGI Settings
WSGIScriptAlias / /sites/mydomain/wsgi_handler.py
WSGIDaemonProcess mydomain user=myuser group=mygroup processes=1 threads=1
WSGIProcessGroup mydomain
# Static Directories
Alias /static /sites/mydomain/static/
<Location "/static">
SetHandler None
</Location>
Alias /img /sites/mydomain/img/
<Location "/img">
SetHandler None
</Location>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin my@email.com
ServerName mydomain.com
ServerAlias www.mydomain.com
DocumentRoot /sites/mydomain
# WSGI Settings
WSGIScriptAlias / /sites/mydomain/wsgi_handler.py
WSGIDaemonProcess mydomain user=myuser group=mygroup processes=1 threads=1
WSGIProcessGroup mydomain
# Static Directories
Alias /static /sites/mydomain/static/
<Location "/static">
SetHandler None
</Location>
Alias /img /sites/mydomain/img/
<Location "/img">
SetHandler None
</Location>
# SSL Stuff
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/crt/vhost1.crt
SSLCertificateKeyFile /etc/apache2/ssl/key/vhost1.key
<Location />
SSLRequireSSL On
SSLVerifyClient optional
SSLVerifyDepth 1
SSLOptions +StdEnvVars +StrictRequire
</Location>
</VirtualHost>
Source: (StackOverflow)
I can't seem to figure out how to access POST data using WSGI. I tried the example on the wsgi.org website and it didn't work. I'm using Python 3.0 right now. Please don't recommend a WSGI framework as that is not what I'm looking for.
I would like to figure out how to get it into a fieldstorage object.
Source: (StackOverflow)