示例#1
0
    def _send_request(self, method, url, body=None, headers={}, timeout=None):
        p = urlparse(url)
        if p.hostname and p.hostname != self._host \
            or p.scheme == "http" and self._secure \
            or p.scheme == "https" and not self._secure \
            or p.port and p.port != self._port:
            self.fire(NotConnected())
            return

        resource = p.path
        if p.query:
            resource += "?" + p.query
        headers = Headers([(k, v) for k, v in list(headers.items())])
        # Clients MUST include Host header in HTTP/1.1 requests (RFC 2616)
        if "Host" not in headers:
            headers["Host"] = self._host \
                + (":" + str(self._port)) if self._port else ""
        command = "%s %s HTTP/1.1" % (method, resource)
        if body is not None:
            headers["Content-Length"] = len(body)
        message = "%s\r\n%s" % (command, headers)
        self._outstanding += 1
        if timeout is not None:
            self._timer = Timer(timeout, SocketError(ETIMEDOUT), self.channel) \
                .register(self)
        self.fire(write(message.encode('utf-8')), self._transport)
        if body is not None:
            self.fire(write(body), self._transport)
示例#2
0
 def _on_response(self, response):
     self._response = response
     self._pending -= 1
     if response.headers.get("Connection", "").lower() == "close" \
             or response.status != 101:
         self.fire(close(), self._transport)
         raise NotConnected()
     self._codec = WebSocketCodec(data=response.body.read(),
                                  channel=self._wschannel).register(self)
示例#3
0
 def _on_ready(self, event, *args, **kwargs):
     p = urlparse(self._url)
     if not p.hostname:
         raise ValueError("URL must be absolute")
     self._host = p.hostname
     if p.scheme == "ws":
         self._secure = False
         self._port = p.port or 80
     elif p.scheme == "wss":
         self._secure = True
         self._port = p.port or 443
     else:
         raise NotConnected()
     self._resource = p.path or "/"
     if p.query:
         self._resource += "?" + p.query
     self.fire(connect(self._host, self._port, self._secure),
               self._transport)