本文的開發環境是windows 7 + python-2.7.3
Thrift官方主頁:http://thrift.apache.org/
1.下載Thrift源代碼及windows執行程序
下載地址:http://thrift.apache.org/download/
分別下載:thrift-0.9.0.tar.gz 和 Thrift compiler for Windows (thrift-0.9.0.exe)
下載目錄為:c:/download/
2.安裝Thrift庫
注意:安裝Thrift for Python之前,須安裝Python
windows下解壓縮thrift-0.9.0.tar.gz,使用命令行工具進入\thrift-0.9.0\lib\py目錄
>cd $thrift-0.9.0\thrift-0.9.0\lib\py
>setup.py install
3.Thrift生成代碼
Thrift能夠利用自帶的代碼生成工具自動生成程序所需的部分代碼,讓你更關注你的業務部分,而不必關注它是怎么通信的
將已經下載的thrift-0.9.0.exe放到$thrift-0.9.0\thrift-0.9.0\tutorial,並執行代碼生成命令:
thrift-0.7.0.exe –r –gen py tutorial.thrift
4.運行示例程序
進入thrift-0.9.0\tutorial\py目錄,在運行之前,先改一下PythonServer.py
可能是Python或者Thrift的版本問題,導致程序API接口寫法有問題:
transport = TSocket.TServerSocket(port=9090)
更改為:
transport = TSocket.TServerSocket('localhost',port=9090)
執行成功后,結果如下:
Starting the server...
ping()
add(1,1)
calculate(1, Work(comment=None, num1=1, num2=0, op=4))
calculate(1, Work(comment=None, num1=15, num2=10, op=2))
getStruct(1)