我是 python 新手,正在嘗試編寫連接到 irc 服務器的代碼。 我正在將代碼輸入 NiFi ExecuteScript 處理器並嘗試執行。 運行時出現以下錯誤。 我不確定是 NiFi 導致了問題還是只是它自己的代碼。
NameError: name 'self' 未在第 6 行定義
import socket
from org.apache.nifi.processor.io import StreamCallback
class PyStreamCallback(StreamCallback):
def __init__(self):
pass
self.ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server = "irc.freenode.net"
self.channel = ""
self.botnick = ""
self.ircsock.connect((server, 6667))
self.ircsock.send(bytes("USER "+ botnick +" "+ botnick +" "+ botnick + " " + botnick + "\n"))
self.ircsock.send(bytes("NICK "+ botnick +"\n"))
def joinchan(self, chan):
self.ircsock.send(bytes("JOIN "+ chan +"\n"))
ircmsg = ""
while ircmsg.find("End of /NAMES list.") == -1:
self.ircmsg = ircsock.recv(2048)
self.ircmsg = ircmsg.strip('\n\r')
print(ircmsg)
def ping(self):
self.ircsock.send(bytes("PONG :pingis\n"))
def sendmsg(self, msg, target=channel):
self.ircsock.send(bytes("PRIVMSG "+ target +" :"+ msg +"\n"))
flowFile = session.get()
if (flowFile != None):
flowFile = session.write(flowFile,PyStreamCallback())
session.transfer(flowFile, REL_SUCCESS)
session.commit()