#!/usr/bin/python """ This isa http server for bots based on python BaseHTTPServer designed to work with bot.py Please address comments to or to or join us in open development at Note: be sure to include the zone*.html files in the working dir to run it. Access it in you browser with the url: credits This derivitive of BaseHTTPSwever was originall written by Daniel Biddle licenseUrl version see server_version in class BotHTTPRequestHandler below revised 05/29/2002 10 am status works for level of development knownBugs if command has no arg, then ?command will blow up server lastProgress "revised name to sailorServer, instantiating a sailor." priorProgress "processArgs passess ?command1=arg&command2=arg to agent" inProgress nextToDo0 accept any html file on the path and look for {} performatives nextToDo1 nextToDo2 accept input parameters for domain, port, and BotName thinkingAbout0 how to use POST to be compatable with REST thinkingAbout1 "". """ import BaseHTTPServer import urllib import sailor def getZone(htmlFile): try: f=open(htmlFile,'r') s='' for i in f: s=s+i return s except: return 'Could not find '+htmlFile+' in working directory' class BotHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): me=sailor.wake(MyName='SethBot',ownedBy='Seth') server_version = 'sailorServer/0.050' zone1=getZone('zone1.html') zone2=getZone('zone2.html') debugSt='' def do_GET(self): """Serve a GET request.""" # so i can see what i am doing # setting debug to 1 will show dignostics on screen self.debug=0 self.debugSt='' self.showDebug('path', self.path) mpath,margs=urllib.splitquery(self.path) if margs == None: self.showDebug('splitquery', 'None') else: self.processArgs(margs) # all output to the screen comes through here x=self.zone1+self.debugSt+'

'+self.me.sayGraphServer()+'
'+self.zone2 self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() self.wfile.write(x) def processArgs(self,xargs): args=xargs.split('&') toDo=[] for arg in args: xname,xvalue=urllib.splitvalue(arg) toDo.append([xname,xvalue]) self.showDebug('xname', xname) self.showDebug('xvalue', xvalue) for do in toDo: # fix the urlencoding do[1]=urllib.unquote(do[1].replace('+',' ')) #send it to the agent for execuition self.showDebug('toDo', toDo) self.me.doit(toDo) def do_POST(self): # Can't seem to get POST to work .. oh well ... # I'll just have to hide from the REST cops self.send_response(200) self.send_header("Content-type", "text/html") self.end_headers() x='path='+self.path self.wfile.write(x) def showDebug(self, xname, xstr): if self.debug: n=xname if type(xstr)==type('a'): v=xstr else: v=repr(xstr) l=v.replace('<','<') r=l.replace('>','>') self.debugSt=self.debugSt+n+'= '+r+'
' def test(HandlerClass = BotHTTPRequestHandler, ServerClass = BaseHTTPServer.HTTPServer): BaseHTTPServer.test(HandlerClass, ServerClass) if __name__ == '__main__': test()