{"id":1818,"date":"2022-11-27T21:33:55","date_gmt":"2022-11-27T13:33:55","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1818"},"modified":"2022-11-27T21:33:56","modified_gmt":"2022-11-27T13:33:56","slug":"python%e8%8e%b7%e5%8f%96%e4%b8%bb%e6%9c%baip_python-%e4%bb%8e%e4%b8%bb%e6%9c%ba%e5%90%8d%e8%8e%b7%e5%8f%96ip%e5%9c%b0%e5%9d%80","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1818","title":{"rendered":"python\u83b7\u53d6\u4e3b\u673aip_Python \u2013\u4ece\u4e3b\u673a\u540d\u83b7\u53d6IP\u5730\u5740"},"content":{"rendered":"\n<p>python\u83b7\u53d6\u4e3b\u673aip<\/p>\n\n\n\n<p><a href=\"https:\/\/www.journaldev.com\/15906\/python-socket-programming-server-client\" target=\"_blank\" rel=\"noreferrer noopener\">Python socket module<\/a>&nbsp;can be used to get the IP address from a hostname.<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.journaldev.com\/15906\/python-socket-programming-server-client\" target=\"_blank\">Python\u5957\u63a5\u5b57\u6a21\u5757<\/a>\u53ef\u7528\u4e8e\u4ece\u4e3b\u673a\u540d\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n\n\n\n<p>The\u00a0socket\u00a0module is part of the Python core libraries, so we don\u2019t need to install it separately.<\/p>\n\n\n\n<p>socket\u6a21\u5757\u662fPython\u6838\u5fc3\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u6211\u4eec\u4e0d\u9700\u8981\u5355\u72ec\u5b89\u88c5\u5b83\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Python\u5957\u63a5\u5b57\u6a21\u5757\u4ece\u4e3b\u673a\u540d\u83b7\u53d6IP\u5730\u5740\u00a0<strong>(Python Socket Module to Get IP Address from Hostname)<\/strong><\/h2>\n\n\n\n<p>Python socket module&nbsp;<code>gethostbyname()<\/code>&nbsp;function accepts hostname argument and returns the IP address in the string format.<\/p>\n\n\n\n<p>Python\u5957\u63a5\u5b57\u6a21\u5757\u7684<code>gethostbyname()<\/code>\u51fd\u6570\u63a5\u53d7\u4e3b\u673a\u540d\u53c2\u6570\uff0c\u5e76\u4ee5\u5b57\u7b26\u4e32\u683c\u5f0f\u8fd4\u56deIP\u5730\u5740\u3002<\/p>\n\n\n\n<p>Here is a simple example in the Python interpreter to find out the IP address of some of the websites.<\/p>\n\n\n\n<p>\u8fd9\u662fPython\u89e3\u91ca\u5668\u4e2d\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff0c\u7528\u4e8e\u67e5\u627e\u67d0\u4e9b\u7f51\u7ad9\u7684IP\u5730\u5740\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\n# python3.7\r\nPython 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 16:52:21) \r\n&#91;Clang 6.0 (clang-600.0.57)] on darwin\r\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n>>> \r\n>>> import socket\r\n>>> socket.gethostbyname('journaldev.com')\r\n'45.79.77.230'\r\n>>> socket.gethostbyname('google.com')\r\n'172.217.166.110'\r\n>>> <\/code><\/pre>\n\n\n\n<p><strong>Note<\/strong>: If the website is behind a load-balancer or working in the cloud, you might get a different result for the IP address lookup.<\/p>\n\n\n\n<p><strong>\u6ce8\u610f<\/strong>&nbsp;\uff1a\u5982\u679c\u7f51\u7ad9\u5728\u8d1f\u8f7d\u5747\u8861\u5668\u540e\u9762\u6216\u5728\u4e91\u4e2d\u8fd0\u884c\uff0c\u5219IP\u5730\u5740\u67e5\u627e\u53ef\u80fd\u4f1a\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>For example, try to run the above command for google.com or facebook.com. If you are not in the same location as mine (India), chances are that you will get a different IP address as output.<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c1d\u8bd5\u5bf9google.com\u6216facebook.com\u8fd0\u884c\u4ee5\u4e0a\u547d\u4ee4\u3002 \u5982\u679c\u60a8\u4e0e\u6211\u7684\u4f4d\u7f6e\u4e0d\u540c\uff08\u5370\u5ea6\uff09\uff0c\u5219\u53ef\u80fd\u4f1a\u83b7\u5f97\u4e0d\u540c\u7684IP\u5730\u5740\u4f5c\u4e3a\u8f93\u51fa\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a><\/a>\u627e\u51fa\u7f51\u7ad9IP\u5730\u5740\u7684Python\u811a\u672c&nbsp;<strong>(<\/strong><strong>Python Script to Find Out the IP Address of a Website<\/strong><strong>)<\/strong><\/h2>\n\n\n\n<p>Let\u2019s look at an example where we ask user to enter a website address and then print its IP address.<\/p>\n\n\n\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u793a\u4f8b\uff0c\u5176\u4e2d\u8981\u6c42\u7528\u6237\u8f93\u5165\u4e00\u4e2a\u7f51\u7ad9\u5730\u5740\uff0c\u7136\u540e\u6253\u5370\u5176IP\u5730\u5740\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\nimport socket\r\n \r\nhostname = input(\"Please enter website address:\\n\")\r\n \r\n# IP lookup from hostname\r\nprint(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')<\/code><\/pre>\n\n\n\n<p>Here is another example to pass the hostname as a&nbsp;<a href=\"https:\/\/www.journaldev.com\/17743\/python-command-line-arguments\" target=\"_blank\" rel=\"noreferrer noopener\">command-line argument<\/a>&nbsp;to the script. The script will find the IP address and print it.<\/p>\n\n\n\n<p>\u8fd9\u662f\u5c06\u4e3b\u673a\u540d\u4f5c\u4e3a<a rel=\"noreferrer noopener\" href=\"https:\/\/www.journaldev.com\/17743\/python-command-line-arguments\" target=\"_blank\">\u547d\u4ee4\u884c\u53c2\u6570<\/a>\u4f20\u9012\u7ed9\u811a\u672c\u7684\u53e6\u4e00\u4e2a\u793a\u4f8b\u3002 \u8be5\u811a\u672c\u5c06\u627e\u5230IP\u5730\u5740\u5e76\u8fdb\u884c\u6253\u5370\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\nimport socket\r\nimport sys\r\n \r\n# no error handling is done here, excuse me for that\r\nhostname = sys.argv&#91;1]\r\n \r\n# IP lookup from hostname\r\nprint(f'The {hostname} IP Address is {socket.gethostbyname(hostname)}')<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n\n<p><strong>\u8f93\u51fa<\/strong>\u00a0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\n# python3.7 ip_address.py facebook.com\r\nThe facebook.com IP Address is 157.240.23.35<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">socket.gethostbyname\uff08\uff09\u7684\u9519\u8bef\u65b9\u6848&nbsp;<strong>(<\/strong><strong>Error Scenarios with socket.gethostbyname()<\/strong><strong>)<\/strong><\/h2>\n\n\n\n<p>If the hostname doesn\u2019t resolve to a valid IP address,&nbsp;<code>socket.gaierror<\/code>&nbsp;is raised. We can catch this error in our program using&nbsp;<a href=\"https:\/\/www.journaldev.com\/14444\/python-exception-handling-try-except\" target=\"_blank\" rel=\"noreferrer noopener\">try-except<\/a>&nbsp;block.<\/p>\n\n\n\n<p>\u5982\u679c\u4e3b\u673a\u540d\u4e0d\u80fd\u89e3\u6790\u4e3a\u6709\u6548\u7684IP\u5730\u5740\uff0c\u5219\u4f1a\u5f15\u53d1<code>socket.gaierror<\/code>&nbsp;\u3002 \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<a href=\"https:\/\/www.journaldev.com\/14444\/python-exception-handling-try-except\" target=\"_blank\" rel=\"noreferrer noopener\">try-except<\/a>\u5757\u5728\u7a0b\u5e8f\u4e2d\u6355\u83b7\u6b64\u9519\u8bef\u3002<\/p>\n\n\n\n<p>Here is the updated script with exception handling for invalid hostname.<\/p>\n\n\n\n<p>\u8fd9\u662f\u66f4\u65b0\u7684\u811a\u672c\uff0c\u5176\u4e2d\u5305\u542b\u5bf9\u65e0\u6548\u4e3b\u673a\u540d\u7684\u5f02\u5e38\u5904\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\nimport socket\r\nimport sys\r\n \r\nhostname = sys.argv&#91;1]\r\n \r\n# IP lookup from hostname\r\ntry:\r\n    ip = socket.gethostbyname(hostname)\r\n    print(f'The {hostname} IP Address is {ip}')\r\nexcept socket.gaierror as e:\r\n    print(f'Invalid hostname, error raised is {e}')<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \r\n# python3.7 ip_address.py jasjdkks.com               \r\nInvalid hostname, error raised is &#91;Errno 8] nodename nor servname provided, or not known\r\n#<\/code><\/pre>\n\n\n\n<p><strong>Reference<\/strong>:&nbsp;<a href=\"https:\/\/docs.python.org\/3.8\/library\/socket.html#socket.gethostbyname\" target=\"_blank\" rel=\"noreferrer noopener\">Socket Module API Docs<\/a><\/p>\n\n\n\n<p><strong>\u53c2\u8003<\/strong>\u00a0\uff1a\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3.8\/library\/socket.html#socket.gethostbyname\" target=\"_blank\">\u5957\u63a5\u5b57\u6a21\u5757API\u6587\u6863<\/a><\/p>\n\n\n\n<p>\u7ffb\u8bd1\u81ea:\u00a0<a href=\"https:\/\/www.journaldev.com\/39296\/python-get-ip-address-from-hostname\">https:\/\/www.journaldev.com\/39296\/python-get-ip-address-from-hostname<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>python\u83b7\u53d6\u4e3b\u673aip Python socket module&nbsp;can be used to g [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[50],"class_list":["post-1818","post","type-post","status-publish","format-standard","hentry","category-python-","tag-python"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1818"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1818"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions"}],"predecessor-version":[{"id":1819,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1818\/revisions\/1819"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}