sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.88k stars 2.2k forks source link

ValueError: check_hostname requires server_hostname #855

Open ZhangGang12 opened 3 years ago

ZhangGang12 commented 3 years ago

Describe the bug I can't connect client, but I use the v2rayN proxy client on my PC. When I try to visit https://api.binance.com/api/v3/ping through my browser, It shows ‘{}’. But I try to run the following code, It prints "not existed". So what should I do?

import socket
try:
    socket.gethostbyname('https://api.binance.com/api/v3/ping')
except socket.gaierror as ex:
    print("not existe")

To Reproduce

client = Client(key , serct)
  File "D:\Python\Python36\lib\site-packages\binance\client.py", line 294, in __init__
    self.ping()
  File "D:\Python\Python36\lib\site-packages\binance\client.py", line 520, in ping
    return self._get('ping', version=self.PRIVATE_API_VERSION)
  File "D:\Python\Python36\lib\site-packages\binance\client.py", line 365, in _get
    return self._request_api('get', path, signed, version, **kwargs)
  File "D:\Python\Python36\lib\site-packages\binance\client.py", line 328, in _request_api
    return self._request(method, uri, signed, **kwargs)
  File "D:\Python\Python36\lib\site-packages\binance\client.py", line 308, in _request
    self.response = getattr(self.session, method)(uri, **kwargs)
  File "D:\Python\Python36\lib\site-packages\requests\sessions.py", line 555, in get
    return self.request('GET', url, **kwargs)
  File "D:\Python\Python36\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\Python\Python36\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "D:\Python\Python36\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "D:\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "D:\Python\Python36\lib\site-packages\urllib3\connectionpool.py", line 964, in _prepare_proxy
    conn.connect()
  File "D:\Python\Python36\lib\site-packages\urllib3\connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "D:\Python\Python36\lib\site-packages\urllib3\connection.py", line 506, in _connect_tls_proxy
    ssl_context=ssl_context,
  File "D:\Python\Python36\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 "D:\Python\Python36\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "D:\Python\Python36\lib\ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "D:\Python\Python36\lib\ssl.py", line 773, in __init__
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

Expected behavior I w

Environment (please complete the following information):

Logs or Additional context I was on China's mainland,Thanks for your response!

Starfarer69 commented 3 years ago

I'm not an expert on python but I found a work around. you need to install PySocks

Then add the following code to your python script, assuming your proxy is localhost and port is 8888

import socket
import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 8888)
socket.socket = socks.socksocket

Moreover, you will need to disable system proxy in v2rayN

ZhangGang12 commented 3 years ago

I'm not an expert on python but I found a work around. you need to install PySocks

Then add the following code to your python script, assuming your proxy is localhost and port is 8888

import socket
import socks

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 8888)
socket.socket = socks.socksocket

Moreover, you will need to disable system proxy in v2rayN

Thank you very much! According to your method, the problem is resolved !!