{"id":5031,"date":"2024-11-22T16:24:51","date_gmt":"2024-11-22T08:24:51","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=5031"},"modified":"2024-11-22T16:24:52","modified_gmt":"2024-11-22T08:24:52","slug":"python%e6%89%ab%e6%8f%8f%e6%9c%ba%e5%99%a8%ef%bc%88%e7%94%b5%e8%84%91%e3%80%81%e6%9c%8d%e5%8a%a1%e5%99%a8%ef%bc%89%e6%89%93%e5%bc%80%e7%9a%84%e7%ab%af%e5%8f%a3","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=5031","title":{"rendered":"python\u626b\u63cf\u673a\u5668\uff08\u7535\u8111\u3001\u670d\u52a1\u5668\uff09\u6253\u5f00\u7684\u7aef\u53e3"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\"\"\"\nCode illustration: 7.09\n    Port Scanner\nTkinter GUI Application Development Blueprints\n\"\"\"\nimport socket\nfrom tkinter import Tk, Label, Entry, Button, Frame, Scrollbar, W, EW, E, Text, \\\n    DISABLED, Y, BOTH, NORMAL, END\nfrom threading import Thread\n\n\nclass PortScanner():\n\n    stop = False\n    url = \"google.com\"\n    start_port = 70\n    end_port = 85\n\n    def __init__(self, root):\n        self.root = root\n        self.create_gui()\n\n    def on_scan_button_clicked(self):\n        self.empty_console()\n        self.scan_in_a_new_thread()\n\n    def empty_console(self):\n        self.console_text.config(state=NORMAL)\n        self.console_text.delete(\"1.0\", END)\n        self.console_text.config(state=DISABLED)\n\n    def scan_in_a_new_thread(self):\n        url = self.host_entry.get()\n        start_port = int(self.start_port_entry.get())\n        end_port = int(self.end_port_entry.get())\n        thread = Thread(target=self.start_scan,\n                        args=(url, start_port, end_port))\n        thread.start()\n\n    def start_scan(self, url, start_port, end_port):\n        for port in range(start_port, end_port + 1):\n            if not self.stop:\n                self.output_to_console(\"Scanning port {}\".format(port))\n                if self.is_port_open(url, port):\n                    self.output_to_console(\" -- Port {} open \\n\".format(port))\n                else:\n                    self.output_to_console(\"-- Port {} closed \\n\".format(port))\n\n    def is_port_open(self, url, port):\n        try:\n            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            s.settimeout(1)\n            s.connect((socket.gethostbyname(url), port))\n            s.close()\n            return True\n        except:\n            return False\n\n    def on_stop_button_clicked(self):\n        self.stop = True\n\n    def output_to_console(self, new_text):\n        self.console_text.config(state=NORMAL)\n        self.console_text.insert(END, new_text)\n        self.console_text.see(END)\n        self.console_text.config(state=DISABLED)\n\n    def create_gui(self):\n        Label(self.root, text='Host :').grid(row=\"1\", column=\"1\", sticky=W)\n        self.host_entry = Entry(self.root)\n        self.host_entry.insert(0, self.url)\n        self.host_entry.grid(row=\"1\", column=\"2\", sticky=EW)\n        Label(self.root, text='Start Port :').grid(\n            row=\"2\", column=\"1\", sticky=W)\n        self.start_port_entry = Entry(self.root)\n        self.start_port_entry.insert(0, self.start_port)\n        self.start_port_entry.grid(row=\"2\", column=\"2\", sticky=EW)\n        Label(self.root, text='End Port :').grid(row=\"3\", column=\"1\", sticky=W)\n        self.end_port_entry = Entry(self.root)\n        self.end_port_entry.insert(0, self.end_port)\n        self.end_port_entry.grid(row=\"3\", column=\"2\", sticky=EW)\n        Button(self.root, text='Scan', command=self.on_scan_button_clicked).grid(\n            row=\"4\", column=\"2\", sticky=E)\n        Button(self.root, text='Stop', command=self.on_stop_button_clicked).grid(\n            row=\"4\", column=\"2\", sticky=W)\n        Label(self.root, text='Scan Result :').grid(\n            row=\"5\", column=\"1\", sticky=W)\n        console_frame = Frame(self.root)\n        console_frame.grid(row=\"6\", column=\"1\", columnspan=\"2\")\n        self.console_text = Text(\n            console_frame, fg=\"green\", bg=\"black\", state=DISABLED)\n        scrollbar = Scrollbar(console_frame, command=self.console_text.yview)\n        scrollbar.pack(side=\"right\", fill=Y)\n        self.console_text.pack(expand=1, fill=BOTH)\n        self.console_text&#91;'yscrollcommand'] = scrollbar.set\n\nif __name__ == '__main__':\n    root = Tk()\n    PortScanner(root)\n    root.mainloop()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5031","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5031"}],"version-history":[{"count":1,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5031\/revisions"}],"predecessor-version":[{"id":5032,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/5031\/revisions\/5032"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}