def handle_request(self): fd_sets = parrot.select([self.socket]) # FIXME: parrot.select should gain a timeout at some future time, # when that happens, this call should respect self.timeout: # fd_sets = parrot.select([self.socket], [], [], self.timeout) # if not fd_sets[0]: # self.handle_timeout() # return self._handle_request_noblock()
def run(self): server = parrot.Socket(self, type=parrot.SOCK_DGRAM) server.bind(("0.0.0.0", 4321)) #while not self.done: # Run until cancelled fd_set = parrot.select([server]) message, client = server.recvfrom(256) # <=256 byte datagram self.log("Client connected: %s" % str(client)) self.log("Echoing message") server.sendto(message, client)
def serve_forever(self, poll_interval=0.5): # FIXME: parrot.select doesn't respect timeout self._BaseServer__is_shut_down.clear() try: while not self._BaseServer__shutdown_request: r = parrot.select([self.socket]) if self.socket in r: self._handle_request_noblock() finally: self._BaseServer__shutdown_request = False self._BaseServer__is_shut_down.set()
def access_socket_main(self): server = parrot.Socket(self) server.bind(("0.0.0.0", 1234)) server.listen() inputs = [server] while not self.done: inputs_ready = parrot.select(inputs) for s in inputs_ready: if s == server: client = server.accept() # self.log("client %s accepted" % client) inputs.append(client) else: # self.log("receiving client request...") data = s.recv() if not data: s.close() inputs.remove(s) continue data = data.rstrip(None) # Remove trailing whitespace content = data.split(' ', 2) # Check availability and access rights of property if content[0] == 'read': if len(content) != 2: s.send("error malformed command '%s'\n" % data) continue key = content[1] access = access_rights(self, key) if (access and 'r' in access) or (key == 'capabilities'): s.send("ok %s\n" % self.get(key)) else: s.send("error read access denied for '%s'\n" % key) elif content[0] == 'write': if len(content) != 3: s.send("error malformed command '%s'\n" % data) continue key = content[1] access = access_rights(self, key) if not access or not 'w' in access: s.send("error write access denied for '%s'\n" % key) continue if not self.set(key, content[2]): s.send("error setting value failed for '%s'\n" % key) else: s.send("ok\n") else: s.send("error unexpected command '%s'\n" % content[0]) self.comm_chan.close()
def run(self): server = parrot.Socket(self) server.bind(("0.0.0.0", 80)) server.listen() self.log("listening 0.0.0.0:80") inputs = [server] #time.sleep(5) while not self.done: inputs_ready = parrot.select(inputs) for s in inputs_ready: if s == server: client = server.accept() self.log("client %s accepted" % client) inputs.append(client) else: content = s.recv() if content: self.log("receiving from client: %s" % str(content)) s.send("<html>Hello World!</html>") s.close() else: self.log("Client closed down!") inputs.remove(s)