空间内容仅为个人学习备份所用!

发布新日志

  • 武汉外企招聘软件开发,软件测试等职位

    2011-06-13 20:40:32

    有没有想回武汉工作的朋友? 从事软件开发或者测试等相关类的工作,各种level的职位都有,福利待遇在武汉算是中上等,想回来的朋友们联系我啊。
  • ruby + rspec + selenium搞定web自动化测试

    2011-06-12 11:14:12

    最近给公司QA做了一期关于自动化测试方面的培训,等有空整理一下把培训内容share出来,供有兴趣的人一起学习。

    大概内容是:
    个人对自动化的理解,对自动化测试框架搭建的看法。

    介绍使用ruby语言,加上rspec框架,借用selenium来快速搭建可以跨平台,支持多浏览器的web自动化测试平台,由于以上使用到的都是免费或者开源的tool,所以可以说是零成本,高效率的一个自动化测试解决方案。另外,还具有易用性,框架搭建好后,以后参与case design的人只需要有简单的ruby知识就可以开始自动化脚本的编写。
  • 行为驱动开发(BDD)

    2011-05-17 22:41:41

    行为驱动开发(BDD)
    转自: CoderZh(CoderZh的技术博客 - 博客园)

    可理解的代码非常重要,测试代码也是如此。在我看来,优秀的测试代码,必须做到一个重要的事情就是保持测试逻辑的清晰。一个完整的测试案例通常包括三个部分:
    1. SetUp
    2. Exercise
    3. Verifiy
    4. TearDown
    一 个测试案例如果能清晰的区分这三个部分,其实已经成功了一半。但是,如果仅仅只是做到这一步,离我们的“可理解的”测试代码还有些距离。在我看来,要做到 测试代码的可理解,首先要做到的是使用简洁清晰的方式表达每个测试案例的测试过程。我们都不希望在一个测试案例中看到一个long long method,因为我们需要去理解,你调用了那么多方法,做了那么多处理,到底是在干啥?当然,我们也不希望看到一个极端情况:一个测试案例中就看到一个 函数调用。这样做确实做到了测试案例表面上的简洁,但是,却失去了表现内部细节的机会。我们需要表述的是测试过程和方法,比如,进行了什么样的操作,输入 了什么样的数据,然后得出了什么样的结果,和预期结果对比怎么样等。如果把这些都封装到一个方法,别人只能理解你可能会做什么,但很难去理解你将如何去 做。其次,在测试案例fail的时候,能够清晰准确的定位问题。极限情况下,一个测试案例里应该只能有一个断言,或者说一个检查点。测试粒度的缩小能够加 快问题的定位。通常,当一个测试案例fail的时候,我们还需要定位到底是测试代码的问题还是被测代码的问题。这是一个烦人的过程,定位问题的人不一定就 是写这段测试代码的人,就算是写这段测试代码的人,也需要时间去理解测试案例干了什么。这时,测试过程及方法的清晰表述就变得尤为重要。
    有的人会 想到通过文档来表述测试过程和方法,在我看来,这不是一个好办法。敏捷开发中的一个思想:“一个过时的文档比没有文档更加糟糕”。为什么需要文档才能表述清楚你的测试过程? 只能说明你的测试代码足够糟糕,无法让人理解。一个糟糕的代码就算当初写下了足够详细的说明文档,随着时间的交替,人员的变更,代码的修改维护,复杂的文 档和代码会变得越来越不同步。导致的结果将会是代码越来越糟糕复杂,文档越来越过时。最好的文档,其实是代码。
    我们先来看一下一个老外写的完整的测试案例代码:
    @Test
    public void shouldBeAbleToEditAPage() {
        Given.thatThe(wiki).wasAbleTo(beAtThe(PointWhereItHasBeen.JUST_INSTALLED));
        And.thatThe(user).wasAbleTo(navigateToTheHomePage());

        When.the(user).attemptsTo(changeTheContent().to("Welcome to Acceptance Test Driven Development"));

        Then.the(textOnTheScreen().ofThe(user)).shouldBe("Welcome to Acceptance Test Driven Development");
    }

    我 之前并没有为这个测试案例做任何说明,相信每个看过这个测试案例的人都能非常容易的理解这个测试案例的行为,甚至是对代码不通的人。因为上面的测试代码使 用了几乎是自然语言的方式,描述了其测试的过程。了解的人一定看出来了,其实,这就是行为驱动开发,Behavior. Driven Development,简称:BDD。

    行为驱动开发(Behavior. Driven Development)

    Behaviour-Driven Development (BDD) is an evolution in the thinking behind TestDrivenDevelopment andAcceptanceTestDrivenPlanning.

    It brings together strands from TestDrivenDevelopment and DomainDrivenDesign into an integrated whole, making the relationship between these two powerful approaches to software development more evident.
    It aims to help focus development on the delivery of prioritised, verifiable business value by providing a common vocabulary (also referred to as a UbiquitousLanguage) that spans the divide between Business and Technology.
    BDD 使用几乎近于自然语言的方式描述了软件的行为过程,因此,可以直接作为软件的需求文档,也可以直接应用到测试中,作为测试的标准文档。我们在做单元测试 时,经常是针对某个函数,或是某个类进行测试,但是被测函数或是被测的类是可能经常变化的,我们的测试案例也需要经常性的随之变化。然后,BDD描述的是软件的整个系统行为,几近于需求文档,可变性大大减小。因此,测试案例不需要做太大变化。同时,这样的测试案例最贴近于需求,贴近于实际的系统行为。
    BDD描述的行为就像一个个的故事(Story),系统业务专家、开发者、测试人员一起合作,分析软件的需求,然后将这些需求写成一个个的故事。开发者负责填充这些故事的内容,测试者负责检验这些故事的结果。通常,会使用一个故事的模板来对故事进行描述:
    As a [X]
    I want [Y]
    so that [Z]

    同样的一个故事,可能会有不同的场景。通过上面的模板描述了故事之后,再通过下面的模板对不同场景进行描述:
    Given some initial context (the givens),
    When an event occurs,
    then ensure some outcomes.

    一个经典的例子就是ATM取款机的例子。故事的描述为:
    Title: Customer withdraws cash
    As a customer,
    I want to withdraw cash from an ATM,
    so that I don’t have to wait in line at the bank.

    作为一个客户,我去ATM取钱,就不需要去排队。同样的故事,会有不同的场景发生:
    Scenario 1: Account is in credit
    Given the account is in credit
    And the card is valid
    And the dispenser contains cash
    When the customer requests cash
    Then ensure the account is debited
    And ensure cash is dispensed
    And ensure the card is returned

    如果你取款的金额比你的存款还多,将是下面的场景:
    Scenario 2: Account is overdrawn past the overdraft limit
    Given the account is overdrawn
    And the card is valid
    When the customer requests cash
    Then ensure a rejection message is displayed
    And ensure cash is not dispensed
    And ensure the card is returned

    有了这样的故事、场景的描述,测试者可以通过一些BDD的测试框架将上面的故事转成测试代码(当然,也可直接由开发来完成,这里说测试者是为了方便理解),开发者实现产品代码,并保证测试代码通过。

    常见的BDD框架

        BOO - Specter: A tool written for the Boo language, a .Net and Mono programming language.

        C - CSpec

        C++ - CppSpec Spec-CPP

        C# .Net - NSpec

        .Net - NBehave

        .Net - NSpecify (incomplete site)

        Delphi - dSpec

        Groovy - GSpec, http://easyb.org easyb, tspec a non-English BDD framework with Thai syntax.
  • PSCP (PuTTY Secure Copy client) PuTTY 提供的文件传输工具

    2011-04-14 21:10:12

    PSCP (PuTTY Secure Copy client)PuTTY 提供的文件传输工具 ,通过 SSH 连接,在两台机器之间安全的传输文件,可以用于任何 SSH(包括 SSH v1、SSH v2) 服务器


    使用说明

    在控制台直接执行 pscp 可以看到帮助

    C:\>pscp
    PuTTY Secure Copy client
    Release 0.58
    Usage: pscp [options] [user@]host:source target
     pscp [options] source [source] [user@]host:target
     pscp [options] -ls [user@]host:filespec
    Options:
    -V       print version information and exit
    -pgpfp   print PGP key fingerprints and exit
    -p       preserve file attributes
    -q       quiet, don't show statistics
    -r       copy directories recursively
    -v       show verbose messages
    -load sessname Load settings from saved session
    -P port  connect to specified port
    -l user  connect with specified username
    -pw passw login with specified password
    -1 -2    force use of particular SSH protocol version
    -4 -6    force use of IPv4 or IPv6
    -C       enable compression
    -i key   private key file for authentication
    -batch   disable all interactive prompts
    -unsafe  allow server-side wildcards (DANGEROUS)
    -sftp    force use of SFTP protocol
    -scp     force use of SCP protocol
    

    可以看出 PSCP 的使用是很简单的,把常用的几个选项说一下:

    • -q 安静模式,传输文件时什么也不显示,否则会显示出文件的传输进度
    • -P port 指定服务器的 SSH 端口,注意这个是大写字母 P,默认是 -P 22,如果主机的 SSH 端口就是 22,就不用指定了
    • -l user 指定以哪个用户的身份登录主机,用户名称也可以和主机名称写在一起,用@分割开,比如:username@server
    • -pw passwd 指定登录时所用的口令为:passwd
    • -C 表示允许压缩传输,提高传输速度


    PSCP 基本用法

    pscp -P 22 -C c:\a.txt username@server:/path/

    例子:

    • c:\>pscp -C c:\a.bat username@server:upload/

    就是把本地的c:\a.bat复制到了主机server上的用户username所在的主目录下的upload子目录中(这个路径可能是 /home/username/upload)

    • c:\>pscp -C c:\a.txt username@server:.

    把本地的 C:\a.txt 复制到主机 server的用户username的主目录下

    • c:\>pscp -C username@server:*.tgz c:\download

    把远程主机server上的用户username 主目录下的所有 *.tgz 文件拷贝到本地的 c:\download目录中,如果 SSH 版本是 SSH v1,那这个命令就会出错。

  • Using PuTTy to copy files from linux server to Windows Desktop

    2011-04-14 20:34:46

    putty uses scp to transfer files over I believe

    If you have already ssh'd into the directory where the file is:

    Code:
    scp [file to be copied] some_user@my_ip:[destination_folder]
    some_user would be your login name and destination folder is pretty obvious

    so as an example on my network
    Code:
    scp file.txt leoniteous@192.168.1.111:/home/leoniteous/Documents
    if not

    Code:
    scp login_name@server_hosting_file:[path_to_file] [destination]
    So at home

    Code:
    scp media_server@192.168.0.199:/media/tv/some_tv.avi /storage
    viola. Over the internet is more complicated by the same idea and I would recommend ssh'ing into the dir where the file is as its the least complicated way

    Assuming you have logged into the school computer from home

    Code:
    scp file.txt login@INTERNET_IP -R [port]:local_ip:[port]:[destination]
    looks like

    Code:
    scp file.txt leoniteous@24.75.119.12 -R 22:192.168.1.111:22:/home/leoniteous/Documents

  • How to call shell script from php that requires SUDO?

    2011-04-11 22:09:11

    How to call shell script. from php that requires SUDO?


    Question:

    I have a file that is a bash script. that requires SUDO to work.

    I can run it from the command line using SUDO but I will be prompted to put in the SUDO password.

    I want to run this script. from php via shell_exec but I if I call SUDO, its not like a command line where I can be prompted for the password. Is there a way to pass the password for sudo with the sudo call?

    How can I do this?


    Answer:

    1:

    Edit the sudoers file (with visudo) and add a rule that allows the web server user to run the command without a password. For example:

    www-data ALL=NOPASSWD: /path/to/script

    2:

        *

          A third, but very insecure method is to read the password from a password file. Warning: This is very insecure, if there's any other possibility, don't do it. And if you do it, try hiding the password file somewhere in your folder hierarchy.

          <?php
          shell_exec('sudo -u root -S bash script.sh < /home/[user]/passwordfile');
          ?>

        *

          And a fourth possibility is to use the NOPASSWD tag in the sudoers file. You should limit this power to the specific commands you need.



  • 使用sudo来允许普通用户使用超级用户权限

    2011-04-11 22:07:22

    sudo是linux下常用的允许普通用户使用超级用户权限的工具。

    它的主要配置文件是sudoers,linux下通常在/etc目录下,如果是solaris,缺省不装sudo的,编译安装后通常在安装目录的etc目录下,不过不管sudoers文件在哪儿,sudo都提供了一个编辑该文件的命令:visudo来对该文件进行修改。强烈推荐使用该命令修改sudoers,因为它会帮你校验文件配置是否正确,如果不正确,在保存退出时就会提示你哪段配置出错的。

    言归正传,下面介绍如何配置sudoers

    首先写sudoers的缺省配置:

    #############################################################
    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #

    # Host alias specification

    # User alias specification

    # Cmnd alias specification

    # Defaults specification

    # User privilege specification
    root    ALL=(ALL) ALL

    # Uncomment to allow people in group wheel to run all commands
    # %wheel        ALL=(ALL)       ALL

    # Same thing without a password
    # %wheel        ALL=(ALL)       NOPASSWD: ALL

    # Samples
    # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
    # %users  localhost=/sbin/shutdown -h now
    ##################################################################

    1. 最简单的配置,让普通用户support具有root的所有权限
    执行visudo之后,可以看见缺省只有一条配置:
    root    ALL=(ALL) ALL
    那么你就在下边再加一条配置:
    support ALL=(ALL) ALL

    这样,普通用户support就能够执行root权限的所有命令

    以support用户登录之后,执行:
    sudo su -
    然后输入support用户自己的密码,就可以切换成root用户了

    2. 让普通用户support只能在某几台服务器上,执行root能执行的某些命令
    首先需要配置一些Alias,这样在下面配置权限时,会方便一些,不用写大段大段的配置。Alias主要分成4种
    Host_Alias
    Cmnd_Alias
    User_Alias
    Runas_Alias

    1) 配置Host_Alias:就是主机的列表
    Host_Alias      HOST_FLAG = hostname1, hostname2, hostname3
    2) 配置Cmnd_Alias:就是允许执行的命令的列表
    Cmnd_Alias      COMMAND_FLAG = command1, command2, command3
    3) 配置User_Alias:就是具有sudo权限的用户的列表
    User_Alias USER_FLAG = user1, user2, user3
    4) 配置Runas_Alias:就是用户以什么身份执行(例如root,或者oracle)的列表
    Runas_Alias RUNAS_FLAG = operator1, operator2, operator3
    5) 配置权限
    配置权限的格式如下:
    USER_FLAG HOST_FLAG=(RUNAS_FLAG) COMMAND_FLAG
    如果不需要密码验证的话,则按照这样的格式来配置
    USER_FLAG HOST_FLAG=(RUNAS_FLAG) NOPASSWD: COMMAND_FLAG

    配置示例:
    ############################################################################
    # sudoers file.
    #
    # This file MUST be edited with the 'visudo' command as root.
    #
    # See the sudoers man page for the details on how to write a sudoers file.
    #

    # Host alias specification
    Host_Alias      EPG = 192.168.1.1, 192.168.1.2

    # User alias specification

    # Cmnd alias specification
    Cmnd_Alias      SQUID = /opt/vtbin/squid_refresh, /sbin/service, /bin/rm

    # Defaults specification

    # User privilege specification
    root    ALL=(ALL) ALL
    support EPG=(ALL) NOPASSWD: SQUID

    # Uncomment to allow people in group wheel to run all commands
    # %wheel        ALL=(ALL)       ALL

    # Same thing without a password
    # %wheel        ALL=(ALL)       NOPASSWD: ALL

    # Samples
    # %users  ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
    # %users  localhost=/sbin/shutdown -h now
    ###############################################################

    其余细节可以参考sudo的中文man文档
    http://www.chinalinuxpub.com/bbs/showthread.php?t=22439


  • Windows环境下Nginx安装配置教程

    2011-03-20 22:14:55

    Nginx是一款和Appahe和IIS一样的HTTP服务器软件,网上对这款由Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的HTTP服务器软件评价很高

    据称国内有几个门户网(新浪、网易、腾讯)都投入了Nginx的怀抱,至于Nginx是不是真的这么有魅力,我非专业人士无法给大家一个测评报道,而Nginx的详细介绍大家可以去百度百科去了解了下。(Nginx百科地址:http://baike.baidu.com/view/926025.htm。)

    虽然非专业人士,但今天给大家带来的是Nginx的Windows环境下的安装配置教程,但是根据前辈们的经验,Nginx运行于Unix或Linux效率会更佳

    1.首先去下载最新版本的Nginx,Nginx下载地址:http://xiazai.zol.com.cn/detail/38/373405.shtml(这是windows版本,用Linux版本的不用我教大家上那下载吧)。

    2.将下载的Nginx解压包解压,就可以看到Nginx的Windows下的主程序“Nginx.exe” 了,一般来说双击鼠标并运行这个程序,那么Nginx就成功安装了。


    这是使用IE浏览器打开“http://127.0.0.1或http://localhost”后的效果图,代表安装成功。

    当然这只代表Nginx安装成功,以下还需要对Nginx进行简单的配置。

    3.进入Nginx主文件夹里的conf文件夹,找到“Nginx.conf”使用记事本之类的软件打开该文件。


    Nginx.conf的第44行(行数如不同请参考上面的Server{)会看到"root html"我们只需后面的html改成自己的web目录就行了。

    最后就是修改一下Nginx.exe为自动启动,因为nginx不是自动启动的,我们只需将其拖动到Windows菜单的启动菜单里就行了。

  • 在Debian/Ubuntu VPS下配置Nginx做反向代理

    2011-03-20 21:21:30

    Debian和Ubuntu都自带了Nginx,用他们来配置Nginx的反向代理,非常方便。

    安装Nginx

    运行如下命令安装并运行Nginx

    apt-get install nginx
    /etc/init.d/nginx start

    然后在浏览器里面访问该IP的80端口,就会看到"Welcome to Nginx!"的信息,这说明Nginx安装完成了!

    配置Nginx做反向代理

    Nginx的缺省站点的配置文件是/etc/nginx/sites-available/default,修改这个文件中的如下部分:
            location / {
                   root   /var/www/nginx-default;
                   index  index.html index.htm;
            }

    修改为:
            location / {
                proxy_pass http://www.google.com/;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }

    然后重启Nginx:

    /etc/init.d/nginx restart

    然后在浏览器里面重新访问该IP上面的80端口,应该就看到google的主页了,反向代理配置成功了

    多域名反向代理配置实例

    在一个VPS上配置多个域名的反向代理,比如我们有两个域名test1.rashost.com和test2.rashost.com,我们希望客户在访问test1.rashost.com的时候出现www.baidu.com的内容,希望客户在访问test2.rashost.com的时候出现www.kernel.org的内容,客户只知道test1.rashost.com和test2.rashost.com的存在,而不知道www.baidu.com和www.kernel.org的存在。

    首先需要把域名test1.rashost.com和test2.rashost.com指向VPS的IP地址。

    然后在/etc/nginx/sites-available 目录下增加两个文件,文件名分别是test1.rashost.com和test2.rashost.com

    test1.rashost.com的文件的内容如下:
    server {
            listen   80;
            server_name  test1.rashost.com;

            location / {
                    proxy_pass http://www.baidu.com/;
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }

    test2.rashost.com的文件的内容如下:
    server {
            listen   80;
            server_name  test2.rashost.com;

            location / {
                    proxy_pass http://www.kernel.org/;
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    }

    然后运行命令:

    cd /etc/nginx/sites-enabled
    ln -sf /etc/nginx/sites-available/test1.rashost.com .ln -sf /etc/nginx/sites-available/test2.rashost.com .
    /etc/init.d/nginx restart

    这时候在浏览器里面访问test1.rashost.com将会出现www.baidu.com的内容,访问test2.rashost.com将会出现www.kernel.org的内容。

  • ubuntu 上安装 Nginx

    2011-03-20 21:17:27

    安装nginx

     

    sudo apt-get install nginx

     

    Ubuntu安装之后的文件结构大致为:

    所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/nginx/sites-available下

    程序文件在/usr/sbin/nginx

    日志放在了/var/log/nginx中

    并已经在/etc/init.d/下创建了启动脚本nginx

    默认的虚拟主机的目录设置在了/var/www/nginx-default

     

    启动nginx

    sudo /etc/init.d/nginx start
  • nginx配置详细说明

    2011-03-20 21:11:08

    配置说明----------------------------
    #运行用户
    user nobody nobody;
    #启动进程
    worker_processes 2;
    #全局错误日志及PID文档
    error_log logs/error.log notice;
    pid        logs/nginx.pid;
    #工作模式及连接数上限
    events {
    use epoll;
    worker_connections      1024;
    }
    #设定http服务器,利用他的反向代理功能提供负载均衡支持
    http {
    #设定mime类型
    include      conf/mime.types;
    default_type application/octet-stream;
    #设定日志格式
    log_format main        '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$gzip_ratio"';
    log_format download '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $bytes_sent '
    '"$http_referer" "$http_user_agent" '
    '"$http_range" "$sent_http_content_range"';
    #设定请求缓冲
    client_header_buffer_size    1k;
    large_client_header_buffers 4 4k;
    #开启gzip模块
    gzip on;
    gzip_min_length 1100;
    gzip_buffers    4 8k;
    gzip_types      text/plain;
    output_buffers 1 32k;
    postpone_output 1460;
    #设定access log
    access_log logs/access.log main;
    client_header_timeout 3m;
    client_body_timeout    3m;
    send_timeout          3m;
    sendfile                on;
    tcp_nopush              on;
    tcp_nodelay            on;
    keepalive_timeout 65;
    #设定负载均衡的服务器列表
    upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
    server 192.168.8.1:3128 weight=5;
    server 192.168.8.2:80 weight=1;
    server 192.168.8.3:80 weight=6;
    }
    #设定虚拟主机
    server {
    listen          80;
    server_name    192.168.8.1
    www.yejr.com
    ;
    charset gb2312;
    #设定本虚拟主机的访问日志
    access_log logs/www.yejr.com.access.log main;
    #假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid
    #假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好
    location ~ ^/(img|js|css)/ {
    root    /data3/Html;
    expires 24h;
    }
    #对 "/" 启用负载均衡
    location / {
    proxy_pass      http://mysvr;
    proxy_redirect          off;
    proxy_set_header        Host $host;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 90;
    proxy_send_timeout      90;
    proxy_read_timeout      90;
    proxy_buffer_size      4k;
    proxy_buffers          4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    }
    #设定查看Nginx状态的地址
    location /NginxStatus {
    stub_status            on;
    access_log              on;
    auth_basic              "NginxStatus";
    auth_basic_user_file conf/htpasswd;
    }
    }
    }
    备注:conf/htpasswd 文档的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:
    3.) 查看 Nginx 运行状态
    输入地址
    http://192.168.8.1/NginxStatus/
    ,输入验证帐号密码,即可看到类似如下内容:
    Active connections: 328
    server accepts handled requests
    9309    8982        28890
    Reading: 1 Writing: 3 Waiting: 324
    第一行表示现在活跃的连接数
    第三行的第三个数字表示Nginx运行到

  • 使用 Nginx 提升网站访问速度

    2011-03-20 21:06:20

    Nginx 简介

      Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。 Igor 将源代码以类 BSD 许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

      根据最新一期(08 年 6 月份)的 NetCraft 调查报告显示,已经有超过两百万的主机使用了 Nginx,这个数字超过了另外一个轻量级的 HTTP 服务器 lighttpd, 排名第四,并且发展迅速。下面是这份报告的前几名的报表:

    产品网站数
    Apache84,309,103
    IIS60,987,087
    Google GFE10,465,178
    Unknown4,903,174
    nginx2,125,160
    Oversee1,953,848
    lighttpd1,532,952

      关于这期调查报告的更详细信息请看下面链接:

      http://survey.netcraft.com/Reports/200806/

      下图是最近几个月使用 Nginx 和 lighttpd 的网站数比较

      图 1. 最近几个月使用 Nginx 和 lighttpd 的网站数比较

    使用 Nginx 提升网站访问速度

      使用 Nginx 前必须了解的事项

      目前官方 Nginx 并不支持 Windows,您只能在包括 Linux、UNIX、BSD 系统下安装和使用;

      Nginx 本身只是一个 HTTP 和反向代理服务器,它无法像 Apache 一样通过安装各种模块来支持不同的页面脚本,例如 PHP、CGI 等;

      Nginx 支持简单的负载均衡和容错;

      支持作为基本 HTTP 服务器的功能,例如日志、压缩、Byte ranges、Chunked responses、SSL、虚拟主机等等,应有尽有。

      在 Linux 下安装 Nginx

      为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包。您可以到 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

    # wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
    # tar zxvf pcre-7.7.tar.gz
    # cd pcre-7.7
    # ./configure
    # make
    # make install

      接下来安装 Nginx,Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /opt/nginx 目录下的详细步骤:

    # wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
    # tar zxvf nginx-0.6.31.tar.gz
    # cd nginx-0.6.31
    # ./configure --with-http_stub_status_module –prefix=/opt/nginx
    # make
    # make install

      其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。

      安装成功后 /opt/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

      常用的 Nginx 参数和控制

      程序运行参数

      Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:

      -c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。

      -t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

      -v:显示 nginx 版本号。

      -V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。

      例如我们要测试某个配置文件是否书写正确,我们可以使用以下命令

    sbin/nginx – t – c conf/nginx2.conf

      通过信号对 Nginx 进行控制

      Nginx 支持下表中的信号:

    信号名作用描述
    TERM, INT快速关闭程序,中止当前正在处理的请求
    QUIT处理完当前请求后,关闭程序
    HUP重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
    USR1重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
    USR2平滑升级可执行程序
    WINCH从容关闭工作进程

      有两种方式来通过这些信号去控制 Nginx,第一是通过 logs 目录下的 nginx.pid 查看当前运行的 Nginx 的进程 ID,通过 kill – XXX <pid> 来控制 Nginx,其中 XXX 就是上表中列出的信号名。如果您的系统中只有一个 Nginx 进程,那您也可以通过 killall 命令来完成,例如运行 killall – s HUP nginx 来让 Nginx 重新加载配置。

      配置 Nginx

      先来看一个实际的配置文件:

    user nobody;# 工作进程的属主
    worker_processes 4;# 工作进程数,一般与 CPU 核数等同
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;
    #pid    logs/nginx.pid;
    events {
      use epoll;#Linux 下性能最好的 event 模式
      worker_connections 2048;# 每个工作进程允许最大的同时连接数
    }
    http {
      include    mime.types;
      default_type application/octet-stream;
      #log_format main '$remote_addr - $remote_user [$time_local] $request '
      #         '"$status" $body_bytes_sent "$http_referer" '
      #         '"$http_user_agent" "$http_x_forwarded_for"';
      #access_log off;
      access_log logs/access.log;# 日志文件名
      sendfile    on;
      #tcp_nopush   on;
      tcp_nodelay   on;
      keepalive_timeout 65;
      include    gzip.conf;
      
      # 集群中的所有后台服务器的配置信息
      upstream tomcats {
       server 192.168.0.11:8080 weight=10;
       server 192.168.0.11:8081 weight=10;
       server 192.168.0.12:8080 weight=10;
       server 192.168.0.12:8081 weight=10;
       server 192.168.0.13:8080 weight=10;
       server 192.168.0.13:8081 weight=10;
      }
      server {
        listen    80;#HTTP 的端口
        server_name localhost;
        charset utf-8;
        #access_log logs/host.access.log main;
       location ~ ^/NginxStatus/ {
        stub_status on; #Nginx 状态监控配置
        access_log off;
       }
       location ~ ^/(WEB-INF)/ {
        deny all;
       }
      
       location ~ .(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|
       zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ {
           root /opt/webapp;
        expires 24h;
        }
        location / {
        proxy_pass http://tomcats;# 反向代理
        include proxy.conf;
        }
        error_page 404 /html/404.html;
        # redirect server error pages to the static page /50x.html
        #
       error_page 502 503 /html/502.html;
        error_page 500 504 /50x.html;
        location = /50x.html {
          root  html;
        }
      }
    }

      Nginx 监控

      上面是一个实际网站的配置实例,其中灰色文字为配置说明。上述配置中,首先我们定义了一个 location ~ ^/NginxStatus/,这样通过 http://localhost/NginxStatus/ 就可以监控到 Nginx 的运行信息,显示的内容如下:

    Active connections: 70
    server accepts handled requests
    14553819 14553819 19239266
    Reading: 0 Writing: 3 Waiting: 67

      NginxStatus 显示的内容意思如下:

      active connections – 当前 Nginx 正处理的活动连接数。

      server accepts handled requests -- 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。

      reading -- nginx 读取到客户端的 Header 信息数。

      writing -- nginx 返回给客户端的 Header 信息数。

      waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

      静态文件处理

      通过正则表达式,我们可让 Nginx 识别出各种静态文件,例如 images 路径下的所有请求可以写为:

    location ~ ^/images/ {
      root /opt/webapp/images;
    }

      而下面的配置则定义了几种文件类型的请求处理方式。

    location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)$ {
      root /opt/webapp;
      expires 24h;
    }

      对于例如图片、静态 HTML 文件、js 脚本文件和 css 样式文件等,我们希望 Nginx 直接处理并返回给浏览器,这样可以大大的加快网页浏览时的速度。因此对于这类文件我们需要通过 root 指令来指定文件的存放路径,同时因为这类文件并不常修改,通过 expires 指令来控制其在浏览器的缓存,以减少不必要的请求。 expires 指令可以控制 HTTP 应答中的“ Expires ”和“ Cache-Control ”的头标(起到控制页面缓存的作用)。您可以使用例如以下的格式来书写 Expires:

    expires 1 January, 1970, 00:00:01 GMT;
    expires 60s;
    expires 30m;
    expires 24h;
    expires 1d;
    expires max;
    expires off;

      动态页面请求处理

      Nginx 本身并不支持现在流行的 JSP、ASP、PHP、PERL 等动态页面,但是它可以通过反向代理将请求发送到后端的服务器,例如 Tomcat、Apache、IIS 等来完成动态页面的请求处理。前面的配置示例中,我们首先定义了由 Nginx 直接处理的一些静态文件请求后,其他所有的请求通过 proxy_pass 指令传送给后端的服务器(在上述例子中是 Tomcat)。最简单的 proxy_pass 用法如下:

    location / {
      proxy_pass    http://localhost:8080;
      proxy_set_header X-Real-IP $remote_addr;
    }

      这里我们没有使用到集群,而是将请求直接送到运行在 8080 端口的 Tomcat 服务上来完成类似 JSP 和 Servlet 的请求处理。

      当页面的访问量非常大的时候,往往需要多个应用服务器来共同承担动态页面的执行操作,这时我们就需要使用集群的架构。 Nginx 通过 upstream 指令来定义一个服务器的集群,最前面那个完整的例子中我们定义了一个名为 tomcats 的集群,这个集群中包括了三台服务器共 6 个 Tomcat 服务。而 proxy_pass 指令的写法变成了:

    location / {
      proxy_pass    http://tomcats;
      proxy_set_header X-Real-IP $remote_addr;
    }

      在 Nginx 的集群配置中,Nginx 使用最简单的平均分配规则给集群中的每个节点分配请求。一旦某个节点失效时,或者重新起效时,Nginx 都会非常及时的处理状态的变化,以保证不会影响到用户的访问。

      总结

      尽管整个程序包只有五百多 K,但麻雀虽小、五脏俱全。 Nginx 官方提供的各种功能模块应有尽有,结合这些模块可以完整各种各样的配置要求,例如:压缩、防盗链、集群、FastCGI、流媒体服务器、Memcached 支持、URL 重写等等,更关键的是 Nginx 拥有 Apache 和其他 HTTP 服务器无法比拟的高性能。您甚至可以在不改变原有网站的架构上,通过在前端引入 Nginx 来提升网站的访问速度。

      本文只是简单介绍了 Nginx 的安装以及常见的基本的配置和使用,更多关于 Nginx 的信息请阅读文章后面的参考资源。在这里要非常感谢我的朋友——陈磊(chanix@msn.com),他一直在做 Nginx 的中文 WIKI(http://wiki.codemongers.com/NginxChs),同时也是他介绍给我这么好的一款软件。

      如果您的网站是运行在 Linux 下,如果您并没有使用一些非常复杂的而且确定 Nginx 无法完成的功能,那您应该试试 Nginx 。

  • nginx upstream的五种分配方式

    2011-03-20 21:04:22

    nginx upstream的五种分配方式

    nginx的upstream目前支持5种方式的分配

      1、轮询(默认)

      每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

      2、weight

      指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

      例如:

    upstream bakend {
    server 192.168.0.14 weight=10;
    server 192.168.0.15 weight=10;
    }

      3、ip_hash

      每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

      例如:

    upstream bakend {
    ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
    }

      4、fair(第三方)

      按后端服务器的响应时间来分配请求,响应时间短的优先分配。

    upstream backend {
    server server1;
    server server2;
    fair;
    }

      5、url_hash(第三方)

      按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

      例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

    upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
    }

      tips:

      upstream bakend{#定义负载均衡设备的Ip及设备 状态

    ip_hash;
    server 127.0.0.1:9090 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6060;
    server 127.0.0.1:7070 backup;
    }

      在需要使用负载均衡的server中增加

      proxy_pass http://bakend/;

      每个设备的状态设置为:

      1.down 表示单前的server暂时不参与负载

      2.weight 默认为1.weight越大,负载的权重就越大。

      3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

      4.fail_timeout:max_fails次失败后,暂停的时间。

      5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

      nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

      client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug

      client_body_temp_path 设置记录文件的目录 可以设置最多3层目录

      location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

  • Oracle学习入门

    2011-03-20 16:05:57

    Introduction

    http://www.oracle.com/pls/db102/portal.all_books
    Details

    Dba:

        * http://st-curriculum.oracle.com/obe/db/10g/r1/2day_dba/index.html
        * Introduction
        * Installing Oracle and Building the Database
        * Getting Started with Oracle Enterprise Manager
        * Configuring the Network Environment
        * Managing the Oracle Instance
        * Managing Database Storage Structures
        * Administering Users and Security
        * Managing Schema Objects
        * Performing Backup and Recovery
        * Monitoring and Tuning the Database
        * Managing Oracle Software

    SQL Reference:

        * http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/toc.htm

    Authentication of training

  • BPM

    2010-11-15 21:44:13

    http://wenku.baidu.com/view/b0da226648d7c1c708a145ba.html
  • BPM介绍

    2010-11-15 21:39:55

    转载  BPM介绍 收藏

    Business Process Management(业务流程管理).是一套达成企业各种业务环节整合的全面管理模式.实现跨应用、跨部门、跨合作伙伴与客户的企业运作.BPM通常以 Internet方式实现信息传递、数据同步、业务监控和企业业务流程的持续升级优化.

    BPM也可以是(Business Process Modeling)业务流程建模.
    是对业务流程进行表述的方式,它是过程分析与重组的重要基础.BPM是一套设计,执行,管理及监控业务流程的技术和标准.一个业务流程是指为了实现某种业务目的行为(盒子)---每个盒子代表一个人的操作,一个内部系统,或一个合作公司的流程----的流程或一系列动作.

    几年前,BPM叫作工作流(workflow)用来管理和驱动在公司内部大型人性化和纸制流程的组件.
    http://www.itisedu.com/manage/Upload/image/2006310161947726.gif
     在这个体系结构的核心部位是一个执行流程的运行时引擎,其流程的源码是由基于XML的BPEL语言写成,BPEL是当今最著名、广泛应用的BPM标准,及最优秀的BPM执行语言。这些流程是由业务和技术分析家使用支持可视化流程图语言BPMN——最好的BMP图形语言——的图形编辑器设计出来的。此编辑器包括一个导出器,可以从BPMN图生成BPEL代码(之后部署到引擎)。(在当前许多Java开发工具中,BPMN到BPEL的流程与UML到Java的流程相类似。)

          人和计算机的交互驱动引擎里流程的执行。人这个参与者使用一个图形化工作列表应用程序浏览并执行未执行完毕的手工工作(在流程运行的引擎里)。依附于公司网络的但在引擎地址空间外的内部IT系统,被储如web服务,j2EE,或COM的集成技术,通过XML作为选用的消息格式所访问;用编成语言如 java、C#写出的内部交互可以是更轻便的内嵌代码片断。外部交互是典型的基于web服务的通信,由编排控制,例如那些用新兴的XML语言——WS- CDL 这个领先的编排语言所创作出的外部交互。虽然编排描述了多个参与者流程交互(在business-to-business电子商务里很典型)的整体、引人注意的视图,但是编排工具包可以用来生成一个基本的BPMN模型,其可以捕捉某个特定参与者流程所要求的通信,同时这个工具还可以验证一个给定的流程是否满足编排的要求。(WS-CDL文献建议由WS-CDL生成BPEL而不是BPMN。但是在现在的体系结构中,BPMN作为一种设计语言是一个必要的间接层。)

          BPM系统管理员里利用一个图形化的监视控制台来维护和跟踪引擎流程的状态。控制台使用一种管理语言与引擎衔接。实时引擎将流程状态持久化到数据库,控制台直接与数据库碰面,而不是用管理语言来沟通。运行时引擎将流程状态持久化到数据库,控制台直接与数据库碰面而不是使用管理语言来专门执行流程的请求。监控构造也支持业务活动监控(Business Activity Monitoring (BAM))或者仪表板式的业务监控。

    原文地址:
    http://www.onjava.com/pub/a/onjava/2005/07/20/businessprocessmodeling.html
    中文地址:
    http://www.matrix.org.cn/resource/article/43/43803_Business_Process_Modeling.html

  • python 访问 sqlite 数据库

    2010-06-25 14:55:03

    1 首先去www.sqlite.org下载一个sqlite,它是一个嵌入式数据库,没有服务器的概念,windows版的就是一个exe,自己把它放到一个合适的目录里,然后把这个目录加入系统的path变量.
       建立数据库:
          XP版本:sqlite3.exe test.db
          Linux版本:./sqlite3.bin test.db

    2.然后去找个pysqlite,这是python访问sqlite的接口,地址在这里 : http://initd.org/tracker/pysqlite
    目前针对不同的python版本,pysqlite有两个版本:2.3和2.4,请根据自己的python版本选用.
    3.然后就可以打开自己喜欢的编辑器,写一段测试代码了.
    4.中文处理要注意的是sqlite默认以utf-8编码存储.
    5.另外要注意sqlite仅支持文件锁,换句话说,它对并发的处理并不好,不推荐在网络环境使用,适合单机环境;

    import pysqlite2.dbapi2 as sqlite
        
    def runTest():
        cx = sqlite.connect('test.db')
        cu = cx.cursor()
        
        #create
        cu.execute('''create table catalog(
            id integer primary key,
            pid integer,
            name varchar(10) unique
            )''')

        #insert
        cu.execute('insert into catalog values(0,0,"张小山")')
        cu.execute('insert into catalog values(1,0,"hello")')
        cx.commit()
        
        #select
        cu.execute('select * from catalog')
        print '1:',
        print cu.rowcount
        rs = cu.fetchmany(1)
        print '2:',
        print rs
        rs = cu.fetchall()
        print '3:',
        print rs
        
        #delete
        cu.execute('delete from catalog where id = 1 ')
        cx.commit()
        
        
        cu.execute('select * from catalog')
        rs = cu.fetchall()
        print '4:',
        print rs
        
        #select count
        cu.execute("select count(*) from catalog")
        rs = cu.fetchone()
        print '5:',
        print rs
        cu.execute("select * from catalog")
        cu.execute('drop table catalog')

    if __name__ == '__main__':
        runTest()
  • sqlite简明教程

    2010-06-25 14:52:40

    本文的主要目的是作为一个入门级教程,教你一些如何使用PySqlite来操作 Sqite 的一些基本的语句,更详细的还要去参考想应的文档以及编写相应的测试程序。希望本文对你有帮助。

    我以前的Blog sqlite一个轻巧的数据库

    PySqlite的主页地址:http://pysqlite.sourceforge.net/ 上面有关于使用PySqlite的文档

    一、安装

    去PySqlite主页上下载安装包,有windows的版本,现支持 Python 2.2和2.3版本。

    二、创建数据库/打开数据库

    Sqlite使用文件作为数据库,你可以指定数据库文件的位置。

    >>> import sqlite
    >>> cx = sqlite.connect("d:/test.db", encoding='cp936')

    使用sqlite的connect可以创建一个数据库文件,上面我指明了路径。当数据库文件不存在的时候,它会自动创建。如果已经存在这个文件,则打开这个文件。encoding指明保存数据所使用的编码,这里cp936是 Python 中自带的编码,其实就是GBK编码。cx为数据库连接对象。

    三、操作数据库的基本对象

    3.1 数据库连接对象

    象前面的cx就是一个数据库的连接对象,它可以有以下操作:

    • commit()--事务提交
    • rollback()--事务回滚
    • close()--关闭一个数据库连接
    • cursor()--创建一个游标

     3.2 游标对象

    所有sql语句的执行都要在游标对象下进行。

    cu = cx.cursor()

    这样定义了一个游标。游标对象有以下的操作:

    • execute()--执行sql语句
    • executemany--执行多条sql语句
    • close()--关闭游标
    • fetchone()--从结果中取一条记录
    • fetchmany()--从结果中取多条记录
    • fetchall()--从结果中取出多条记录
    • scroll()--游标滚动

    关于对象的方法可以去 Python 主页上查看DB API的详细文档。不过PySqlite到底支持DB API到什么程序,我就不知道了。我列出的操作都是支持的,不过我不是都使用过。

    四、使用举例

    4.1 建库

    前面已经有了,不再重复。(这些例子,如果你有兴趣,可以直接在Python的交互环境下试试)

    4.2 建表

    >>> cu=cx.cursor()
    >>> cu.execute("""create table catalog (
       id integer primary key,
       pid integer,
       name varchar(10) UNIQUE
      )""")

    上面语句创建了一个叫catalog的表,它有一个主键id,一个pid,和一个name,name是不可以重复的。

    关于sqlite支持的数据类型,在它主页上面的文档中有描述,可以参考:Version 2 DataTypes 

    4.3 insert(插入)

    >>> cu.execute("insert into catalog values(0, 0, 'name1')")
    >>> cu.execute("insert into catalog values(1, 0, 'hello')")
    >>> cx.commit()

    如果你愿意,你可以一直使用cu游标对象。注意,对数据的修改必须要使用事务语句:commit()或rollback(),且对象是数据库连接对象,这里为cx。

    4.4 select(选择)

    >>> cu.execute("select * from catalog")
    >>> cu.fetchall()
    [(0, 0, 'name2'), (1, 0, 'hello')]

    fetchall()返回结果集中的全部数据,结果为一个tuple的列表。每个tuple元素是按建表的字段顺序排列。注意,游标是有状态的,它可以记录当前已经取到结果的第几个记录了,因此,一般你只可以遍历结果集一次。在上面的情况下,如果执行fetchone()会返回为空。这一点在测试时需要注意。

    >>> cu.execute("select * from catalog where id = 1")
    >>> cu.fetchone()
    (1, 0, 'hello')

    对数据库没有修改的语句,执行后不需要再执行事务语句。

    4.5 update(修改)

    >>> cu.execute("update catalog set name='name2' where id = 0")
    >>> cx.commit()
    >>> cu.execute("select * from catalog")
    >>> cu.fetchone()
    (0, 0, 'name2')

    4.6 delete(删除)

    >>> cu.execute("delete from catalog where id = 1")
    >>> cx.commit()
    >>> cu.execute("select * from catalog")
    >>> cu.fetchall()
    [(0, 0, 'name2')]

    以上是关于如何使用PySqlite来操作Sqlite的简单示例。

    五、后记

    以上都是可以在交互环境下可以运行的,有兴趣可以试一试。现在Sqlite已经升级到3.0.2(beta)了。在我写上一个Blog的时候,我下载的还是2.8.13,变化挺大的了。而且它的主页也进行了改版。

  • 关于切换Mac os自带的几个python version

    2009-11-18 11:52:07

    关于切换Mac os自带的几个python version

    1. 单纯切换Mac os自带的几个python version, 可以使用以下命令:
    defaults write com.apple.versioner.python Version 2.5

    Note: 只是切换了当前python运行的版本而已。系统环境变量设置仍然是系统默认的.

    2. 重新安装python并配置环境指向新安装的python version

    Leopard默认带了python的环境,但是版本有点老,是python2.5.1的,主流版本是2.5.4(python3太新,大部分类库还不支持,暂时不考虑)

    下面将介绍如何安装配置一个新的python开发环境:

    安装python2.5.4
    点击 http://www.python.org/ftp/python/2.5.4/Python-2.5.4.tgz 下载2.5.4的源码版进入解压缩后的目录运行
    1)./configure
    2)make
    3)sudo make install
    完成后,python将装在/usr/local中,/usr/local/bin中有python的相关可执行文件,python的库在/usr/local/lib/python2.5中
    删掉/usr/bin中旧python的符号链接,sudo rm py*,建立新的符号链接指向刚刚安装的python2.5.4
    sudo ln -s /usr/local/bin/python /usr/bin/python
    sudo ln -s /usr/local/bin/pydoc /usr/bin/pydoc
    sudo ln -s /usr/local/bin/pythonw /usr/bin/pythonw

     

    安装easy_install
    os x默认有一个easy_install工具,但它的package安装路径是指向原来的python2.5.1的。我们需要重新安装easy_install工具(setuptools)
    下载setuptools的源码 ,在你的个人目录中新建.pydistutils.cfg文件,即vim ~/.pydistutils.cfg
    添加如下内容
    [install]
    install_lib = /usr/local/lib/python2.5/site-packages
    install_scripts = /usr/bin
    进入setuptools的目录,运行 python setup.py build, python setup.py install

     

     

     

  • install Oracle and cx_oracle on Mac

    2009-11-08 09:21:59

    steps

    Oracle InstantClient installation
    $ unzip instantclient-basic-macosx-10.1.0.3.zip
    $ unzip instantclient-sqlplus-macosx-10.1.0.3.zip
    $ cd instantclient10_1
    $ ln -s libclntsh.dylib.10.1 libclntsh.dylib
    $ ln -s libocci.dylib.10.1 libocci.dylib
    $ cd..
    $ sudo mkdir /usr/local/oracle
    $ sudo mv instantclient10_1 /usr/local/oracle/instantclient10_1
    $ sudo pico /etc/profile
    (add)

    #Oracle settings
    ORACLE_HOME=”/usr/local/oracle”
    export ORACLE_HOME

    DYLD_LIBRARY_PATH=”/usr/local/oracle/instantclient10_1″
    export DYLD_LIBRARY_PATH

    SQLPATH=”/usr/local/oracle/instantclient10_1″
    export SQLPATH

    export PATH=”$PATH:/usr/local/oracle/instantclient10_1″

    $ source /etc/profile
    $ sqlplus
    $ sudo mkdir -p /b/729/rdbms/lib/
    $ sudo ln -s /usr/local/oracle/instantclient10_1/libclntsh.dylib.10.1 /b/729/rdbms/lib/libclntsh.dylib.10.1

    cx_Oracle installation

    $ cd cx_Oracle-4.2/
    $ sudo python setup.py build
    $ sudo python setup.py install

    Important link:

    From: http://pedro.emanuel.familiasalgado.org/?p=103

     

     

211/212>
Open Toolbar