multithreading - SocketServer Python with multiple requests -
i'm coding python server (with socketserver) raspberry pi. server wait incoming clients , activate components relay , leds remote connection. first project i'm having trouble:
in main create object , send arraylist of components want control; led own functions
server.server(componentlist) while true: stuff
in server.py launch server , set settings
class server(): def __init__(self, componentlist): self.componentlist = componentlist self.current_component = 0 self.server = socket.threadedtcpserver((settings.hostname,settings.port), socket.threadedtcprequesthandler) self.server_thread = threading.thread(target=self.server.serve_forever) self.server_thread.daemon = true self.server_thread.start() self.server.set_componentlist(self.componentlist) self.server.set_current_component(self.current_component) def set_current_component(self, current_component): self.current_component = current_component def get_current_component(self): return self.current_component def set_componentlist(self, componentlist): self.componentlist = componentlist def get_componentlist(self): return self.componentlist
finally in socket.py coded real server spawning bugs
class threadedtcprequesthandler(socketserver.baserequesthandler): def handle(self): print "client connected ", self.client_address self.request.send("insert password\r\n") if self.request.recv(1024).strip() == settings.password: time.sleep(1) self.request.send("correct password\r\n") try: while 1: data = none self.request.send("---------------------\r\n") self.request.send("listening commands\r\n") self.request.send("1: connected devices\r\n") self.request.send("2: select devices\r\n") self.request.send("0: close connection\r\n") self.request.send("---------------------\r\n") data = self.request.recv(1024).strip() if data not none: if data == "1": self.request.send(str(len(self.server.get_componentlist()))+" components loaded\r\n") c=0 in self.server.get_componentlist(): self.request.send(str(str(c)+": "+i.get_note()+"\r\n")) c=c+1 if data == "2": data = self.request.recv(1024).strip() self.request.send("select component id\r\n") self.server.set_current_component(data) self.request.send("selected component: "+data+"\r\n") if data == "0": break finally: self.request.close() print "client exited" else: time.sleep(1) self.request.send("incorrect password\r\n") self.request.send("bye\r\n") self.request.close() print "client unauthorized"
class threadedtcpserver(socketserver.threadingmixin, socketserver.tcpserver):
allow_reuse_address = true def set_componentlist(self, componentlist): self.componentlist = componentlist def get_componentlist(self): return self.componentlist def set_current_component(self, current_component): self.current_component = current_component def get_current_component(self): return self.current_component
in loop while, after password request, can first choice (1,2 or 0) can't choose device, , can value request.recv(1024).
if data == "2": data = self.request.recv(1024).strip() self.request.send("select component id\r\n") self.server.set_current_component(data) self.request.send("selected component: "+data+"\r\n")
the program skip data = self.request... keeping data empty , print output twice.
i guess i'm missing , cannot sending multiple information server 1 connection.
thanks
Comments
Post a Comment