首先,這個東東貌似只有windows上才能實現,所以,需要部署windows下的相關環境
1、需要安裝python
2、安裝python的win32com的lib,下載地址:
http://sourceforge.net/projects/pywin32/files/
3、安裝本地的web容器,我用的apache的tomcat
4、編寫例子:
import uuid
import win32com.server.register
class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
#_reg_clsid_ = "{A6688635-62F5-41cb-AF54-CBA84C2F0F86}"
_reg_clsid = "{"+uuid.uuid1()+"}"
def SplitString(self, val):
return "Hello world ", val
if __name__ == '__main__':
print "Registering COM server..."
win32com.server.register.UseCommandLine(PythonUtilities)
將這個文件保存到tomcat的webapps下的一個目錄中
5、編寫測試的html頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<SCRIPT LANGUAGE="JavaScript">
window.onload = function(){
var obj = new ActiveXObject("Python.Utilities");
alert(obj.SplitString("Hello world"));
}
</SCRIPT>
</head>
<body>
</body>
</html>
放到python文件的同一個目錄
6、到命令行執行該python 代碼
7、再請求該網頁