使用testnet的web3.py isConnected()函数

时间:2018-09-08 18:52:58

标签: python web3 geth

我正在用Python开发一个应用程序来处理一些以太坊。我已经安装了geth和web3.py,但确实存在isConnected()的其他行为问题。

Geth:geth-alltools-darwin-amd64-1.8.15-89451f7c(最新)

web3.py:4.6.0(最新)

Python:3.7

Mac OS X

我像这样开始葛:

import sys
from PyQt5 import QtCore, QtGui, QtWidgets

class WinDialog(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(WinDialog, self).__init__(parent)

        self.setGeometry(300,300,350,300)
        self.setWindowTitle("Signal & Slot")

        self.propertyWidget = PropertyWidget()

        section_lay = QtWidgets.QHBoxLayout()
        section_label = QtWidgets.QLabel("Name: ")
        section_edit = QtWidgets.QLineEdit('')

        length_lay = QtWidgets.QHBoxLayout()

        length_label = QtWidgets.QLabel("Input a number:     L = ")
        self.length_edit = QtWidgets.QLineEdit()
        self.length_edit.setFocus(True)
        val_lenght = QtGui.QIntValidator(0, 100000, self.length_edit)
        self.length_edit.setValidator(val_lenght)

        thick_lay = QtWidgets.QHBoxLayout()
        thick_label = QtWidgets.QLabel("Input a text: T = ")
        self.thick_edit = QtWidgets.QLineEdit()
        val_thick = QtGui.QIntValidator(0, 100000, self.thick_edit)
        self.thick_edit.setValidator(val_thick)

        section_lay.addWidget(section_label)
        section_lay.addWidget(section_edit)

        length_lay.addWidget(length_label)
        length_lay.addWidget(self.length_edit)
        length_lay.addStretch()

        thick_lay.addWidget(thick_label)
        thick_lay.addWidget(self.thick_edit)
        thick_lay.addStretch()

        VB_lay = QtWidgets.QVBoxLayout()
        VB_lay.addStretch()
        VB_lay.addLayout(length_lay)
        VB_lay.addLayout(thick_lay)
        VB_lay.addStretch()

        buttonBox = QtWidgets.QDialogButtonBox()

        buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel
            | QtWidgets.QDialogButtonBox.Ok)
        buttonBox.accepted.connect(self.accept) 
        buttonBox.rejected.connect(self.reject)

        grid = QtWidgets.QGridLayout(self)
        grid.addLayout(section_lay, 0, 0, 1, 2)
        grid.addLayout(VB_lay, 1, 0)
        grid.addWidget(self.propertyWidget, 2, 0)
        grid.addWidget(buttonBox, 3, 0, 1, 2)

        self.length_edit.textEdited.connect(self.onTextEdited)
        self.thick_edit.textEdited.connect(self.onTextEdited)

    def onTextEdited(self):        
        l = self.length_edit.text()
        t = self.thick_edit.text()
        self.propertyWidget.display(l, t)

class PropertyWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
       super(PropertyWidget, self).__init__(parent)        
       HB_lay = QtWidgets.QHBoxLayout(self)
       self.Displaylabel = QtWidgets.QLabel('')
       HB_lay.addWidget(self.Displaylabel)
       HB_lay.addStretch()

    def display(self, l, t):
        try:
            L_Display = int(l)
            T_Display = int(t) 
            fmt = "L = {}mm\nT = {}mm"
            self.Displaylabel.setText(fmt.format(L_Display, T_Display))
        except ValueError:
            self.Displaylabel.clear()       


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = WinDialog()
    w.show()
    sys.exit(app.exec_())

示例代码:

./geth --syncmode=light --cache=1024 --maxpeers=12
./geth --testnet console

只要我使用主网,isConnected()的行为就可以预期。如果testnet isConnected()始终为False,则即使链已完全同步!

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

您需要将mainnet的网址更改为任何测试网