CGI XML-RPC (xmlrpccgi.py) demo?

Eugene Leitl Eugene.Leitl at lrz.uni-muenchen.de
Thu Mar 29 07:32:39 EST 2001


Caution, confused braindump follows.

I'm trying to wrap the coils of my puny little mind around XML-RPC,
specifically Kalle Svensson's CGI XML-RPC server:

	http://mail.python.org/pipermail/python-announce-list/2001-March/000698.html

(interesting enough, according to Google there seems to be also Jeff's

	http://starship.python.net/crew/jjkunce/python/xmlrpccgi.tgz

advertised as "A XML-RPC "server" that is accessed via a web server
through CGI calls (as opposed to a long-running XML-RPC server that
monitors a dedicated port).", which seems 1) entirely unrelated to 2) much
smaller than Kalle's code 3) designed for Winders).

To reiterate, I'm trying to be able to trap URI's looking like

	http://blah.net:666/cgi/boo.cgi?hiss=aargh#boom

or parameters POSTed via HTML like

<FORM TARGET="result">
<INPUT TYPE=button VALUE="Start search now!" onClick="jsCopyFormData();return (0);">
</FORM>
<FORM name=submitform METHOD=post TARGET="result" ACTION="">
<INPUT TYPE=hidden NAME="smi"     VALUE="">
<INPUT TYPE=hidden NAME="jme"     VALUE="">
<INPUT TYPE=hidden NAME="stype"   VALUE="">
<INPUT TYPE=hidden NAME="sstype"  VALUE="">
<INPUT TYPE=hidden NAME="checkin" VALUE="$cwkey">
<INPUT TYPE=hidden NAME="usr" VALUE="$user">
</FORM> 

to some piece of code called boo.cgi, using above arguments as parameters.
Lacking that, I don't mind going the usual route with CGI

import cgi
results=cgi.FieldStorage()
etc.

To make things more difficult, I also might have to process parameters
passed via GET, which current (ick, feh) Perl bit does thusly:

sub get_params {
        if ($ENV{'REQUEST_METHOD'} eq 'GET') {
                @pairs = split(/&/, $ENV{'QUERY_STRING'});
        } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
                read(STDIN, $buf, $ENV{'CONTENT_LENGTH'});
                @pairs = split(/&/, $buf);
        } else {
#               &error('request');
        }
        foreach $pair (@pairs) {
                local($name, $value) = split(/=/, $pair);
                $name =~ tr/+/ /;
                $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                $value =~ s/<!--(.|\n)-->//g;
                $form{$name} = $value;
#               last;
        }
        return(%form);
}
1;                 

Anyway, as above xmlrpccgi demo doesn't seem to work without input, and I
can't generate input without knowing what it does I'd appreciate any
pointers to simple demo code which I could put into cgi-bin and test
drive a bit.

I guess I could also use a regexp looking for a cgi-bin/boo.cgi pattern in
the code below instead of a specific reference, and then parse the
self.path string by hand, but that looks unnecessary ugly. I might have to
go that route, though, if I can't figure any better way to process
arguments. Anyway, comments welcome.

import StringIO
import SimpleHTTPServer
 
BaseClass = SimpleHTTPServer.SimpleHTTPRequestHandler
 
CounterTemplate = """ <H1>Server
Statistics</H1>
 
This <A HREF=./>server</A> has been accessed
<b>%d</b> times.  """
 
count = 0
 
class MyRequestHandler(BaseClass):
 
    def do_GET(self):
        global count
        count = count + 1
        BaseClass.do_GET(self)
 
    def send_head(self):
        if self.path == "/counter.html":
            return self.send_counter()
        else:
            return BaseClass.send_head(self)
 
    def send_counter(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        text = CounterTemplate % count
        return StringIO.StringIO(text)
 
def test():
    SimpleHTTPServer.test(MyRequestHandler)
 
test()  





More information about the Python-list mailing list