{"id":297,"date":"2017-07-24T03:18:23","date_gmt":"2017-07-24T02:18:23","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=297"},"modified":"2017-07-24T23:14:46","modified_gmt":"2017-07-24T22:14:46","slug":"python-code-snippets-check-internet-connection","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=297","title":{"rendered":"Python: Code Snippets &#8211; Check Internet Connection"},"content":{"rendered":"<p>This code snippets checks if the computer has an internet connection:<\/p>\n<p>To run this script copy the code below and save to a file called internet_connection.py<\/p>\n<p>internet_connection.py:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\n#!\/usr\/bin\/env python\r\n \r\n# Check Internet Connection\r\n# A quick way to check if computer is\r\n# currently connected to the internet\r\n \r\n# Date: 07 March 2016\r\n# Written By: Phantom Raspberry Blower\r\n\r\nimport urllib2  # Module to request web page\r\n\r\n\r\nclass InternetConnection():\r\n\r\n    def internet_connected(self, url):\r\n        # Function that checks the internet is available\r\n        try:\r\n            response = urllib2.urlopen(url, timeout=5)\r\n            return True\r\n        except:\r\n            return False\r\n\r\n# Check if running stand-alone or imported\r\nif __name__ == '__main__':\r\n    import internet_connection\r\n    it = InternetConnection()\r\n    try:\r\n        if it.internet_connected(&quot;http:\/\/www.google.co.uk&quot;):\r\n            print(&quot;Internet connection is available :)&quot;)\r\n        else:\r\n            print(&quot;Internet connection not avaiable :(&quot;)\r\n    except:\r\n        print(&quot;Something wicked happened :(&quot;)\r\n\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython internet_connection.py\r\nInternet connection is available : )\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This code snippets checks if the computer has an internet connection: To run this script copy the code below and save to a file called internet_connection.py internet_connection.py: #!\/usr\/bin\/env python # Check Internet Connection # A quick way to check if computer is # currently connected to the internet # Date: 07 March 2016 # Written [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":239,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=297\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"This code snippets checks if the computer has an internet connection: To run this script copy the code below and save to a file called internet_connection.py internet_connection.py: #!\/usr\/bin\/env python # Check Internet Connection # A quick way to check if computer is # currently connected to the internet # Date: 07 March 2016 # Written [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=297\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2017-07-24T22:14:46+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=297\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=297\",\"name\":\"Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-07-24T02:18:23+00:00\",\"dateModified\":\"2017-07-24T22:14:46+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=297#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=297\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=297#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=73\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Python: Code Snippets\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=239\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Python: Code Snippets &#8211; Check Internet Connection\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=297","og_locale":"en_GB","og_type":"article","og_title":"Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower","og_description":"This code snippets checks if the computer has an internet connection: To run this script copy the code below and save to a file called internet_connection.py internet_connection.py: #!\/usr\/bin\/env python # Check Internet Connection # A quick way to check if computer is # currently connected to the internet # Date: 07 March 2016 # Written [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=297","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2017-07-24T22:14:46+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=297","url":"http:\/\/www.bulis.co.uk\/?page_id=297","name":"Python: Code Snippets - Check Internet Connection - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-07-24T02:18:23+00:00","dateModified":"2017-07-24T22:14:46+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=297#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=297"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=297#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Python Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=73"},{"@type":"ListItem","position":4,"name":"Python: Code Snippets","item":"http:\/\/www.bulis.co.uk\/?page_id=239"},{"@type":"ListItem","position":5,"name":"Python: Code Snippets &#8211; Check Internet Connection"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/297"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=297"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/297\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/239"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}