| title |
|---|
Python / XML-RPC |
-
20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
The
xmlrpclibmodule has been renamed toxmlrpc.clientin Python 3. The2to3tool will automatically adapt imports when converting your sources to Python 3. -
XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP(S) as a transport. With it, a client can call methods WITH PARAMETERS on a remote server (the server is named by a URI) and get back STRUCTURED DATA.
Changed in version 2.7.9: For HTTPS URIs,
xmlrpclibnow performs all the necessary CERTIFICATE AND HOSTNAME CHECKS by default. -
This module supports writing XML-RPC client code; it handles all the details of TRANSLATING between conformable Python objects and XML on the wire.
The
xmlrpclibmodule is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML vulnerabilities.自動把 ODM (Object Document Mapping) 做掉了;因為會自動解析/轉換 XML response 的關係,有可能遭到攻擊。
-
-
xmlrpc — XMLRPC server and client modules — Python 3.7.3 documentation #ril
-
xmlrpc.client — XML-RPC client access — Python 3.7.2 documentation #ril
-
Example of Client Usage - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation
# simple test program (from the XML-RPC specification) from xmlrpclib import ServerProxy, Error # server = ServerProxy("http://localhost:8000") # local server server = ServerProxy("http://betty.userland.com") print server try: print server.examples.getStateName(41) except Error as v: print "ERROR", v透過
ServerProxy跟 XML-RPC server 溝通,範例.examples.getStateName(41)中的getStateName(41)是 method 與 parameters,那examples是什麼 ??
-
class
xmlrpclib.ServerProxy- 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentationclass xmlrpclib.ServerProxy(uri[, transport[, encoding[, verbose[, allow_none[, use_datetime[, context]]]]]])-
A
ServerProxyinstance is an object that manages communication with a remote XML-RPC server. The required first argument is a URI (Uniform Resource Indicator), and will normally be the URL of the server.Serveris retained as an alias forServerProxyfor backwards compatibility. New code should useServerProxy. -
The optional second argument is a TRANSPORT FACTORY instance; by default it is an internal
SafeTransportinstance for https: URLs and an internal HTTPTransportinstance otherwise.The optional third argument is an encoding, by default UTF-8. The optional fourth argument is a debugging flag.
-
The following parameters govern the use of the RETURNED PROXY INSTANCE. If
allow_noneis true, the Python constantNonewill be translated into XML; the default behaviour is forNoneto raise aTypeError. This is a commonly-used extension to the XML-RPC specification, but isn’t supported by all clients and servers; see http://ontosys.com/xml-rpc/extensions.php for a description.allow_none是控制 Python --> XML-RPC 的轉換,預設None會丟出TypeError,好奇None在 XML 裡會怎麼表現 ??The
use_datetimeflag can be used to cause date/time values to be presented asdatetime.datetimeobjects; this is false by default.datetime.datetimeobjects may be passed to calls.搭配 "to be presented as" 與 "may be passed to calls" 的說法,呼叫時一定可以傳
datetime.datetime,use_datetime是在控制 XML-RPC --> Python 的轉換。 -
Both the HTTP and HTTPS transports support the URL syntax extension for HTTP Basic Authentication:
http://user:pass@host:port/path. Theuser:passportion will be BASE64-ENCODED as an HTTP ‘Authorization’ header, and sent to the remote server as part of the connection process when invoking an XML-RPC method. You only need to use this if the remote server requires a Basic Authentication user and password.不用自己做 Base64 編碼,寫成
user:pass內部會自動處理編碼。If an HTTPS URL is provided, context may be
ssl.SSLContextand configures the SSL settings of the underlying HTTPS connection.只能用這種方式驗證身份嗎 ??
-
The returned instance is a PROXY OBJECT with methods that can be used to invoke corresponding RPC calls on the remote server.
ServerProxy名副其實是個 proxy object !! -
Changed in version 2.5: The
use_datetimeflag was added. -
Changed in version 2.6: Instances of new-style classes can be passed in if they have an
__dict__attribute and don’t have a base class that is marshalled in a special way. -
Changed in version 2.7.9: Added the
contextargument.
-
-
ServerProxy Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
A
ServerProxyinstance has a method corresponding to each remote procedure call accepted by the XML-RPC server. Calling the method performs an RPC, dispatched by both name and argument signature (e.g. the same method name can be OVERLOADED with multiple argument signatures).The RPC finishes by returning a value, which may be either returned data in a conformant type or a
FaultorProtocolErrorobject indicating an error.
-
-
class
xmlrpclib.ServerProxy- 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation-
Types that are conformable (e.g. that can be MARSHALLED through XML), include the following (and except where noted, they are UNMARSHALLED as the same Python type):
XML-RPC type -> Python type
-
boolean->bool -
int/i4-->intorlongin range from -2147483648 to 2147483647. -
double->float -
string->strorunicode -
array->listortuplecontaining conformable elements. Arrays are returned aslists. -
struct->dict.Keys must be strings, values may be any conformable type. Objects of user-defined classes can be passed in; only their
__dict__attribute is transmitted. -
dateTime.iso8601->xmlrpclib.DateTimeordatetime.datetime. Returned type depends on values of theuse_datetimeflags.預設傳回
xmlrpclib.DateTime,啟用use_datetime才會傳回datetime.datetime;但有什麼理由不用datetime.datetime?? -
base64->xmlrpclib.Binary為什麼不是
str(Python 2) 或bytes(Python 3)? 會跟string->str/unicode混淆 ?? -
nil-> TheNoneconstant. Passing is allowed only ifallow_noneis true.若 XML-RPC 有
nil的概念,為什麼傳入None要特別用allow_none控制,而且預設還沒開啟 ??
-
-
Note that even though starting with Python 2.2 you can subclass built-in types, the
xmlrpclibmodule currently does not marshal instances of such subclasses. ??這跟上面的一段說明有關:
Changed in version 2.6: Instances of new-style classes can be passed in if they have an
__dict__attribute and don’t have a base class that is marshalled in a special way. -
When passing strings, characters special to XML such as
<,>, and&will be AUTOMATICALLY ESCAPED. However, it’s the caller’s responsibility to ensure that the string is free of characters that aren’t allowed in XML, such as the CONTROL CHARACTERS with ASCII values between 0 and 31 (except, of course, tab, newline and carriage return); failing to do this will result in an XML-RPC request that ISN’T WELL-FORMED XML.If you have to pass arbitrary strings via XML-RPC, use the
Binarywrapper class described below.
-
-
Boolean Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
DateTime Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
Binary Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
class
xmlrpclib.ServerProxy- 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation-
This is the full set of data types supported by XML-RPC. Method calls may also raise a special
Faultinstance, used to signal XML-RPC server errors, orProtocolErrorused to signal an error in the HTTP/HTTPS transport layer. BothFaultandProtocolErrorderive from a base class calledError.可以把
Fault理解成 server-side error,而ProtocolError是 connection error;而xmlrpclib.Error則是繼承自Exception。
-
-
Fault Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
ProtocolError Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
-
class
xmlrpclib.ServerProxy- 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation- If the remote server supports the INTROSPECTION API, the proxy can also be used to QUERY the remote server for the methods it supports (SERVICE DISCOVERY) and fetch other server-associated metadata.
-
ServerProxy Objects - 20.23. xmlrpclib — XML-RPC client access — Python 2.7.16 documentation #ril
Servers that support the XML introspection API support some common methods grouped under the RESERVED
systemattribute:
相關:
手冊: