def get_ethclient(host): if host == "": return None if "https" in host: _port = 443 _host = host.split(':')[1].split('//')[1] else: _port = host.split(':')[2] _host = host.split(':')[1].split('//')[1] try: client = EthJsonRpc(host=_host, port=_port, tls=True) except Exception as e: QMessageBox.information(self,"Information",str(e)) return if client.net_listening(): return client else: return
class Ethclient(QDialog, Ui_ethclient): ethClient = pyqtSignal(object) finishedWork = pyqtSignal() ethClientHost = pyqtSignal(str) def __init__(self): super(Ethclient, self).__init__() # super(Yglian, self).__init__(None, Qt.Dialog | Qt.WindowMaximizeButtonHint | Qt.WindowMinimizeButtonHint) self.setupUi(self) self.work = WorkThreadEthConnect() self.thread = QThread() self.finishedWork.connect(self.work.Finished) self.ethClient.connect(self.work.EthClient) self.ethClientHost.connect(self.set_host) self.work.moveToThread(self.thread) self.work.finished.connect(self.thread.quit) self.thread.started.connect(self.work.run) self.work.response.connect(self.connect_show) self.comboBox_webNo.addItem("https://web3.yglian.com") self.comboBox_webNo.addItem("https://yglian-qa.qschou.com") self.comboBox_webNo.setEditText("请选择节点") # self.comboBox_webNo.focusOutEvent(,self.post_tradeinfo) self.comboBox_webNo.installEventFilter(self) self.pushButtonConnectButton = True self.client = None @pyqtSlot() def on_pushButtonConnect_clicked(self): if self.pushButtonConnectButton: host = self.comboBox_webNo.currentText() if host == "" or (":" not in host): QMessageBox.information(self, "Information", "请输入节点地址") return try: self.client = eth.get_ethclient(host) except exceptions.ConnectionError: QMessageBox.warning(self, "Warning", "节点链接失败,请检查节点") return if self.client: self.pushButtonConnectButton = False self.pushButtonConnect.setText("断开") self.label_blockNumber.clear() else: QMessageBox.warning(self, "Warning", "节点链接失败,请检查节点") return self.ethClient.emit(self.client) self.thread.start() else: try: self.finishedWork.emit() self.label_blockNumber.clear() self.pushButtonConnectButton = True self.pushButtonConnect.setText("连接") except BaseException as e: print(e) self.pushButtonConnectButton = True self.label_blockNumber.clear() self.pushButtonConnect.setText("连接") @pyqtSlot(str) def connect_show(self, string): self.label_blockNumber.setText(string) if self.pushButtonConnectButton: self.label_blockNumber.clear() def get_ethclient(self): host = self.comboBox_webNo.text() if host == "": return None if "https" in host: _port = 443 _host = host.split(':')[1].split('//')[1] else: _port = host.split(':')[2] _host = host.split(':')[1].split('//')[1] try: self.client = EthJsonRpc(host=_host, port=_port, tls=True) except Exception as e: QMessageBox.information(self, "Information", str(e)) return if self.client.net_listening(): return self.client else: QMessageBox.warning("Warning", "节点链接失败,请检查节点") def eventFilter(self, widget, event): if event.type() == QEvent.FocusIn: # self.inp_text_signal.emit("已进") if self.comboBox_webNo.currentText().strip() == '请输入节点': self.comboBox_webNo.clear() elif event.type() == QEvent.FocusOut: if self.comboBox_webNo.currentText().strip() == '': self.comboBox_webNo.setEditText("请输入节点") self.ethClientHost.emit(self.comboBox_webNo.currentText()) else: pass return False @pyqtSlot(str) def set_host(self, host): eth.set_host(host)
from ethjsonrpc import EthJsonRpc # to use Parity-specific methods, import ParityEthJsonRpc base_address = "0xbb1588c5debc2871cd8852c4bd6c6e4cb1d9fe15" c = EthJsonRpc('127.0.0.1', 8545) print(c.net_version()) print(c.web3_clientVersion()) print(c.net_listening()) print(c.net_peerCount()) print(c.eth_mining()) print(c.eth_gasPrice()) contract_addr = "0xc1bba31875a1a66eb4794e6e4dd07811fb58b5c5" my_addr = str(c.eth_coinbase()) print my_addr tx = c.call_with_transaction(my_addr, contract_addr, 'pushByte(string)', ['Hello, world']) print(tx) results = c.call(contract_addr, 'getdata()', [], ['string']) print(results)