Debian and Ubuntu server locale and encoding

I recently had «fun» supporting server installation of software that needed locale set properly.

In Debian, you can just use

dpkg-reconfigure locales

and the console GUI will pop up.

In Ubuntu server, you’ll more steps to make^

http://askubuntu.com/questions/89976/how-to-change-the-default-locale-in-ubuntu-10-10-server

https://debian.pro/503

https://help.ubuntu.com/community/Locale

And make full use of UTF, please: http://docs.python.org/2/library/locale.html

Django soft image thumbnail replacement

Using new template tag, you’re now free to cache images before rolling out new design and avoid heavy load (maybe via random call)!

https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#with

<img src="{{img.url|thumb:"500x500"%}" />


{% if rand % 10 }
{with newthumb=img.newthumb %}}

or <!-- {{img.url|thumb:"500x580"%} -->

{% endif %}

MySQL with Django on Debian

First, install MySQL driver for Python:

sudo apt-get install python-mysqldb

Next, modify your config of MySQL: http://dba.stackexchange.com/a/8289

If you’ve already messed up the install, you can either drop/create database from scratch, or apply the following script to every table of your DB:

ALTER TABLE `table` CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

Alternative mirgation path: http://docs.joomla.org/Convert_a_MySQL_database_to_UTF-8

Python chroot

What is important to remember is that chroot will allow you cd into /, and that will correspond to the dir you chrooted in:

os.chroot(os.path.abspath(os.path.dirname(__file__)))
for opt in glob.glob("/*"):
    print "\t-> %s" % opt

try:
    os.chdir("/root/")
    print "Chroot FAIL"
    print os.getcwd()
except Exception as e:
    print "Chroot OK"

Nonblocking console input in Python

By Nemesis Fixx:

import sys
import select
import tty
import termios
from threading import Thread

program_run = True
input_thread_timeout = 0.005 #seconds
quit_key = '\x1b' # x1b is ESC

#check stdin for input...
def isData():
        return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

#check n terminate program on terminal condition,
#from a separate thread
class waitOnInput(Thread):
    def run(self):
        old_settings = termios.tcgetattr(sys.stdin)
        try:
            tty.setcbreak(sys.stdin.fileno())
            global program_run
            thread_run = True
            while thread_run:
                if isData():
                    c = sys.stdin.read(1)
                    if c == quit_key:
                        break
                        thread_run = False
                        program_run = False
        finally:
            termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
            thread_run = False


t = waitOnInput()

#start work here...
i = 1

while program_run :
    if not t.is_alive():
        t.start()

    #test for terminal condition or timeout...
    t.join(input_thread_timeout)

    if t.is_alive():
        #continue work here...
        print i
        i += 1
    else:
        break

Scrapy 0.12 Parsing with python

based on Scrapy Tutorial

1. Install scrapy and dependencies

sudo apt-get install python-lxml
sudo easy_install -U Scrapy

2. Create project

scrapy startproject dmoz

3. Create item models

from scrapy.item import Item, Field
 
class DmozItem(Item):
    title = Field()
    link = Field()
    desc = Field()

Читать далее

Installing MongoDB 1.8.1 on Ubuntu 11.04 and PyMongo

Install everything you need:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
sudo nano /etc/apt/sources.list

Next, add a line to sources.list:

— on Ubuntu

deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen

— on Debian

deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
sudo apt-get update
sudo apt-get install mongodb-10gen
sudo apt-get install python-setuptools
sudo easy_install pymongo

Next, test the connection:
Читать далее

Django nginx Debian

How to make a simple install of django onto small Debian-6 VPS.
I’ll stick with flup, which enables python to serve fastcgi and some other protocols.
I use it in conjunction with nginx, which in turn is used save memory.

Literature:
http://library.linode.com/using-linux/administration-basics#system_diagnostics
http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/
http://www.mindinmotion.ru/post/django-postgresql-nginx-on-debian-server
Читать далее