用Cython编写Wrapper(续)
Reiase
posted @ 2009年10月14日 17:15
in 编程杂项
, 1931 阅读
上次提到的Demo程序没有入口,这次我尝试为那个Demo添加一个Web接口。
服务器端
首先导入上次编译出来的模块:
1 | from a import a |
创建class a的一个实例,并开始主循环:
1 2 | x = a() thread.start_new_thread(x.mainloop,()) |
开始处理Web事务处理,首先导入相应模块:
1 2 3 4 5 | import os from BaseHTTPServer import HTTPServer from CGIHTTPServer import CGIHTTPRequestHandler from SocketServer import ThreadingMixIn from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler |
创建一个XMLRPCServer,处理Web请求,允许用户发送Web请求来修改参数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class a: global x def setparam1(self,v): x.param1 = v def setparam2(self,v): x.param2 = v class myHandler(SimpleXMLRPCRequestHandler,CGIHTTPRequestHandler): pass class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer): pass; serveraddr = ('',8080); srv = ThreadingServer(serveraddr,myHandler); srv.register_instance(a()) srv.register_introspection_functions() srv.serve_forever() |
Python Web编程部分内容可参考《Python网络编程基础》这本书。
客户端
在客户端使用AJAX技术向服务器发送XMLRPC申请,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script language="javascript"> function createXmlRPCRequest(){ try{return new XMLHttpRequest();}catch(e){}; } function sendCall(x,Method,ResponseArea) { var xhr = createXmlRPCRequest(); xhr.open("POST","http://127.0.0.1:8080/RPC2",true); //xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); var sending; sending = "<?xml version=\"1.0\"?>\n"; sending = "<methodCall>\n"; sending = "<methodName>" Method "</methodName>\n"; sending = "<params>"; sending = "<param><value>"; sending = "<i4>" x "</i4>"; sending = "</value></param>"; sending = "</params>\n"; sending = "</methodCall>\n"; xhr.onreadystatechange = function (){handleResponse(xhr,ResponseArea);}; xhr.send(sending); } function handleResponse(xhr,ResponseArea) { var responseOutput = document.getElementById(ResponseArea); responseOutput.innerHTML = xhr.responseText; } </script> </head> <body> <h1>Param1</h1> <form action="#" method="get"> <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="5"/> 5 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="10"/> 10 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="15"/> 15 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="20"/> 20 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="25"/> 25 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="30"/> 30 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam1','Response')" value="100"/> 100 </form> <br\> <div id="Response"></div> <h1>Param2</h1> <form action="#" method="get"> <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="5"/> 5 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="10"/> 10 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="15"/> 15 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="20"/> 20 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="25"/> 25 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="30"/> 30 <input type="radio" name="HTH" onclick = "sendCall(this.value,'setparam2','Response')" value="100"/> 100 </form> <br\> <div id="Response"></div> </body></CENTER> </html> |
Demo程序所有代码可下载:Cython Example.tar.gz