我的 python 代码没有在 pycharm 中运行。它开始并且永远不会结束,我也没有收到任何错误消息

My python code is not running in pycharm. It starts and never end and I also doesn't get any error message

提问人:Manas Agrawal 提问时间:11/2/2023 更新时间:11/2/2023 访问量:29

问:

import nmap
from getmac import get_mac_address
from telegram import Bot
import asyncio

IP = '192.168.0.1'
KNOWN_DEVICES = []
TELEGRAM_BOT_TOKEN = '6577977517:AAFIqsW2UNE8d5HNyEEL2-3t_XK-8010scc'
CHAT_ID = '-4089379289'


class NetworkScanner:

    def __init__(self, ip: str):
        self.ip = ip
        self.connected_devices = set()

    def scan(self):
        network = f"{self.ip}/24"
        nm = nmap.PortScanner()

        while True:
            nm.scan(hosts=network, arguments='-sn')
            host_list = nm.all_hosts()

            for host in host_list:
                mac = get_mac_address(ip=host)
                print(mac)

                if mac and mac not in self.connected_devices and mac not in KNOWN_DEVICES:
                    print("new Device Found")
                    self.notify_new_devices(mac)
                    self.connected_devices.add(mac)

    async def send_telegram_message(self, bot, chat_id, message):
        await bot.send_message(chat_id=chat_id, text=message)

    def notify_new_devices(self, mac):
        print("Notifying new device:", mac)
        bot = Bot(token=TELEGRAM_BOT_TOKEN)
        asyncio.run(self.send_telegram_message(bot, CHAT_ID, f"New Device Connected! MAc address : {mac}"))


if __name__ == "__main__":
    scanner = NetworkScanner(IP)
    scanner.scan()

我的 python 代码没有在 pycharm 中运行。它开始并且永远不会结束,我也没有收到任何错误消息。 我正在制作一个 python 程序,当新设备连接到我的电报机器人上的 wifi 网络时,我会收到通知 请帮我 enter image description here #python #debugging #programming #telegram 机器人 #bots

python 调试 pycharm 电报机器人

评论

0赞 Codist 11/2/2023
您的代码是否在 Pycharm 之外运行?您是否尝试过调试代码?
0赞 user19315471 11/2/2023
在第一句话中,你说代码没有运行,但在第二句话中,你说“它开始了”。在提出问题之前,您真的应该三思而后行。
1赞 jhutar 11/8/2023
请将打印添加到代码的各个位置,以查看发生了什么以及它在代码中的位置。恕我直言,这是您可以更深入地了解它的最简单方法。

答: 暂无答案