Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 CommandInvokeError:命令引发异常:RuntimeError:在嗅探数据包时,当另一个循环正在运行时,无法运行事件循环_Python_Python 3.x_Python Asyncio_Discord.py_Pyshark - Fatal编程技术网

Python CommandInvokeError:命令引发异常:RuntimeError:在嗅探数据包时,当另一个循环正在运行时,无法运行事件循环

Python CommandInvokeError:命令引发异常:RuntimeError:在嗅探数据包时,当另一个循环正在运行时,无法运行事件循环,python,python-3.x,python-asyncio,discord.py,pyshark,Python,Python 3.x,Python Asyncio,Discord.py,Pyshark,我试图根据通过discord发送的参数捕获数据包,然后将捕获文件发送到调用bot的通道。我在捕获数据包部分时遇到了问题,我认为这是由PyShark使用asyncio的sniff函数引起的 我尝试过很多事情,包括将我的嗅探函数设置为异步、使用.get_event_loop()、使用wait sniffing函数等等。我感到非常失落和沮丧。以下是我尝试过的一些代码: 导入sharkBot 导入异步 从discord.ext导入命令 进口不和 令牌=“” bot=commands.bot(命令前缀='

我试图根据通过discord发送的参数捕获数据包,然后将捕获文件发送到调用bot的通道。我在捕获数据包部分时遇到了问题,我认为这是由PyShark使用asyncio的sniff函数引起的

我尝试过很多事情,包括将我的嗅探函数设置为异步、使用.get_event_loop()、使用wait sniffing函数等等。我感到非常失落和沮丧。以下是我尝试过的一些代码:

导入sharkBot
导入异步
从discord.ext导入命令
进口不和
令牌=“”
bot=commands.bot(命令前缀='!')
shark=sharkBot.sharkBot()
@bot.command()
异步def cap(ctx,*参数):
#创建用于存储值的字典
值={}
params={'p':'packet_count','o':'output_file','t':'tshark_path'}
#向字典添加值
对于args中的arg:
arg=arg.replace('-','')
value=arg[arg.find(“”+1:arg.find(“”))
sep='('
arg=arg.split(9月1日)[0]
command=params.get(arg,“无效命令”)
值[命令]=值
#基于值嗅探数据包
packets=asyncio.get_event_loop()。运行_直到完成(packetCap(值))
等待ctx发送(数据包)
def packetCap(值):
capture=shark.capturePacketsByCount(**值)
返回捕获
bot.run(令牌)
capturePacketsByCount(**值)为:

def capturePacketsByCount(self,interface=“en0”,packet\u count=100,tshark\u path=“/”,output\u file=None):
#捕获指定数量的数据包
self.capture=pyshark.LiveCapture(tshark\u path=tshark\u path,interface=interface,output\u file=output\u file)
self.capture.sniff(数据包计数=数据包计数)
self.caplen=len(self.capture)
#捕获给定时间内的数据包
回归自我捕捉
我还尝试用以下代码片段替换cap中的最后两行:

packets=wait shark.capturePacketsByCount(**值)
等待ctx发送(数据包)
我还尝试了使packetCap和capturePacketsByCount异步函数

我收到的错误是:

 File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 79, in wrapped
    ret = await coro(*args, **kwargs)
  File "bot2.py", line 23, in cap
    x = asyncio.get_event_loop().run_until_complete(packetCap(values))
  File "bot2.py", line 26, in packetCap
    x = shark.capturePacketsByCount(**values)
  File "/Users/cooldude/Projects/PySharkStuff/PySharkBot/sharkBot.py", line 12, in capturePacketsByCount
    self.capture.sniff(packet_count=packet_count)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyshark/capture/capture.py", line 133, in load_packets
    self.apply_on_packets(keep_packet, timeout=timeout)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pyshark/capture/capture.py", line 248, in apply_on_packets
    return self.eventloop.run_until_complete(coro)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 455, in run_until_complete
    self.run_forever()
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 412, in run_forever
    'Cannot run the event loop while another loop is running')
RuntimeError: Cannot run the event loop while another loop is running

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 859, in invoke
    await ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 725, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/discord/ext/commands/core.py", line 88, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: Cannot run the event loop while another loop is running
谢谢你能为我提供的任何帮助