Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过python调用函数向slack打印通知_Python_Oop - Fatal编程技术网

如何通过python调用函数向slack打印通知

如何通过python调用函数向slack打印通知,python,oop,Python,Oop,我基本上知道如何打印使用slack API向slack发送通知,例如,如果我有一个名为#testbot的slack通道,那么我可以将通知打印为: slack.chat.post_message('#testbot','This is a test',username='NMAP_bot') 我遵循python中的面向对象方法,其中我有两个类,最重要的是定义了一个方法: def notify_slack(): class Report(object): . . def new_ho

我基本上知道如何打印使用slack API向slack发送通知,例如,如果我有一个名为#testbot的slack通道,那么我可以将通知打印为:

slack.chat.post_message('#testbot','This is a test',username='NMAP_bot')
我遵循python中的面向对象方法,其中我有两个类,最重要的是定义了一个方法:

def notify_slack():

class Report(object):
  .
  .
  def new_hosts(self):
        """Return a list of new hosts added in latest scan"""
        self.curr_hosts - self.prev_hosts

  .
  .

class Scanner(object):
  .
   #Lot of code here
我有一个主要的方法,基本上看起来像:

if __name__ == "__main__":            
        print "New Hosts"
        report.new_hosts()     #This calls new_hosts() method in class Report 
因此,report.new_hosts()能够调用report类中定义的方法并打印结果。 我要做的是调用
notify\u slack()
并在其中传递
report.new\u hosts()
,以便将结果打印到slack

谢谢你的帮助,带我上去吧

我要做的是调用notify_slack()并在其中传递report.new_hosts()

像这样

def notify_slack(hosts):
    print hosts
    msg = ' '.join(map(str, hosts)) # delimits all the hosts by space
    slack.chat.post_message('#testbot',msg,username='NMAP_bot')


class Report(object):
    def new_hosts(self):
        """Return a list of new hosts added in latest scan"""
        return self.curr_hosts - self.prev_hosts

# s = ? 
r = s.run("172.16.0.0-255")
report = Report(r)
notify_slack(report.new_hosts())

您需要更改
new\u hosts
方法以返回结果,而不是打印结果:

class Report(object):
  .
  .
  def new_hosts(self):
      """Return a list of new hosts added in latest scan"""
      return self.curr_hosts - self.prev_hosts
您需要更改
notify\u slack
的定义,以便它采用一个参数,该参数将作为调用
report的结果。new\u host

def notify_slack(host_list):
    # Don't notify Slack if list of hosts is empty
    if not host_list:
        return

    slack_msg = ' '.join(map(str, msg_list))
    slack.chat.post_message('#testbot', slack_msg,username='NMAP_bot')
调用
report.new\u hosts()
的结果可以传递到
notify\u slack
方法中

notify_slack(report.new_hosts())

返回一个新主机列表-该方法只打印,不返回…哦,是的。。那么,如果该方法返回,那么我如何继续?您调用该方法并将结果传递给另一个方法如果我想在发送到slack之前在notify_slack(msg_list)中设置一个条件,即msg_list不是空的,即如果new_hosts()返回一个空列表,则不向slack发送通知,否则,您可以检查
msg\u列表的长度,如果长度小于等于0,则返回。我将编辑
notify\u slack
方法。