Why requests raise this exception "check_hostname requires server_hostname"?

84,162

Solution 1

As I understand, in new urllib3 the main schema of proxy was changed... can read here. This settings help for me (for urllib3=1.26.4).

An old

proxy={
    'http':'8.88.888.8:8888',
    'https':'8.88.888.8:8888'
    }

The new

proxy={
    'https': 'https://8.88.888.8:8888',
    'http': 'http://8.88.888.8:8888',
    } 

UPDATED

I have met this issue again for requests 2.26.0, but in this time it works with an old schema...

proxy={
    'http':'8.88.888.8:8888',
    'https':'8.88.888.8:8888'
    }

Solution 2

as a work around:

pip install urllib3==1.25.11

Solution 3

I have solved the problem. It is a bug with urllib3.

You can use pip install urllib3==1.25.8

Solution 4

Downgrading urllib3 has solved it for me too. Though I had to reset the proxy env var: https_proxy="http://127.0.0.1:<PROXY_PORT>" pip install urllib3==1.25.11

Without that, I was getting the following error and it couldn't downgrade urllib3 (see SSLError installing with pip).

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1091)'))': /simple/pip/

Solution 5

In my case, Charles' windows proxy function caused this error. After I close windows proxy, this error is gone.

Share:
84,162
KEVIN
Author by

KEVIN

Updated on July 09, 2022

Comments

  • KEVIN
    KEVIN almost 2 years
    p={
        'http':'http://my correct proxy here',
        'https':'https://my correct proxy here'
        }
    self.response=requests.get(url=url,headers=self.headers,timeout=(6,15),proxies=p)
    

    And then it raise the exception:

    Traceback (most recent call last):
      File "C:\Users\xyl13509876955\Desktop\Monitor\dicks.py", line 61, in send_request
        self.response=requests.get(url=url,headers=self.headers,timeout=(6,15),proxies=p)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 76, in get
        return request('get', url, params=params, **kwargs)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 61, in request
        return session.request(method=method, url=url, **kwargs)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 542, in request
        resp = self.send(prep, **send_kwargs)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 655, in send
        r = adapter.send(request, **kwargs)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send
        timeout=timeout
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
        self._prepare_proxy(conn)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
        conn.connect()
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 359, in connect
        conn = self._connect_tls_proxy(hostname, conn)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 506, in _connect_tls_proxy
        ssl_context=ssl_context,
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket
        ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl
        return ssl_context.wrap_socket(sock)
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 423, in wrap_socket
        session=session
      File "C:\Users\xyl13509876955\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 827, in _create
        raise ValueError("check_hostname requires server_hostname")
    ValueError: check_hostname requires server_hostname
    

    Please help me solve the problem and the best way is to show me the right code. I am very confused and frustrated for the problem!!

    • DeepSpace
      DeepSpace about 3 years
    • S.V
      S.V almost 3 years
      I get the error just from running pip3 install --upgrade pip
  • dhgoratela
    dhgoratela about 3 years
    @scrubjay55 This actually worked! I spent one day and had almost given up on the SSL error. Tried a lot of solutions but nothing worked.. Relieved finally :) Thank you! How did you find out the fix?
  • Steven Lee
    Steven Lee about 3 years
    mee too. What's the value of PROXY_PORT?
  • mikhail
    mikhail about 3 years
    Depends on your proxy server. It should be your proxy address (not necessarily 127.0.0.1 as in my case) and the port.
  • shcrela
    shcrela about 3 years
    In my case, changing the value of the https_proxy variable from https to http solved the issue by itself.
  • wings
    wings about 3 years
    Same error when executing "pip install urllib3==1.25.11" ...
  • Theo F
    Theo F almost 3 years
    this worked for me. But I'm keen to properly fix this issue soon.
  • mihca
    mihca over 2 years
    I had to turn of the proxy (in Windows settings) to be able to install urllib3==1.25.11 as suggested in other answers. Otherwise the installation of old urllib3 using pip failed with the same error message. After the installation of old urllib3, the issue is gone even with active proxy settings.
  • mihca
    mihca over 2 years
    I had to disable the proxy in Windows settings to be able to downgrade urllib3. Afterwards the issue is gone even with active proxy settings.
  • diaryfolio
    diaryfolio over 2 years
    can confirm this worked with python 2.7 and very useful to put in requirements.txt to ensure the version is precise
  • Ian
    Ian over 2 years
    Similar situation, I turned down my "System Proxy" and the problem solved.
  • ah-shiang han
    ah-shiang han over 2 years
    Thanks, this works in windows too, set HTTPS_PROXY=127.0.0.1:3128, then it worked
  • Kevin Seymour
    Kevin Seymour over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • Eric Aya
    Eric Aya over 2 years
    This has already been mentioned in other answers. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.
  • Corey
    Corey over 2 years
    Work for me! But can you explain why?
  • JaY KuMaR
    JaY KuMaR over 2 years
    worked for me thanks a lot man ;)
  • Vincent
    Vincent over 2 years
    This is the right answer!!!
  • Sithum Batrow
    Sithum Batrow over 2 years
    Yes but I did not vote
  • nlhnt
    nlhnt over 2 years
    This is might be important to some: github.com/conda/conda/issues/10590
  • Diego Suarez
    Diego Suarez about 2 years
    This solved my problem with python 3.9.6 requests module using proxies, thanks KEVIN