6

i'm coding a Python Server (with SocketServer) for my Raspberry Pi. This server wait for incoming clients and activate any components like Relay and leds from that remote connection. This is my first project so i'm having some trouble:

in my main i create the object and i send the arraylist of the components i want to control; like led with all his own functions

Server.Server(ComponentList)
while True:
    do stuff

in Server.py i launch the server and set some 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 i coded the real server where are spawning the bugs

class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):

def handle(self):

    print "Client connected with ", 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 for 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 is not None:
                    if data == "1":
                        self.request.send(str(len(self.server.get_componentlist()))+" Components loaded\r\n")
                        c=0
                        for i 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 the loop while, after the password request, i can get the first choice (1,2 or 0) but i can't choose the device, and i can get another value from 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 the data = self.request... keeping data empty and print the output twice.

i guess i'm missing something and i cannot sending multiple information to the server with one connection.

Thanks

AeonDave
  • 781
  • 1
  • 8
  • 17

0 Answers0