[Python-au] Python Sockets

Andy Dent dent@oofile.com.au
Wed, 22 Jan 2003 13:01:13 +0800


At 15:31 +1100 22/01/2003, Graeme Matthew wrote:
>Garth, with my server I dont want the client to actually execute an xml
>based rpc request, im sending an xml order (and orderlines) across the wire,
>in the server there is a conditional check based on the message type in this
>case order, this is then delegated to an orderProcessor, that uses
>xml.dom.minidom

This makes a lot of sense - I'm doing something similar in the BEEP-based
protocol for the IBVA brainwave stuff (except I've just prototyped it in
Python, the real thing is C++ as it also runs on PocketPC and Palm).

You need to also wrap your parsing in case of error, eg:

            try:
                dom = xml.dom.minidom.parseString(inStr)
            except xml.sax.expatreader.SAXParseException, e:
                print "Parsing error", e
                print "parsing\n---[%s]\n---\n"%inStr
                break

BTW you may find this useful - the Flash guy I'm working with was sending
an extra null at the end, which Flash requires, so I use this to clean up
such rubbish at the end of an XML packet.

    def recvCleanPacket(self, numBytes):
    " use instead of just using recv"
        inStr=self.request.recv(numBytes)
        lastI = len(inStr)-1
        #while lastI>0 and ord(inStr[lastI])==0:  if you just wanted to
strip nulls
        while lastI>1 and not inStr[lastI-1]==">":  # strip any garbage off
end of XML
            lastI-=1
        inStr = inStr[0:lastI]
        return inStr


-- 
Andy Dent BSc  MACS  AACM   http://www.oofile.com.au/
OOFILE - Database, Reports, Graphs, GUI for c++ on Mac, Unix & Windows
PP2MFC - PowerPlant->MFC portability