Skip to content

Port forwarding not working with python requests library #569

@Rami-Kassouf-FOO

Description

@Rami-Kassouf-FOO

Which project are you reporting a bug for?

kr8s

What happened?

kubectl create namespace argo
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

after the apply you will get an initial root token, put this token as the password in the payload and it should be reproduced

This works (with manual port-forwarding)

import requests
from utils.kubernetes_utils import K8

url = "http://127.0.0.1:1009/api/v1/session"
payload = {"username": "admin", "password": "---"}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers, verify=False)

print(response.status_code, response.text)

This does not work

import requests
from utils.kubernetes_utils import K8

payload = {"username": "admin", "password": "---"}
headers = {"Content-Type": "application/json"}

with K8().get_service("argocd-server", namespace="argo").portforward(remote_port=80, local_port="auto") as local_port:
    url = f"http://127.0.0.1:{local_port}/api/v1/session"
    response = requests.post(url, json=payload, headers=headers, verify=False)
    print(response.status_code, response.text)

Anything else?

Error Log

Traceback (most recent call last):
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request
    response = conn.getresponse()
               ^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connection.py", line 507, in getresponse
    httplib_response = super().getresponse()
                       ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1395, in getresponse
    response.begin()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 325, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 294, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\adapters.py", line 667, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connectionpool.py", line 843, in urlopen       
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\util\retry.py", line 474, in increment
    raise reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\util\util.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connectionpool.py", line 789, in urlopen       
    response = self._make_request(
               ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connectionpool.py", line 536, in _make_request 
    response = conn.getresponse()
               ^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\urllib3\connection.py", line 507, in getresponse       
    httplib_response = super().getresponse()
                       ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 1395, in getresponse
    response.begin()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 325, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2544.0_x64__qbz5n2kfra8p0\Lib\http\client.py", line 294, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\User\Desktop\FOO_\Project Setup Automation\test.py", line 38, in <module>
    response = requests.post(url, json=payload, headers=headers, verify=False)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\Desktop\FOO_\Project Setup Automation\.venv\Lib\site-packages\requests\adapters.py", line 682, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions