As I said before, we are working on several artificial intelligence agents. Experiments are being performed on amazon AWS server (http://korobov-labs.com), and now we need to make some features available via web services. It is convenient way to integrate some parts of our code into a smart system. We wanted to avoid complex solutions like SOAP or separate REST server launched via Apache or something similar...
So, what about xml-rpc or json-rpc web service? This kind of services is not always a good choice for real production, but good enough for quick experiments or internal communication. Old thing, but works good today. Open your python and just try this client code (at the time server is running):
In three lines we have very convenient call to remote xml-rpc service. Cool thing.
What about server code? Just take a look at this:
Sure, it is insecure. It is not robust or stable enough (I didn't work on it, but the xml-rpc server can be improved really quick). In Python ESSENTIAL REFERENCE Fourth Edition you can find recommendations for enhancements. Just take a look at 524 and 530 pages (xmlrpc Package)
Several ideas are represented in this code snippet (from this book):
It is to be noted that it is cool for heterogeneous systems. There are implementations of XML-RPC client/server classes for .net, java, C++. Believe me, it is easy to write client in many languages because these protocols are wide used.
C# Example (Using 3rd part xml-rpc.net library) :
It is very simple in use. And this is really cool.
You can also take a look at xml data sniffed from tcp packages:
Further reading: Python ESSENTIAL REFERENCE Fourth Edition
So, what about xml-rpc or json-rpc web service? This kind of services is not always a good choice for real production, but good enough for quick experiments or internal communication. Old thing, but works good today. Open your python and just try this client code (at the time server is running):
from xmlrpclib import ServerProxy connect = ServerProxy("http://korobov-labs.com:8001") print connect.classify_text('Put your English text here...')
In three lines we have very convenient call to remote xml-rpc service. Cool thing.
What about server code? Just take a look at this:
from SimpleXMLRPCServer import SimpleXMLRPCServer import cls class ServerClass(): def __init__(self): self.ai_agent = cls.Classifier() def classify_text(self, text): genre = self.ai_agent.document_class(text) return genre server = SimpleXMLRPCServer(("", 8001)) server.register_instance(ServerClass()) server.serve_forever()
Sure, it is insecure. It is not robust or stable enough (I didn't work on it, but the xml-rpc server can be improved really quick). In Python ESSENTIAL REFERENCE Fourth Edition you can find recommendations for enhancements. Just take a look at 524 and 530 pages (xmlrpc Package)
Several ideas are represented in this code snippet (from this book):
try: from xmlrpc.server import (SimpleXMLRPCServer, SimpleXMLRPCRequestHandler) except ImportError: from SimpleXMLRPCServer import (SimpleXMLRPCServer, SimpleXMLRPCRequestHandler) class MaxSizeXMLRPCHandler(SimpleXMLRPCRequestHandler): MAXSIZE = 1024*1024 # 1MB def do_POST(self): size = int(self.headers.get('content-length',0)) if size >= self.MAXSIZE: self.send_error(400,"Bad request") else: SimpleXMLRPCRequestHandler.do_POST(self) s = SimpleXMLRPCServer(('',8080),MaxSizeXMLRPCHandler)
C# Example (Using 3rd part xml-rpc.net library) :
[XmlRpcUrl("http://korobov-labs.com:8001")] public interface IGenreDetector : IXmlRpcProxy { [XmlRpcMethod("classify_text")] string Classify(string text); } private string Detect(string text) { var detector = XmlRpcProxyGen.Create<IGenreDetector>(); return detector.Classify(text); }
It is very simple in use. And this is really cool.
You can also take a look at xml data sniffed from tcp packages:
Further reading: Python ESSENTIAL REFERENCE Fourth Edition
Комментариев нет:
Отправить комментарий