2013年8月1日 星期四

[Python] 使用 Bottle 開發簡易 CGI Prototype @ Ubuntu 12.04

Python Bottle 提供 web server 與簡易的 MVC framework 服務(或許稱不上MVC架構),且 bottle 單純只是一個 python 檔案而已,十分輕巧,姑且不論效能,但應該稱得上是個不錯的測試、製作 prototype 的好工具。

常見的簡易 Web server 用法:

$ cd /path/workdir
$ python -m SimpleHTTPServer


安裝 bottle (單純下載單一檔案):

$ wget --no-check-certificate http://bottlepy.org/bottle.py


撰寫相關程式碼(main.py):

from bottle import Bottle, request, response, run

import json
#import sqlite3
#conn = sqlite3.connect('/tmp/example.db')


app = Bottle()

@app.route('/test')
def test():
        out = { 'status': True }
        #print request.headers
        response.headers['Accept-Ranges'] = 'none'
        return json.dumps(out)

@app.route('/')
def rootTest():
        out = { 'status': True }
        out['request.url'] = request.url
        out['request.urlparts'] = request.urlparts
        out['request.fullpath'] = request.fullpath
        out['request.query_string'] = request.query_string
        out['request.script_name'] = request.script_name
        out['request.content_type'] = request.content_type
        out['request.is_xhr'] = request.is_xhr
        out['request.is_ajax'] = request.is_ajax
        out['request.auth'] = request.auth
        out['request.remote_route'] = request.remote_route
        out['request.remote_addr'] = request.remote_addr
        response.headers['X-My-Resonse-Header'] = 'none'
        return json.dumps(out)

run(app, host='127.0.0.1', port=8000)


執行:

$ python main.py
Bottle v0.12-dev server starting up (using WSGIRefServer())...
Listening on http://127.0.0.1:8000/

Hit Ctrl-C to quit.


如此就有一個包含 web server 跟簡易 APIs 可以被呼叫測試,並且不需去安裝 web server、處理 python cgi 等問題,十分方便啊。

此例執行結果:
提供 http://127.0.0.1:8000/test 位置,並回傳 {"status": true} 資料。

[Linux] 使用 C/C++ 撰寫 FastCGI 與 Nginx 設定筆記 @ Ubuntu 12.04

大概有半年了,跟 fastcgi 與 nginx 的淵源還不斷地繼續著,不過,開始更有機會真的用 C/C++ 寫 Fastcgi 啦,在此簡易筆記用法

/etc/nginx/sites-enabled/fcgidev:

server {
  listen 55688;

  root /tmp/fcgidev;
  client_max_body_size 0;

  location /fcgi {
    fastcgi_pass 127.0.0.1:55699;
    include /path/etc/fastcgi_params;
  }

  location / {
  }
}

用 C 撰寫 (https://github.com/changyy/fastcgi-study/blob/master/fcgi/main.c):

#include <stdlib.h>
#include <fcgi_stdio.h>

#define CGI_HEAD_BODY_SEPARATOR "\r\n\r\n"
#define CGI_CONTENT_TYPE_TEXT_HTML "Content-type: text/html"
#define CGI_CONTENT_TYPE_APPLICATION_JSON "Content-type: application/json"
#define CGI_WELCOME_HEAD CGI_CONTENT_TYPE_TEXT_HTML
#define CGI_WELCOME_BODY \
"<html>\
<head>\
<title>Hello FastCGI</title>\
</head>\
<body>\
<h1>Hello FastCGI</h1>\
</body>\
</html>"

int main(int argc, char** argv)
{
int count = 0;
while(FCGI_Accept() >= 0)
{
printf(
CGI_WELCOME_HEAD
CGI_HEAD_BODY_SEPARATOR
CGI_WELCOME_BODY
);
}
return 0;
}

編譯:

$ gcc -o fcgi-out main.c -lfcgi 

用 C++ 撰寫(https://github.com/changyy/fastcgi-study/blob/master/fcgicc/main.cpp):

#include <cstdlib>
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPResponseHeader.h"
#include "cgicc/HTMLClasses.h"
#include "FCgiIO.hpp"

#define CGI_WELCOME_BODY \
"<html>\
<head>\
<title>Hello FastCGI</title>\
</head>\
<body>\
<h1>Hello FastCGI</h1>\
</body>\
</html>"

int main(int argc, char **argv)
{
if (argc < 2) exit(1);

FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0,0);

while(FCGX_Accept_r(&request) == 0)
{
try
{
cgicc::FCgiIO io(request);
cgicc::Cgicc cgi(&io);

cgicc::HTTPResponseHeader resp("Status:", 200, "OK");
resp.addHeader("Content-Type", "text/html");
io << CGI_WELCOME_BODY;
}
catch (std::exception const &e)
{
}
FCGX_Finish_r(&request);
}
return 0;
}


編譯:

$ g++ -o fcgi-out main.cpp FCgiIO.cpp -lfcgi -lcgicc -lfcgi++

執行:

$ spawn-fcgi -a 127.0.0.1 -n -p 55699 -F 1 -- fcgi-out /tmp

2013年7月29日 星期一

nginx worker process exited on signal 7 @ ARM & LOA

嵌入式就是這麼奇妙!nginx 在 x86 可以穩穩跑、在 ARM-based 的 linux 也能穩穩跑,但是在 LOA 上頭就是會收到 signal 7 訊號而掛掉!

最後則是想要編 debug 訊息來用,意外發現 -O0 時就一切正常了!

故,最佳化這種事,還是小心為妙。

Scripting Nginx with Lua (lua-nginx-module) 筆記 @ Linux

最近常用 nginx 開發服務,接著又碰碰 lua 的部分,十分彈性。接下來,移植到 ARM 裝置後,就開始進入 debug 模式 XD 需要追蹤一些 requests 的流向,又想到 lua 啦

nginx-test.conf

  location /lua {
    default_type 'text/plain';
    content_by_lua '
        local currtime = ngx.localtime()
        local file = io.open("/tmp/nginx_lua.log", "a");
        file:write(currtime, "\\t", ngx.var.uri, "\\n");
        file:write("\\t", ngx.var.args, "\\n");
        file:close();

        ngx.print(currtime, "\\n");
        ngx.print(ngx.var.uri,"\\n");
        ngx.print(ngx.var.args,"\\n");
        --ngx.print(ngx.var.request_body, "\\n");
    ';
  }


/tmp/nginx_lua.log:
2013-07-29 04:19:07     /lua
        a=1&b=c&d


Web:
2013-07-29 04:19:07
/lua
a=1&b=c&d


如此一來,當 request 進來時,可以查看 /tmp/nginx_lua.log 看看到底走了幾個 requests,留意項目:

  • 在 content_by_lua 中,要印出 newline 必須用 ngx.print( "\\n" ) ,但如果是在 access_by_lua_file 中,只需 ngx.print( "\n" ) 即可。
  • 註解是用 "--" 而非用 "#" 或 "//" 開頭

2013年7月27日 星期六

使用微軟 Windows Live 管理中心免費郵件託管服務

livedomain01

想說前陣子買的 domain 該好好把玩一下,就想到 Mail server 服務啦。若曾架過站,應該不難體會維護一台機器、甚至一台 Mail server 是多重的任務了 XD 更別說中毒、被駭之後的處理流程等。所以,就先看看市面上有什麼好用的資源,除非不得已不然就不自己架站 :P

前陣子用過 Google Apps for Business 了,這次就改用微軟 Windows Live 管理中心吧!其實我沒特別討厭微軟,至從我用 MBP 被我操到常常出現多國語言的招呼聲或是瞬間登出等奇異現象後,我覺得微軟其實沒那麼糟 XDD

首先進入 https://domains.live.com 網頁後,點選"馬上開始":


livedomain02

livedomain03

輸入一些資料後,即可進入郵件託管設定頁面,首要之事就是(必須欄位)的設定,其實也真的只要設定這欄就好。

livedomain04

接著就是要去設定 DNS MX record 的部分,由於我是使用 Namecheap 服務,所以可以直接在 Namecheap 網站上設定完,十分便利(可參考 Namecheap - How can I set up MX records for my domain?):

livedomain05

如此一來,在切回 Windows Live 管理中心,點選"立刻更新"後,就會看到畫面更新為以下狀態,可以從 live.com 用自己的 email address 登入囉!

livedomain06

最後一提的,建立好的信箱可以用 POP3 協定收信囉:

POP3: pop3.live.com:995 (是否加密:Yes)
帳號:完整的 Email address (如:user@yourdomain.com)