简体   繁体   中英

Why requests raise this exception "check_hostname requires server_hostname"?

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!!

as a work around:

pip install urllib3==1.25.11

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'
    }

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

You can use pip install urllib3==1.25.8

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/

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

Had the same problem on Ubuntu 18.04 using Python 3.6.9, searched through a lot of pages and finally fixed it via trial and error.

The fix is changing the environment variable https_proxy . Previously https_proxy is https://127.0.0.1:<PORT> and I removed the https header via:

export https_proxy=127.0.0.1:<PORT>

Then I can do python3 -m pip install --upgrade urllib3 .

None of the answers did it for me. Here's a silly looking solution that fixed my problem:

If you, like me trying to install packages in a corporate environment, you probably have given a proxy to limit interactions with world wide web, so all you have to do is to export that proxy in http and https format.

something like this:

export http_proxy=http://x.x.x.x:8080

export https_proxy=http://x.x.x.x:8080

(my proxy didnt use the ssl, it didnt even matter.)

replace x with your proxy ip

On Linux, the problem can be resolved by replacing https with http in the proxy settings environment variable export https_proxy=http://123.123.123.123:8888 . Note, it is proxy settings for https , but an http address is used.

I ran into the same issue, the proxy config is enabled by accident. Just turning off the proxy worked for me.

Turn Your VPN/Proxy thing (which enables as System Proxy) maybe it fixed.

i was using QV2ray and i had this issue, when i disabled it, pip works fine.

Don't use VPN proxy service off it and try again

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!!

I got this error when I was using a proxy. I disabled the proxy, the error was fixed.

As others mention, it has something to do with the connection to the server.

I solved it simply by closing Fiddler.

For those of you that can't disable their proxy, I found that downgrading pip was the only thing that worked. The version that works for me is located here: https://pypi.org/project/pip/20.2.4/

Download the whl and use

pip install pip-20.2.4-py2.py3-none-any.whl

at the location you downloaded it to.

I had the same problem using Python 3.10.7 (64-bit) on Windows 10.

Rolling back urllib3 to 1.25.11 didn't help.

Using python 3.19.13 (64-bit) instead resolved this error message no matter which urllib3 is used.

I assume that this is caused by some compatibility issue with Python 3.10.x.

if you open the off poxy tools, you sh close it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM