Python网页服务器使用其他类

2024-04-20 03:49:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的树莓开发一个python应用程序。这个软件的目的是监听来自另一个软件的POST,当我收到POST时,我想通过一个连接到树莓的zigbee模块发送消息。在

我的问题是,如何在python中实现raspberry的web服务器,我无法访问能够通过zigbee发送消息的类。在

[更新] 更清楚地说,我的问题是我不能在do\u POST方法中访问wsanProtocol。所以我不能用我的xbee接口。在

你能告诉我怎么做吗?在

以下是我的Web服务器的代码:

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)

非常感谢!在


Tags: pathselfsendportresponsedefpostdo