How can I listen on my server on specific port and just return HTTP 200 status if someone tries to connect ?

Can nc be used to return HTTP status ?


There's really no reason to use nc nowadays. With socat:

socat tcp-listen:12345,reuseaddr,fork \
   "exec:printf \'HTTP/1.0 200 OK\r\n\r\n\'"

With zsh and its ztcp builtin:

zmodload zsh/net/tcp
ztcp -ld 3 12345
while ztcp -ad 4 3; do
  print -u4 'HTTP/1.0 200 OK\r\n\r'
  ztcp -c 4

python -m SimpleHTTPServer 8000 will serve the current directory as a web page on port 8000 (the default port; ports below 1024 will require you to run with root (e.g. sudo) privileges). A "200 OK" will be returned if you fetch from that port:

$ python -m SimpleHTTPServer 8000 &
[1] 19976
$ Serving HTTP on port 8000 ...

$ wget localhost:8000
--2014-03-12 00:42:30--  http://localhost:8000/
Resolving localhost (localhost)... ::1,
Connecting to localhost (localhost)|::1|:8000... failed: Connection refused.
Connecting to localhost (localhost)||:8000... connected.
HTTP request sent, awaiting response... - - [12/Mar/2014 00:42:30] "GET / HTTP/1.1" 200 -
200 OK
Length: 178 [text/html]
Saving to: `index.html'

100%[======================================>] 178         --.-K/s   in 0s      

2014-03-12 00:42:30 (16.0 MB/s) - `index.html' saved [178/178]

$ cat index.html 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>
<title>Directory listing for /</title>
<h2>Directory listing for /</h2>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.