<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.zugiart &#187; rpc</title>
	<atom:link href="http://www.zugiart.com/tag/rpc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.zugiart.com</link>
	<description>Software Engineering, buddhism, and everything else in between.</description>
	<lastBuildDate>Mon, 09 Jan 2012 05:50:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Classic ASP: cross domain json-rpc proxy controller</title>
		<link>http://www.zugiart.com/2011/07/classic-asp-cross-domain-json-rpc-proxy-controller/</link>
		<comments>http://www.zugiart.com/2011/07/classic-asp-cross-domain-json-rpc-proxy-controller/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 01:26:37 +0000</pubDate>
		<dc:creator>zen</dc:creator>
				<category><![CDATA[geeky]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[json-rpc]]></category>
		<category><![CDATA[notes]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[rpc]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://www.zugiart.com/?p=1676</guid>
		<description><![CDATA[A controller written in classic ASP / VBScript to proxy json-rpc request to a remote service point.]]></description>
			<content:encoded><![CDATA[<p>Through some sheer bad luck, I am now working on restructuring a system written in classic ASP. There&#8217;s a json-rpc service point on a separate domain that the app could consume, and I wanted it to consume it rather than reinventing the functionality again on the system. As much as I hate VBscript, ASP and IIS, I went googling and figure out a way forward.</p>
<p>Basically, it&#8217;s difficult to integrate JSON-RPC into a classic ASP system. However, it is very easy to integrate it into jQuery. Problem is, the service existed on an external domain, so what I needed is a proxy asp controller that will forward json-rpc request &amp; response back and forth between the local client and the external json-rpc point.</p>
<p><a href="http://www.zugiart.com/main/wp-content/uploads/2011/07/proxy-json-rpc.png"><img class="aligncenter size-full wp-image-1678" title="proxy-json-rpc" src="http://www.zugiart.com/main/wp-content/uploads/2011/07/proxy-json-rpc.png" alt="" width="605" height="143" /></a></p>
<p>One word of warning though: I haven&#8217;t tested this code extensively so use it at your own risk. &#8211; anyway here&#8217;s the controller script:</p>

<div class="wp_syntax"><div class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> LANGUAGE<span style="color: #006600; font-weight: bold;">=</span>VBScript <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;%</span>
<span style="color: #008000;">'# ==========================================================================================================</span>
<span style="color: #008000;">'#</span>
<span style="color: #008000;">'# JSON-RPC Proxy ASP [classic asp]</span>
<span style="color: #008000;">'#</span>
<span style="color: #008000;">'# This asp page/component will forward the json-rpc request to an existing json-rpc service </span>
<span style="color: #008000;">'# and transparently forward the result back to the caller via the HTTP response. </span>
<span style="color: #008000;">'# </span>
<span style="color: #008000;">'# @author Zen Sugiarto</span>
<span style="color: #008000;">'# ==========================================================================================================</span>
&nbsp;
<span style="color: #008000;">'# configuration</span>
<span style="color: #008000;">'# -------------</span>
&nbsp;
<span style="color: #008000;">'# This URl points to the json-rpc service point. </span>
<span style="color: #008000;">'# if your system uses a different configuration mechanism, please adjust it accordingly</span>
<span style="color: #008000;">'#</span>
<span style="color: #990099; font-weight: bold;">Dim</span> servicePointUrl
servicePointUrl <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;http://url-to-json-rpc/point&quot;</span>
&nbsp;
<span style="color: #008000;">'# main body of execution</span>
<span style="color: #008000;">'# ----------------------</span>
&nbsp;
<span style="color: #008000;">'# this function allows for msg to be logged into a log file so we can trace / analyze the traffic.</span>
<span style="color: #0000ff; font-weight: bold;">Sub</span> logInfo <span style="color: #006600; font-weight:bold;">&#40;</span>message<span style="color: #006600; font-weight:bold;">&#41;</span>
	logFile <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #990099; font-weight: bold;">Server</span>.<span style="color: #330066;">MapPath</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;event.log&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">Const</span> ForWriting   <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #800000;">2</span>
    <span style="color: #990099; font-weight: bold;">Const</span> ForAppending <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #800000;">8</span>
    <span style="color: #990099; font-weight: bold;">Set</span> fs <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #330066;">CreateObject</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;Scripting.FileSystemObject&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">If</span> fs.<span style="color: #330066;">FileExists</span><span style="color: #006600; font-weight:bold;">&#40;</span>logFile<span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #990099; font-weight: bold;">Then</span>
        <span style="color: #990099; font-weight: bold;">Set</span> logFile <span style="color: #006600; font-weight: bold;">=</span> fs.<span style="color: #330066;">OpenTextFile</span><span style="color: #006600; font-weight:bold;">&#40;</span>logFile, ForAppending<span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">Else</span>
        <span style="color: #990099; font-weight: bold;">Set</span> logFile <span style="color: #006600; font-weight: bold;">=</span> fs.<span style="color: #9900cc;">CreateTextFile</span><span style="color: #006600; font-weight:bold;">&#40;</span>logFile, <span style="color: #0000ff; font-weight: bold;">True</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #990099; font-weight: bold;">End</span> <span style="color: #990099; font-weight: bold;">If</span>    
    logFile.<span style="color: #9900cc;">WriteLine</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">Now</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span> <span style="color: #006600; font-weight: bold;">&amp;</span> vbTab <span style="color: #006600; font-weight: bold;">&amp;</span> message<span style="color: #006600; font-weight:bold;">&#41;</span>
    logFile.<span style="color: #330066;">Close</span>
    <span style="color: #990099; font-weight: bold;">Set</span> logFile <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
    <span style="color: #990099; font-weight: bold;">Set</span> fs <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #0000ff; font-weight: bold;">nothing</span>
<span style="color: #990099; font-weight: bold;">End</span> <span style="color: #0000ff; font-weight: bold;">Sub</span>
&nbsp;
<span style="color: #008000;">' we read all of the POST data, json-rpc supports get, but this component only does POST for now.</span>
<span style="color: #008000;">' the safe array read from .BinaryRead is then converted into string, which is then sent into the </span>
<span style="color: #008000;">' forwarding HTTP request object to the bus.</span>
<span style="color: #990099; font-weight: bold;">dim</span> safeArray, <span style="color: #330066;">totalBytes</span>
<span style="color: #330066;">totalBytes</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">TotalBytes</span>
safeArray<span style="color: #006600; font-weight: bold;">=</span><span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">BinaryRead</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #330066;">totalBytes</span><span style="color: #006600; font-weight:bold;">&#41;</span>
jsonBody <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #cc0000;">&quot;&quot;</span>
<span style="color: #990099; font-weight: bold;">For</span> n <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #800000;">1</span> <span style="color: #990099; font-weight: bold;">To</span> LenB<span style="color: #006600; font-weight:bold;">&#40;</span>safeArray<span style="color: #006600; font-weight:bold;">&#41;</span>
  jsonBody <span style="color: #006600; font-weight: bold;">=</span> jsonBody <span style="color: #006600; font-weight: bold;">&amp;</span> Chr<span style="color: #006600; font-weight:bold;">&#40;</span>AscB<span style="color: #006600; font-weight:bold;">&#40;</span>MidB<span style="color: #006600; font-weight:bold;">&#40;</span>safeArray,n,<span style="color: #800000;">1</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #990099; font-weight: bold;">Next</span>
&nbsp;
<span style="color: #008000;">' create the HTTP request object to be forwarded into the service bus, </span>
<span style="color: #008000;">' inject the json body in.</span>
<span style="color: #990099; font-weight: bold;">Dim</span> httpreq
<span style="color: #990099; font-weight: bold;">Set</span> httpreq <span style="color: #006600; font-weight: bold;">=</span> <span style="color: #990099; font-weight: bold;">Server</span>.<span style="color: #330066;">CreateObject</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;MSXML2.XMLHTTP&quot;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
httpreq.<span style="color: #330066;">open</span> <span style="color: #cc0000;">&quot;POST&quot;</span>,servicePointUrl, <span style="color: #0000ff; font-weight: bold;">False</span>
httpreq.<span style="color: #9900cc;">setRequestHeader</span> <span style="color: #cc0000;">&quot;Content-Type&quot;</span>,<span style="color: #cc0000;">&quot;application/json-rpc&quot;</span>
<span style="color: #008000;">'httpreq.setRequestHeader &quot;Cookie&quot;,Request.Cookies</span>
&nbsp;
<span style="color: #008000;">' Send out the request</span>
<span style="color: #990099; font-weight: bold;">On</span> <span style="color: #990099; font-weight: bold;">Error</span> <span style="color: #990099; font-weight: bold;">Resume</span> <span style="color: #990099; font-weight: bold;">Next</span>
httpreq.<span style="color: #330066;">send</span> jsonBody
logInfo<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #cc0000;">&quot;REQUEST={ queryString=&quot;</span> <span style="color: #006600; font-weight: bold;">&amp;</span> <span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">QueryString</span> <span style="color: #006600; font-weight: bold;">&amp;</span> <span style="color: #cc0000;">&quot;, cookies=&quot;</span><span style="color: #006600; font-weight: bold;">&amp;</span> <span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">Cookies</span> <span style="color: #006600; font-weight: bold;">&amp;</span><span style="color: #cc0000;">&quot;, totalBytes=&quot;</span> <span style="color: #006600; font-weight: bold;">&amp;</span> <span style="color: #990099; font-weight: bold;">Request</span>.<span style="color: #330066;">TotalBytes</span> <span style="color: #006600; font-weight: bold;">&amp;</span> <span style="color: #cc0000;">&quot;, jsonBody=&quot;</span><span style="color: #006600; font-weight: bold;">&amp;</span>jsonBody<span style="color: #006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color: #008000;">' TODO: implement error handling</span>
&nbsp;
<span style="color: #008000;">' get response text, set mime type to application/json-rpc and write the bus response to the response stream</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">ContentType</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;application/json-rpc&quot;</span>
<span style="color: #990099; font-weight: bold;">Response</span>.<span style="color: #330066;">Write</span><span style="color: #006600; font-weight:bold;">&#40;</span>httpreq.<span style="color: #9900cc;">responseText</span><span style="color: #006600; font-weight:bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">%&gt;</span></pre></div></div>

<p>And now I can skip the whole shitty ASP architecture altogether and just go straight to web2.0 style programming. The controller will also log and output the request into a file called event.log. I haven&#8217;t finished writing a proper log rotator and whatnot with it. Just comment out the logInfo part if you&#8217;re not using it. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.zugiart.com/2011/07/classic-asp-cross-domain-json-rpc-proxy-controller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python XML-RPC</title>
		<link>http://www.zugiart.com/2009/08/python-xml-rpc/</link>
		<comments>http://www.zugiart.com/2009/08/python-xml-rpc/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 02:10:35 +0000</pubDate>
		<dc:creator>zen</dc:creator>
				<category><![CDATA[geeky]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[rpc]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.zugiart.com/main/?p=398</guid>
		<description><![CDATA[XML-RPC in python, made easy. Copy paste and code away.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve added this snippets into my <a href="/main/notes/python">python notes</a>. Here they are for convenience.</p>
<h2>Dynamic Xml-Rpc server</h2>
<p>This server code will use python &#8220;reflection&#8221; to publish all methods whose name are prefixed with PREFIX. In this case, the prefix is xmlrpc_, so all methods within this server such as xmlrpc_hello, will get published. The code already uses options, so it can be easily started like so:</p>
<p>python server.py &#8211;host=$HOSTNAME &#8211;port=portNum</p>
<h3>Server skeleton code</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">SimpleXMLRPCServer</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">SimpleXMLRPCServer</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DynamicXMLRPCServer<span style="color: black;">&#40;</span><span style="color: #dc143c;">SimpleXMLRPCServer</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    An XMLRPC server that will register all function whose name are prefixed with PREFIX via xml-rpc
    Zen Sugiarto - 2011
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># this is the prefix used by this sever to decide whether a given method should be published over xml-rpc or not.</span>
    PREFIX=<span style="color: #483d8b;">&quot;xmlrpc_&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># constructor</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, params<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">params</span>=params
        <span style="color: #008000;">self</span>.<span style="color: black;">host</span>=params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'host'</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">port</span>=params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'port'</span><span style="color: black;">&#93;</span>
        <span style="color: #dc143c;">SimpleXMLRPCServer</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">host</span>, <span style="color: #008000;">self</span>.<span style="color: black;">port</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># register all xmlrpc_ functions over xml-rpc</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> name<span style="color: black;">&#91;</span>:<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>DynamicXMLRPCServer.<span style="color: black;">PREFIX</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> == DynamicXMLRPCServer.<span style="color: black;">PREFIX</span>:
                function=<span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,name<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;registering function: %s as %s &quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span> name, name<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>DynamicXMLRPCServer.<span style="color: black;">PREFIX</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">register_function</span><span style="color: black;">&#40;</span>function, name<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>DynamicXMLRPCServer.<span style="color: black;">PREFIX</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># note: xmlrpc_ prefix is used by this server to mark that this function will be published</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> xmlrpc_hello<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,msg<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        A simple hello function used for simple ping check. 
        @param msg - msg to pass to hello
        @return dictionary {&quot;hello&quot;:msg}
        &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;hello&quot;</span>:msg<span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> xmlrpc_describe<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Retrieves the description/documentation of available functions within this service.
        by the grace of python's .__doc__ object.
        @return dictionary of the following format
        { 
            'function_name' : 'documentation/description of that function '} 
        } 
        &quot;&quot;&quot;</span>
        metaDescription=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> name<span style="color: black;">&#91;</span>:<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>DynamicXMLRPCServer.<span style="color: black;">PREFIX</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> == DynamicXMLRPCServer.<span style="color: black;">PREFIX</span>:
                function=<span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,name<span style="color: black;">&#41;</span>
                metaDescription<span style="color: black;">&#91;</span>name<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>DynamicXMLRPCServer.<span style="color: black;">PREFIX</span><span style="color: black;">&#41;</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>=function.__doc__
        <span style="color: #ff7700;font-weight:bold;">return</span> metaDescription<span style="color: #66cc66;">;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> xmlrpc_yourFunction<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Put in more function here!~
        &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;IMPLEMENT ME&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; main method &quot;&quot;&quot;</span>
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot; 
        Template service object - please put your comment in here
        &quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--host&quot;</span>, dest=<span style="color: #483d8b;">&quot;host&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;hostname to publish the service against&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--port&quot;</span>, dest=<span style="color: #483d8b;">&quot;port&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;port number to use&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span> options.<span style="color: black;">host</span> == <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">or</span> options.<span style="color: black;">port</span> == <span style="color: #008000;">None</span> <span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;insufficient options provided. try --help&quot;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
    server = DynamicXMLRPCServer<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'host'</span>:options.<span style="color: black;">host</span>,<span style="color: #483d8b;">'port'</span>:<span style="color: #008000;">int</span><span style="color: black;">&#40;</span>options.<span style="color: black;">port</span><span style="color: black;">&#41;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    server.<span style="color: black;">serve_forever</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Dynamic XML-RPC Client</h3>
<p>This will invoke Hello world function via xml-rpc, this is as simple as it gets</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xmlrpclib</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #dc143c;">xmlrpclib</span>.<span style="color: black;">ServerProxy</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://localhost:8000/&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hello</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;zen&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>But this is even better: this is a client that will allow you to connect to any xml-rpc service (yes, even java ones in xml-rpc) and call any function as long as you know its signature.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">xmlrpclib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">readline</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">cmd</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">traceback</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">pprint</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">datetime</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> XmlRpcClientConsole<span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span>.<span style="color: black;">Cmd</span><span style="color: black;">&#41;</span>: 
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Generic xml rpc client console, works with any xml rpc service
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, url<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">cmd</span>.<span style="color: black;">Cmd</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">url</span> = url<span style="color: #66cc66;">;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">server</span> = <span style="color: #dc143c;">xmlrpclib</span>.<span style="color: black;">ServerProxy</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">pp</span> = <span style="color: #dc143c;">pprint</span>.<span style="color: black;">PrettyPrinter</span><span style="color: black;">&#40;</span>indent=<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">updatePrompt</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> updatePrompt<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">prompt</span> = <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>[%s] %s<span style="color: #000099; font-weight: bold;">\n</span>xmlrpc &gt;&gt; &quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">datetime</span>.<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">strftime</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%Y.%m.%d %H:%M:%S&quot;</span><span style="color: black;">&#41;</span>,<span style="color: #008000;">self</span>.<span style="color: black;">url</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> default<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, line<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            result=<span style="color: #008000;">None</span>
            <span style="color: #ff7700;font-weight:bold;">exec</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;result=self.server.%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">pp</span>.<span style="color: #dc143c;">pprint</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># default formatter: pretty print the resultant data</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, ex:
            <span style="color: #dc143c;">traceback</span>.<span style="color: black;">print_exc</span><span style="color: black;">&#40;</span>ex<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span><span style="color: #66cc66;">;</span> <span style="color: #008000;">self</span>.<span style="color: black;">updatePrompt</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> do_help<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,line<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;GENERIC XML RPC CLIENT CONSOLE - HELP&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;-------------------------------------&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Simply type in the function name on the xml-rpc service end, like you would&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;on any good ol' python program. For example, if the other side has a method&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;called helloWorld(strparam), you can invoke it like so:&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;... xmlrpc &gt;&gt; helloWorld('hello zen')&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;the client is therefore completely transparent and exposes the full capabilities&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;of the server end. have fun.&quot;</span> <span style="color: #808080; font-style: italic;"># -zen</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;Generic XML RPC Client console&quot;&quot;&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--url&quot;</span>, dest=<span style="color: #483d8b;">&quot;url&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;The XML RPC service URL to connect&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--cmd&quot;</span>, dest=<span style="color: #483d8b;">&quot;cmd&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;A one-off command to execute&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: black;">url</span> == <span style="color: #008000;">None</span> :
        <span style="color: #dc143c;">parser</span>.<span style="color: black;">print_help</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    console = XmlRpcClientConsole<span style="color: black;">&#40;</span>options.<span style="color: black;">url</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> options.<span style="color: #dc143c;">cmd</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">None</span> :
        <span style="color: #808080; font-style: italic;"># run the cmd and bomb out</span>
        console.<span style="color: black;">default</span><span style="color: black;">&#40;</span>options.<span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;for help using the console, type 'help'&quot;</span>
        <span style="color: #808080; font-style: italic;"># enter into cmd loop for interactive session</span>
        console.<span style="color: black;">cmdloop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Example</h3>
<p>Starting the server:</p>
<pre>python server.py --host=localhost --port=6666 </pre>
<p>Starting the client in console mode:</p>
<pre>python client.py --url=http://localhost:6666 </pre>
<p>Within the console: </p>
<pre>
[2011.06.22 11:31:25] http://localhost:6666
xmlrpc >> hello("zen")
{ 'hello': 'zen'}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.zugiart.com/2009/08/python-xml-rpc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

