Python

為什麼我有一個帶有 python-bitcoinlib 的 socket.timeout?

  • March 13, 2022

我正在使用一個小腳本並從 python 獲得超時。這是我的腳本:

import bitcoin
import bitcoin.rpc
from bitcoin.rpc import RawProxy
p = RawProxy()
hash = '0000000000000001b6b9a13b095e96db41c4a928b97ef2d944a9b31b2cc7bdc4'
infoblock = p.getblock(hash)
print(infoblock['difficulty'])

這是我的錯誤:

bitcoin@raspberrypi ~/.bitcoin/BuchSkripte $ python3 49_rpc_example.py
Traceback (most recent call last):
 File "49_rpc_example.py", line 11, in <module>
   infoblock = p.getblock(hash)
 File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 306, in <lambda>
   f = lambda *args: self._call(name, *args)
 File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 236, in _call
   response = self._get_response()
 File "/usr/local/lib/python3.5/dist-packages/bitcoin/rpc.py", line 261, in _get_response
   http_response = self.__conn.getresponse()
 File "/usr/lib/python3.5/http/client.py", line 1198, in getresponse
   response.begin()
 File "/usr/lib/python3.5/http/client.py", line 297, in begin
   version, status, reason = self._read_status()
 File "/usr/lib/python3.5/http/client.py", line 258, in _read_status
   line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
 File "/usr/lib/python3.5/socket.py", line 576, in readinto
   return self._sock.recv_into(b)
socket.timeout: timed out

當使用相同的方法getblockbitcoin-cli,我的 Raspberry 需要 120 秒才能完成。如果超時與此持續時間有關,我可以在腳本中增加它嗎?

讀到可以設置套接字超時,但我想知道如果我使用庫進行連接是否可以這樣做。

更改文件 bitcoin.conf 或啟動 bitcoind 時的超時,

這是一個例子

##
## bitcoin.conf configuration file. Lines beginning with # are comments.
##
# On client-side, you add the normal user/password pair to send commands:
rpcuser=alice
rpcpassword=bob
rpcclienttimeout=200

或者您可以使用此命令執行 bitcoindbitcoind -rpcuser=alice -rpcpassword=bob -rpcclienttimeout=200

或者您可以將設置庫更改為程式碼,試試這個例子

from bitcoin.rpc import RawProxy

# Create a connection to local Bitcoin Core node
p = RawProxy(None, None, None, 120)

# Run the getblockchaininfo command, store the resulting data in info
info = p.getblockchaininfo()

# Retrieve the 'blocks' element from the info
print(info['blocks'])

我必須通過在呼叫方法時為其賦值來更改庫方法中使用的超時變數。

引用自:https://bitcoin.stackexchange.com/questions/90080