Tuesday, April 17, 2012

Django Notes

一、建立新的專案
  (1)執行 "django-admin startproject "
  (2)編輯裡的settings.py檔,修改一些需要的參數,如果有需要用到DB的話,在修改完之後要執行"python manage.py syncdb",才會把DB的table建立到DB中。
  (4)執行"python manage runserver <[IP:]port>",可以看到一個空白的網頁。
  (5)__init__.py:讓python知道這個資料夾是一個套件,其中含有相關的模組(model)和程式檔案。
  (6) manage.py:管理用script,和django-admin有相同的作用。
  (7)settings.py:專案主要的參數設定檔。
  (8)urls.py:專案中網頁位址及函數的對應檔,使用正規化(RE)語法!!!

二、靜態網頁
  (1)建立app:"/python manage.py startapp "
  (2)建立一個view:編輯views.py
from django.http import HttpResponse

def main_page(request):
        output = '''
                <html>
                        <head><title>%s</title></head>
                        <body>
                                <h1>%s</h1><p>%s</p>
                        </body>
                </html>
        ''' % (
                        'My first page from Django',
                        'My first page.',
                        'I\'m testing page!'
                        )
        return HttpResponse(output)
  (3) 將view加入/urls.py
urlpatterns = patterns('',
    # Example:
    # (r'^start/', include('start.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # (r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # (r'^admin/', include(admin.site.urls)),

                                (r'^$', main_page),
)
  (4) __init__.py:讓python知道是一個套件。
  (5)views.py:主要的html內容
  (6)models.py:資料模組

三、資料庫
  參考網址:Django Book

  (1)大原則
      <1>在<app_name>/models.py中,一個class只能對應一個table;
      <2>在<project_name>/settings中,一個db設定只能對應一個db(eg. Name=lab,只對應到lab這個db);
      <3>join還沒看完~~~~~

  (2)常用語法
      <1>"python manage.py validate": 檢查models.py的語法是否有錯誤。
      <2>"python manage.py sqlall <app_name>":顯示create table的sql語法。

  (3)參數
      <1> class Meta option db_table:指定db中table的名稱,預設為" <app_name>_<class name> "
 class <name>(models.Model):
 <colume name> = models.CharField(max_length=64)
.........<colume name> as you need
 class Meta:
    db_table = '<table name>'

.........還沒試完........XD

Sunday, July 31, 2011

Python雜記

主題:Twisted Notes
參考網址:Python官網Twisted官網Twisted Network Programming Essentials
(1) 自行定義的函式
  使用root執行因/root權限問題,將不會搜尋PYTHONPATH路徑以外的函式。
  解決方式:
    (i) 使用非root的權限。
    (ii) 將自行定義的函式複製到PYTHONPATH有定義的路徑中。
(2) run application as a daemon
  twistd不要使用root權限執行,會導致python執行時不會搜尋當前資料夾中的自定函式。

Monday, December 13, 2010

NFS configure

參考網址:Debian WiKitldp

系統:Debian5.0
Kernel:2.6.32-5-686
套件:(v)nfs-server:nfs-kernel-server,nfs-common、nfswatch
相依性:libblkid1 (>= 2.16), libc6 (>= 2.3), libcomerr2 (>= 1.01), libgssapi-krb5-2 (>= 1.6.dfsg.2), libgssglue1, libk5crypto3 (>= 1.6.dfsg.2), libkrb5-3 (>= 1.6.dfsg.2), libnfsidmap2, librpcsecgss3, libwrap0 (>= 7.6-4~), nfs-common (= 1:1.2.2-4), ucf, lsb-base (>= 1.3-9ubuntu3)

Server設定:
(1) mount和file 的存取權限(/etc/exports、/etc/hosts.allow、/etc/hosts.deny)。
(2) 如果Server放在PORT Base的防火牆內,mountd參數RPCMOUNTDOPTS(/etc/default/nfs-kernel-server)要設固定port("--port xxxx" xxxx>1024,1024之前是系統使用,不建議用1024之前的port)。
(3) quota(/etc/default/quota)、statd(/etc/default/nfs-common)、lockd(Create new file like /etc/modprobe.d/nfs.local.conf)設定檔也可加上固定的port。

Client設定:
(1) mount -t nfs IP:/path/to/share /mountpoint

步驟:

(1) Server
(a) /etc/exports
/shareDIR Client(restriction_1, restriction_2, ...)
(I) rw
(II) root_squash/no_root_squash
(III)...
(b) /etc/hosts.deny
portmap:ALL
(c) /etc/hosts.allow
portmap: allowIP_1/netmask allowIP_2/netmask

(2)restart service
(a) /etc/init.d/portmap restart
(b) /etc/init.d/nfs-kernel-server restart
(c) /etc/init.d/nfs-common restart



**************************沒空~~~~後補啦...=.=+***************************

Friday, October 29, 2010

Linux嘸蝦米輸入法安裝

env: Xubuntu 10.04、gcin
ref: 合併cin檔gcin同好會

准備工作:

一、找一台有正版嘸蝦米的windows,從c:\windows\system32\中複製出四個table檔:liu-uni.tab、liu-uni2.tab、liu-uni3.tab、liu-uni4.tab。
二、從GCIN同好會上下載轉檔用的兩個程式:uni2txt.sh和uni2txt.exe,以及四個vrsf提供的特殊字選字檔:liu-uni.vrsf、liu-uni2.vrsf、liu-uni3.vrsf、liu-uni4.vrsf。
三、安裝wine,因為uni2txt.exe是windows下執行的binary,執行uni2txt.sh時會使用wine來執行uni2txt.exe來將tab檔轉成txt檔。
四、從蝦米族樂園下載liu.box。此檔為utf-16格式,用途為EXTA/B字的擴充,並做一些修改:

(1)在xubuntu上下載的話,記得檢查locale,設為utf-8再下載,比較不會出錯。

export LC_ALL=zh_TW.UTF-8
export LANG=zh_TW.UTF-8 (以下這兩行是方便從console上直接看檔案內容用)
export LANGUAGE=zh_TW.UTF-8

(2)刪除檔頭的文字敘述直到 ree; 為止。
(3)將 ; 取代掉。

vi liu.box
:1,$s/;//g


五、將locale設定為utf-8環境:export LC_ALL=zh_TW.UTF-8

步驟:
一、執行gcin同好會上提供的uni2txt.sh,將tab檔轉為linux可用的cin檔。

bash uni2txt.sh
(1)如果有錯,自行除錯,沒有出現錯誤的話,會產生出*.unix、*.txt、noseeing*.cin、noseeing*.gtab。
(2)如果不想合併cin檔此處的gtab檔就可以使用了,直接進行步驟四。

二、合併cin檔。

cat noseeing.cin liu-uni2.unix liu-uni3.unix liu-uni4.unix liu-utf8.box | perl -nle '(print,$hash{$_}=1) unless defined $hash{$_}' > boshiamy-hyper.cin

檔名解釋
(1)合併的檔案可能是要解決字元重複性的問題所以是用noseeing.cin liu-uni2.unix liu-uni3.unix liu-uni4.unix來合併。
(2)liu-utf8.box由准備工作四來決定檔名,要記得改成符合的檔名。
(3)perl -nle '(print,$hash{$_}=1) unless defined $hash{$_}' > 就是把上述檔案stdout到>後面所接的檔案中。
(4) boshiamy-hyper.cin可以自己取,在同好會上用的是noseeing.cin。


三、使用gcin2tab將合併好的cin檔轉為gcin可用的gtab檔。

gcin2tab boshiamy-hyper.cin
(1)會產生*.gtab檔。

四、將gtab放入gcin的目錄中。

(1)同好會中直接放進~/.gcin/,這樣只能是單一使用者使用。
(2)因為是自己的電腦,所以放進/usr/share/gcin/table/中,這樣每個使用者都可以用了。

五、附錄:

#!/bin/bash
# uni2txt.sh version 0.0.1

wine ./uni2txt.exe liu-uni.tab liu-uni.txt
wine ./uni2txt.exe liu-uni2.tab liu-uni2.txt
wine ./uni2txt.exe liu-uni3.tab liu-uni3.txt
wine ./uni2txt.exe liu-uni4.tab liu-uni4.txt

# sed is used to remove byte-order mark
if [ -f liu-uni.txt ]; then
iconv -f UTF-16LE -t UTF-8 -c liu-uni.txt | \
tr -d "\r" | sed 's/^//' | awk '{print $1,$2}' > liu-uni.unix
tail -n 39 "$0" | sed 's/^%cname$/%cname 嘸蝦米/' > noseeing.cin
cat liu-uni.unix >> noseeing.cin
if [ -f liu-uni.vrsf ]; then
cat liu-uni.vrsf >> noseeing.cin
fi
gcin2tab noseeing.cin
fi

if [ -f liu-uni2.txt ]; then
iconv -f UTF-16LE -t UTF-8 -c liu-uni2.txt | \
tr -d "\r" | sed 's/^//' | awk '{print $1,$2}' > liu-uni2.unix
tail -n 39 "$0" | sed 's/^%cname$/%cname 簡體蝦/' > noseeing2.cin
cat liu-uni2.unix >> noseeing2.cin
if [ -f liu-uni2.vrsf ]; then
cat liu-uni2.vrsf >> noseeing2.cin
fi
gcin2tab noseeing2.cin
fi

if [ -f liu-uni3.txt ]; then
iconv -f UTF-16LE -t UTF-8 -c liu-uni3.txt | \
tr -d "\r" | sed 's/^//' | awk '{print $1,$2}' > liu-uni3.unix
tail -n 39 "$0" | sed 's/^%cname$/%cname 台簡蝦/' > noseeing3.cin
cat liu-uni3.unix >> noseeing3.cin
if [ -f liu-uni3.vrsf ]; then
cat liu-uni3.vrsf >> noseeing3.cin
fi
gcin2tab noseeing3.cin
fi

if [ -f liu-uni4.txt ]; then
iconv -f UTF-16LE -t UTF-8 -c liu-uni4.txt | \
tr -d "\r" | sed 's/^//' | awk '{print $1,$2}' > liu-uni4.unix
tail -n 39 "$0" | sed 's/^%cname$/%cname 日文蝦/' > noseeing4.cin
cat liu-uni4.unix >> noseeing4.cin
if [ -f liu-uni4.vrsf ]; then
cat liu-uni4.vrsf >> noseeing4.cin
fi
gcin2tab noseeing4.cin
fi

exit

%gen_inp
%ename liu
%cname
%selkey 123456789
%space_style 1
%keyname begin
a A
b B
c C
d D
e E
f F
g G
h H
i I
j J
k K
l L
m M
n N
o O
p P
q Q
r R
s S
t T
u U
v V
w W
x X
y Y
z Z
, ,
. .
' ’
[ 〔
] 〕
%keyname end
%chardef begin

Sunday, January 24, 2010

SQUID的Transparent Proxy測試...未完

最近又把squid拿出來測試,version 3把transparent proxy的function加進去,目前只測到bridge mode,我想要的parallel mode似乎還是得靠wccp來做,可是wccp library似乎很久沒更新了,臺灣的論壇資料好少,是大家都不用proxy這個服務了嗎?!!QQ

-------------我---是---分---格---線-------------
回到主題,以下是安裝及測試的過程,有瑕疵加上主機目前等上架測試,已經關機,所以缺很大,開機後再補上,但基本功能已經搞定了。

作業環境:
OS:Debian Linux 5 kernel-2.6.30-2
gcc:4.3.2
squid:3.1.5
library:tproxy4.1(還沒抓下來測~~)、netfilter
utilities:modconf(設定模組,debian使用)、iptables

步驟:
  1. bridge mode
    1. compile squid
    2. config squid
    3. config firewall rules
  2. parallel mode
    1. compile squid
    2. config squid
過程:
  1. compile squid
    • tar zxvf squid-verison.tar.gz
    • cd /path/to/squid-version
    • ./configure --prefix=/path/you/want --with-large-files--with-openssl=/usr --with-dl --with-aufs-threads=128 --with-gnu-ld --enable-gnuregex --enable-stacktraces --enable-linux-netfilter --enable-ipfw-transparent --enable-arp-acl --enable-disk-io=AIO,Blocking,DiskDaemon,DiskThreads --enable-storeio=aufs,ufs,diskd --enable-esi --enable-async-io=128
    • --enable-wccp:要先裝wccp library,印象中直接make;make install就搞定了。
    • --enable-linux-netfilter、--enable-ipfw-transparent:bridge mode才會使用,有netfilterip-filter、ipfw可以選擇。
    • --enable-disk-io=AIO,Blocking,DiskDaemon,DiskThreads
    • --enable-storeio=aufs,ufs,diskd
    • --enable-async-io=128
    • --enable-esi
    • make;make install
  2. config squid
    #
    # Define squid user and group ID
    #
    cache_effective_user squid
    cache_effective_group squid

    #
    # Define logs
    #
    logformat squid %ts.%03tu %6tr %>a %Ss/%03>Hs %a %Ss/%03>Hs %h] [%a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %a %ui %un [%tl] "%rm %ru HTTP/%rv" %>Hs %h" "%{User-Agent}>h" %Ss:%Sh

    access_log /log/path/access.log squid
    cache_log /log/path/cache.log
    pid_filename /log/path/squid.pid

    #
    # Squid normally listens to port 3128
    #
    http_port 3128
    http_port 3129 tproxy
    #http_port 3128

    #
    # Define Transparent Proxy
    #

    visible_hostname OMSProxy.fcu.edu.tw
    #always_direct allow all
    #http_reply_access allow all
    #icp_access allow all

    #hierarchy_stoplist cgi-bin ?
    #acl QUERY urlpath_regex cgi-bin \?
    #acl apache rep_header Server ^Apache
    #cache deny QUERY

    #
    # Recommended minimum configuration:
    #
    acl manager proto cache_object
    acl localhost src 127.0.0.1/32
    acl localhost src ::1/128
    acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
    acl to_localhost dst ::1/128
    acl allow_access src 140.134.21.0/24
    acl allow_dist dst all

    # Example rule allowing access from your local networks.
    # Adapt to list your (internal) IP networks from where browsing
    # should be allowed
    acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
    acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
    acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
    acl localnet src fc00::/7 # RFC 4193 local private network range
    acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines

    acl SSL_ports port 443
    acl Safe_ports port 80 # http
    acl Safe_ports port 21 # ftp
    acl Safe_ports port 443 # https
    acl Safe_ports port 70 # gopher
    acl Safe_ports port 210 # wais
    acl Safe_ports port 1025-65535 # unregistered ports
    acl Safe_ports port 280 # http-mgmt
    acl Safe_ports port 488 # gss-http
    acl Safe_ports port 591 # filemaker
    acl Safe_ports port 777 # multiling http
    acl CONNECT method CONNECT

    #
    # Recommended minimum Access Permission configuration:
    #
    # Only allow cachemgr access from localhost
    http_access allow manager localhost
    http_access deny manager

    # Deny requests to certain unsafe ports
    http_access deny !Safe_ports

    # Deny CONNECT to other than secure SSL ports
    http_access deny CONNECT !SSL_ports

    # We strongly recommend the following be uncommented to protect innocent
    # web applications running on the proxy server who think the only
    # one who can access services on "localhost" is a local user
    #http_access deny to_localhost

    #
    # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
    #

    # Example rule allowing access from your local networks.
    # Adapt localnet in the ACL section to list your (internal) IP networks
    # from where browsing should be allowed
    http_access allow localnet
    http_access allow localhost
    http_access allow allow_access
    http_access allow allow_dist

    # And finally deny all other access to this proxy
    http_access deny all

    # We recommend you to use at least the following line.
    hierarchy_stoplist cgi-bin ?

    # Uncomment and adjust the following to add a disk cache directory.
    cache_dir aufs /cache0 8000 64 256
    cache_dir aufs /cache1 8000 64 256
    cache_dir aufs /cache2 8000 64 256
    cache_dir aufs /cache3 8000 64 256

    # Leave coredumps in the first cache dir
    coredump_dir /cache0

    # Add any of your own refresh_pattern entries above these.
    refresh_pattern ^ftp: 1440 20% 10080
    refresh_pattern ^gopher: 1440 0% 1440
    refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
    refresh_pattern . 0 20% 4320
  3. config firewall rules
    1. bridge mode
    2. parallel mode
  4. start squid service

===============還===是===分===隔===線===============

startup squid scripts
For Debian:兩種方式。(1)寫在rc.local中。(2)寫在init.d中的script。
  1. vi /etc/rc.local
    新增一行:/bin/sh /path/to/squid/bin/RunCache &
  2. vi /etc/init.d/squid#! /bin/sh -e
    ### BEGIN INIT INFO
    # Provides: squid
    # Required-Start: $network $remote_fs $syslog
    # Required-Stop: $network $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: HTTP Proxy startup script
    # Description: HTTP Proxy startup (http://www.squid-cache.org)
    ### END INIT INFO
    set -e
    . /lib/lsb/init-functions
    export PATH=/sbin:/usr/sbin:/bin:/usr/bin
    test -x /path/to/squid/sbin/squid || exit 0
    test -x /path/to/squid/bin/Runcache || exit 0
    /bin/sh /path/to/squid/bin/RunCache



Wednesday, December 9, 2009

vim 縮排設定

設定縮排:
  • edit /etc/vim/vimrc
set tabstop=1
set shiftwidth=1
PS1.預設值是8
PS2.根據vim的document,tabstop和shiftwidth最好是設成相同的值。

Saturday, September 5, 2009

LUSTRE筆記--1

  • 系統環境
    OS: Debian 5.02a(lenny-2.6.26-2)
    預先安裝套件:gcc-4.1、kernel-package、debhelper、lyx、automake1.7、libsnmp-dev、libreadline5-dev、quilt、fakeroot
    Source Code: KernelSource2.6.22LUSTRE-1.8.0
  • LUSTRE安裝
    • cd /usr/bin
    • ls -al gcc* (確認gcc版本在4.1、指向4.3則rm gcc -> ln -s gcc-4.1 gcc)
    • cd /usr/src
    • tar jxvf linux-2.6.22.tar.bz2
    • ln -s linux-2.6.22 linux
    • tar zxvf lustre-1.6.7.2.tar.gz(此版本的kernel patch是完整的)
    • tar zxvf lustre-1.8.0.tar.gz
    • cd linux
    • ln -s /usr/src/lustre-1.6.7.2/lustre/kernel-patches/patches/ .
    • ln -s /usr/src/lustre-1.6.7.2/lustre/kernel-patches/series/2.6.22-vanzilla.series series
    • quilt -av push
    • rm patches
    • rm series
    • ln -s /usr/src/lustre-1.8.0/lustre/kernel-patches/patches/ .
    • ln -s /usr/src/lustre-1.8.0/lustre/kernel-patches/series/2.6.22-vanzilla.series series
    • cp /usr/src/lustre-1.8.0/lustre/kernel-patches/kernel-configs/kernel-2.6.22-vanzilla-i686.config .config
    • make menuconfig (確認General setup->local version有設定、File System->jbd有選取)
    • make;make modules
    • make install;make modules_install
    • reboot to patched kernel 2.6.22
    • cd /usr/src/lustre-1.8.0
    • ./configure --with-linux=/usr/src/linux/
    • make
    • make install
    • depmod -a
    • reboot to patched kernel
    • modconf (確認lustre、lustre-ldiskfs、jbd的module有被安裝)
    • vi /etc/modules (確認ldiskfs、lnet networks=tcp有加入檔案中)
    • reboot to enable modules
  • LUSTRE設定
    • mkdir -p /lustre-mdt-mgs
    • mkdir -p /lustre-ost0~n
    • mkdir -p /12321
    • mkfs.lustre --fsname=123fs --reformat --mdt --mgs /dev/hdax
    • mount -t lustre /dev/hdax /lustre-mdt-mgs
    • mkfs.lustre --fsname=123fs --reformat --ost --mgsnode=host@tcp0 /dev/hday
    • mount -t lustre /dev/hday /ustre-ost0
    • mount -t lustre host@tcp:/123fs /12321