-1

I am developing a python app for my raspberry. The aim of this piece of software is to listen POST from another software, when I get the POST I want to send a message through a zigbee module attached to the raspberry.

My problem is that how I have implemented the web server for the raspberry in python, I am not able to access the class that is able to send the message through zigbee.

[Update] To make it more clear, my problem is that I cant access to wsanProtocol in the do_POST method. So I can't use my interface for xbee.

Could you give me any ideas on how to do it?

Here is the code of my webserver:

class webServer(threading.Thread):    
    def __init__(self, serverAddress, port, wsanProtocol):
        self.wsanProtocol = wsanProtocol
        self.serverAddress = serverAddress

    self.port = port
    self.serverRunning = True
        threading.Thread.__init__(self)

    def run(self):
        server_class = BaseHTTPServer.HTTPServer        
        httpd = server_class((self.serverAddress, self.port), MyHandler)
        print time.asctime(), "Server Starts - %s:%s" % (self.serverAddress, self.port)
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            pass
        httpd.server_close()
        print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
    def do_GET(s):
        """Respond to a GET request."""
        if None != re.search('api/v1/nodo/*', s.path):
            nodeID = s.path.split('/')[-1]
            print 'Solicitando informacion del nodo '+nodeID
            s.send_response(200)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            #Prepare JSON with node information        
            #jsonnode = json.dumps(nodeOjbect.__dict__)
            jsonnode = '{ "mac":'+nodeID+', "bat":4.0 }'
            s.wfile.write(jsonnode)        
        if None != re.search('api/v1/st_act/*', s.path):
            nodeID = s.path.split('/')[-1]
            print 'Solicitando informacion del actuador '+nodeID
            s.send_response(200)
            s.send_header("Content-type", "text/html")
            s.end_headers()
            #Prepare JSON with node information        
            #jsonnode = json.dumps(nodeOjbect.__dict__)
            jsonnode = '{ "mac":'+nodeID+', "status":"off" }'
            s.wfile.write(jsonnode)

    def do_POST(self):
        print "ejecutando POST"
        if None != re.search('api/v1/act/*', self.path):
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'application/json':
                length = int(self.headers.getheader('content-length'))
                data = cgi.parse_qs(self.rfile.read(length), keep_blank_values=1)
                print data
                req = json.loads(data.keys()[0])
                resCommand = None
                #Cant get access to my wsanProtocol!!
                #webServer.wsanProtocol.executeCommand(10, resCommand, req.tolist())
                print 'Actuacion sobre el nodo ' + req["mac"]
                self.send_response(200)

Thank you very much!

cjbs
  • 329
  • 1
  • 4
  • 12
  • You've posted a lot of irrelevant code, but haven't said what exactly the problem is. What happens when you try to call zigbee? – Daniel Roseman Oct 23 '14 at 09:33
  • Dont think it is irrelevant, Im trying to access to webServer in do_POST method. When I do that I get a fail of python telling me that wsanProtocol does not have executeCommand procedure. – cjbs Oct 23 '14 at 09:39
  • Try to advance testable step by testable step and not build the whole thing and then find it doesn't fully work. First, test that you can do `wsanProtocol.executeCommand(10, something, somethingelse)`. Then test that you get the web server working e.g., by adding debug logging. – Anton Oct 23 '14 at 09:44
  • Not sure if I got you right Anton, I have already tested the executeCommand in another socket-based server and it is working without problems. And the webserver by itself (without calling the executeCommand) is also working well... the issue is that cant figure out how to make wsanProtocol accesible in do_POST – cjbs Oct 23 '14 at 09:46

1 Answers1

0

wsanProtocol is an attribute of the server instance, not of the class. You can access that instance via self.server, as shown in the BaseHTTPRequestHandler docs.

Daniel Roseman
  • 541,889
  • 55
  • 754
  • 786
  • Thank you very much Daniel, I tried the following call: self.server.wsanProtocol.executeCommand(10, resCommand, req.tolist()) But got an error saying: AttributeError: HTTPServer instance has no attribute 'wsanProtocol' I guess Im doing something wrong... – cjbs Oct 23 '14 at 09:50
  • OK, I see that wsanProtocol is actually an argument to the thread. But can you explain why you're bothering with threading at all, since you're only listening to a single POST? What's the point? – Daniel Roseman Oct 23 '14 at 10:05
  • Yes you are right, I could avoid the thread. The thing was that my original server was a socket-based one and I needed a thread, so I probably didnt realized. – cjbs Oct 23 '14 at 10:24