3 /fK@sddlmZmZmZmZmZddlmZddlZddlZ ddl Z ddl Z ddl Z ddl Z ddlZddlZddlZy ddlZWnek rdZYnXd)ddZddZGd d d ZGd d d eZGd dde jeZGdddeZGdddeZGdddejZGdddZGdddeZGdddeeZGdddeeZ e!dkrddl"Z"GdddZ#ed*~Z$e$j%e&e$j%d"d#d$e$j'e#dd%e$j(e)d&e)d'y e$j*Wn(e+k re)d(e j,dYnXWdQRXdS)+)Faultdumpsloads gzip_encode gzip_decode)BaseHTTPRequestHandlerNTcCsJ|r|jd}n|g}x.|D]&}|jdr8td|qt||}qW|S)N._z(attempt to access private attribute "%s")split startswithAttributeErrorgetattr)objattrallow_dotted_namesZattrsir%/usr/lib64/python3.6/xmlrpc/server.pyresolve_dotted_attribute{s     rcsfddtDS)Ncs*g|]"}|jd rtt|r|qS)r )r callabler ).0member)rrr s z'list_public_methods..)dir)rr)rrlist_public_methodssrc@sleZdZdddZdddZdddZd d Zd d Zdd dZddZ ddZ ddZ ddZ ddZ dS)SimpleXMLRPCDispatcherFNcCs&i|_d|_||_|pd|_||_dS)Nzutf-8)funcsinstance allow_noneencodinguse_builtin_types)selfrrr rrr__init__s  zSimpleXMLRPCDispatcher.__init__cCs||_||_dS)N)rr)r!rrrrrregister_instances!z(SimpleXMLRPCDispatcher.register_instancecCs|dkr|j}||j|<dS)N)__name__r)r!Zfunctionnamerrrregister_functionsz(SimpleXMLRPCDispatcher.register_functioncCs|jj|j|j|jddS)N)zsystem.listMethodszsystem.methodSignaturezsystem.methodHelp)rupdatesystem_listMethodssystem_methodSignaturesystem_methodHelp)r!rrr register_introspection_functionss z7SimpleXMLRPCDispatcher.register_introspection_functionscCs|jjd|jidS)Nzsystem.multicall)rr'system_multicall)r!rrrregister_multicall_functionssz3SimpleXMLRPCDispatcher.register_multicall_functionsc CsyPt||jd\}}|dk r(|||}n |j||}|f}t|d|j|jd}Wntk r}zt||j|jd}WYdd}~XnNtj\}} } z$ttdd|| f|j|jd}Wdd}} } XYnX|j |jdS)N)r )Zmethodresponserr)rrz%s:%s)rrxmlcharrefreplace) rr _dispatchrrrrsysexc_infoencode) r!datadispatch_methodpathparamsmethodresponsefaultexc_type exc_valueexc_tbrrr_marshaled_dispatchs&   z*SimpleXMLRPCDispatcher._marshaled_dispatchcCs^t|jj}|jdk rVt|jdr8|t|jjO}nt|jdsV|tt|jO}t|S)N _listMethodsr0)setrkeysrhasattrr?rsorted)r!methodsrrrr(s   z)SimpleXMLRPCDispatcher.system_listMethodscCsdS)Nzsignatures not supportedr)r! method_namerrrr))s z-SimpleXMLRPCDispatcher.system_methodSignaturec Csd}||jkr|j|}nX|jdk rrt|jdr<|jj|St|jdsryt|j||j}Wntk rpYnX|dkr~dStj|SdS)N _methodHelpr0) rrrBrFrrr pydocgetdoc)r!rEr8rrrr*6s"       z(SimpleXMLRPCDispatcher.system_methodHelpc Csg}x|D]}|d}|d}y|j|j||gWq tk rl}z|j|j|jdWYdd}~Xq tj\}}} z|jdd||fdWdd}}} XYq Xq W|S)NZ methodNamer7) faultCode faultStringr.z%s:%s)appendr0rrJrKr1r2) r!Z call_listresultsZcallrEr7r:r;r<r=rrrr,Us$  z'SimpleXMLRPCDispatcher.system_multicallcCsy|j|}Wntk r"YnX|dk r4||Std||jdk rt|jdrd|jj||Syt|j||j}Wntk rYnX|dk r||Std|dS)Nzmethod "%s" is not supportedr0) rKeyError ExceptionrrBr0rrr )r!r8r7funcrrrr0ys(    z SimpleXMLRPCDispatcher._dispatch)FNF)F)N)NN)r$ __module__ __qualname__r"r#r&r+r-r>r(r)r*r,r0rrrrrs   $   ) $rc@sbeZdZdZdZdZdZejdej ej BZ ddZ d d Z d d Zd dZddZdddZdS)SimpleXMLRPCRequestHandler//RPC2ixr.Tz \s* ([^\s;]+) \s* #content-coding (;\s* q \s*=\s* ([0-9\.]+))? #q cCsbi}|jjdd}xJ|jdD]<}|jj|}|r|jd}|rHt|nd}|||jd<qW|S)NzAccept-EncodingrG,g?r.)headersgetr aepatternmatchgroupfloat)r!rZaeer[vrrraccept_encodingss  z+SimpleXMLRPCRequestHandler.accept_encodingscCs|jr|j|jkSdSdS)NT) rpc_pathsr6)r!rrris_rpc_path_valids z,SimpleXMLRPCRequestHandler.is_rpc_path_validc Cs|js|jdSyd}t|jd}g}x>|rjt||}|jj|}|sNP|j||t|d8}q.Wdj |}|j |}|dkrdS|j j |t |dd|j}Wntk r6}zp|jdt|j do|j jr|jd t|tj} t| jd d d } |jd | |jd d|jWYdd}~XnX|jd|jdd|jdk rt||jkr|jjdd} | ryt|}|jddWntk rYnX|jd tt||j|jj|dS)N izcontent-lengthr.r0i_send_traceback_headerz X-exceptionASCIIbackslashreplacez X-tracebackzContent-length0z Content-typeztext/xmlgziprzContent-Encodingi(i) rc report_404intrXminZrfilereadrLlenjoindecode_request_contentserverr>r r6rO send_responserBrf send_headerstr traceback format_excr3 end_headersencode_thresholdrarYrNotImplementedErrorwfilewrite) r!Zmax_chunk_sizeZsize_remainingLZ chunk_sizechunkr4r9r_Ztraceqrrrdo_POSTsX             z"SimpleXMLRPCRequestHandler.do_POSTc Cs|jjddj}|dkr|S|dkrtyt|Stk rR|jdd|Yqtk rp|jddYqXn|jdd||jdd |jdS) Nzcontent-encodingZidentityrkizencoding %r not supportedizerror decoding gzip contentzContent-lengthri) rXrYlowerrr|ru ValueErrorrvrz)r!r4rrrrrss z1SimpleXMLRPCRequestHandler.decode_request_contentcCsF|jdd}|jdd|jdtt||j|jj|dS)Nis No such pagez Content-typez text/plainzContent-length)rurvrwrqrzr}r~)r!r9rrrrm/s   z%SimpleXMLRPCRequestHandler.report_404-cCs|jjrtj|||dS)N)rt logRequestsr log_request)r!codesizerrrr8sz&SimpleXMLRPCRequestHandler.log_requestN)rTrUrl)rr)r$rQrRrbr{ZwbufsizeZdisable_nagle_algorithmrecompileVERBOSE IGNORECASErZrarcrrsrmrrrrrrSs  G rSc@s*eZdZdZdZedddddfddZdS)SimpleXMLRPCServerTFNcCs,||_tj||||tjj||||dS)N)rrr" socketserver TCPServer)r!addrrequestHandlerrrrbind_and_activater rrrr"QszSimpleXMLRPCServer.__init__)r$rQrRZallow_reuse_addressrfrSr"rrrrr>s  rc@s<eZdZedddddfddZddZdd Zd d d ZdS) MultiPathXMLRPCServerTFNc Cs2tj||||||||i|_||_|p*d|_dS)Nzutf-8)rr" dispatchersrr)r!rrrrrrr rrrr"bs  zMultiPathXMLRPCServer.__init__cCs||j|<|S)N)r)r!r6Z dispatcherrrradd_dispatcherls z$MultiPathXMLRPCServer.add_dispatchercCs |j|S)N)r)r!r6rrrget_dispatcherpsz$MultiPathXMLRPCServer.get_dispatchercCs|y|j|j|||}Wn^tjdd\}}z2ttdd||f|j|jd}|j|jd}Wdd}}XYnX|S)Nr.z%s:%s)rrr/) rr>r1r2rrrrr3)r!r4r5r6r9r;r<rrrr>ss z)MultiPathXMLRPCServer._marshaled_dispatch)NN)r$rQrRrSr"rrr>rrrrrZs rc@s0eZdZd ddZddZddZd d d ZdS) CGIXMLRPCRequestHandlerFNcCstj||||dS)N)rr")r!rrr rrrr"sz CGIXMLRPCRequestHandler.__init__cCsP|j|}tdtdt|ttjjtjjj|tjjjdS)NzContent-Type: text/xmlzContent-Length: %d)r>printrqr1stdoutflushbufferr~)r! request_textr9rrr handle_xmlrpcs  z%CGIXMLRPCRequestHandler.handle_xmlrpccCsd}tj|\}}tjj|||d}|jd}td||ftdtjjtdt|tt j j t j j j |t j j j dS)Ni)rmessageexplainzutf-8z Status: %d %szContent-Type: %szContent-Length: %d)rZ responseshttprtZDEFAULT_ERROR_MESSAGEr3rZDEFAULT_ERROR_CONTENT_TYPErqr1rrrr~)r!rrrr9rrr handle_gets   z"CGIXMLRPCRequestHandler.handle_getc Csz|dkr$tjjdddkr$|jnRyttjjdd}Wnttfk rVd}YnX|dkrltjj |}|j |dS)NZREQUEST_METHODZGETZCONTENT_LENGTHr.rl) osenvironrYrrnr TypeErrorr1stdinrpr)r!rZlengthrrrhandle_requests   z&CGIXMLRPCRequestHandler.handle_request)FNF)N)r$rQrRr"rrrrrrrrs  rc@s:eZdZdiiifddZdiiidfddZddZdS) ServerHTMLDocNcCs^|p|j}g}d}tjd}x|j||} | s2P| j\} } |j|||| | j\} } }}}}| r|| jdd}|jd||fn|rdt|}|jd||| fn~|rdt|}|jd||| fnV|| | dd kr|j|j ||||n(|r$|jd |n|j|j ||| }q W|j|||dd j |S) NrzM\b((http|ftp)://\S+[\w/]|RFC[- ]?(\d+)|PEP[- ]?(\d+)|(self\.)?((?:\w|\.)+))\b"z"z%sz'http://www.rfc-editor.org/rfc/rfc%d.txtz(http://www.python.org/dev/peps/pep-%04d/r.(zself.%srG) escaperrsearchspanrLgroupsreplacernZnamelinkrr)r!textrrclassesrDrMherepatternr[startendallschemeZrfcZpepZselfdotr%Zurlrrrmarkups8      zServerHTMLDoc.markupcCs$|r |jp dd|}d} d|j||j|f} tj|rrtj|} tj| jdd| j| j| j | j |j d} n%sr.) annotations formatvaluez(...)rz'%sz
%s
z
%s
%s
)r$rinspectZismethodZgetfullargspecZ formatargspecargsZvarargsZvarkwZdefaultsrrZ isfunction isinstancetuplerHrIZgreyr preformat)r!objectr%modrrrDZclZanchorZnotetitlerZargspecZ docstringZdecldocrrr docroutines<           zServerHTMLDoc.docroutinec Csi}x,|jD] \}}d|||<||||<qW|j|}d|}|j|dd}|j||j|} | old| } |d| }g} t|j} x&| D]\}}| j|j|||dqW||jddd d j | }|S) Nz#-z)%sz#ffffffz#7799eez %sz

%s

)rZMethodsz#eeaa77rG) itemsrZheadingrrrCrLrZ bigsectionrr) r! server_nameZpackage_documentationrDZfdictkeyvalueheadresultrcontentsZ method_itemsrrr docserver$s"     zServerHTMLDoc.docserver)r$rQrRrrrrrrrrs),rc@s4eZdZddZddZddZddZd d Zd S) XMLRPCDocGeneratorcCsd|_d|_d|_dS)NzXML-RPC Server DocumentationzGThis server exports the following methods through the XML-RPC protocol.)rserver_documentation server_title)r!rrrr"DszXMLRPCDocGenerator.__init__cCs ||_dS)N)r)r!rrrrset_server_titleLsz#XMLRPCDocGenerator.set_server_titlecCs ||_dS)N)r)r!rrrrset_server_nameQsz"XMLRPCDocGenerator.set_server_namecCs ||_dS)N)r)r!rrrrset_server_documentationVsz+XMLRPCDocGenerator.set_server_documentationc Csi}x|jD]}||jkr(|j|}n|jdk rddg}t|jdrV|jj||d<t|jdrr|jj||d<t|}|dkr|}qt|jdsyt|j|}Wqtk r|}YqXq|}n|||<qWt }|j |j |j |}|j tj|j|S)N_get_method_argstringrrFr.r0)NN)r(rrrBrrFrrr rrrrZpagehtmlrr)r!rDrEr8Z method_infoZ documenterZ documentationrrrgenerate_html_documentation[s8          z.XMLRPCDocGenerator.generate_html_documentationN)r$rQrRr"rrrrrrrrr=s rc@seZdZddZdS)DocXMLRPCRequestHandlercCsf|js|jdS|jjjd}|jd|jdd|jdtt||j |j j |dS)Nzutf-8rjz Content-typez text/htmlzContent-length) rcrmrtrr3rurvrwrqrzr}r~)r!r9rrrdo_GETs  zDocXMLRPCRequestHandler.do_GETN)r$rQrRrrrrrrs rc@s"eZdZedddddfddZdS)DocXMLRPCServerTFNc Cs&tj||||||||tj|dS)N)rr"r)r!rrrrrrr rrrr"s zDocXMLRPCServer.__init__)r$rQrRrr"rrrrrsrc@seZdZddZddZdS)DocCGIXMLRPCRequestHandlercCsT|jjd}tdtdt|ttjjtjjj|tjjjdS)Nzutf-8zContent-Type: text/htmlzContent-Length: %d) rr3rrqr1rrrr~)r!r9rrrrs z%DocCGIXMLRPCRequestHandler.handle_getcCstj|tj|dS)N)rr"r)r!rrrr"s z#DocCGIXMLRPCRequestHandler.__init__N)r$rQrRrr"rrrrrsr__main__c@s"eZdZddZGdddZdS)ExampleServicecCsdS)NZ42r)r!rrrgetDataszExampleService.getDatac@seZdZeddZdS)zExampleService.currentTimecCs tjjS)N)datetimeZnowrrrrgetCurrentTimesz)ExampleService.currentTime.getCurrentTimeN)r$rQrR staticmethodrrrrr currentTimesrN)r$rQrRrrrrrrrsr localhost@cCs||S)Nr)xyrrrsradd)rz&Serving XML-RPC on localhost port 8000zKIt is advisable to run this example server within a secure, closed network.z& Keyboard interrupt received, exiting.)T)rr)-Z xmlrpc.clientrrrrrZ http.serverrrrrr1rrrHrrxZfcntl ImportErrorrrrrSrrrrZHTMLDocrrrrrr$rrrtr&powr#r-rZ serve_foreverKeyboardInterruptexitrrrrks^    ,ErQ