【发布时间】:2016-01-21 18:28:21
【问题描述】:
基本上,我想运行连接函数,但我不断收到 CMD 错误消息“class StraussBot 没有属性”connectSock,但我显然可以看到它确实如此。我试过在这里搜索,但找不到任何解决此问题的方法。因此,如果您能帮我找出为什么找不到“connectSock”功能,我们将不胜感激。
代码:
import socket
from config import HOST, PORT, CHANNEL
# User Info
USER = "straussbot" # The bots username
PASS = "oauth:sj175lp884ji5c9las089sm9vvaklf" # The auth code
class StraussBot:
def __init__(self):
self.Ssock = socket.socket()
def connectSock(self):
self.Ssock.connect((HOST, PORT))
self.Ssock.send(str("Pass " + PASS + "\r\n").encode('UTF-8'))
self.Ssock.send(str("NICK " + USER + "\r\n").encode('UTF-8'))
self.Ssock.send(str("JOIN " + CHANNEL + "\r\n").encode('UTF-8'))
if __name__ == "__main__":
print "Starting the bot..."
while True:
straussbot = StraussBot
try:
straussbot.connectSock()
except Exception as e:
print e
【问题讨论】:
-
straussbot = StraussBot(),你缺少括号。 -
在您的主块中,您将
straussbot设置为类 StraussBot,而不是该类的实例。