Hi<br><br>I started to learn Twisted with the O'Reilly Book Twisted Network Programming Essentials<br>I want to do a daemon to listen on port<br>- when a correct sentence is received, the daemon must to check <span id="result_box" class="" lang="en"><span class="hps">by querying</span> <span class="hps">a server</span> <span class="hps">XML_RPC<br>
- if the answer from </span></span><span id="result_box" class="" lang="en"> <span class="hps">XML_RPC</span></span><span id="result_box" class="" lang="en"><span class="hps"> is 1, the HTTP answer will be OK else NOT FOUND<br>
<br>I adapted the basic example HttpEchoProtocol.py<br><br>from twisted.protocols import basic<br>from twisted.web.xmlrpc import Proxy<br>from twisted.internet import protocol, reactor<br>import pdb<br><br>KEYS_ADDR="192.168.0.254"<br>
KEYS_PORT=2101<br><br># URL of the XML-RPC server<br> OF_XMLRPC_URL="<a href="http://127.0.0.1/xmlRpc.rpc.php">http://127.0.0.1/xmlRpc.rpc.php</a>"<br># HTTP Port from which the OF client contact the service<br>
SERVICE_PORT=4080<br># url sent for test: localhost:4080/?sessid=dddddd&key=1&resource=2<br><br>class HttpProtocol(basic.LineReceiver):<br><br> def __init__(self):<br> self.lines = []<br> self.answer = "HTTP/1.0 200 No value"<br>
self.gotRequest = False<br> self.proxy = Proxy(OF_XMLRPC_URL)<br><br> def xmlRpcResponse(self, value):<br> self.xmlRpc = value<br> if self.xmlRpc == 1:<br> self.answer = "HTTP/1.0 200 OK"<br>
print "xmlRpc : ",self.xmlRpc<br> else:<br> self.answer = "HTTP/1.0 406 Invalid key"<br> self.xmlRpc = 0<br> <br> def xmlRpcError(self, error):<br> self.answer = "HTTP/1.0 405 NOT FOUND"<br>
print "Error"<br><br> def lineReceived(self, line):<br> self.lines.append(line)<br> if not line and not self.gotRequest:<br> text = self.lines[0]<br> startText = text.find("sessid")<br>
if startText>0 :<br> endText = text.find("HTTP")<br> text=text[startText:endText]<br> data = text.split("&")<br> sessionId = data[0][data[0].find("=")+1:]<br>
keyId = int(data[1][data[1].find("=")+1:])<br> resourceId = int(data[2][data[2].find("=")+1:])<br> self.proxy.callRemote('checkCommand', sessionId, keyId).addCallbacks(self.xmlRpcResponse, self.xmlRpcError)<br>
self.sendResponse()<br> self.gotRequest = True <br> <br> def sendResponse(self):<br> responseBody = self.answer<br> self.sendLine(self.answer)<br> self.sendLine("Content-Type: text/plain")<br>
self.sendLine("Content-Length: %i" % len(responseBody))<br> self.sendLine("")<br> self.transport.write(responseBody)<br> self.transport.loseConnection()<br><br>factory = protocol.ServerFactory()<br>
factory.protocol = HttpProtocol<br>reactor.listenTCP(SERVICE_PORT, factory)<br>reactor.run()<br><br>XML_RPC answer is correctly </span></span><span id="result_box" class="" lang="en"><span class="hps"> received </span></span><span id="result_box" class="" lang="en"><span class="hps"> (</span></span><span id="result_box" class="" lang="en"><span class="hps">"xmlRpc : 1")</span></span><span id="result_box" class="" lang="en"><span class="hps"> but the HTTP answer is always </span></span><span id="result_box" class="" lang="en"><span class="hps"> "HTTP/1.0 200 No value" (first initialization) <br>
<br>I tried a other example, based on requesthandler,py<br><br>from twisted.web.xmlrpc import Proxy<br>from twisted.web import http<br>from twisted.web.xmlrpc import Proxy<br>from twisted.internet import reactor<br>import pdb<br>
<br><br># HTTP Port from which the OF client contact OpenKeys service<br>SERVICE_PORT=4080<br><br># URL of the XML-RPC server<br>OF_XMLRPC_URL="<a href="http://127.0.0.1/openkeys.rpc.php">http://127.0.0.1/openkeys.rpc.php</a>"<br>
<br>class MyRequestHandler(http.Request):<br><br> xmlRpc = 0<br> def xmlRpcResponse(self, value):<br> self.xmlRpc = value<br> print repr(value)<br> if self.xmlRpc == 1:<br> self.setResponseCode(http.OK)<br>
self.write("<h1>OK</h1>")<br> else:<br> self.setResponseCode(http.NOT_ALLOWED)<br> self.write("<h1>Invalid key</h1>.")<br> self.xmlRpc = 0<br>
self.finish()<br> <br> def xmlRpcError(self, error):<br> print 'error', error<br> self.setResponseCode(http.NOT_FOUND)<br> self.write("<h1>Not Found</h1>")<br>
self.xmlRpc = 0<br> self.finish()<br> <br> def process(self):<br> self.setHeader('Content-Type', 'text/html')<br> data = self.args <br> if 'sessid' in data:<br>
sessionId = data['sessid'][0]<br> keyId = data["key"][0]<br> resourceId = data["resource"][0]<br> print "session = ", sessionId <br>
print "key = ", keyId <br> print "resource = ", resourceId <br> proxy.callRemote('checkCommand', sessionId, keyId).addCallbacks(self.xmlRpcResponse, self.xmlRpcError)<br>
else:<br> print "data = ", data <br> self.setResponseCode(http.NOT_FOUND)<br> self.write("<h1>Not Found</h1>Sorry, no such page.")<br> self.finish()<br>
<br>class MyHttp(http.HTTPChannel):<br> requestFactory = MyRequestHandler<br><br>class MyHttpFactory(http.HTTPFactory):<br> protocol = MyHttp<br><br>proxy = Proxy(OF_XMLRPC_URL)<br>reactor.listenTCP(SERVICE_PORT, MyHttpFactory())<br>
reactor.run()<br><br>But in this case an error occur<br><br>error [Failure instance: Traceback: <class 'xmlrpclib.Fault'>: <Fault -32602: 's<br>erver error. invalid method parameters'><br>C:\python27\lib\site-packages\twisted\internet\posixbase.py:263:_disconnectSelec<br>
table<br>C:\python27\lib\site-packages\twisted\internet\tcp.py:433:connectionLost<br>C:\python27\lib\site-packages\twisted\internet\tcp.py:277:connectionLost<br>C:\python27\lib\site-packages\twisted\web\xmlrpc.py:373:connectionLost<br>
--- <exception caught here> ---<br>C:\python27\lib\site-packages\twisted\web\xmlrpc.py:444:parseResponse<br>C:\python27\lib\xmlrpclib.py:1137:loads<br>C:\python27\lib\xmlrpclib.py:793:close<br>]<br><br>I try to declare the Proxy</span></span><span id="result_box" class="" lang="en"><span class="hps">(OF_XMLRPC_URL) differently, without success<br>
<br>Where is my mistake ....<br>Thanks<br>Bingo<br></span></span>