[&@sdZddlZddlZddlZddlmZddlZddlZddl m Z ddl Z ddl m Z yddlZWnek rdZYnXddZejddZdXZdZZd[Zd\Zd]Zd^Zd_Zd`ZdaZdbZdcZddZdeZdfZ Gddde!Z"Gddde"Z#Gddde"Z$Gddde"Z%e&Z'Z(ed d d Z)e)j*ddkrd d!Z+n0e)j*d"dkrd#d!Z+n d$d!Z+[)d%d&Z,Gd'd(d(Z-d)d*Z.d+d,Z/Gd-d.d.Z0d/d0Z1e-e0fZ2Gd1d2d2Z3Gd3d4d4Z4Gd5d6d6Z5Gd7d8d8Z6Gd9d:d:Z7Gd;d<d<Z8dZ9Z:Z;d=d=d>d?Z<dddd=d@dAZ=d=d=dBdCZ>dDdEZ?dFdGdHZ@GdIdJdJer<ejAneBZCGdKdLdLZDGdMdNdNZEGdOdPdPeEZFGdQdRdRZGeGZHeIdSkreGdTZJyeKeJjLjMWn2e"k rZNzeKdUeNWYddZN[NXnXe8eJZOeOjPeOjQd dVeOjRd d y"xeOD]ZSeKeSqAWWqe"k rZNzeKdUeNWYddZN[NXqXndS)ga An XML-RPC client interface for Python. The marshalling and response parser code can also be used to implement XML-RPC servers. Exported exceptions: Error Base class for client errors ProtocolError Indicates an HTTP protocol error ResponseError Indicates a broken response package Fault Indicates an XML-RPC fault package Exported classes: ServerProxy Represents a logical connection to an XML-RPC server MultiCall Executor of boxcared xmlrpc requests DateTime dateTime wrapper for an ISO 8601 string or time tuple or localtime integer value to generate a "dateTime.iso8601" XML-RPC value Binary binary data wrapper Marshaller Generate an XML-RPC params chunk from a Python data structure Unmarshaller Unmarshal an XML-RPC response from incoming XML event message Transport Handles an HTTP transaction to an XML-RPC server SafeTransport Handles an HTTPS transaction to an XML-RPC server Exported constants: (none) Exported functions: getparser Create instance of the fastest available parser & attach to an unmarshalling object dumps Convert an argument tuple or a Fault instance to an XML-RPC request (or response, if the methodresponse option is used). loads Convert an XML-RPC packet to unmarshalled data plus a method name (None if not present). N)datetime)expat)BytesIOcCs4|jdd}|jdd}|jddS)N&z&z>)replace)sr #/usr/lib/python3.4/xmlrpc/client.pyescapesr iiXi~i~i,~iiiYiZi[c@s"eZdZdZddZdS)ErrorzBase class for client errors.cCs t|S)N)repr)selfr r r __str__sz Error.__str__N)__name__ __module__ __qualname____doc__rr r r r rs rc@s.eZdZdZddZddZdS) ProtocolErrorz!Indicates an HTTP protocol error.cCs5tj|||_||_||_||_dS)N)r__init__urlerrcodeerrmsgheaders)rrrrrr r r rs     zProtocolError.__init__cCsd|j|j|jfS)Nz)rrr)rr r r __repr__szProtocolError.__repr__N)rrrrrrr r r r rs  rc@seZdZdZdS) ResponseErrorz$Indicates a broken response package.N)rrrrr r r r r s r c@s.eZdZdZddZddZdS)Faultz#Indicates an XML-RPC fault package.cKs#tj|||_||_dS)N)rr faultCode faultString)rr"r#Zextrar r r rs  zFault.__init__cCsd|j|jfS)Nz)r"r#)rr r r rszFault.__repr__N)rrrrrrr r r r r!s  r!z%YZ0001cCs |jdS)Nz%Y%m%dT%H:%M:%S)strftime)valuer r r _iso8601_format sr&z%4YcCs |jdS)Nz%4Y%m%dT%H:%M:%S)r$)r%r r r r&scCs|jdjdS)Nz%Y%m%dT%H:%M:%S)r$zfill)r%r r r r&scCspt|trt|St|ttjfs^|dkrLtj}ntj|}nd|ddS)Nrz%04d%02d%02dT%02d:%02d:%02d) isinstancerr&tupletimeZ struct_timeZ localtime)r%r r r _strftimes  r-c@seZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZddZddZdS)DateTimezDateTime wrapper for an ISO 8601 string or time tuple or localtime integer value to generate 'dateTime.iso8601' XML-RPC value. rcCs.t|tr||_nt||_dS)N)r*strr%r-)rr%r r r r's zDateTime.__init__cCst|tr$|j}|j}nt|trK|j}t|}nt|trl|j}|}nmt|dr|j}|j}nCt|dr|jj pt |}t d|jj |f||fS)N timetuple __class__zCan't compare %s and %s) r*r.r%rr&r/hasattrr0r1rtype TypeError)rotherr oZotyper r r make_comparable-s$        zDateTime.make_comparablecCs|j|\}}||kS)N)r7)rr5r r6r r r __lt__BszDateTime.__lt__cCs|j|\}}||kS)N)r7)rr5r r6r r r __le__FszDateTime.__le__cCs|j|\}}||kS)N)r7)rr5r r6r r r __gt__JszDateTime.__gt__cCs|j|\}}||kS)N)r7)rr5r r6r r r __ge__NszDateTime.__ge__cCs|j|\}}||kS)N)r7)rr5r r6r r r __eq__RszDateTime.__eq__cCs|j|\}}||kS)N)r7)rr5r r6r r r __ne__VszDateTime.__ne__cCstj|jdS)Nz%Y%m%dT%H:%M:%S)r,strptimer%)rr r r r0ZszDateTime.timetuplecCs|jS)N)r%)rr r r rbszDateTime.__str__cCsd|jt|fS)Nz)r%id)rr r r reszDateTime.__repr__cCst|j|_dS)N)r/stripr%)rdatar r r decodehszDateTime.decodecCs.|jd|j|j|jddS)Nzz )writer%)routr r r encodeks zDateTime.encodeN)rrrrrr7r8r9r:r;r<r=r0rrrBrEr r r r r.!s            r.cCst}|j||S)N)r.rB)rAr%r r r _datetimeps  rFcCstj|dS)Nz%Y%m%dT%H:%M:%S)rr>)rAr r r _datetime_typevsrGc@saeZdZdZdddZddZddZd d Zd d Zd dZ dS)BinaryzWrapper for binary data.NcCs\|dkrd}n:t|ttfsCtd|jjnt|}||_dS)Nz#expected bytes or bytearray, not %s)r*bytes bytearrayr4r1rrA)rrAr r r rs   zBinary.__init__cCst|jdS)Nzlatin-1)r/rA)rr r r rszBinary.__str__cCs(t|tr|j}n|j|kS)N)r*rHrA)rr5r r r r<s z Binary.__eq__cCs(t|tr|j}n|j|kS)N)r*rHrA)rr5r r r r=s z Binary.__ne__cCstj||_dS)N)base64 decodebytesrA)rrAr r r rBsz Binary.decodecCsF|jdtj|j}|j|jd|jddS)Nz asciiz )rCrL encodebytesrArB)rrDencodedr r r rEs z Binary.encode) rrrrrrr<r=rBrEr r r r rHs     rHcCst}|j||S)N)rHrB)rAr%r r r _binarys  rQc@s4eZdZddZddZddZdS) ExpatParsercCs`tjdd|_}||_|j|_|j|_|j|_ d}|j |ddS)N) rZ ParserCreate_parser_targetstartZStartElementHandlerendZEndElementHandlerrAZCharacterDataHandlerxml)rtargetparserencodingr r r rs    zExpatParser.__init__cCs|jj|ddS)Nr)rSParse)rrAr r r feedszExpatParser.feedcCs#|jjdd|`|`dS)Nr)rSr[rT)rr r r closeszExpatParser.closeN)rrrrr\r^r r r r rRs  rRc@s\eZdZdZddddZiZddZdd Zd d Zeee d r"r#z z z z z r])append_Marshaller__dumpr*r!r"r#join)rvaluesrDrCdumpvresultr r r dumpss"          zMarshaller.dumpsc Csy|jt|}Wntk rt|dsOtdt|nxEt|jD]4}||jjkr_tdt|q_q_W|jd}YnX||||dS)N__dict__zcannot marshal %s objects_arbitrary_instance)dispatchr3KeyErrorr2r4__mro__keys)rr%rCfZtype_r r r Z__dumps zMarshaller.__dumpcCs&|jstdn|ddS)Nz0cannot marshal None unless allow_none is enabledz)rar4)rr%rCr r r dump_nils zMarshaller.dump_nilcCs.|d||rdpd|ddS)Nz10z r )rr%rCr r r dump_bools zMarshaller.dump_boolcCsU|tks|tkr'tdn|d|tt||ddS)Nzint exceeds XML-RPC limitsz z )MAXINTMININT OverflowErrorr/int)rr%rCr r r dump_longs  zMarshaller.dump_longcCs(|d|t||ddS)Nzz )r)rr%rCr r r dump_double's zMarshaller.dump_doublecCs(|d||||ddS)Nzz r )rr%rCr r r r dump_unicode-s zMarshaller.dump_unicodecCs:|dtj|}||jd|ddS)Nz rNz )rLrOrB)rr%rCrPr r r dump_bytes3s zMarshaller.dump_bytescCst|}||jkr*tdnd|j|<|j}|dx|D]}|||qQW|d|j|=dS)Nz"cannot marshal recursive sequencesz z )r?r`r4rc)rr%rCirfrgr r r dump_array;s      zMarshaller.dump_arraycCst|}||jkr*tdnd|j|<|j}|dxm|jD]_\}}|dt|tstdn|d||||||dqWW|d|j|=dS)Nz%cannot marshal recursive dictionariesz z zdictionary key must be stringz%s z z )r?r`r4rcitemsr*r/)rr%rCr r}rfkrgr r r dump_structIs       zMarshaller.dump_structcCs(|d|t||ddS)Nzz )r-)rr%rCr r r dump_datetime[s zMarshaller.dump_datetimecCsE|jtkr.||_|j||`n|j|j|dS)N)r1WRAPPERSrCrErrj)rr%rCr r r dump_instanceas    zMarshaller.dump_instancerk)!rrrrrrlrircrqr3rtboolryrxZdump_intrzfloatr r{r/r|rJrKr~r+listrdictrrrr.rHr r r r r_s<                     r_c@seZdZdZddddZddZddZd d Zd d Zd dZ ddZ ddZ iZ ddZ e e d UnmarshalleraUnmarshal an XML-RPC response, based on incoming XML event messages (start, data, end). Call close() to get the resulting data structure. Note that this reader is fairly tolerant, and gladly accepts bogus XML-RPC data without complaining (but not bogus XML). FcCsad|_g|_g|_g|_d|_d|_|jj|_|pN||_||_dS)Nzutf-8) _type_stack_marks_data _methodname _encodingrb _use_datetime _use_bytes)r use_datetimeuse_builtin_typesr r r rs      zUnmarshaller.__init__cCsV|jdks|jr$tn|jdkrIt|jdnt|jS)Nfaultr)rrr r!rr+)rr r r r^s  zUnmarshaller.closecCs|jS)N)r)rr r r getmethodnameszUnmarshaller.getmethodnamecCs ||_dS)N)r)rrZZ standaloner r r rWszUnmarshaller.xmlcCsP|dks|dkr4|jjt|jng|_|dk|_dS)Narraystructr%)rrblenrr_value)rtagZattrsr r r rUs zUnmarshaller.startcCs|jj|dS)N)rrb)rtextr r r rAszUnmarshaller.datac CsCy|j|}Wntk r%YnX||dj|jSdS)Nr])rlrmrdr)rrrpr r r rVs  zUnmarshaller.endc Cs7y|j|}Wntk r%YnX|||SdS)N)rlrm)rrrArpr r r end_dispatchs  zUnmarshaller.end_dispatchcCs|jdd|_dS)Nr)rbr)rrAr r r end_nils zUnmarshaller.end_nilZnilcCsQ|dkr|jdn(|dkr8|jdn tdd|_dS)NrsFrrTzbad boolean valuer)rbr4r)rrAr r r end_booleans    zUnmarshaller.end_booleanbooleancCs |jt|d|_dS)Nr)rbrxr)rrAr r r end_intszUnmarshaller.end_intZi4Zi8rxcCs |jt|d|_dS)Nr)rbrr)rrAr r r end_doubleszUnmarshaller.end_doubleZdoublecCs8|jr|j|j}n|j|d|_dS)Nr)rrBrbr)rrAr r r end_strings  zUnmarshaller.end_stringstringnamecCs?|jj}|j|dg|j|d a object used to boxcar method calls server should be a ServerProxy object. Methods can be added to the MultiCall using normal method call syntax e.g.: multicall = MultiCall(server_proxy) multicall.add(2,3) multicall.get_address("Guido") To execute the multicall, call the MultiCall object e.g.: add_result, address = multicall() cCs||_g|_dS)N)_MultiCall__server_MultiCall__call_list)rserverr r r rMs zMultiCall.__init__cCsdt|S)Nz)r?)rr r r rQszMultiCall.__repr__cCst|j|S)N)rr)rrr r r rVszMultiCall.__getattr__cCsTg}x2|jD]'\}}|ji|d6|d6qWt|jjj|S)Nrr)rrbrrsystemZ multicall)rZmarshalled_listrrr r r rYszMultiCall.__call__N) rrrrrrrrrr r r r r<s    rFcCstrltrl|r$t}tj}n!|r9t}t}n t}t}tdd||t}t|}n6td|d|}trt|}n t |}||fS)zgetparser() -> parser, unmarshaller Create an instance of the fastest available parser, and attach it to an unmarshalling object. Return both objects. TFrr) FastParserFastUnmarshallerrGrLrMrQrFr!rrR)rrZ mkdatetimeZmkbytesrXrYr r r getparserks     rcCsDt|ttfs!tdt|tr9d}n6|rot|trot|dksotdn|s~d}ntrt|}nt||}|j|}|dkrdt|}nd}|rt|ts|j |}n|d|d|d f}n|r3|d |d f}n|Sd j |S) adata [,options] -> marshalled data Convert an argument tuple or a Fault instance to an XML-RPC request (or response, if the methodresponse option is used). In addition to the data object, the following options can be given as keyword arguments: methodname: the method name for a methodCall packet methodresponse: true to create a methodResponse packet. If this option is used with a tuple, the tuple must be a singleton (i.e. it can contain only one element). encoding: the packet encoding (default is UTF-8) All byte strings in the data structure are assumed to use the packet encoding. Unicode strings are automatically converted, where necessary. z(argument must be tuple or Fault instancerz"response tuple must be a singletonzutf-8z$ z z z z z z r]) r*r+r!AssertionErrorrFastMarshallerr_rir/rErd)r methodnameZmethodresponserZramrAZ xmlheaderr r r ris:! !    ricCsHtd|d|\}}|j||j|j|jfS)zdata -> unmarshalled data, method name Convert an XML-RPC packet to unmarshalled data plus a method name (None if not present). If the XML-RPC packet represents a fault condition, this function raises a Fault exception. rr)rr\r^r)rArrpur r r loadss   rcCsgtstnt}tjddd|dd}|j||j|j}|j|S)zhdata -> gzip encoded data Encode data using the gzip content encoding as described in RFC 1952 modewbfileobjZ compresslevelr)gzipNotImplementedErrorrGzipFilerCr^getvalue)rArpgzfrPr r r gzip_encodes      ri@c Cststnt|}tjddd|}y2|dkrQ|j}n|j|d}Wntk rtdYnX|j|j|dkrt||krtdn|S)zrgzip encoded data -> unencoded data Decode data using the gzip content encoding as described in RFC 1952 rrbrrrz invalid dataz#max gzipped payload length exceeded) rrrrreadOSErrorrr^r)rAZ max_decoderprZdecodedr r r gzip_decodes      rc@s.eZdZdZddZddZdS)GzipDecodedResponsezha file-like object to decode a response encoded with the gzip method, as described in RFC 1952. cCsGtstnt|j|_tjj|ddd|jdS)Nrrr)rrrriorr)rresponser r r r1s zGzipDecodedResponse.__init__cCs!tjj||jjdS)N)rrr^r)rr r r r^9szGzipDecodedResponse.closeN)rrrrrr^r r r r r-s  rc@s4eZdZddZddZddZdS)_MethodcCs||_||_dS)N) _Method__send _Method__name)rsendrr r r rDs z_Method.__init__cCst|jd|j|fS)Nz%s.%s)rrr)rrr r r rGsz_Method.__getattr__cGs|j|j|S)N)rr)rrr r r rIsz_Method.__call__N)rrrrrrr r r r rAs   rc@seZdZdZdeZdZdZddddZddd Z dd d Z d d Z ddZ ddZ ddZddZddZddZddZdS) Transportz1Handles an HTTP transaction to an XML-RPC server.zPython-xmlrpc/%sTNFcCs(||_||_d|_g|_dS)N)NN)r_use_builtin_types _connection_extra_headers)rrrr r r r`s   zTransport.__init__cCsxdD]}y|j||||SWqtk r{}z2|sc|jtjtjtjfkrinWYdd}~Xqtjjk r|rnYqXqWdS)Nrr)rr) single_requestrerrnoZ ECONNRESETZ ECONNABORTEDZEPIPEhttpclientZ BadStatusLine)rhosthandler request_bodyverboser}er r r requestps zTransport.requestcCsyM|j||||}|j}|jdkrL||_|j|SWn3tk rdYntk r|jYnX|jddr|j nt |||j|j t |j dS)Nzcontent-lengthr]) send_requestZ getresponseZstatusrparse_responser! Exceptionr^ getheaderrrreasonrZ getheaders)rrrrrZ http_connZrespr r r r}s"       zTransport.single_requestcCstd|jd|jS)Nrr)rrr)rr r r rszTransport.getparsercCsi}t|tr$|\}}ntjj|\}}|rtjj|}tj|jd}dj |j }dd|fg}ng}|||fS)Nzutf-8r]Z AuthorizationzBasic ) r*r+urllibparseZ splituserZunquote_to_bytesrLrOrBrdsplit)rrx509ZauthZ extra_headersr r r get_host_infoszTransport.get_host_infocCsh|jr'||jdkr'|jdS|j|\}|_}|tjj|f|_|jdS)Nrr)rrrrrZHTTPConnection)rrchostrr r r make_connections  zTransport.make_connectioncCs.|jdr*|jdjd|_ndS)Nr)NN)rr^)rr r r r^s zTransport.closecCs|j|}|jdd}|r8|jdn|jrmtrm|jd|dd|jd n|jd||jd |jd |jf|j|||j |||S) NrZPOSTZskip_accept_encodingTAccept-Encodingr Content-Typetext/xmlz User-Agent)rzgzip)rr) rrZset_debuglevelaccept_gzip_encodingrZ putrequestrb user_agent send_headers send_content)rrrrdebug connectionrr r r rs zTransport.send_requestcCs+x$|D]\}}|j||qWdS)N) putheader)rrrkeyvalr r r rszTransport.send_headerscCsv|jdk rI|jt|krItrI|jddt|}n|jdtt||j|dS)NzContent-EncodingrzContent-Length)encode_thresholdrrrrr/Z endheaders)rrrr r r rszTransport.send_contentcCst|dr?|jdddkr6t|}qE|}n|}|j\}}xI|jd}|ssPn|jrtdt|n|j|qZW||k r|j n|j |j S)NrzContent-Encodingr]rizbody:) r2rrrrrprintrr\r^)rrstreamrrrAr r r rs"     zTransport.parse_response)rrrr __version__rrrrrrrrrr^rrrrr r r r rRs   !      rcs@eZdZdZddddfddZddZS) SafeTransportz2Handles an HTTPS transaction to an XML-RPC server.FcontextNcs&tjd|d|||_dS)Nrr)superrr )rrrr )r1r r r7szSafeTransport.__init__cCs|jr'||jdkr'|jdSttjdsHtdn|j|\}|_}|tjj|dd|j|pif|_|jdS)NrrHTTPSConnectionz1your version of http.client doesn't support HTTPSr ) rr2rrrrrrr )rrrrr r r r>s  !zSafeTransport.make_connection)rrrrrrr r )r1r r 4s  r c @s|eZdZdZddddddddddZddZd d Zd d ZeZd dZ ddZ dS) ServerProxyauri [,options] -> a logical connection to an XML-RPC server uri is the connection point on the server, given as scheme://host/target. The standard implementation always supports the "http" scheme. If SSL socket support is available (Python 2.0), it also supports "https". If the target part and the slash preceding it are both omitted, "/RPC2" is assumed. The following options can be given as keyword arguments: transport: a transport factory encoding: the request encoding (default is UTF-8) All 8-bit strings passed to the server proxy are assumed to use the given encoding. NFr c Cstjj|\} }| d kr3tdntjj|\|_|_|jsfd|_n|dkr| dkrt} i|d6} n t} i} | d|d|| }n||_ |pd|_ ||_ ||_ dS) Nrhttpszunsupported XML-RPC protocolz/RPC2r rrzutf-8)zhttpr) rrZ splittyperZ splithost_ServerProxy__host_ServerProxy__handlerr r_ServerProxy__transport_ServerProxy__encoding_ServerProxy__verbose_ServerProxy__allow_none) rZuri transportrZrrarrr r3rZ extra_kwargsr r r rss&         zServerProxy.__init__cCs|jjdS)N)rr^)rr r r Z__closeszServerProxy.__closecCswt||d|jd|jj|j}|jj|j|j|d|j}t |dkrs|d}n|S)NrZrarrr) rirrrErrrrrr)rrrrrr r r Z __requests   zServerProxy.__requestcCsd|j|jfS)Nz)rr)rr r r rszServerProxy.__repr__cCst|j|S)N)r_ServerProxy__request)rrr r r rszServerProxy.__getattr__cCs=|dkr|jS|dkr&|jStd|fdS)z|A workaround to get special attributes on the ServerProxy without interfering with the magic __getattr__ r^rzAttribute %r not foundN)_ServerProxy__closerAttributeError)rattrr r r rs   zServerProxy.__call__) rrrrrrrrrrrr r r r r]s       r__main__zhttp://localhost:8000ZERROR liliiDii ipiԁiDiCiBiiii)TrrLsysr,rZ http.clientrZ urllib.parserZ xml.parsersrrrrr ImportErrorr versionr rurvZ PARSE_ERRORZ SERVER_ERRORZAPPLICATION_ERRORZ SYSTEM_ERRORZTRANSPORT_ERRORZNOT_WELLFORMED_ERRORZUNSUPPORTED_ENCODINGZINVALID_ENCODING_CHARZINVALID_XMLRPCZMETHOD_NOT_FOUNDZINVALID_METHOD_PARAMSZINTERNAL_ERRORrrrr r!rrZBooleanZ_day0r$r&r-r.rFrGrHrQrrRr_rrrrrrrrrirrrrobjectrrrr rZServerrrrZ currentTimeZgetCurrentTimergZmultiZgetDatapowaddrr r r r s             O  (   '' L %)c