星期日, 1月 30, 2011

當前命令的路徑

有時候常忘記自己所執行的command在系統中到底在哪,

因此在這裡記錄一下,上次找了一次又忘記,
放在這總不會忘了。

$ which easy_install
/usr/bin/easy_install

-a 所有找的到的
-s 只回傳數字,1有找到,0沒找到。

星期五, 1月 28, 2011

用Django, Orbited, Stomp 來完成Server Push 功能

現行的ajax大多都是需client有事情,才發request對server,拿資料做事。

如果換成server有事想通知web client呢?

用Django + Orbited + Stomp 就可以完成了。

Orbited : 很神奇的東西,用javascrip+html提供了socket的功能,所以在這socket上就可以實作各種東西。

等等會把stomp client和這在browser端整合。

Stomp:只要有stompclient,就可以接收stomp broker的message了。

首先進行安裝
easy_install install orbited
easy_install install stomp.py

在django project的目錄下,加上個orbited.cnf,內容是
[listen]
http://:9000
stomp://:61613

[access]
* -> localhost:61613

[global]
session.ping_interval = 300

把orbited跑起來
orbited -c orbited.cnf

在someapp/view.py下新增如下
from django.shortcuts import render_to_response
import stomp
conn = stomp.Connection()
conn.start()
conn.connect()
def index:
return render_to_response('index.html', {'dummy': "stupid"})
def stepEventHub(request):
conn.send(json.dumps({"msg":"server"}), destination="/EventHub")
return HttpResponse("ok")

記得去編輯一下urls.py
urlpatterns = patterns('someapp.views',
(r'^index/$', 'index'),
(r'^stepEventHub/$', 'stepEventHub'),
)

然後再編輯index.html一下網頁加上下列script





function main(){
stomp = new STOMPClient();
stomp.onopen = function(){
console.log("Open Stomp Client");
}
stomp.onclose = function(c){
alert("Lost connection, Code:"+c);
}
stomp.onerror = function(error){
alert("Error : " + error);
}
stomp.onerrorframe = function(frame){
alert("Error : " + frame.body);
}
stomp.onconnectedframe = function(){
console.log("Connected. Subcribing");
stomp.subscribe("/EventHub");
}
stomp.onmessageframe = function(frame){
var data = $.parseJSON(frame.body);
alert(data['msg']);
}
stomp.connect("localhost", 61613);
};
$(document).ready(main);
下載Orbited.js&stomp.js
stomp.js在protocol下。

最後說明一下stompclient 內容
onopen – 連線開始時候要呼叫的
onclose –連線結束時候要呼叫的
onerror – 當stompclinet有錯誤時呼叫
onerrorframe – 如果有錯誤的frame抵達時呼叫
onconnectedframe – 當client成功傳送以及接收frame時
onmessageframe – 當收到一個frame時

reset – 重設連線
connect – 連到stomp server

記得frame過來後,json是在body中,所以要用$.parseJSON(frame.body)才可以。

最後先打開網頁http://127.0.0.1:8000/someapp/
再開另外一個網頁http://127.0.0.1:8000/someapp/stepEventHub

就會在第一個網頁看到alert message了。

這裡有詳細範例

星期二, 1月 25, 2011

Caught UnicodeDecodeError while rendering ... unexpected code byte

Django的樣板裡頭打上中文的時候,出現了如標題的錯誤

Caught UnicodeDecodeError while rendering ... unexpected code byte

找了各種原因,原來是Vim儲存時沒用utf-8編碼去存。
只要在vim下打
:set fenc=utf-8
再去編輯儲存就可以了。

星期六, 1月 22, 2011

Hello World of Linux Kernel Module Development

花了好些時間從算搞定。

首先先編譯Kernel,參照下列網址

http://blog.avirtualhome.com/2010/11/06/how-to-compile-a-ubuntu-10-10-maverick-kernel/
http://duopetalflower.blogspot.com/2010/10/ubuntu-maverick-64bit-kernel.html

編譯好裝上新Kernel後,就可以在/lib裡頭看到新的kernel了。

接著在home目錄下編輯hello.c
#include
#include
#include
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);
接著再編輯Makefile
obj-m := hello.o // 目標module
KERNELDIR = /lib/modules/2.6.35-24-cacore/build // 2.6.35-24-cacore是編譯過的Kernerl名稱
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
接著執行make,應該會出現hello.ko檔
然後
#sudo insmod hello.ko
可以裝上module

#sudo rmmod
會移除module

完成!

後記:用inmod不會去檢查相依性,所以建議改用modprobe
下列是新增及移除的方法
#modprobe module [parameter]
#modprobe -r module

星期四, 1月 20, 2011

Generic Relation in Django

有時候,Model中的某個欄位是ForeignKey,

而偏偏又不能確定該欄位是指向什麼Model的時候該怎麼辦呢?

Generic Relation是在Django裡的好辦法!

使用方法如下:
1.給Model一個ForeignKey欄位,指向ContentType
2.再給Model一個欄位,用來儲存所要指向的Model的Primary。
在這裡注意一下,如果型態給IntegerField,那就不能指向PrimaryKey是CharField的Model囉!
3.再一個欄位,型態是GericForeignKey,然後把上面兩個欄位給它。就可以了。

文件中有個簡單的範例:
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')

def __unicode__(self):
return self.tag
可以看到tag本身當然還不確定會去tag什麼model,所以就用Generic Relation是再好不過了!
執行時再決定所指向的是什麼就可以了。
>>> from django.contrib.auth.models import User
>>> guido = User.objects.get(username='Guido')
>>> t = TaggedItem(content_object=guido, tag='bdfl')
>>> t.save()
>>> t.content_object


參考自:http://docs.djangoproject.com/en/1.2/ref/contrib/contenttypes/