Flask (or other WSGI) @ Binero

CGI WSGI (Flask) @ Binero

Experimental serving of python wsgi applications on binero hosting. Binero doesn’t have support for mod_wsgi or other wsgi server, but they have python and CGI support (or, officialy not, but it works), which together with other useful CLI apps makes it possible to set up. This is not so kind on the servers, the whole python interpreter having to be started for each call and all, but we’re only letting them serve a few pages to ourselves, so neglectable impact, right? Here’s an example of how to get a Flask app up and running.

Local installation of python 2.7 + Flask and deps

We cannot install python packages globally (obviously) and my guess is that crafting a lib directory by hand with Flask and all dependencies is a pita (not really, pip can install into arbitrary dir…).

Download python source package and unpack and cd to dir. Let’t not bother with virtualenv as it will depend on local installation anyways, just enable pip and install into local install dir.

cd domain.tld
mkdir python
cd python
wget Python-2.7.x.tar.xz --no-check-certificate
tar -Jxf Python-2.7.x.tar.xz
cd Python-2.7.x
./configure --prefix=[installdir]
make && make install
cd [installdir]
bin/python -m ensurepip
bin/pip install Flask

Create cgi-bin directory

A cgi-bin directory is no longer created by default when creating a new site using the control panel, but the servers will still execute scripts in such a dir if one exists. The CGI handler seems rather picky when it comes to file ownership and permissions though and the error messages are virtually useless. Creating the directory with “mkdir cgi-bin” over ssh won’t work as it will get user’s default gid and scripts won’t run. However if using FTP or the control panel filemanager to create the dir it will get a working gid (=33). Set the gid sticky bit on the directory so all files added to it will get the same gid automatically (chmod g+s cgi-bin). Also make sure that the scripts aren’t writable by group or the scripts won’t execute (chmod 755 script.cgi).

Or maybe just choose other dir and turn into cgi dir by adding appropriate directives to .htaccess. Like “Options ExecCGI” and “SetHandler cgi-script”, if permitted…

Set up test wsgi Flask app

Use python’s wsgiref.handlers.CGIHandler and a simple hello world app to test the setup. Browse to http://domain.tld/cgi-bin/wsgi.py to test. Replace PWD in path with path to your home directory, and again domain.tld with your domain name.

cgi-bin/wsgi.py

#!PWD/domain.tld/python/Python-2.7
from wsgiref.handlers import CGIHandler
from main import app

CGIHandler().run(app)

cgi-Bin/main.py

from flask import Flask
app = = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!

 

Letting apache serve static files

We probably have a bunch of static files and want apache to serve exising files from public_html as usual and redirect all other requests (like to /) to our serve.py script. mod_rewrite makes this easy:

.htaccess

RewriteEngine On 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteRule ^(.*)$ PWD/domain.tld/public_html/cgi-bin/wsgi.py/$1 [L]

Congrats, you can now serve wsgi applications!

Now you can create a more useful app, add some templates etc., and perhaps move it to the python dir if you wish, so to only keep wsgi.py in the cgi-bin dir (need to prepend python dir path to sys.path then though). Or move it to the virtualenv libs and the default sys.path should suffice for module import… or… or… whatever you please.

Simple way to get mail off the system – nullmailer and nail

nullmailer

Nullmail provides a sendmail implementation that forwards the mails though a smtp-server.¬† You have to set up some basic information in nullmailer’s config files. Which address to send from, which domain to use if only a username is provided and the smtp server through which to do the actual sending.

mignon@hades nullmailer % sudo tail -n+1 *
==> adminaddr <==
root@eleusis.se

==> defaultdomain <==
eleusis.se

==> me <==
eleusis.se

==> remotes <==
smtp.eleusis.se smtp auth-login user=******@eleusis.se pass=******** port=587 starttls
nail

Some existing scripts / software doesn’t invoke sendmail directly to send mail but expects the mail command to be present on the system (mdadm, smartd…). For those we can simply install the nail which will provide minimal mail and mailx commands and will with an empty configuration file send via nullmailers sendmail just like we want.

One could actually use nail exclusively as it also has support for sending via an smtp server directly. But other packages had dependencies on virtual/mta (Gentoo) that pulled in nullmailer (unless other mta installed) anyways.

LX50 Scroll Wheel Tilt Fix

This was another task I never thought I’d figure out, but, eventually I got it… kind of.

The problem was that my Logitech mouse (part of the S510 cordless keyboard/mouse combo package) sent <scroll lock> -> left/right -> <scroll lock> combinations when tilting the wheel instead of mouse button presses. As this mouse lacks side buttons I would have liked to use the wheel as mouse buttons 4 and 5. I could not understand why Logitech had configured this mouse to send this key-press sequence instead of just letting the mouse report button presses by default (perhaps this setting can be saved in the mouse by sending some command?)

Wheel tilts reported as mouse button presses was possible to set up using the SetPoint utility under Windows (and is by default) but there was no tool to change it for those of us running Linux. I couldn’t even find any reports of any others with the same request as I. There existed some Logitech-mouse-tweak software for Linux out there though, revoco, lomoco, g5hack, g400fix), that could change other settings on other¬† mouse models.

I downloaded whatever sources I could find to study the source to try figure out how I could make the mouse work like I wanted it. I found that some was based on libusb (0.1) and others utilized hiddev.

I downloaded a couple of trial USB sniffers for windows and tried to sniff the communications between the Logitech drivers and the device on device initialization, without any luck at first. It seemed like whenever I activated the sniffing software, Logitech SetPoint would no longer be able to identify the device so no initialization was attempted. This put a stop to my first attempt.

I retried some year later though with another piece of sniffing software which didn’t screw up device identification with which I managed to capture some of the communication. Not knowing anything really about USB protocols though I had to learn more about that in order to be able to interpret anything the logs told me (even though the software had many nice protocol decoding functions.).

First I attempted to use libusb, but got the message that a kernel driver had claimed interface, one could detach / reattach by echoing theh corresponding string to some file in the /sys filesystem. But still could not get it to work.

HIDDEV seemed like the simpler solution, and I could borrow the code from g5_hiddev.c almost in its entity, just changing the HID-report payload.

Tried to send the whole windows init sequence that I had captured, which would change the operation the button performed. Something like (shortened):

10 01 41 02 01 22 36
10 FF 80 00 00 00 00
10 01 81 07 00 00 00
10 01 81 07 00 00 00
10 03 41 02 02 11 56
10 01 81 07 05 00 00
10 01 81 00 00 00 00
10 01 81 00 00 00 00
10 01 81 00 00 00 00
10 01 80 00 10 03 00
10 01 80 00 10 03 00
10 01 80 00 00 00 00
10 03 81 07 00 00 00
10 03 81 07 00 00 00
10 03 81 07 05 00 00
10 01 81 01 00 00 00
10 01 81 01 00 00 00
10 01 81 01 42 00 00
10 03 81 00 00 00 00
10 03 81 00 00 00 00
10 03 81 00 00 00 00
10 01 80 01 42 00 00
10 01 80 01 42 00 00

Then tried to send them on by one until I found the one that worked:

10 01 80 01 00 00 00

The very same sequence but with 00 in the second position disables the “change DPI” buttons on many corded Logitech mice. Think 0x0 addresses corded mouse and 0x1 wireless (something about the dongle).

lx5fix.c