3.1.1.Evolution of REST/JSON API
Web service
This is used for easy communication over a network between two electronic devices
RPC, XML-RPC -> 2003 SOAP (XML-base) -> Google & Yahoo offered JSON/RPC, REST in 2006
RPC
遠程調用框架
從一台機器 (客戶端)上通過參數傳遞調用另一台機器 (服務器)上的一個函數或方法
使用形式上像調用本地函數 (或方法)一樣調用遠程函數 (或方法)
RPC風格的web service跨語言性不佳
XML-RPC, JSON-RPC
XML-RPC透過向裝置了這個協定的伺服器發出HTTP請求。發出請求的用戶端一般都是需要向遠端系統要求呼叫的軟體
<?xml version="1.0"?> <methodCall> <methodName>examples.getStateName</methodName> <params> <param> <value><i4>40</i4></value> </param> </params> </methodCall>
服務器端
from xmlrpc.server import SimpleXMLRPCServer class KeyValueServer: _rpc_methods_ = ['get', 'set', 'delete', 'exists', 'keys'] def __init__(self, address): self._data = {} self._serv = SimpleXMLRPCServer(address, allow_none=True) for name in self._rpc_methods_: self._serv.register_function(getattr(self, name)) def get(self, name): return self._data[name] def set(self, name, value): self._data[name] = value def delete(self, name): del self._data[name] def exists(self, name): return name in self._data def keys(self): return list(self._data) def serve_forever(self): self._serv.serve_forever() # Example if __name__ == '__main__': kvserv = KeyValueServer(('', 15000)) kvserv.serve_forever()
客戶端
>>> from xmlrpc.client import ServerProxy >>> s = ServerProxy('http://localhost:15000', allow_none=True) >>> s.set('foo', 'bar') >>> s.set('spam', [1, 2, 3]) >>> s.keys() ['spam', 'foo'] >>> s.get('foo') 'bar' >>> s.get('spam') [1, 2, 3] >>> s.delete('spam') >>> s.exists('spam') False >>>
SOAP (Simple Object Access Protocol)
數據交換協議規範
XML based standard, Document風格的web service有以下問題:
Issues of XML based Protocol
XML is heavy in terms of network traffic, large payload
Parsing of XML is CPU & memory intensive
Slow performance of XML on browser front end not desired
Mobile devices battery performance reduced due to XML
Standards & versions of standards caused confusion and complexity
Mobile and Single page application developers were not happy
跨平台, 跨語言
Defined:
structure of the messages exchanged, security, transaction, other standards
POST /WebServices/WeatherWebService.asmx HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.3603) Content-Type: text/xml; charset=utf-8 SOAPAction: "http://WebXml.com.cn/getSupportCity" Host: www.webxml.com.cn Content-Length: 348 Expect: 100-continue Connection: Keep-Alive <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <getSupportCity xmlns="http://WebXml.com.cn/"> <byProvinceName>广东</byProvinceName> </getSupportCity> </soap:Body> </soap:Envelope>
JSON (JavaScript Object Notation)
HTTP://JSON.ORG launch in 2002
Google & Yahoo offered JSON/RPC, REST in 2006
Last updated
Was this helpful?