Python: WebSocketBadStatusException: Handshake status 400 Bad Request

Created on 19 Sep 2019  路  3Comments  路  Source: kubernetes-client/python

What happened *:
Run pod_exec example
Raise exception: *
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

url https://35.197.67.77/api/v1/namespaces/default/pods/time-series-daily-fb7c655d9-dztlz/exec?command=%2Fbin%2Fsh&command=-c&command=echo+This+message+goes+to+stderr+%3E%262%3B+echo+This+message+goes+to+stdout&stderr=True&stdin=False&stdout=True&tty=False
headers {'Accept': '*/*', 'Content-Type': 'application/json', 'User-Agent': 'Swagger-Codegen/10.0.1/python', 'authorization': 'Bearer ya29.c.EluIB9pDg1h-JblzCfQnMY3jswAfyVEZnILrS0hI1GTfS9Cx-hUXFfp88dk_9nZbZy4NlCzNudL-vfumlh8thrBTUHqFb8zMArmBtKxRmmgy7kLV1xE3UASB9eO8'}
get_websocket_url wss://35.197.67.77/api/v1/namespaces/default/pods/time-series-daily-fb7c655d9-dztlz/exec?command=%2Fbin%2Fsh&command=-c&command=echo+This+message+goes+to+stderr+%3E%262%3B+echo+This+message+goes+to+stdout&stderr=True&stdin=False&stdout=True&tty=False
sslopt {'cert_reqs': <VerifyMode.CERT_REQUIRED: 2>, 'ca_certs': '/tmp/tmp6ezew065', 'check_hostname': False}

Traceback (most recent call last):
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 260, in websocket_call
    client = WSClient(configuration, get_websocket_url(url), headers)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 78, in __init__
    self.sock.connect(url, header=header)
  File "/usr/local/lib/python3.7/site-packages/websocket/_core.py", line 226, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python3.7/site-packages/websocket/_handshake.py", line 79, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python3.7/site-packages/websocket/_handshake.py", line 160, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d %s", status, status_message, resp_headers)
websocket._exceptions.WebSocketBadStatusException: Handshake status 400 Bad Request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 36, in <module>
    exec_pod_cmd(K8S_PODNAME, ['echo This message goes to stderr >&2; echo This message goes to stdout'])
  File "test.py", line 26, in exec_pod_cmd
    resp = stream(api.connect_get_namespaced_pod_exec, pod_managed_name, namespace, command=exec_command, stderr=True, stdin=False, stdout=True, tty=False,_request_timeout=30, _preload_content=False)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 36, in stream
    return func(*args, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 835, in connect_get_namespaced_pod_exec
    (data) = self.connect_get_namespaced_pod_exec_with_http_info(name, namespace, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 935, in connect_get_namespaced_pod_exec_with_http_info
    collection_formats=collection_formats)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 334, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/client/api_client.py", line 168, in __call_api
    _request_timeout=_request_timeout)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/stream.py", line 31, in _intercept_request_call
    return ws_client.websocket_call(config, *args, **kwargs)
  File "/usr/local/airflow/.local/lib/python3.7/site-packages/kubernetes/stream/ws_client.py", line 268, in websocket_call
    raise ApiException(status=0, reason=str(e))
kubernetes.client.rest.ApiException: (0)
Reason: Handshake status 400 Bad Request

Environment:

  • Kubernetes version (v1.12.9-gke.7):
  • OS (Debian 9):
  • Python version (3.6, 3.7)
  • Python client version (10.0.1)
kinbug

Most helpful comment

@EricCat How did you get it to work with Istio ?

All 3 comments

I found it's due to Istio blocked the WebSocket that sent from kubernetes. so the issue will be closed

@EricCat How did you get it to work with Istio ?

@Anany-k No, there isn't a solution to get python CLI to work with Istio. I created a new namespace (k8s) just for running the python CLI pods. Otherwise, I have to use kubectl exec to call the pods in the Istio namespace

Was this page helpful?
0 / 5 - 0 ratings