def train(args): """ Some sample code just to help you getting started """ ws_url = 'ws://{}:{}'.format(host2ip(args.host), args.ws_port) queue = Queue() websocket.connect(ws_url, queue) policy = RandomPolicy() agent = Agent(args.team_id, policy) while True: # advance by one tick rpc.step() # observe new game state json_state = queue.get() game_state = json.loads(json_state) tick = get_tick(game_state) observation = observe(game_state) # apply some logic to select a next action action = agent.forward(observation) # send action rpc.action(action, tick) # reset game (if you want to) rpc.reset()
def testProxyConnect(self): ws = websocket.WebSocket() ws.connect("ws://127.0.0.1:" + LOCAL_WS_SERVER_PORT, http_proxy_host="127.0.0.1", http_proxy_port="8899", proxy_type="http") ws.send("Hello, Server") server_response = ws.recv() self.assertEqual(server_response, "Hello, Server") # self.assertEqual(_start_proxied_socket("wss://api.bitfinex.com/ws/2", OptsList(), proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8899", proxy_type="http"))[1], ("api.bitfinex.com", 443, '/ws/2')) self.assertEqual( _get_addrinfo_list( "api.bitfinex.com", 443, True, proxy_info(http_proxy_host="127.0.0.1", http_proxy_port="8899", proxy_type="http")), (socket.getaddrinfo("127.0.0.1", 8899, 0, socket.SOCK_STREAM, socket.SOL_TCP), True, None)) self.assertEqual( connect( "wss://api.bitfinex.com/ws/2", OptsList(), proxy_info(http_proxy_host="127.0.0.1", http_proxy_port=8899, proxy_type="http"), None)[1], ("api.bitfinex.com", 443, '/ws/2'))
def testSSLopt(self): ssloptions = { "cert_reqs": ssl.CERT_NONE, "check_hostname": False, "ssl_version": ssl.PROTOCOL_TLSv1_2, "ciphers": "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:\ TLS_AES_128_GCM_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:\ ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:\ ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:\ DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:\ ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:\ ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:\ DHE-RSA-AES256-SHA256:ECDHE-ECDSA-AES128-SHA256:\ ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:\ ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA", "ecdh_curve": "prime256v1" } ws = websocket.WebSocket(sslopt=ssloptions) ws.connect("wss://api.bitfinex.com/ws/2") ws.send("Hello") ws.close()
def run(args): ws_url = 'ws://{}:{}'.format(host2ip(args.host), args.ws_port) queue = Queue() websocket.connect(ws_url, queue) # Change these to run your own policies: policy = RandomPolicy() # policy = HeuristicPolicy() agent = Agent(args.team_id, policy) tick = None while True: json_state = queue.get() game_state = json.loads(json_state) new_tick = get_tick(game_state) if tick is not None: if new_tick == tick: print( 'Warning: we received game state with the same tick twice...' ) continue elif new_tick < tick: print( 'Warning: we received an earlier tick, maybe the server state was restarted?' ) elif new_tick != tick + 1: print('Warning: it seems that we skipped some game steps') tick = new_tick # print('tick: ' + str(tick)) # print('game state["teams"]: ' + repr(game_state['teams'])) observation = observe(game_state) action = agent.forward(observation) rpc.action(action, tick)
async def subscribe_without_login(url, channels): async with websocket.connect(url) as websocket: sub_param = {"op": "subscribe", "args": channels} sub_str = json.dumps(sub_param) await websocket.send(sub_str) print(f"send: {sub_str}") print("receive:") res = await websocket.recv() res = inflate(res) print(f"{res}") res = await websocket.recv() res = inflate(res) print(f"{res}")
async def subscribe(url, access_key, secret_key, subs, callback=None, auth=False): """ Huobi Future subscribe websockets. Args: url: the url to be signatured. access_key: API access_key. secret_key: API secret_key. subs: the data list to subscribe. callback: the callback function to handle the ws data received. auth: True: Need to be signatured. False: No need to be signatured. """ async with websocket.connect(url) as websocket: if auth: timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") data = { "AccessKeyId": access_key, "SignatureMethod": "HmacSHA256", "SignatureVersion": "2", "Timestamp": timestamp } sign = generate_signature(url,"GET", data, "/notification", secret_key) data["op"] = "auth" data["type"] = "api" data["Signature"] = sign msg_str = json.dumps(data) await websocket.send(msg_str) print(f"send: {msg_str}") for sub in subs: sub_str = json.dumps(sub) await websocket.send(sub_str) print(f"send: {sub_str}") while True: rsp = await websocket.recv() data = json.loads(gzip.decompress(rsp).decode()) #print(f"recevie<--: {data}") if "op" in data and data.get("op") == "ping": pong_msg = {"op": "pong", "ts": data.get("ts")} await websocket.send(json.dumps(pong_msg)) print(f"send: {pong_msg}") continue if "ping" in data: pong_msg = {"pong": data.get("ping")} await websocket.send(json.dumps(pong_msg)) print(f"send: {pong_msg}") continue rsp = await callback(data)
import click_spinner import websocket websocket = websocket.WebSocket() websocket.connect('ws://0.0.0.0:8000/api/stt/ws') try: print('Performing speech-to-text with WebSocket...') with click_spinner.spinner(): with open('audio/8455-210777-0068.wav', mode='rb') as file: websocket.send('{"power":1000, "paris":-1000}') audio = file.read() websocket.send_binary(audio) result = websocket.recv() print() print(result) websocket.close() except Exception as ex: print(ex)