add "__all__"
This commit is contained in:
@@ -35,6 +35,8 @@ from ._exceptions import *
|
|||||||
from ._logging import *
|
from ._logging import *
|
||||||
from websocket._abnf import ABNF
|
from websocket._abnf import ABNF
|
||||||
|
|
||||||
|
__all__ = ["WebSocketApp"]
|
||||||
|
|
||||||
|
|
||||||
class WebSocketApp(object):
|
class WebSocketApp(object):
|
||||||
"""
|
"""
|
||||||
|
143
websocket/_handshake.py
Normal file
143
websocket/_handshake.py
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
"""
|
||||||
|
websocket - WebSocket client library for Python
|
||||||
|
|
||||||
|
Copyright (C) 2010 Hiroki Ohtani(liris)
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||||
|
Boston, MA 02110-1335 USA
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import six
|
||||||
|
if six.PY3:
|
||||||
|
from base64 import encodebytes as base64encode
|
||||||
|
else:
|
||||||
|
from base64 import encodestring as base64encode
|
||||||
|
|
||||||
|
import uuid
|
||||||
|
import hashlib
|
||||||
|
|
||||||
|
from ._logging import *
|
||||||
|
from ._url import *
|
||||||
|
from ._socket import*
|
||||||
|
from ._http import *
|
||||||
|
|
||||||
|
__all__ = ["handshake"]
|
||||||
|
|
||||||
|
# websocket supported version.
|
||||||
|
VERSION = 13
|
||||||
|
|
||||||
|
|
||||||
|
def handshake(sock, host, port, resource, **options):
|
||||||
|
headers, key = _get_handshake_headers(resource, host, port, options)
|
||||||
|
|
||||||
|
header_str = "\r\n".join(headers)
|
||||||
|
send(sock, header_str)
|
||||||
|
dump("request header", header_str)
|
||||||
|
|
||||||
|
resp = _get_resp_headers(sock)
|
||||||
|
success, subproto = _validate(resp, key, options.get("subprotocols"))
|
||||||
|
if not success:
|
||||||
|
raise WebSocketException("Invalid WebSocket Header")
|
||||||
|
|
||||||
|
return subproto
|
||||||
|
|
||||||
|
|
||||||
|
def _get_handshake_headers(resource, host, port, options):
|
||||||
|
headers = []
|
||||||
|
headers.append("GET %s HTTP/1.1" % resource)
|
||||||
|
headers.append("Upgrade: websocket")
|
||||||
|
headers.append("Connection: Upgrade")
|
||||||
|
if port == 80:
|
||||||
|
hostport = host
|
||||||
|
else:
|
||||||
|
hostport = "%s:%d" % (host, port)
|
||||||
|
headers.append("Host: %s" % hostport)
|
||||||
|
|
||||||
|
if "origin" in options:
|
||||||
|
headers.append("Origin: %s" % options["origin"])
|
||||||
|
else:
|
||||||
|
headers.append("Origin: http://%s" % hostport)
|
||||||
|
|
||||||
|
key = _create_sec_websocket_key()
|
||||||
|
headers.append("Sec-WebSocket-Key: %s" % key)
|
||||||
|
headers.append("Sec-WebSocket-Version: %s" % VERSION)
|
||||||
|
|
||||||
|
subprotocols = options.get("subprotocols")
|
||||||
|
if subprotocols:
|
||||||
|
headers.append("Sec-WebSocket-Protocol: %s" % ",".join(subprotocols))
|
||||||
|
|
||||||
|
if "header" in options:
|
||||||
|
headers.extend(options["header"])
|
||||||
|
|
||||||
|
cookie = options.get("cookie", None)
|
||||||
|
|
||||||
|
if cookie:
|
||||||
|
headers.append("Cookie: %s" % cookie)
|
||||||
|
|
||||||
|
headers.append("")
|
||||||
|
headers.append("")
|
||||||
|
|
||||||
|
return headers, key
|
||||||
|
|
||||||
|
|
||||||
|
def _get_resp_headers(sock, success_status=101):
|
||||||
|
status, resp_headers = read_headers(sock)
|
||||||
|
if status != success_status:
|
||||||
|
raise WebSocketException("Handshake status %d" % status)
|
||||||
|
return resp_headers
|
||||||
|
|
||||||
|
_HEADERS_TO_CHECK = {
|
||||||
|
"upgrade": "websocket",
|
||||||
|
"connection": "upgrade",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _validate(headers, key, subprotocols):
|
||||||
|
subproto = None
|
||||||
|
for k, v in _HEADERS_TO_CHECK.items():
|
||||||
|
r = headers.get(k, None)
|
||||||
|
if not r:
|
||||||
|
return False, None
|
||||||
|
r = r.lower()
|
||||||
|
if v != r:
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
if subprotocols:
|
||||||
|
subproto = headers.get("sec-websocket-protocol", None)
|
||||||
|
if not subproto or subproto not in subprotocols:
|
||||||
|
error("Invalid subprotocol: " + str(subprotocols))
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
result = headers.get("sec-websocket-accept", None)
|
||||||
|
if not result:
|
||||||
|
return False, None
|
||||||
|
result = result.lower()
|
||||||
|
|
||||||
|
if isinstance(result, six.text_type):
|
||||||
|
result = result.encode('utf-8')
|
||||||
|
|
||||||
|
value = (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode('utf-8')
|
||||||
|
hashed = base64encode(hashlib.sha1(value).digest()).strip().lower()
|
||||||
|
success = (hashed == result)
|
||||||
|
if success:
|
||||||
|
return True, subproto
|
||||||
|
else:
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
|
||||||
|
def _create_sec_websocket_key():
|
||||||
|
uid = uuid.uuid4()
|
||||||
|
return base64encode(uid.bytes).decode('utf-8').strip()
|
185
websocket/_http.py
Normal file
185
websocket/_http.py
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
"""
|
||||||
|
websocket - WebSocket client library for Python
|
||||||
|
|
||||||
|
Copyright (C) 2010 Hiroki Ohtani(liris)
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||||
|
Boston, MA 02110-1335 USA
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import six
|
||||||
|
import socket
|
||||||
|
|
||||||
|
try:
|
||||||
|
import ssl
|
||||||
|
from ssl import SSLError
|
||||||
|
if hasattr(ssl, "match_hostname"):
|
||||||
|
from ssl import match_hostname
|
||||||
|
else:
|
||||||
|
from backports.ssl_match_hostname import match_hostname
|
||||||
|
|
||||||
|
HAVE_SSL = True
|
||||||
|
except ImportError:
|
||||||
|
# dummy class of SSLError for ssl none-support environment.
|
||||||
|
class SSLError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
HAVE_SSL = False
|
||||||
|
|
||||||
|
if six.PY3:
|
||||||
|
from base64 import encodebytes as base64encode
|
||||||
|
else:
|
||||||
|
from base64 import encodestring as base64encode
|
||||||
|
|
||||||
|
from ._logging import *
|
||||||
|
from ._url import *
|
||||||
|
from ._socket import*
|
||||||
|
from ._exceptions import *
|
||||||
|
|
||||||
|
__all__ = ["connect", "read_headers"]
|
||||||
|
|
||||||
|
|
||||||
|
def connect(url, sockopt, sslopt, timeout, **options):
|
||||||
|
hostname, port, resource, is_secure = parse_url(url)
|
||||||
|
addrinfo_list, need_tunnel, auth = _get_addrinfo_list(hostname, port, is_secure, **options)
|
||||||
|
if not addrinfo_list:
|
||||||
|
raise WebSocketException(
|
||||||
|
"Host not found.: " + hostname + ":" + str(port))
|
||||||
|
|
||||||
|
sock = None
|
||||||
|
try:
|
||||||
|
sock = _open_socket(addrinfo_list, sockopt, timeout)
|
||||||
|
if need_tunnel:
|
||||||
|
sock = _tunnel(sock, hostname, port, auth)
|
||||||
|
|
||||||
|
if is_secure:
|
||||||
|
if HAVE_SSL:
|
||||||
|
sock = _ssl_socket(sock, sslopt)
|
||||||
|
else:
|
||||||
|
raise WebSocketException("SSL not available.")
|
||||||
|
|
||||||
|
return sock, (hostname, port, resource)
|
||||||
|
except:
|
||||||
|
if sock:
|
||||||
|
sock.close()
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
def _get_addrinfo_list(hostname, port, is_secure, **options):
|
||||||
|
phost, pport, pauth = get_proxy_info(hostname, is_secure, **options)
|
||||||
|
if not phost:
|
||||||
|
addrinfo_list = socket.getaddrinfo(hostname, port, 0, 0, socket.SOL_TCP)
|
||||||
|
return addrinfo_list, False, None
|
||||||
|
else:
|
||||||
|
pport = pport and pport or 80
|
||||||
|
addrinfo_list = socket.getaddrinfo(phost, pport, 0, 0, socket.SOL_TCP)
|
||||||
|
return addrinfo_list, True, pauth
|
||||||
|
|
||||||
|
|
||||||
|
def _open_socket(addrinfo_list, sockopt, timeout):
|
||||||
|
err = None
|
||||||
|
for addrinfo in addrinfo_list:
|
||||||
|
family = addrinfo[0]
|
||||||
|
sock = socket.socket(family)
|
||||||
|
sock.settimeout(timeout)
|
||||||
|
for opts in DEFAULT_SOCKET_OPTION:
|
||||||
|
sock.setsockopt(*opts)
|
||||||
|
for opts in sockopt:
|
||||||
|
sock.setsockopt(*opts)
|
||||||
|
|
||||||
|
address = addrinfo[4]
|
||||||
|
try:
|
||||||
|
sock.connect(address)
|
||||||
|
except socket.error as error:
|
||||||
|
error.remote_ip = str(address[0])
|
||||||
|
if error.errno in (errno.ECONNREFUSED, ):
|
||||||
|
err = error
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
raise err
|
||||||
|
|
||||||
|
return sock
|
||||||
|
|
||||||
|
|
||||||
|
def _ssl_socket(sock, sslopt):
|
||||||
|
sslopt = dict(cert_reqs=ssl.CERT_REQUIRED)
|
||||||
|
certPath = os.path.join(
|
||||||
|
os.path.dirname(__file__), "cacert.pem")
|
||||||
|
if os.path.isfile(certPath):
|
||||||
|
sslopt['ca_certs'] = certPath
|
||||||
|
sslopt.update(sslopt)
|
||||||
|
check_hostname = sslopt.pop('check_hostname', True)
|
||||||
|
sock = ssl.wrap_socket(sock, **sslopt)
|
||||||
|
if (sslopt["cert_reqs"] != ssl.CERT_NONE
|
||||||
|
and check_hostname):
|
||||||
|
match_hostname(sock.getpeercert(), hostname)
|
||||||
|
|
||||||
|
|
||||||
|
def _tunnel(sock, host, port, auth):
|
||||||
|
debug("Connecting proxy...")
|
||||||
|
connect_header = "CONNECT %s:%d HTTP/1.0\r\n" % (host, port)
|
||||||
|
# TODO: support digest auth.
|
||||||
|
if auth and auth[0]:
|
||||||
|
auth_str = auth[0]
|
||||||
|
if auth[1]:
|
||||||
|
auth_str += ":" + auth[1]
|
||||||
|
encoded_str = base64encode(auth_str.encode()).strip().decode()
|
||||||
|
connect_header += "Proxy-Authorization: Basic %s\r\n" % encoded_str
|
||||||
|
connect_header += "\r\n"
|
||||||
|
dump("request header", connect_header)
|
||||||
|
|
||||||
|
send(sock, connect_header)
|
||||||
|
|
||||||
|
try:
|
||||||
|
status, resp_headers = read_headers()
|
||||||
|
except Exepiton as e:
|
||||||
|
raise WebSocketProxyException(str(e))
|
||||||
|
|
||||||
|
if status != 200:
|
||||||
|
raise WebSocketProxyException(
|
||||||
|
"failed CONNECT via proxy status: " + str(status))
|
||||||
|
|
||||||
|
|
||||||
|
def read_headers(sock):
|
||||||
|
status = None
|
||||||
|
headers = {}
|
||||||
|
trace("--- response header ---")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
line = recv_line(sock)
|
||||||
|
line = line.decode('utf-8').strip()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
trace(line)
|
||||||
|
if not status:
|
||||||
|
|
||||||
|
status_info = line.split(" ", 2)
|
||||||
|
status = int(status_info[1])
|
||||||
|
else:
|
||||||
|
kv = line.split(":", 1)
|
||||||
|
if len(kv) == 2:
|
||||||
|
key, value = kv
|
||||||
|
headers[key.lower()] = value.strip().lower()
|
||||||
|
else:
|
||||||
|
raise WebSocketException("Invalid header")
|
||||||
|
|
||||||
|
trace("-----------------------")
|
||||||
|
|
||||||
|
return status, headers
|
@@ -25,6 +25,9 @@ import logging
|
|||||||
_logger = logging.getLogger()
|
_logger = logging.getLogger()
|
||||||
_traceEnabled = False
|
_traceEnabled = False
|
||||||
|
|
||||||
|
__all__ = ["enableTrace", "dump", "error", "debug", "trace",
|
||||||
|
"isEnableForError", "isEnableForDebug"]
|
||||||
|
|
||||||
|
|
||||||
def enableTrace(tracable):
|
def enableTrace(tracable):
|
||||||
"""
|
"""
|
||||||
|
@@ -26,7 +26,7 @@ import six
|
|||||||
from ._exceptions import *
|
from ._exceptions import *
|
||||||
from ._utils import *
|
from ._utils import *
|
||||||
|
|
||||||
DEFAULT_SOCKET_OPTION = [(socket.SOL_TCP, socket.TCP_NODELAY, 1),]
|
DEFAULT_SOCKET_OPTION = [(socket.SOL_TCP, socket.TCP_NODELAY, 1)]
|
||||||
if hasattr(socket, "SO_KEEPALIVE"):
|
if hasattr(socket, "SO_KEEPALIVE"):
|
||||||
DEFAULT_SOCKET_OPTION.append((socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1))
|
DEFAULT_SOCKET_OPTION.append((socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1))
|
||||||
if hasattr(socket, "TCP_KEEPIDLE"):
|
if hasattr(socket, "TCP_KEEPIDLE"):
|
||||||
@@ -38,6 +38,10 @@ if hasattr(socket, "TCP_KEEPCNT"):
|
|||||||
|
|
||||||
_default_timeout = None
|
_default_timeout = None
|
||||||
|
|
||||||
|
__all__ = ["DEFAULT_SOCKET_OPTION", "setdefaulttimeout", "getdefaulttimeout",
|
||||||
|
"recv", "recv_line", "send"]
|
||||||
|
|
||||||
|
|
||||||
def setdefaulttimeout(timeout):
|
def setdefaulttimeout(timeout):
|
||||||
"""
|
"""
|
||||||
Set the global timeout setting to connect.
|
Set the global timeout setting to connect.
|
||||||
@@ -76,6 +80,7 @@ def recv(sock, bufsize):
|
|||||||
|
|
||||||
return bytes
|
return bytes
|
||||||
|
|
||||||
|
|
||||||
def recv_line(sock):
|
def recv_line(sock):
|
||||||
line = []
|
line = []
|
||||||
while True:
|
while True:
|
||||||
|
@@ -23,6 +23,9 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
|
|||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
__all__ = ["parse_url", "get_proxy_info"]
|
||||||
|
|
||||||
|
|
||||||
def parse_url(url):
|
def parse_url(url):
|
||||||
"""
|
"""
|
||||||
parse url and the result is tuple of
|
parse url and the result is tuple of
|
||||||
@@ -68,6 +71,7 @@ def parse_url(url):
|
|||||||
|
|
||||||
DEFAULT_NO_PROXY_HOST = ["localhost", "127.0.0.1"]
|
DEFAULT_NO_PROXY_HOST = ["localhost", "127.0.0.1"]
|
||||||
|
|
||||||
|
|
||||||
def _is_no_proxy_host(hostname, no_proxy):
|
def _is_no_proxy_host(hostname, no_proxy):
|
||||||
if not no_proxy:
|
if not no_proxy:
|
||||||
v = os.environ.get("no_proxy", "").replace(" ", "")
|
v = os.environ.get("no_proxy", "").replace(" ", "")
|
||||||
@@ -77,21 +81,26 @@ def _is_no_proxy_host(hostname, no_proxy):
|
|||||||
|
|
||||||
return hostname in no_proxy
|
return hostname in no_proxy
|
||||||
|
|
||||||
|
|
||||||
def get_proxy_info(hostname, is_secure, **options):
|
def get_proxy_info(hostname, is_secure, **options):
|
||||||
"""
|
"""
|
||||||
try to retrieve proxy host and port from environment if not provided in options.
|
try to retrieve proxy host and port from environment
|
||||||
|
if not provided in options.
|
||||||
result is (proxy_host, proxy_port, proxy_auth).
|
result is (proxy_host, proxy_port, proxy_auth).
|
||||||
proxy_auth is tuple of username and password of proxy authentication information.
|
proxy_auth is tuple of username and password
|
||||||
|
of proxy authentication information.
|
||||||
|
|
||||||
hostname: websocket server name.
|
hostname: websocket server name.
|
||||||
|
|
||||||
is_secure: is the connection secure? (wss)
|
is_secure: is the connection secure? (wss)
|
||||||
looks for "https_proxy" in env before falling back to "http_proxy"
|
looks for "https_proxy" in env
|
||||||
|
before falling back to "http_proxy"
|
||||||
|
|
||||||
options: "http_proxy_host" - http proxy host name.
|
options: "http_proxy_host" - http proxy host name.
|
||||||
"http_proxy_port" - http proxy port.
|
"http_proxy_port" - http proxy port.
|
||||||
"http_no_proxy" - host names, which doesn't use proxy.
|
"http_no_proxy" - host names, which doesn't use proxy.
|
||||||
"http_proxy_auth" - http proxy auth infomation. tuple of username and password.
|
"http_proxy_auth" - http proxy auth infomation.
|
||||||
|
tuple of username and password.
|
||||||
defualt is None
|
defualt is None
|
||||||
"""
|
"""
|
||||||
if _is_no_proxy_host(hostname, options.get("http_no_proxy", None)):
|
if _is_no_proxy_host(hostname, options.get("http_no_proxy", None)):
|
||||||
@@ -99,7 +108,9 @@ def get_proxy_info(hostname, is_secure, **options):
|
|||||||
|
|
||||||
http_proxy_host = options.get("http_proxy_host", None)
|
http_proxy_host = options.get("http_proxy_host", None)
|
||||||
if http_proxy_host:
|
if http_proxy_host:
|
||||||
return http_proxy_host, options.get("http_proxy_port", 0), options.get("http_proxy_auth", None)
|
port = options.get("http_proxy_port", 0)
|
||||||
|
auth = options.get("http_proxy_auth", None)
|
||||||
|
return http_proxy_host, port, auth
|
||||||
|
|
||||||
env_keys = ["http_proxy"]
|
env_keys = ["http_proxy"]
|
||||||
if is_secure:
|
if is_secure:
|
||||||
@@ -113,6 +124,3 @@ def get_proxy_info(hostname, is_secure, **options):
|
|||||||
return proxy.hostname, proxy.port, auth
|
return proxy.hostname, proxy.port, auth
|
||||||
|
|
||||||
return None, 0, None
|
return None, 0, None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -22,6 +22,8 @@ Copyright (C) 2010 Hiroki Ohtani(liris)
|
|||||||
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
__all__ = ["NoLock", "validate_utf8", "extract_err_message"]
|
||||||
|
|
||||||
class NoLock(object):
|
class NoLock(object):
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pass
|
pass
|
||||||
|
Reference in New Issue
Block a user