Exemplo n.º 1
0
    def connect(self):
        af = socket.AF_UNSPEC

        addrinfo = socket.getaddrinfo(
                self.host, self.port, af, 
                socket.SOCK_STREAM,
                0, 0)

        af, socktype, proto, canonname, sockaddr = addrinfo[0]

        self.stream = IOStream(
                socket.socket(af, socktype, proto),
                self.io_loop,
                max_buffer_size=self.MAX_BUF,
        )

        self.stream.connect(sockaddr, self.connected)

        self.request = NR()
        self.proto = WS(self)
Exemplo n.º 2
0
    def _execute(self, transforms, *args, **kwargs):
        from tornado.websocket import WebSocketProtocol8, WebSocketProtocol76
        
        self.open_args = args
        self.open_kwargs = kwargs

        # The difference between version 8 and 13 is that in 8 the
        # client sends a "Sec-Websocket-Origin" header and in 13 it's
        # simply "Origin".
        if self.request.headers.get("Sec-WebSocket-Version") in ("7", "8", "13"):
            self.ws_connection = WebSocketProtocol8(self)
            self.ws_connection.accept_connection()
            
        elif self.request.headers.get("Sec-WebSocket-Version"):
            self.stream.write(tornado.escape.utf8(
                "HTTP/1.1 426 Upgrade Required\r\n"
                "Sec-WebSocket-Version: 8\r\n\r\n"))
            self.stream.close()
            
        else:
            self.ws_connection = WebSocketProtocol76(self)
            self.ws_connection.accept_connection()
Exemplo n.º 3
0
class Connector(object):

    MAX_BUF = 104857600

    def __init__(self, host, port):
        self.host, self.port = host, port
        self.io_loop = ioloop.IOLoop()
        self.key = str(uuid4())

        self.connect()

    def connect(self):
        af = socket.AF_UNSPEC

        addrinfo = socket.getaddrinfo(
                self.host, self.port, af, 
                socket.SOCK_STREAM,
                0, 0)

        af, socktype, proto, canonname, sockaddr = addrinfo[0]

        self.stream = IOStream(
                socket.socket(af, socktype, proto),
                self.io_loop,
                max_buffer_size=self.MAX_BUF,
        )

        self.stream.connect(sockaddr, self.connected)

        self.request = NR()
        self.proto = WS(self)

    def connected(self):
        request_lines = [
                utf8("%s %s HTTP/1.1" % ('GET', '/events'))
        ]
        headers = (
                ("Host", "%s:%d" %(self.host,self.port)),
                ("Upgrade", "websocket"),
                ("Connection", "Upgrade"),
                ("Sec-Websocket-Version", 8),
                ("Sec-Websocket-Key", self.key,),
        )
        for k, v in headers:
            request_lines.append("%s: %s" % (k,v))

        self.stream.write(b("\r\n").join(request_lines) + b("\r\n\r\n"))

        self.stream.read_until(b("\r\n\r\n"), self._upgraded)

    def _upgraded(self, data):
        data = native_str(data.decode("latin1"))
        first_line, _, header_data = data.partition("\n")
        match = re.match("HTTP/1.[01] ([0-9]+)", first_line)
        assert match
        code = int(match.group(1))
        headers = HTTPHeaders.parse(header_data)

        self.accept = headers.get('Sec-Websocket-Accept')

        self.open()
        self.proto._receive_frame()

    def write_message(self, data):
        self.proto.write_message(data)