質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
bitFlyer API

bitFlyer APIは、仮想通貨取引所bitFlyerが公開しているAIPツール。bitFlyer Lightning/API playground/chainFlyer/Echoの4種類あり、bitFlyerソフトをカスタマイズすることが可能です。

WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

1840閲覧

【python】bitFlyerのWebSocketを使用した通信が接続されない

OXamarin

総合スコア59

bitFlyer API

bitFlyer APIは、仮想通貨取引所bitFlyerが公開しているAIPツール。bitFlyer Lightning/API playground/chainFlyer/Echoの4種類あり、bitFlyerソフトをカスタマイズすることが可能です。

WebSocket

WebSocketとは双方向・全二重コミュニケーションのためのAPIでありプロトコルのことを指します。WebSocketはHTML5に密接に結びついており、多くのウェブブラウザの最新版に導入されています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2021/03/10 04:23

##分からない事
bitFlyerのRelTimeApi をwebsocketを使用した際の書き方が分かりません。

##困っている点
なぜ接続ができないのかが分からず、何をとっかかりに調べればよいのかがわかりません。

###調べた事
以下のようなミニマムコードのプログラムを作成しました。

python

1import websocket 2import json 3import pprint 4import time 5 6class RealtimeAPI(object): 7 8 def __init__(self,url,channel): 9 self.url = url 10 self.channel = channel 11 self.ws = websocket.WebSocketApp(self.url,header=None,on_message=self.on_message,\ 12 on_open=self.on_open,on_close=self.on_close,on_error=self.on_error) 13 # websocket.enableTrace(True) 14 15 def run(self): 16 self.ws.run_forever() 17 18 def on_message(self,message): 19 message = json.loads(message) 20 print(message) 21 22 def on_error(self,error): 23 print(error) 24 25 def on_open(self): 26 print("websocket connection started.") 27 ws.send(json.dumps({"method": "subscribe","params": {"channel": self.channel}})) 28 29 def on_close(self): 30 print("###websocket connection closed.###") 31 32if __name__ == "__main__": 33 url = "wss://ws.lightstream.bitflyer.com/json-rpc" 34 channel = "lightning_executions_FX_BTC_JPY" 35 json_rpc = RealtimeAPI(url=url,channel=channel) 36 json_rpc.run()

実行したところ、以下のようなエラーが発生しました。

[WinError 10060] 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。

理由はわかりませんが、on_openメソッドを通っていないようです。

次に、
https://github.com/websocket-client/websocket-client にある以下のサンプルコードを実行してみました。

python

1import websocket 2try: 3 import thread 4except ImportError: 5 import _thread as thread 6import time 7 8def on_message(ws, message): 9 print(message) 10 11def on_error(ws, error): 12 print(error) 13 14def on_close(ws): 15 print("### closed ###") 16 17def on_open(ws): 18 def run(*args): 19 for i in range(3): 20 time.sleep(1) 21 ws.send("Hello %d" % i) 22 time.sleep(1) 23 ws.close() 24 print("thread terminating...") 25 thread.start_new_thread(run, ()) 26 27if __name__ == "__main__": 28 websocket.enableTrace(True) 29 ws = websocket.WebSocketApp("ws://echo.websocket.org/", 30 on_open = on_open, 31 on_message = on_message, 32 on_error = on_error, 33 on_close = on_close) 34 35 ws.run_forever()

このプログラムを実行したところ、

Hello 0
send: b'\x81\x87Tm\x8fm\x1c\x08\xe3\x01;M\xbe'
Hello 1
send: b'\x81\x87\n\xce\x06\xa7B\xabj\xcbe\xee4'
Hello 2
send: b'\x88\x82\xb1\x9f)\xc4\xb2w'
closed ###
thread terminating...

と出力されうまく通信が出来ているようでした。

そこで、
https://bf-lightning-api.readme.io/docs/endpoint-json-rpc に記載されているエンドポイント(wss://ws.lightstream.bitflyer.com/json-rpc)に書き換えて実行してみました。

python

1if __name__ == "__main__": 2 websocket.enableTrace(True) 3 ws = websocket.WebSocketApp("wss://ws.lightstream.bitflyer.com/json-rpc", 4 on_open = on_open, 5 on_message = on_message, 6 on_error = on_error, 7 on_close = on_close) 8 9 ws.run_forever()

先ほどと同じく、以下のエラーが出力されるかつ、on_openメソッドすら実行されません。

[WinError 10060] 接続済みの呼び出し先が一定の時間を過ぎても正しく応答しなかったため、接続できませんでした。または接続済みのホストが応答しなかったため、確立された接続は失敗しました。

これは何が問題なのでしょうか。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

[投稿 2021/03/10 13:23時点の内容に対する回答]
「調べた事」に記載してあるコードに関してのみですが、下記のように、__init__で生成したWebSocketApp (=self.ws)に対してmessageを送る必要があると思います。

diff

1 def on_open(self): 2 print("websocket connection started.") 3- ws.send(json.dumps({"method": "subscribe","params": {"channel": self.channel}})) 4+ self.ws.send(json.dumps({"method": "subscribe","params": {"channel": self.channel}}))

なお、手元の環境では、修正前のコードでは「websocket connection started.」と表示されて止まるだけで、[Winerror 10060]は再現できませんでした。
上記のように修正しても同じエラーが発生する場合は、環境固有の問題である可能性があります。

自分の環境:
Windows10
python 3.8.6
websockets-client 0.57.0

投稿2021/03/10 05:27

編集2021/03/10 05:39
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

OXamarin

2021/03/10 06:34

ありがとうございます! on_openメソッド自体を通らないので、その記述に到達できていないですね…。 仰ることはその通りだと思いますので、self は付与しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問