所有我需要說我從來沒有嘗試過在Python之前編碼的第一...AttributeError的「nonetype」對象有沒有屬性「的recv」
我試圖做一個抽搐IRC bot的工作,但我持續失敗...
我bot.py代碼如下所示:
from src.lib import irc as irc_
from src.lib import functions_general
from src.lib import functions_commands as commands
from src.config import config
class PartyMachine:
def __init__(self, config):
self.config = config
self.irc = irc_.irc(config)
self.socket = self.irc.get_irc_socket_object()
def sock(self):
irc = self.irc
sock = self.socket
config = self.config
kage = sock
while True:
data = sock.recv(2048).rstrip()
if len(data) == 0:
pp('Connection was lost, reconnecting.')
sock = self.irc.get_irc_socket_object()
if config['debug']:
print (data)
我config.py是在這裏:
'socket_buffer_size': 1024
我irc.py是在這裏:
def get_irc_socket_object(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
self.sock = sock
try:
sock.connect((self.config['server'], self.config['port']))
except:
pp('Cannot connect to server (%s:%s).' % (self.config['server'], self.config['port']), 'error')
sys.exit()
sock.settimeout(None)
def sock_send(sock, send, self):
sock.send('USER %s\r\n' % self.config['username'], sock.encode('utf-8'), send.encode('utf-8'))
sock.send('PASS %s\r\n' % self.config['oauth_password'])
sock.send('NICK %s\r\n' % self.config['username'])
if self.check_login_status(sock.recv(1024)):
pp('Login successful.')
else:
pp('Login unsuccessful. (hint: make sure your oauth token is set in self.config/self.config.py).', 'error')
sys.exit()
和我serve.py是在這裏:
from sys import argv
from src.bot import *
from src.config.config import *
bot = PartyMachine(config).sock()
它保持與 「AttributeError 'nonetype' object has no attribute 'recv'
」 失敗。怎麼會這樣 ?
請張貼完整的異常追蹤,並指出哪些代碼行被引用。 – glibdud
你使用'sock'這個名字來做太多事情。每個標識符只有一件事。 sock定義在哪裏? –
@Bim請注意修正Python問題的格式。在某些情況下,您可能無意中修復了實際問題。在這種情況下,您對縮進進行了更改,這可能不正確。 – glibdud