Skip to content

Commit 395cfe1

Browse files
authored
v4.0.0 release (#125)
1 parent 7fc9623 commit 395cfe1

File tree

10 files changed

+94
-8
lines changed

10 files changed

+94
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 4.0.0 - 2023-08-08
4+
5+
### Changed
6+
- Add proxy support for websockets
7+
- Remove support for Python 3.7
8+
39
## 4.0.0rc3 - 2023-07-03
410

511
### Changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,47 @@ my_client.agg_trade(symbol="bnbusdt", id="my_request_id")
210210
# library will generate a random uuid string
211211
my_client.agg_trade(symbol="bnbusdt")
212212
```
213+
#### Proxy
214+
215+
Proxy is supported for both WebSocket CM futures and UM futures.
216+
217+
To use it, pass in the `proxies` parameter when initializing the client.
218+
219+
The format of the `proxies` parameter is the same as the one used in the Spot RESTful API.
220+
221+
It consists on a dictionary with the following format, where the key is the type of the proxy and the value is the proxy URL:
222+
223+
For websockets, the proxy type is `http`.
224+
225+
```python
226+
proxies = { 'http': 'http://1.2.3.4:8080' }
227+
```
228+
229+
You can also use authentication for the proxy by adding the `username` and `password` parameters to the proxy URL:
230+
231+
```python
232+
proxies = { 'http': 'http://username:password@host:port' }
233+
```
234+
235+
```python
236+
# WebSocket Stream Client
237+
import time
238+
from binance.websocket.um_futures.websocket_client import UMFuturesWebsocketClient
239+
240+
proxies = {'http': 'http://1.2.3.4:8080'}
241+
242+
def message_handler(_, message):
243+
logging.info(message)
244+
245+
my_client = UMFuturesWebsocketClient(on_message=message_handler, proxies=proxies)
246+
247+
# Subscribe to a single symbol stream
248+
my_client.agg_trade(symbol="bnbusdt")
249+
time.sleep(5)
250+
logging.info("closing ws connection")
251+
my_client.stop()
252+
```
253+
213254

214255
#### Combined Streams
215256
- If you set `is_combined` to `True`, `"/stream/"` will be appended to the `baseURL` to allow for Combining streams.

binance/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "4.0.0rc3"
1+
__version__ = "4.0.0"

binance/lib/utils.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
import time
33

4-
from urllib.parse import urlencode
4+
from urllib.parse import urlencode, urlparse
55
from binance.error import (
66
ParameterRequiredError,
77
ParameterValueError,
@@ -40,7 +40,7 @@ def check_enum_parameter(value, enum_class):
4040

4141

4242
def check_type_parameter(value, name, data_type):
43-
if value is not None and type(value) != data_type:
43+
if value is not None and not isinstance(value, data_type):
4444
raise ParameterTypeError([name, data_type])
4545

4646

@@ -64,3 +64,19 @@ def convert_list_to_json_array(symbols):
6464

6565
def config_logging(logging, logging_devel, log_file=None):
6666
logging.basicConfig(level=logging_devel, filename=log_file)
67+
68+
69+
def parse_proxies(proxies: dict):
70+
"""Parse proxy url from dict, only support http and https proxy, not support socks5 proxy"""
71+
proxy_url = proxies.get("http") or proxies.get("https")
72+
if not proxy_url:
73+
return {}
74+
75+
parsed = urlparse(proxy_url)
76+
return {
77+
"http_proxy_host": parsed.hostname,
78+
"http_proxy_port": parsed.port,
79+
"http_proxy_auth": (parsed.username, parsed.password)
80+
if parsed.username and parsed.password
81+
else None,
82+
}

binance/websocket/binance_socket_manager.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
import logging
24
import threading
35
from websocket import (
@@ -6,6 +8,7 @@
68
WebSocketException,
79
WebSocketConnectionClosedException,
810
)
11+
from binance.lib.utils import parse_proxies
912

1013

1114
class BinanceSocketManager(threading.Thread):
@@ -19,6 +22,7 @@ def __init__(
1922
on_ping=None,
2023
on_pong=None,
2124
logger=None,
25+
proxies: Optional[dict] = None,
2226
):
2327
threading.Thread.__init__(self)
2428
if not logger:
@@ -31,15 +35,19 @@ def __init__(
3135
self.on_ping = on_ping
3236
self.on_pong = on_pong
3337
self.on_error = on_error
38+
self.proxies = proxies
39+
40+
self._proxy_params = parse_proxies(proxies) if proxies else {}
41+
3442
self.create_ws_connection()
3543

3644
def create_ws_connection(self):
3745
self.logger.debug(
38-
"Creating connection with WebSocket Server: %s", self.stream_url
46+
f"Creating connection with WebSocket Server: {self.stream_url}, proxies: {self.proxies}",
3947
)
40-
self.ws = create_connection(self.stream_url)
48+
self.ws = create_connection(self.stream_url, **self._proxy_params)
4149
self.logger.debug(
42-
"WebSocket connection has been established: %s", self.stream_url
50+
f"WebSocket connection has been established: {self.stream_url}, proxies: {self.proxies}",
4351
)
4452
self._callback(self.on_open)
4553

binance/websocket/cm_futures/websocket_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from binance.websocket.websocket_client import BinanceWebsocketClient
24

35

@@ -12,6 +14,7 @@ def __init__(
1214
on_ping=None,
1315
on_pong=None,
1416
is_combined=False,
17+
proxies: Optional[dict] = None,
1518
):
1619
if is_combined:
1720
stream_url = stream_url + "/stream"
@@ -25,6 +28,7 @@ def __init__(
2528
on_error=on_error,
2629
on_ping=on_ping,
2730
on_pong=on_pong,
31+
proxies=proxies,
2832
)
2933

3034
def agg_trade(self, symbol: str, id=None, action=None, **kwargs):

binance/websocket/um_futures/websocket_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from binance.websocket.websocket_client import BinanceWebsocketClient
24

35

@@ -12,6 +14,7 @@ def __init__(
1214
on_ping=None,
1315
on_pong=None,
1416
is_combined=False,
17+
proxies: Optional[dict] = None,
1518
):
1619
if is_combined:
1720
stream_url = stream_url + "/stream"
@@ -25,6 +28,7 @@ def __init__(
2528
on_error=on_error,
2629
on_ping=on_ping,
2730
on_pong=on_pong,
31+
proxies=proxies,
2832
)
2933

3034
def agg_trade(self, symbol: str, id=None, action=None, **kwargs):

binance/websocket/websocket_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from typing import Optional
2+
13
import json
24
import logging
5+
36
from binance.lib.utils import get_timestamp
47
from binance.websocket.binance_socket_manager import BinanceSocketManager
58

@@ -18,6 +21,7 @@ def __init__(
1821
on_ping=None,
1922
on_pong=None,
2023
logger=None,
24+
proxies: Optional[dict] = None,
2125
):
2226
if not logger:
2327
logger = logging.getLogger(__name__)
@@ -31,6 +35,7 @@ def __init__(
3135
on_ping,
3236
on_pong,
3337
logger,
38+
proxies,
3439
)
3540

3641
# start the thread
@@ -47,6 +52,7 @@ def _initialize_socket(
4752
on_ping,
4853
on_pong,
4954
logger,
55+
proxies,
5056
):
5157
return BinanceSocketManager(
5258
stream_url,
@@ -57,6 +63,7 @@ def _initialize_socket(
5763
on_ping=on_ping,
5864
on_pong=on_pong,
5965
logger=logger,
66+
proxies=proxies,
6067
)
6168

6269
def _single_stream(self, stream):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
"Intended Audience :: Developers",
4242
"Intended Audience :: Financial and Insurance Industry",
4343
"License :: OSI Approved :: MIT License",
44-
"Programming Language :: Python :: 3.7",
4544
"Programming Language :: Python :: 3.8",
4645
"Programming Language :: Python :: 3.9",
4746
"Programming Language :: Python :: 3.10",
47+
"Programming Language :: Python :: 3.11",
4848
],
4949
python_requires=">=3.7",
5050
)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = py36,py37,py38,py39
2+
envlist = py38,py39,py310,py311
33

44
[testenv]
55
deps =

0 commit comments

Comments
 (0)