import websocket
import json
import time
# Connection settings
binance_base_endpoint = "wss://stream.binance.com:9443"
raw_stream = "/ws"
combined_stream = "/stream?streams="
symbol = "btcusdt"
# Subscriptions
agg_trade_stream = f"{symbol}@aggTrade"
depth_stream = f"{symbol}@depth"
# Proxy settings
http_proxy_host = "host"
http_proxy_port = port
# Proxy authentication
proxy_username = "user"
proxy_password = "pass"
# Connect to Binance websocket
ws = websocket.WebSocket(http_proxy_host=http_proxy_host, http_proxy_port=http_proxy_port, http_proxy_auth=(proxy_username, proxy_password))
ws.connect(binance_base_endpoint + raw_stream + "/" + agg_trade_stream)
# Subscribe to a stream
subscribe_request = {
"method": "SUBSCRIBE",
"params": [
agg_trade_stream,
depth_stream
],
"id": 1
}
ws.send(json.dumps(subscribe_request))
# Listen for messages
try:
while True:
response = json.loads(ws.recv())
if "result" in response and response["result"] is None:
print(f'Subscription successful: {response["id"]}')
else:
print(f'Received message: {response}')
except KeyboardInterrupt:
print("Closing websocket connection...")
# Unsubscribe from a stream
unsubscribe_request = {
"method": "UNSUBSCRIBE",
"params": [
depth_stream
],
"id": 312
}
ws.send(json.dumps(unsubscribe_request))
unsubscribe_response = json.loads(ws.recv())
if "result" in unsubscribe_response and unsubscribe_response["result"] is None:
print(f'Unsubscription successful: {unsubscribe_response["id"]}')
# Close the websocket connection
ws.close()
I’m trying to connect to websocket through HTTP, HTTPS and SOCKS5 proxies and I can’t seem to connect to it, what should I do? Can I use any proxy to connect? What would be a better solution?
P.S: I’m developing a bot for a client and I should develop the code with proxy.