python - Am I using classes and implementing functionality correctly? -


I have to create a listening server that will receive HTTP POST / XML alert traffic from a network sensor and parse the received XML. Being the beginner of Python, and having hard times understanding classes, I wanted to consult whether I am implementing classes and functionality, I am trying to get it right, and if better or " More Pythonic "Way It Is I hope that I am compelled to use classes to understand the better concept, I know that I can only use regular tasks.

The script is so far:

The xml.dom.minidom class to make the baseHttpSaver and the Socket Surface module, to create a threaded HTTP server, and to parse XML data. So far, I have two class sets - one for threading setup (Threaded HTTPPasser) and everything else (Threaded HTTPEx Handler). The "Everything" class is currently managing sessions and manipulating the data I think I need three sections, in the third to break the data manipulation and to manage the inbound connection except for the other. Will it be right? How do I pass the connection data from threaded HTPR questhunder class to new class which will have to be parsed and added XML data?

Any help for this newbie will be appreciated. The code is down, and it is currently working on all the time it accepts the incoming connection and makes an exclusive tag I'm interested in XML prints. Importing from CGI, HTTPServer SocketServer Import ThreadingMixIn #Server setting HOST = '' port = 5000 fond = 2048 square ThreadedHTTPServer (ThreadingMixIn, HTTPServer) imported from parseString import from BaseHTTPServer import BaseHTTPSEverHandler. : "" "Near class ThreadedHTTPRequestHandler (BaseHTTPRequestHandler)" Multi threading sets for this class server ": '' 'This class is the overall request handler. There are functions in this class to manage client connections and manipulate data' '' Def do_POST (self): '' 'This method contains HTTP POST data' '' Print ' The connection handles: 'self.client_address [0], self.client_address [1] CTYPE = self.headers.getheader (' content type ') content_len = integer (self.headers.getheader (' content-length ')) if CTYPE == 'Multilateral / Farm-Data': self.post_body = cgi.parse_multipart (self.rfile) elif CTYPE == 'application / x-www-form-urlencoded': self.post_body = self.rfile.read (content_len) ) Other: self.post_body = "" self.done (200) self.handleXML () def (self, code): '' 'Send an HTTP back 200 OK and try to close connection' '': self.send_response (code ) Exclude self.end_headers (): Print 'Connection to': ', self.client_address [0], self .client_address [1],' closed. ' #class XMLHandler (): def handleXML (self): '' 'This method parses and manipulates XML alert data' '' xml_dom = parseString (self.post_body) xmlTag = xml_dom.getElementsByTagName ('malware') [0] .toxml () # Print the XML tag and data in this format: & lt; Tag & gt; Data & lt; / Tag & gt; Print xmlTag if __name__ == "__main__": try: server = ThreadedHTTPServer (host, port), threadedHTTPRequestHandler) .serve_forever () except for KeyboardInterrupt printed: passed

You do not need the required third grade. What you have to do is a freestanding function,

  def handle_xml (post_body): #work  

so that you are no longer to store Need to post_body ThreadedHTTPRequestHandler

Class hierarchy is perfect for some problems, and for most, a bad fit. If you do not need it then do not use them, they'll just confuse your code.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

lua - HowTo create a fuel bar -