-1
\$\begingroup\$

Inspired from Is this site down or is it just me?

Challenge

You have to print if a website server is online or not, for you.

Input

The domain name, like codegolf.stackexchange.com

Output

  • 1/True if online
  • 0/False if offline

Rules

  • Any kind of web access is allowed

  • URL shorteners allowed

  • Except 2 no. Rule, all standard loopholes forbidden

  • You cannot use a language which cannot access internet

  • This is , so shortest answer wins

Cheers!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Is the definition of "online" flexible? For example ping may ensure that a computer is running at that address but there may not be a webserver actually running. Does it have to respond to HTTP requests and return a successful status code? \$\endgroup\$
    – Suever
    Feb 17 at 16:57
  • 6
    \$\begingroup\$ @Alovelygirl Ping does not return HTTP status codes as it is not HTTP \$\endgroup\$
    – Suever
    Feb 17 at 17:01
  • 5
    \$\begingroup\$ I would highly recommend you start posting your challenge ideas to the Sandbox first to get feedback \$\endgroup\$ Feb 17 at 17:09
  • 2
    \$\begingroup\$ To be clear, is the challenge to 1) resolve a hostname to an IP address to confirm the existence of a DNS record 2) a successful ICMP ping reply (not conclusive either way if the host is accessible or not) 3) accepting connections on TCP 80 or 443 4) make any HTTP (or HTTPS?) request and test that it returns a 2XX status code? \$\endgroup\$
    – 640KB
    Feb 17 at 19:21
  • 1
    \$\begingroup\$ Also does the input need to include the scheme and path (ex: https://codegolf.stackexchange.com/) or is that implied by codegolf.stackexchange.com? \$\endgroup\$
    – 640KB
    Feb 17 at 19:33

11 Answers 11

3
\$\begingroup\$

APL (Dyalog Unicode), 15 bytes (SBCS)

Full program. Prompts for URL from stdin.

1≠≢⎕SH'ping ',⍞

Try it online!

 prompt for URL

'ping ', prepend command

⎕SHshell out

count the number of lines in the response (will be 1 on fail, more on success)

1≠ does 1 differ from that? (1 or 0)

\$\endgroup\$
3
\$\begingroup\$

PowerShell, 21 26 bytes

+5 bytes thanks to Joel Coehoorn, who correctly pointed out that I misunderstood the challenge.

!!((ping @args)-match's=')
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Sneaky one, +1 ;-) \$\endgroup\$
    – wasif
    Feb 17 at 17:56
  • 1
    \$\begingroup\$ I'd like to understand this more. I understand it resolves the hostname, but how does it then check if the host is actually responding? \$\endgroup\$ Feb 17 at 18:05
  • \$\begingroup\$ @JoelCoehoorn thanks for pointing this out - I misunderstood the prompt. Should be fixed now. \$\endgroup\$ Feb 17 at 18:42
2
\$\begingroup\$

MATL, 4 bytes

This answer takes the challenge literally and ensures that an HTTP web server is successfully running at the address.

4#Xi

The online MATL compilers do not have access to arbitrary URLs, but this can be run using MATL locally.

Explanation

     % Implicitly retrieve the input URL
4#   % Specify that we want only the second output of the next function call
Xi   % Call urlread function which attempts to make an HTTP request to the URL.
     % The second output value that we save indicates success (1) or failure (0).
\$\endgroup\$
2
\$\begingroup\$

Bash, 10 bytes

curl -f $1

(Can't) try it online!

Works fine on my laptop. Returns 0 (linux truthy) if all is well or some other int if not (linux falsey).

From man curl:

-f, --fail (HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts.

\$\endgroup\$
2
\$\begingroup\$

Powershell, 32 25 bytes

!!(resolve-dnsname @args)

Equal byte alternative (Will take 4 seconds, to send 4 pings):

!!(test-connection @args)

-7 bytes thx to @ZaelinGoodman

\$\endgroup\$
3
  • \$\begingroup\$ You can save 3 bytes with splatting; @args rather than $args[0] \$\endgroup\$ Feb 17 at 17:22
  • \$\begingroup\$ you can also save another 4 bytes by using the not operator to coerce a boolean, rather than explicitly casting: !!(resolve-dnsname @args) \$\endgroup\$ Feb 17 at 17:39
  • \$\begingroup\$ @ZaelinGoodman thank you \$\endgroup\$
    – wasif
    Feb 17 at 17:47
2
\$\begingroup\$

PowerShell, 13 bytes

!!(iwr @args)

There's also this, at 30 bytes:

(iwr @args).StatusCode -eq 200

Which isn't as short as some other answers, but IMO so far it's the only one that actually meets the goal of validating the web server. Others could be getting an HTTP 500-series response and saying everything is fine.

Powershell 7 also has -SkipHttpErrorCheck, so we can avoid some exceptions and show a nice False more often, but that almost doubles the size.

\$\endgroup\$
2
  • \$\begingroup\$ I'm having an issue testing this solution; cases which should return false are returning null. I'd think the !! would fix that, but that's not what I'm seeing. I thought maybe setting the error action would fix it, but it did not. Any insights? \$\endgroup\$ Feb 17 at 18:54
  • \$\begingroup\$ The shortest way I can find to output properly using Invoke-WebRequest is !!(iwr @args||0) \$\endgroup\$ Feb 17 at 19:04
2
\$\begingroup\$

Using only built-in libs.

Python 3, 77 bytes

from urllib.request import*
try:print(bool(urlopen(input())))
except:print(0)

Try it online!

-42 bytes by @Makonede!

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to Code Golf! It looks like you can golf this down quite a bit, by removing some unnecessary whitespace. It also looks like z can be inlined, although I don't know much python. You may want to check out Tips for golfing in Python. \$\endgroup\$ Feb 17 at 18:36
  • \$\begingroup\$ Nice first answer! It's quite annoying that this doesn't work on TIO because of their limitations... You can golf off 54 bytes like so; here's a commented version. \$\endgroup\$
    – Makonede
    Feb 17 at 18:52
  • \$\begingroup\$ Thanks! I made small changes, but I can't admit that @Makonede examples are much better then mine without any functionality lose. Maybe it's better to post them as answer and I'll delete mine? \$\endgroup\$
    – Charnel
    Feb 17 at 18:57
  • \$\begingroup\$ Sure, if it's fine with you I can post it as a separate answer. But you should still keep your answer, I like the idea. However, considering that I just golfed your code for you, I recommend that you use my idea. It's the exact same concept, just with a few minor golfs. \$\endgroup\$
    – Makonede
    Feb 17 at 18:59
  • \$\begingroup\$ @Makonede thanks for helping to improve my golf! I updated an answer and now it's -42 bytes off from last update. \$\endgroup\$
    – Charnel
    Feb 18 at 10:21
1
\$\begingroup\$

PHP (-F), 34 bytes

<?=$argn!=gethostbyname($argn)?:0;

(Cannot) Try it online!

Will display 1 or 0. Reminds me a recent answer.. don't know why..

Explanation: gethostbyname returns the domain string when it fails, and when the first alternative b is missing in ternary a?b:c, a's value is used. Truthy values are displayed 1 by default

Such kind of requests are disabled on TIO though.

\$\endgroup\$
1
\$\begingroup\$

Python 2, 80 66 bytes

 try:get(x).ok
 except:print 1
 else:print 0
from requests import*

Try it online!

-14 bytes for @Suever!

\$\endgroup\$
3
  • \$\begingroup\$ What about using the .ok method instead of comparing to 200 given that many other status codes are considered successful \$\endgroup\$
    – Suever
    Feb 17 at 17:49
  • \$\begingroup\$ @Suever thanks! \$\endgroup\$
    – wasif
    Feb 17 at 17:55
  • \$\begingroup\$ unfortunately, the creation of a function can be removed only in the case of using lambda, but you have a def so its length must also be taken into account \$\endgroup\$
    – Danis
    Feb 17 at 18:49
1
\$\begingroup\$

SlimSharp, 56 bytes

P((new System.Net.WebClient.DownloadData(R())).L() > 0);

Can't test this on .Net Fiddle because it guts the WebClient type.

\$\endgroup\$
0
\$\begingroup\$

Windows Batch, 38 bytes

@ping %1 >nul&&(echo 1&exit /b)&echo 0
\$\endgroup\$
1
  • \$\begingroup\$ ping doesn't actually fail if the site cannot be pinged, does it? \$\endgroup\$
    – Neil
    Feb 17 at 18:51