diff options
author | liris <liris.pp@gmail.com> | 2018-10-28 10:32:51 +0900 |
---|---|---|
committer | liris <liris.pp@gmail.com> | 2018-10-28 10:32:51 +0900 |
commit | 8637ace03264acfaa4ca5a590659fd774e2cbbf9 (patch) | |
tree | 66f02d07bd7e6a713f54d9853e34acbd3a4b9692 | |
parent | 05c1d9444cb4d97736097f2af671933e9b764f64 (diff) | |
parent | bb3159de4f50f18626e3762fbc8356619fcd74ec (diff) | |
download | websocket-client-8637ace03264acfaa4ca5a590659fd774e2cbbf9.tar.gz |
add docs
-rw-r--r-- | websocket/_app.py | 10 | ||||
-rw-r--r-- | websocket/_core.py | 9 | ||||
-rw-r--r-- | websocket/_handshake.py | 18 | ||||
-rw-r--r-- | websocket/_http.py | 2 |
4 files changed, 30 insertions, 9 deletions
diff --git a/websocket/_app.py b/websocket/_app.py index e5ea661..9cea877 100644 --- a/websocket/_app.py +++ b/websocket/_app.py @@ -48,7 +48,7 @@ class Dispatcher: def read(self, sock, read_callback, check_callback): while self.app.sock.connected: r, w, e = select.select( - (self.app.sock.sock, ), (), (), self.ping_timeout) + (self.app.sock.sock, ), (), (), self.ping_timeout) if r: if not read_callback(): break @@ -206,13 +206,13 @@ class WebSocketApp(object): True if other exception was raised during a loop """ - if ping_timeout is not None and (not ping_timeout or ping_timeout <= 0): + if ping_timeout is not None and ping_timeout <= 0: ping_timeout = None if ping_timeout and ping_interval and ping_interval <= ping_timeout: raise WebSocketException("Ensure ping_interval > ping_timeout") - if sockopt is None: + if not sockopt: sockopt = [] - if sslopt is None: + if not sslopt: sslopt = {} if self.sock: raise WebSocketException("socket is already opened") @@ -237,7 +237,7 @@ class WebSocketApp(object): try: self.sock = WebSocket( self.get_mask_key, sockopt=sockopt, sslopt=sslopt, - fire_cont_frame=self.on_cont_message and True or False, + fire_cont_frame=self.on_cont_message is not None, skip_utf8_validation=skip_utf8_validation, enable_multithread=True if ping_interval else False) self.sock.settimeout(getdefaulttimeout()) diff --git a/websocket/_core.py b/websocket/_core.py index aea16e6..a85fc78 100644 --- a/websocket/_core.py +++ b/websocket/_core.py @@ -210,6 +210,7 @@ class WebSocket(object): "http_proxy_auth" - http proxy auth information. tuple of username and password. default is None + "redirect_limit" -> number of redirects to follow. "subprotocols" - array of available sub protocols. default is None. "socket" - pre-initialized stream socket. @@ -220,6 +221,13 @@ class WebSocket(object): try: self.handshake_response = handshake(self.sock, *addrs, **options) + for attempt in range(options.pop('redirect_limit', 3)): + if self.handshake_response.status in SUPPORTED_REDIRECT_STATUSES: + url = self.handshake_response.headers['location'] + self.sock.close() + self.sock, addrs = connect(url, self.sock_opt, proxy_info(**options), + options.pop('socket', None)) + self.handshake_response = handshake(self.sock, *addrs, **options) self.connected = True except: if self.sock: @@ -482,6 +490,7 @@ def create_connection(url, timeout=None, class_=WebSocket, **options): tuple of username and password. default is None "enable_multithread" -> enable lock for multithread. + "redirect_limit" -> number of redirects to follow. "sockopt" -> socket options "sslopt" -> ssl option "subprotocols" - array of available sub protocols. diff --git a/websocket/_handshake.py b/websocket/_handshake.py index d9dae47..3b8ddc0 100644 --- a/websocket/_handshake.py +++ b/websocket/_handshake.py @@ -36,7 +36,15 @@ if six.PY3: else: from base64 import encodestring as base64encode -__all__ = ["handshake_response", "handshake"] +if six.PY3: + if six.PY34: + from http import client as HTTPStatus + else: + from http import HTTPStatus +else: + import httplib as HTTPStatus + +__all__ = ["handshake_response", "handshake", "SUPPORTED_REDIRECT_STATUSES"] if hasattr(hmac, "compare_digest"): compare_digest = hmac.compare_digest @@ -47,6 +55,8 @@ else: # websocket supported version. VERSION = 13 +SUPPORTED_REDIRECT_STATUSES = [HTTPStatus.MOVED_PERMANENTLY, HTTPStatus.FOUND, HTTPStatus.SEE_OTHER] + CookieJar = SimpleCookieJar() @@ -67,6 +77,8 @@ def handshake(sock, hostname, port, resource, **options): dump("request header", header_str) status, resp = _get_resp_headers(sock) + if status in SUPPORTED_REDIRECT_STATUSES: + return handshake_response(status, resp, None) success, subproto = _validate(resp, key, options.get("subprotocols")) if not success: raise WebSocketException("Invalid WebSocket Header") @@ -134,9 +146,9 @@ def _get_handshake_headers(resource, host, port, options): return headers, key -def _get_resp_headers(sock, success_status=101): +def _get_resp_headers(sock, success_statuses=(101, 301, 302, 303)): status, resp_headers, status_message = read_headers(sock) - if status != success_status: + if status not in success_statuses: raise WebSocketBadStatusException("Handshake status %d %s", status, status_message) return status, resp_headers diff --git a/websocket/_http.py b/websocket/_http.py index 8d82521..fe97480 100644 --- a/websocket/_http.py +++ b/websocket/_http.py @@ -101,7 +101,7 @@ def _open_proxied_socket(url, options, proxy): def connect(url, options, proxy, socket): - if proxy.host and not socket and not(proxy.type == 'http'): + if proxy.host and not socket and not (proxy.type == 'http'): return _open_proxied_socket(url, options, proxy) hostname, port, resource, is_secure = parse_url(url) |