14 articles Python

Python, a clean programming language enforcing syntax above all.

File uploads in a Passenger-backed application fail

Overview A file upload initiated in an application written in Ruby, Node, or Python launched through Passenger will fail to upload. Thus far, the confirmed failure occurs in RefineryCMS with a generic undefined route message following upload. Cause It is a conflict between upload screening and Passenger, but the underlying cause is not clearly understood. Upload…

Pyramid Quickstart

Overview Pyramid is a Python framework that is the spiritual successor to Pylon and Zope, frameworks popular in the mid-to-late 2000s. Pyramid is supported with on v6+ platforms using any Python version from 2.7 onward with Passenger. Quickstart All commands are done from the terminal for convenience. PREREQUISITE: create a suitable Passenger-compatible filesystem layout cd /var/www…

Flask Quickstart

Overview Flask is a Python microframework for building web sites with minimal overhead. Think of it as a lightweight version of Django with fewer features, but better speed. Flask is supported on v6+ platforms using Passenger. Quickstart All steps are done from the terminal. While it may be possible to deploy a Flask application without using terminal,…

E-mails sent from Django appear as webmaster@host.domain.tld

Overview E-mails sent from a Django application may use “webmaster@host.domain.tld” or another erroneous address as the From field. Cause ServerAdmin values are not set in Apache configuration for virtual hosts to prevent unintentional information leakage. Solution Set DEFAULT_FROM_EMAIL and SERVER_EMAIL in your Django settings file. An example configuration follows: EMAIL_HOST=’localhost’ EMAIL_PORT=’587′ EMAIL_HOST_USER=’myadmin@mydomain.com’ EMAIL_PASSWORD=’mysmtppassword’ DEFAULT_FROM_EMAIL=EMAIL_HOST_USER SERVER_EMAIL=EMAIL_HOST_USER Replace EMAIL_HOST_USER and EMAIL_PASSWORD values with your…

Viewing launcher errors

Overview Applications launched by Passenger on v6+ platforms may emit output on stdout or stderr channels. Any output emitted is logged to an aggregate log called passenger.log in /var/log. Important: since these logs are combined among all accounts using Passenger, never output anything confidential to stdout or stderr when launched using Passenger. Once an application is up and…

pip install fails with “Permission denied” on Python 3+

Overview Python’s integrated package manager, pip, fails to install packages when Python 3.0 and above is used raising a PermissionError. Below is an abbreviated sample output: [myadmin@sol]$ pip install django Downloading/unpacking django Installing collected packages: django Cleaning up… Exception: Traceback (most recent call last): File “/.socket/python/python3.4/site-packages/pip/wheel.py”, line 205, in clobber os.makedirs(destdir) File “/.socket/python/python3.4/os.py”, line 237,…

Python bins fail to import library

Overview A binary (bin) file installed as part of a Python package, e.g. django-admin from Django will fail upon execution – even after successful installation via pip – because it cannot locate its corresponding Python library. Example: [myadmin@sol www]$ django-admin Traceback (most recent call last): File “/usr/local/bin/django-admin”, line 7, in <module> from django.core.management import execute_from_command_line portError: No module…

Disabling Passenger built-in error handler

Overview Passenger provides a user-friendly, on-screen error handler to assist debugging a Python/Ruby/Node.js application. During production, however, this may result in unnecessary and possibly dangerous information disclosure. You can turn off Passenger’s built-in logger and use Apache’s generic error handler by adding the following line to your .htaccess file located within the public/ folder of your app:…

Using WSGI

Overview Python applications can be launched using Passenger offering improved throughput and lifecycle management. Launching CGI scripts wrapped by pyenv will yield very poor throughput as a result of multiple shell subprocesses necessary to ascertain the correct Python interpreter. Adapting a CGI application to WSGI improves throughput significantly by reducing overhead through a persistent process. Pages load quickly, and applications utilize…

Restarting Passenger processes

Overview An application launched by Passenger may be restarted by creating a file in tmp/ (NB: not /tmp) within the application root directory, usually one level down from public/. Create a file under tmp/ named restart.txt to restart the application once. A restart will happen within 2 minutes. To restart an application on every request, very useful for…