示例#1
0
    def serve(port_queue: "Queue[int]", shutdown_queue: "Queue[bool]") -> None:
        httpd = TCPServer(("", 0), handler_class)
        httpd.timeout = 0.1
        if ssl_context:
            httpd.socket = ssl_context.wrap_socket(httpd.socket,
                                                   server_side=True)

        port_queue.put(httpd.server_address[1])
        while shutdown_queue.empty():
            httpd.handle_request()
示例#2
0
def main():
    global domoticz_user,domoticz_pass,domoticz_url,domoticz_idx
    try:
        print('Init intesisHome')
        initIntesis()
        server = TCPServer(('', 8000), intesisServer)
        print ('started httpserver...')
        if domoticz_url != '' and domoticz_idx != '':
            # Start timer to poll domoticz
            getSetPoint()

        while True:
            server.handle_request()
    except KeyboardInterrupt:
        print ('^C received, shutting down server')
        server.socket.close()
示例#3
0
class EventListener:
    """
    EventListener is used to collect events from the Marathon eventbus.
    """

    def __init__(self, request_count=100, port=9999, path='.', queue=None):
        self.request_count = request_count
        self.queue = queue
        self.port = port
        self.httpd = TCPServer(("", self.port), Handler)
        self.path = os.path.abspath(path) + '/marathon_log.json'
        self.counter = 0
        print('Server listening on port {}'.format(port))

    def log_output(self):
        print('Processed {} requests'.format(self.counter))

    def get_requests(self):
        responses = list()
        while True:
            self.httpd.handle_request()
            self.counter += 1
            self.log_output()
            response = self.httpd.RequestHandlerClass.posts.pop()
            if self.queue:
                self.queue.put(response)
            else:
                responses.append(response)
            if self.counter >= self.request_count:
                break
        return responses

    def record_requests(self):
        with open(self.path, 'w') as fp:
            while self.counter < self.request_count:
                self.httpd.handle_request()
                self.counter += 1
                self.log_output()
                fp.write(json.dumps(self.httpd.RequestHandlerClass.posts.pop()))
示例#4
0
 def serve(port_queue, shutdown_queue):
   httpd = TCPServer(("", 0), handler_class)
   httpd.timeout = 0.1
   port_queue.put(httpd.server_address[1])
   while shutdown_queue.empty():
     httpd.handle_request()
示例#5
0
 def serve(port_queue: "Queue[int]", shutdown_queue: "Queue[bool]") -> None:
     httpd = TCPServer(("", 0), handler_class)
     httpd.timeout = 0.1
     port_queue.put(httpd.server_address[1])
     while shutdown_queue.empty():
         httpd.handle_request()
示例#6
0
Created on 24 aug. 2016

@author: Ruud
"""

from sliplib import encode, decode
from socketserver import TCPServer, BaseRequestHandler


class SlipHandler(BaseRequestHandler):
    # Dedicated handler to show the encoded bytes.
    def handle(self):
        while True:
            data = self.request.recv(1024)
            if not data:
                break

            print('raw data received:', data)
            message = decode(data)
            print('decoded data:', message)

            data_to_send = encode(bytes(reversed(message)))
            print('raw data to send:', data_to_send)
            self.request.sendall(data_to_send)


if __name__ == '__main__':
    server = TCPServer(('localhost', 0), SlipHandler)
    print('Slip server listening on', server.server_address)
    server.handle_request()
示例#7
0
        def run(self):
            """
            This starts a rendezvous "server" that listens to a port for
            messages from a given client. It will handle the rendezvous
            protocol based on being the "contacted" party.
            The thread should be set to DONE if this node decides to
            be the "contacting" party instead.
            """
            success = False
            tries = 0
            server = None

            while not success and tries < 10:
                self.port = self.port + tries
                try:
                    logger.debug("Opening 'server' port: {}".format(self.port))
                    server = TCPServer((socket.gethostname(), self.port),
                                       self._RendezvousHandler)
                    success = True
                except:
                    logger.debug("Problems opening port")
                    tries = tries + 1

            if not success:
                logger.error(
                    "Could not open port for rendezvous server. Failure.")
                self.done = True
                self.result = (False, None)
                self.queue.put("FAIL")
                self.callback()
                return

            # Set up data needed by the handler
            server.timeout = self.timeout
            server.shared_secret = self.shared_secret
            server.own_rendezvous_info = self.own_rendezvous_info
            server.provider = self.provider
            server.timeout = 0.5
            server.done = False
            server.result = (False, None)

            # Signal to parent that we are now up and running
            self.queue.put("RUNNING")

            start = time.time()
            while not server.done and not self.done:
                server.handle_request()
                now = time.time()
                if now - start > self.timeout:
                    self.done = True

            # If the server is done, we are done as well.
            if server.done:
                server.socket.close()
                self.done = True
                self.result = server.result
            else:
                server.socket.close()
                self.done = True

            self.queue.put("STOPPED")
            self.callback()