我试图根据通过不和谐发送的参数捕获数据包,然后将捕获文件发送到调用该漫游器的通道。我在捕获数据包部分时遇到了麻烦,我认为这是PyShark使用asyncio的嗅探功能引起的。
我尝试了很多事情,包括将嗅探函数设置为异步,使用.get_event_loop(),使用等待嗅探函数等。我相当迷失和沮丧。这是我尝试过的一些代码:
import sharkBot
import asyncio
from discord.ext import commands
import discord
TOKEN = '<Bot Token Here>'
bot = commands.Bot(command_prefix='!')
shark = sharkBot.sharkBot()
@bot.command()
async def cap (ctx, *args):
#Create dictionary for storing values
values = {}
params = {'p' : 'packet_count', 'o' : 'output_file', 't' : 'tshark_path'}
#Add values to dictionary
for arg in args:
arg = arg.replace('-', '')
value = arg[arg.find("(")+1:arg.find(")")]
sep = '('
arg = arg.split(sep, 1)[0]
command = params.get(arg, "Invalid Command")
values[command] = value
#Sniff packet based on values
packets = asyncio.get_event_loop().run_until_complete(packetCap(values))
await ctx.send(packets)
def packetCap(values):
capture = shark.capturePacketsByCount(**values)
return capture
bot.run(TOKEN)
capturePacketsByCount(** values)是:
def capturePacketsByCount(self, interface="en0", packet_count=100, tshark_path="/<PATH NAME>", output_file=None):
#capture specified number of packets
self.capture = pyshark.LiveCapture(tshark_path=tshark_path, interface=interface, output_file=output_file)
self.capture.sniff(packet_count=packet_count)
self.caplen = len(self.capture)
#capture packets for given amount of time
return self.capture
我还尝试用以下代码段替换cap的最后两行:
packets = await shark.capturePacketsByCount(**values)
await ctx.send(packets)
我也尝试过使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
感谢您能为我提供的任何帮助。