【发布时间】:2013-08-09 23:23:23
【问题描述】:
我似乎无法弄清楚问题出在哪里,
错误:
Traceback (most recent call last):
File "./miningScreensaver", line 171, in <module>
miningScreensaver().loop.run()
File "./miningScreensaver", line 81, in __init__
self.rxAddress = self.getRxAddress
AttributeError: miningScreensaver instance has no attribute 'getRxAddress'
代码:
#! Python
class miningScreensaver:
def __init__(self):
DBusGMainLoop(set_as_default=True)
self.mem='ActiveChanged'
self.dest='org.gnome.ScreenSaver'
self.bus=SessionBus()
self.loop=MainLoop()
self.bus.add_signal_receiver(self.catch,self.mem,self.dest)
self.pipe = ""
#if you would like to specify a different rx address
# change rxAddress to the desired rx address
self.rxAddress = self.getRxAddress() #<--------------------ERROR HERE line 81
#self.rxaddress = "18X3TEigc6PVTsF9Atx5br7rEXfuZRqXEz"
def catch(self,ssOn):
if ssOn == 1: #Screensaver turned on
self.start()
else: #Screensaver turned off
self.stop()
def start(self):
self.pipe = Popen(["cgminer -o stratum+tcp://stratum.mining.eligius.st:3334 -u " + self.rxAddress + " -p x -I 9"], shell=True)
def stop(self):
self.pipe.kill()
def getRxAddress(self):
#check if bitcoin.conf exists
cmd = "ls $HOME/.bitcoin/bitcoin.conf"
pipe = Popen(cmd,stdout=PIPE,stderr=STDOUT)
pout = pipe.stdout.read()
pout = pout.split()
if pout[len(pout)-5]=='No' and \
pout[len(pout)-4]=='such' and \
pout[len(pout)-3]=='file' and \
pout[len(pout)-2]=='or' and \
pout[len(pout)-1]=='directory\n':
password = self.createBitConf()
else:
#check password
password = self.checkPassword()
#Launch bitcoin-qt -server
Popen(["bitcoin-qt","-server"])
#Access
access = ServiceProxy("http://darkPenguin:"+password+"@127.0.0.1:8332")
#access.getinfo()
return access.listreceivedbyaddress(0)
#access.sendtoaddress("12yBwyDJHABCvohdT8qBTeMJEYDqpXnVYV", 0.01)
def createBitConf(self):
randomPW = self.createRandomPW()
path = expanduser("~") + "/"
defFile = open("bitosbitcoinconf", "r")
newFile = open(path + ".bitcoin/bitcoin.conf","w")
for line in range(1,55):
newFile.write(defFile.readline())
password = "rpcpassword="+randomPW+"\n"
for line in range(56,110):
newFile.write(defFile.readline())
defFile.close()
newFile.close()
return randomPW
def createRandomPW(self):
myrg = random.SystemRandom()
length = 44
alphabet = string.ascii_letters + string.digits
pw = str().join(myrg.choice(alphabet) for _ in range(length))
return pw
def checkPassword(self):
path = expanduser("~") + "/"
bitConfFile = open(path + ".bitcoin/bitcoin.conf","r")
password = bitConfFile.readline(56)
bitConfFile.close()
return password[12:12+44-1] # "rpcpassword="+randomPW+"\n"
miningScreensaver().loop.run()
这让我很生气,方法调用和方法的拼写完全相同,解决其他人的问题也无济于事。
【问题讨论】:
-
您的错误与您的代码不匹配。出现该错误后,您是否更改了代码?重新运行它,看看会发生什么。 (此外,强烈建议任何不从任何东西继承的类,而是从
object继承。否则,你会得到老式的类,这很糟糕。) -
作为与您的问题完全无关的旁注,对
ls命令执行Popen并解析错误消息必须是我见过的最复杂的方式验证文件是否存在…… -
检查缩进?
getRxAddress可能会被删除,因此它是在类之后定义的函数,而不是方法。 -
另一件事:你为什么在 linux 上使用
#! Python作为你的 shbang 行?那不可能是一个有用的解释器路径…… -
在 abarnert 的 LMFAO,谢谢。这在当时很有意义,我得到了这个错误,只需检查这个错误而不是谷歌搜索如何检查 python 中是否存在文件,我应该知道。
标签: python ubuntu-12.04 attributeerror bitcoin linux-mint