I got a VPS from linode one month ago and selected CentOS as the operating system.

I was pretty happy with it but soon found that if the command is longer than the window width, bash doesn’t wrap the line. I compared every possible configuration I could think out with my local CentOS installations but all are identical. When typing a very long command, it shows the command in one line, with the left side truncated, showing only a “>” character instead.

At last I was reminded by someone in the forum that the linode CentOS ships an empty /etc/termcap file. Yes. Reinstalling termcap package solved my problem.

Of course you need to “force” removing the package first:

# rpm -e –nodeps termcap
# yum install termcap

Ad:

In case you want a VPS of your own, I strongly recommend linode. They’re doing very great. You get control of everything and there’s an easy-to-use dashboard (even api). Up/down-grading can be done effortlessly.

If you’re interested, you can consider using my referral URL (thanks a lot :-) ):

http://www.linode.com/?r=e79251a33caf24fa1cf09c860deb2a6042999c52

The other day I was trying to deploy my newly written app on my VPS. It’s written based on the neat framework of web.py and I was planning to deploy it on lighttpd through fastcgi.

The process was not very smooth for me, as we always expect when trying out new things. The lighttpd server failed to start and by looking into the error log I found these lines:


2009-12-15 19:48:04: (server.c.1503) server stopped by UID = 0 PID = 25128 2009-12-15 19:48:30: (log.c.166) server started
2009-12-15 19:48:30: (mod_fastcgi.c.1104) the fastcgi-backend /var/www/code.py failed to start:
2009-12-15 19:48:30: (mod_fastcgi.c.1108) child exited with status 1 /var/www/code.py
2009-12-15 19:48:30: (mod_fastcgi.c.1111) If you're trying to run your app as a FastCGI backend, make sure you're using the FastCGI-enabled version.If this is PHP on Gentoo, add 'fastcgi' to the USE flags.
2009-12-15 19:48:30: (mod_fastcgi.c.1399) [ERROR]: spawning fcgi failed. 2009-12-15 19:48:30: (server.c.931) Configuration of plugins failed. Going down.

Finally I got it to work and here I’ll provide a check list in case someone else encounter the same problem.

Is code.py executable?

If not:

chmod 755 code.py

Also you need something like this on top of file:

#!/usr/bin/env python

Does helloword example work?

Use the example on web.py’s homepage (remember to add the line of python executable to the top of the file):


#!/usr/bin/env python
import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:
    def GET(self, name):
        if not name:
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

Chances are this will work. Because this app is so simple and it doesn’t require many libraries.

check egg cache dir permission

Some libraries need to extract files to a directory (python egg cache). I used MySQL-python in my program and it might fail on that.
/sbin/.python-eggs/
Now change the GET method of hello class to:


try:
    import MySQLdb
except Exception, e:
    return str(e);
return 'hello'

If egg cache permission is the cause, you’ll see something like this when visiting the server:

Can’t extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: ‘/sbin/.python-eggs’ The Python egg cache directory is currently set to: /root/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

There are many ways to solve this problem, but I just changed the owner of this directory to the user who runs lighttpd (daemon in my case):

chown -R daemon.daemon /sbin/.python-eggs

Is lighttpd using the expected version of python?

My VPS is running CentOS 5.4 and I installed python 2.6 on it (CentOS ships with old 2.4 version). I have two python executables (/usr/local/bin/python and /usr/bin/python) on my $PATH, but python 2.6 has precedence on mine (root). At last it turned out that lighttpd was using the old python 2.4

So I changed the first line of code.py to be:

#!/usr/local/bin/python

Now lighttpd can start happily.

Today I needed to pull some web page down from the internet and extract some specific contents in PHP. Sounds like a crawler, huh? Actually not the real crawler, just pulling our own contents. I was doing this because it’s not convenient for me to access the database directly.

I’m not quite familiar with PHP, but with version 5 on my local dev machine, I was able to do this very quickly. Just use file_get_contents to get the whole page as a string, and then use preg_match_all to search for the parts I want.

Unexpected things happened after I uploaded the script to the server. It said function file_get_contents was not defined. Then I realized that I was on a machine with Red Hat 9, the PHP I was using was version 4.2.2 bundled with RH9. OK. I rewrote the code to use fopen/fread directly. This time, it complained that it couldn’t handle the scheme (I don’t remember the error report string clearly).

I don’t know if it was because of my configuration, or version 4.2.2 doesn’t support the wrappers. It made me crazy. I don’t want to do an upgrade because all the packages are old. It takes time and may cause more problems. I even couldn’t find the apxs binary to compile PHP from source.

Finally, I got a workaround. First use exec to call wget to download the url to a file in /tmp, and then use fopen/fread to read this temp file. It really works.

Another problem was that preg_match_all doesn’t accept the last $offset parameter in PHP 4.2.2, but it’s simple to fix, I think.

This took me some time, but made me realize that how the development of software/language tools eased our daily work.

One month ago, I became interested in Django and made studying Python well a goal for myself.

Yes I know there are other ways to study a language. For example, learn Python by practicing with Django. But I want to be a bit familiar with Python before coding Django websites. So I decided to implement the algorithms in the famous book “Introduction to Algorithms“. The even greater benefit for me, I thought, was that I could get more familiar with algorithms.

It’s a great plan for me, one without great determination. Some friends said it’s hard when I told them. Now the fact turns out to be I really can’t go on with it. At least it must be paused, if not terminated.

I just got a new job. Although I really love it, I’m overwhelmed by the amount of new tools and knowledge I must learn. The good news is that I will learn Python for this job. The bad is that I’m afraid I can’t learn Python through implementing the famous algorithms. I must learn fast through practicing in real productions work.

So to learn Python is easy. But the road I chose to this goal is hard. Maybe it’ll return great profits – reading the book helped me a lot in the interview for this great job.

Will I resume the process when time is not so expensive as now? I wish.

Recently I noticed that one of my Greasemonkey script called “Google Reader Unread Count in Gmail” was displaying the wrong count. I was too busy but at least two users urged me to solve the problem so I took a look into the issue, which turned out to be very simple and easy to fix.

In fact Google changed the xml schema of the output so my script was displaying a timestamp. That’s why the count look so strange :)

Now the source code on userscripts.org is updated(here’s the diff). Please update your local version if you’re facing the same problem.

A very common purpose of Greasemonkey scripts is modifying the DOM structure of the document, mostly adding something new.

I’ve written several scripts before and have been doing such modifications in the “load” event handler, as in this script. The problem is that “load” event is fired after all images on the page have been downloaded completely, so users may suffer a great delay to see the changes occur if there’re many images.

jQuery users know that jQuery has a function “ready”. It won’t wait for the images to load. We are writing Greasemonkey scripts, so we only care about Firefox. Firefox has a “DOMContentLoaded” event explained in detail here.

Fired on a Window object when a document’s DOM content is finished loaded, but unlike “load”, does not wait till all images are loaded. Used for example by GreaseMonkey to sneak in to alter pages before they are displayed.

So when writing my last script I tried to rely on this event:

window.addEventListener('DOMContentLoaded', function(e) {
	...
}, false);

But the codes in the handler were not executed.

I was confused and did some searching. Finally I got this page saying that “the code in a Greasemonkey user script gets invoked when the DOMContentLoaded event fires”. That explains all. The “DOMContentLoaded” event was already fired so the codes never had a chance to get executed.

The solution, of course, is pulling the handler codes out of the wrapper. If your script doesn’t really rely on the “load” event, don’t put them into its handler because there may be a obvious delay.