{"id":818,"date":"2023-05-29T22:18:24","date_gmt":"2023-05-29T14:18:24","guid":{"rendered":"http:\/\/xinyixx.com\/?p=818"},"modified":"2023-05-30T08:37:50","modified_gmt":"2023-05-30T00:37:50","slug":"python3","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/05\/29\/python3\/","title":{"rendered":"python\u7a0b\u5e8f4\uff1a\u81ea\u52a8ping\u5305\u7684\u5c0f\u7a0b\u5e8f"},"content":{"rendered":"<p>\u5e73\u65f6\u81ea\u52a8\u5316\u8fd0\u7ef4\u7684\u5de5\u4f5c\u6bd4\u8f83\u7e41\u7410\u4e14\u67af\u71e5\uff0c\u600e\u4e48\u901a\u8fc7\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u5b9e\u73b0\u81ea\u52a8ping\u5305\uff0c\u5e76\u8fd4\u56de\u6587\u672c\u6846\u5185\u3002<\/p>\n\n\n\n<p>Socket \u662f\u4e00\u79cd\u7f51\u7edc\u7f16\u7a0b\u63a5\u53e3\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u5728\u7f51\u7edc\u4e0a\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u800c ping \u547d\u4ee4\u5219\u662f\u4e00\u79cd\u7528\u4e8e\u6d4b\u8bd5\u7f51\u7edc\u8fde\u63a5\u7684\u5de5\u5177\uff0c\u5b83\u901a\u8fc7\u5411\u76ee\u6807\u670d\u52a1\u5668\u53d1\u9001 ICMP \u62a5\u6587\u5e76\u7b49\u5f85\u54cd\u5e94\uff0c\u6765\u5224\u65ad\u76ee\u6807\u670d\u52a1\u5668\u662f\u5426\u53ef\u8fbe\u3002<\/p>\n\n\n\n<p>\u5728\u4f7f\u7528 Socket \u53bb ping \u670d\u52a1\u5668\u65f6\uff0c\u901a\u5e38\u9700\u8981\u521b\u5efa\u4e00\u4e2a Socket \u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u8be5\u5bf9\u8c61\u53d1\u9001 ICMP \u62a5\u6587\u5230\u76ee\u6807\u670d\u52a1\u5668\u7684 IP \u5730\u5740\u3002\u5982\u679c\u76ee\u6807\u670d\u52a1\u5668\u80fd\u591f\u63a5\u6536\u5230\u8be5\u62a5\u6587\u5e76\u6210\u529f\u54cd\u5e94\uff0c\u90a3\u4e48\u5c31\u8bf4\u660e\u76ee\u6807\u670d\u52a1\u5668\u662f\u53ef\u8fbe\u7684\u3002<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cICMP \u62a5\u6587\u662f\u4e00\u79cd\u7279\u6b8a\u7c7b\u578b\u7684\u6570\u636e\u5305\uff0c\u5b83\u4e0d\u540c\u4e8e\u666e\u901a\u7684 TCP \u6216 UDP \u6570\u636e\u5305\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528 Socket \u53bb ping \u670d\u52a1\u5668\u65f6\uff0c\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684 ICMP \u534f\u8bae\u6765\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f\u53ef\u4ee5\u53c2\u8003\u76f8\u5173\u7684 Socket \u7f16\u7a0b\u6587\u6863\u548c ICMP \u534f\u8bae\u89c4\u8303\u3002<\/p>\n\n\n\n<p>\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-header-gradient-color\">import sys\nimport socket\nfrom PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit, QAction\n\nclass MainWindow(QMainWindow):\n    def __init__(self):\n        super().__init__()\n\n        # \u521b\u5efa\u83dc\u5355\u680f\n        menu_bar = self.menuBar()\n        file_menu = menu_bar.addMenu('File')\n        exit_action = QAction('Exit', self)\n        exit_action.triggered.connect(self.close)\n        file_menu.addAction(exit_action)\n\n        # \u521b\u5efa\u6587\u672c\u7f16\u8f91\u6846\n        self.text_edit = QTextEdit()\n        self.setCentralWidget(self.text_edit)\n\n        # \u68c0\u67e5\u670d\u52a1\u5668\u548c\u7aef\u53e3\u7684\u901a\u65ad\u60c5\u51b5\n        self.check_status('www.google.com', 80)\n        self.check_status('www.baidu.com', 80)\n        self.check_status('www.bing.com', 443)\n        self.check_status('www.xinyixx.com', 80)\n        self.check_status('c.zzgao.eu.org', 443)\n\n\n    def check_status(self, host, port):\n        try:\n            # \u521b\u5efa\u4e00\u4e2asocket\u5bf9\u8c61\u5e76\u8fde\u63a5\u5230\u6307\u5b9a\u7684\u4e3b\u673a\u548c\u7aef\u53e3\n            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n            s.settimeout(5)\n            s.connect((host, port))\n\n            # \u8f93\u51fa\u8fde\u63a5\u6210\u529f\u7684\u6d88\u606f\n            message = f'{host}:{port} is open'\n            print(message)\n            self.text_edit.append(message)\n\n            # \u5173\u95edsocket\u8fde\u63a5\n            s.close()\n        except:\n            # \u8f93\u51fa\u8fde\u63a5\u5931\u8d25\u7684\u6d88\u606f\n            message = f'{host}:{port} is closed'\n            print(message)\n            self.text_edit.append(message)\n\nif __name__ == '__main__':\n    app = QApplication(sys.argv)\n    window = MainWindow()\n    window.show()\n    sys.exit(app.exec_())<\/mark><\/pre>\n\n\n\n<p>\u5728pycharm\u91cc\u8fd0\u884c\u770b\u4e00\u4e0b\u6548\u679c\u3002\u5982\u679c\u9700\u8981\u5176\u4ed6\u5730\u5740\u53ef\u4ee5\u5728python\u6587\u4ef6\u4e2d\u76f4\u63a5\u6dfb\u52a0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"531\" height=\"398\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17.png\" alt class=\"wp-image-819\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17.png 531w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17-300x225.png 300w\" sizes=\"(max-width: 531px) 100vw, 531px\"><\/noscript><img decoding=\"async\" width=\"531\" height=\"398\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20531%20398%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-819 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20531%20398%22%3E%3C%2Fsvg%3E 531w\" sizes=\"(max-width: 531px) 100vw, 531px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17.png 531w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17-300x225.png 300w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/05\/image-17.png\"><\/figure>\n\n\n\n<p>http\u534f\u8bae\u9ed8\u8ba4\u7aef\u53e3\u662f80\uff1bhttps\u9ed8\u8ba4\u7aef\u53e3\u662f443\uff1b\u5982\u4e0a\u5c31\u53ef\u4ee5\u76f4\u89c2\u7684\u89c2\u5bdf\u5177\u4f53\u54ea\u4e2a\u7f51\u7ad9\u662f\u4e2d\u65ad\u7684\uff0c\u53ca\u65f6\u7684\u6392\u67e5\u5e76\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u6309\u4e4b\u524d\u4e00\u6837\u53ef\u4ee5\u76f4\u63a5\u7528pyinstaller\u751f\u6210\u4e00\u4e2a\u53ef\u6267\u884c\u6587\u4ef6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e73\u65f6\u81ea\u52a8\u5316\u8fd0\u7ef4\u7684\u5de5\u4f5c\u6bd4\u8f83\u7e41\u7410\u4e14\u67af\u71e5\uff0c\u600e\u4e48\u901a\u8fc7\u5199\u4e00\u4e2a\u7a0b\u5e8f\u6765\u5b9e\u73b0\u81ea\u52a8ping\u5305\uff0c\u5e76\u8fd4\u56de\u6587\u672c\u6846\u5185\u3002 Socket \u662f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10],"tags":[71,85],"class_list":["post-818","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","tag-python","tag-exe","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=818"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/818\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}