49
3
Write a program which, according to whether the script has access to the internet, produces an output which is "Truthy/Falsey". You may try and connect to any existing site, at your own discretion (don't use a shady site which only has 10% uptime - try to keep to above 80% annual uptime). If the site is down, your program does not have to work.
It must be a standalone program or a function. You may use libraries outside of the standard library to achieve this. Standard loopholes are forbidden. This is code golf, so the code with the shortest byte-count wins.
Example pseudocode:
function a:
try:
connect to internet
return 1
catch error:
return 0
This is my first post on code golf, so if this violates any rules in any way or is a dupe, please alert me.
EDIT: Due to numerous suggestions, I have removed the UTF-8 byte count restriction
4
Instead of
– Blue – 2017-01-13T13:34:07.287true
andfalse
, I recommend allowing any of our defaults for truthy and falsiness. Also, by internet, do you mean the network outside your local network? Do programs still have to work if say google is down or any other large site?@muddyfish thanks so much for your answer! I will edit my question to be more clear :) – Restioson – 2017-01-13T13:38:37.083
3Byte count is usually done in the language's native or most convenient encoding, which is not always UTF-8. Unless you a have a good reason to enforce UTF-8, I think the encoding should be left at the programmer's choice – Luis Mendo – 2017-01-13T14:04:23.273
@LuisMendo I chose this to prevent ambiguity – Restioson – 2017-01-13T14:20:41.357
4
I see almost everyone is using
– Kevin Cruijssen – 2017-01-13T14:28:36.790g.gl
/http://g.gl/
, butto.
/http://to./
seems to be one byte shorter (not all languages see it as a valid url through).9Commodore Basic:
PRINT "0"
– Mark – 2017-01-13T19:24:58.2603The very machine I'm typing this at, is technically a part of the "Internet", as it can be accessed from the outside (via NAT and port forwarding).
So, if you think of it, the "internet detection" script can probably be reduced to "true" :) – zeppelin – 2017-01-13T19:55:05.117
2@Restioson We have a Meta question to define how to count size of programs. Enforcing counting in UTF8 is just going to make things more confusing. Also, what if someone solves this in, say, Vim? Or TI-Basic? How do you count those in UTF8? – Fund Monica's Lawsuit – 2017-01-13T22:18:57.807
1
@KevinCruijssen
– NobodyNada - Reinstate Monica – 2017-01-13T22:26:30.033to
(without the.
at the end) also seems to work.What is an objective definition of "80% uptime"? And is that defined to be in the last week, last month, or what? – Buffer Over Read – 2017-01-14T13:15:18.100
Another thing, does the code have to check/ping live internet sites or is checking the DNS server okay? Because the DNS server being operational doesn't technically mean your computer can connect to the WWW, even if it might usually do. – Buffer Over Read – 2017-01-14T13:18:49.333
@H Walters The reason I said "WWW" is because that's how I interpreted the question. If OP meant just the internet, that would be different. – Buffer Over Read – 2017-01-14T21:07:29.453
@TheBitByte true. But I mean do not use a site which is only on on Thursdays, for example. Keep it sensible – Restioson – 2017-01-15T09:28:43.377
@zeppelin your computer would no longer be connected to the internet, if, say, I made your means of network communication break, e.g unplugging your ethernet cable – Restioson – 2017-01-15T09:34:06.807
@TheBitByte yes, checking a DNS server is OK, as long as it isn't on your local network (LAN, hosted by your local router) – Restioson – 2017-01-15T09:35:07.830
It's nice that my question has mainly "mainstream" languages in answers as opposed to the usual horde of esolangs in answers. – Restioson – 2017-01-15T12:53:02.383
@Restioson This is Code Golf SE. I'm not sure what answers you were expecting to get. Anyways, can I just ping the DNS server without having to ping a live WWW website? In other words, technically the ISP's DNS server working isn't always an indication of the WWW working, even if it usually is. – Buffer Over Read – 2017-01-15T21:22:29.057
@Mark
? 0
(or even?0
?) should do it. Untested - from memory. – Thorbjørn Ravn Andersen – 2017-01-16T03:04:05.453@NobodyNada Hmm.. It doesn't for me, and I tested it on three different devices/networks. :S – Kevin Cruijssen – 2017-01-16T09:16:51.873
@Mark - not really. There is a TCP/IP implementation for Commodore 64, and there even are network cards for C64 with embedded TCP/IP. – Radovan Garabík – 2017-03-01T12:17:08.650
@KevinCruijssen seems http://to. is down now
– Restioson – 2017-07-27T17:18:38.217