[Question]
  [
As we all should know, [there's a HTTP status code **418: I'm a teapot**](http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#418).
Your mission, should you choose to accept it, is to use your creativitea and write the smallest possible server that responds with the above status code to any and every HTTP request made to it.
[Standard loopholes](http://meta.codegolf.stackexchange.com/questions/1061/standard-loopholes-which-are-no-longer-funny) apply, including
> 
> ### Fetching the desired output from an external source
> 
> 
> This includes doing an HTTP request to fetch the page with the
> question and extracting a solution from that page. This was mildly
> amusing back in 2011, but now is derivative and uninteresting.
> 
> 
> 
Meaning you cannot simply redirect the request to another server to have it return the response.
Your server can do anything (or nothing) while no HTTP request is made to it as long as it replies with the correct response once a HTTP request is made.
  
[Answer]
# GNU Awk: 69 characters
A server itself (endlessly serves one request at a time), no library used.
Send 418 to everybody who connects (~~82~~ 69 characters):
```
BEGIN{while(s="/inet/tcp/80/0/0"){print"HTTP/1.1 418\n"|&s
close(s)}}
```
Send 418 to everybody who sends something (~~93~~ 80 characters):
```
BEGIN{while(s="/inet/tcp/80/0/0"){s|&getline
print"HTTP/1.1 418\n"|&s
close(s)}}
```
Send 418 to everybody who sends a valid HTTP GET request (~~122~~ 109 characters):
```
BEGIN{while(s="/inet/tcp/80/0/0"){s|&getline
if(/^GET \S+ HTTP\/1\.[01]$/)print"HTTP/1.1 418\n"|&s
close(s)}}
```
[Answer]
## Bash (33)
```
nc -lp80 -q1<<<"HTTP/1.1 418
";$0
```
[Answer]
## PHP - 85 bytes
```
<?for($s=socket_create_listen(80);socket_write(socket_accept($s),"HTTP/1.1 418
"););
```
Saved with Windows-Style (CR-LF) line endings, requires `php_sockets` enabled.
I actually used this as my error code for the [Hard Code Golf: Create a Chatroom](https://codegolf.stackexchange.com/q/12221/) challenge, but no one noticed.
---
**Browser-Friendly version**
```
<?for(socket_getsockname($s=socket_create_listen(80),$n);$t="I'm a teapot";socket_write($c=socket_accept($s),"HTTP/1.0 418 $t
Content-Length: $l
<title>418 $t</title><h1>$t</h1>The requested resource is not suitable for brewing coffee.<hr><i>$n:80</i>"))$l=124+strlen($n);
```
Start the script in the CLI, and point your browser at `http://localhost`.
[Answer]
# Node.js (LiveScript)
## `http` module - 66
```
require(\http)createServer (->&1.writeHead 418;&1.end!) .listen 80
```
Inspired by [Qwertiy's answer](https://codegolf.stackexchange.com/a/41663/344).
## `net` module - 76
```
require(\net)createServer (->it.write 'HTTP/1.1 418\r\n';it.end!) .listen 80
```
[Answer]
# Ruby + Rack, 19 bytes
```
run->e{[418,{},[]]}
```
Must be saved as `config.ru`, and run with the `rackup` command.
Or if you prefer "pure" Ruby:
```
Rack::Handler::WEBrick.run->e{[418,{},[]]}
```
42 bytes + `-rrack` flag = **48 bytes**
[Answer]
# Bash+BSD general commands, 29
Borrowing back a little bit from other answers:
```
nc -lp80<<<"HTTP/1.1 418
";$0
```
Works for me with `wget`.
### First answer to use `nc`, 38
```
for((;;)){
nc -l 80 <<<HTTP/1.1\ 418
}
```
I'm assuming root privileges - run as follows:
```
sudo bash ./418.sh
```
[Answer]
# Ruby (nc system command) - 35
```
loop{`nc -l 80 <<<"HTTP/1.1 418"`}
```
DigitalTrauma should get the credit for the idea of using `nc`, however Ruby can make an infinite loop with fewer characters than Bash :)
# Ruby (TCPServer) - 75
```
require'socket'
s=TCPServer.new 80
loop{(s.accept<<'HTTP/1.1 418
').close}
```
That newline is intentional -- the actual newline character is one character shorter than "\n".
# Ruby (WEBrick HTTPServer) - 87
```
require'webrick'
(s=WEBrick::HTTPServer.new).mount_proc(?/){|_,r|r.status=418}
s.start
```
[Answer]
## Node.js, 72
```
require('http').createServer((q,s)=>s.writeHead(418)+s.end()).listen(80)
```
Response:
```
HTTP/1.1 418 I'm a Teapot
Date: Thu, 01 Apr 2021 16:12:59 GMT
Connection: keep-alive
Transfer-Encoding: chunked
0
```
---
## Node.js, 80 with ES5 syntax only
```
require('http').createServer(function(q,s){s.writeHead(418);s.end()}).listen(80)
```
The response is
```
HTTP/1.1 418 I'm a teapot
Date: Wed, 19 Nov 2014 21:08:27 GMT
Connection: keep-alive
Transfer-Encoding: chunked
0
```
[Answer]
# Python 3 106
```
s=__import__('socket').socket(2,1) 
s.bind(('',80))
s.listen(9)
while 1:s.accept()[0].send('HTTP/1.1 418\n')
```
[Answer]
# Haskell - 142 bytes
```
import Network
import System.IO
main=listenOn(PortNumber 8888)>>=f
f s=do{(h,_,_)<-accept s;hPutStr h"HTTP/1.1 418\r\n";hFlush h;hClose h;f s}
```
[Answer]
# R, 80 characters
Never did socket programming with R before, but I'll give it a try:
```
repeat{s=socketConnection(,80,T,open="r+");cat("HTTP/1.1 418\n",file=s);close(s)}
```
Here `socketConnection` opens a socket: first argument should be the host, the default being `localhost` we can skip it here; the second argument is the port which has no default, then argument `server` when specified `TRUE` creates the socket, if `FALSE` it just connects to an existing one. `T` is, by default, equals to `TRUE`, in R.
**Edit:** As suggested in a suggested edit by @AlexBrown, this could be shorten into **69 characters**:
```
repeat cat("HTTP/1.1 418\n",file=s<-socketConnection(,80,T))+close(s)
```
[Answer]
## Shell + socat, 60
```
socat tcp-l:80,fork exec:'printf HTTP/1.1\\ 418\\ T\r\n\r\n'
```
[Answer]
# Tcl (>= 8.5), 78 bytes
Edit - added in an extra newline (total of 2 newlines) for the sake of compliance.
```
socket -server {apply {{c - -} {puts $c "HTTP/1.1 418
";close $c}}} 80
vwait f
```
[Answer]
# Powershell, 398
```
$Listener = New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse("10.10.10.10"), 80)
$Listener.Start()
while($true)
{
    $RemoteClient = $Listener.AcceptTcpClient()
    $Stream = $RemoteClient.GetStream()
    $Writer = New-Object System.IO.StreamWriter $Stream
    $Writer.Write("HTTP/1.1 418 I'm a Teapot`nConnection: Close`n`n")
    $Writer.Flush()
    $RemoteClient.Close()
}
```
# 258
```
$l=New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Parse("10.10.10.10"),80);$l.Start();while($true){$r = $l.AcceptTcpClient();$s = $r.GetStream();$w = New-Object System.IO.StreamWriter $s;$w.Write("HTTP/1.1 418`n`n");$w.Flush();$r.Close()}
```
[Answer]
# [Julia 0.6](http://julialang.org/), ~~86~~ 73 bytes
```
s=listen(80)
while 1<2
c=accept(s)
write(c,"HTTP/1.1 418
")
close(c)
end
```
[Answer]
# Node.js [**koa**](https://github.com/koajs/koa), 61 Bytes
```
require('koa')().use(function*(){this.status=418}).listen(80)
```
Response:
```
HTTP/1.1 418 I'm a teapot
X-Powered-By: koa
Content-Type: text/plain; charset=utf-8
Content-Length: 12
Date: Thu, 20 Nov 2014 07:20:36 GMT
Connection: close
I'm a teapot
```
Requires node v0.11.12 +
Run as:
```
node --harmony app.js
```
[Answer]
# MATLAB, ~~97~~ 86 bytes
Not really a serious contender in terms of absolute byte-count, but I'd like to post it because I didn't think it would be possible to write a fully functional webserver using a mathematical tool. Note the use of [property shortening](https://codegolf.stackexchange.com/a/48268/32352): `'Ne','s'` internally expands to `'NetworkRole', 'server'`.
```
t=tcpip('0.0.0.0','Ne','s');while 1
fopen(t)
fprintf(t,'HTTP/1.1 418\n')
fclose(t)
end
```
[Answer]
## Perl, 78
```
use Web::Simple;sub dispatch_request{sub{[418,[],[]]}}__PACKAGE__->to_psgi_app
```
run as `plackup whatever.pl`.
[Answer]
# Go, 162 bytes
```
package main
import "net/http"
func main(){http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){w.WriteHeader(418)})
http.ListenAndServe(":80", nil)
}
```
[Answer]
# Python 2.7/Django, 94 bytes
(added from default boilerplate from `django-admin.py startproject`)
In urls.py:
```
import django.http.HttpResponse as r;urlpatterns=patterns(url(r'^*$',lambda q:r(status=418)))
```
[Answer]
## node.js with CoffeeScript (76)
```
require("connect")().use((q,s,n)->s.writeHead 418;s.end();return;).listen 80
```
Just compile it to JavaScript, then you need to run `npm install connect`. After that start it with `node server.js`
[Answer]
# Factor, ~~101~~ 141 bytes
```
[ utf8 <threaded-server> readln 10 base> >>insecure [ "HTTP/1.1 418\r" print flush ] >>handler [ start-server ] in-thread start-server drop ]
```
Return 418 to everyone who connects.
[Answer]
# C# + OWIN ~~251~~ 240
I was really hoping it'd be shorter, but the long namespaces ruined that plan. Requires the `Microsoft.Owin.SelfHost` package available on NuGet.
```
using Owin;class P{static void Main(){Microsoft.Owin.Hosting.WebApp.Start<P>("http://localhost");while(0<1);}public void Configuration(IAppBuilder a){a.Run(c=>{c.Response.StatusCode=418;return System.Threading.Tasks.Task.FromResult(0);});}}
```
[Answer]
## node.js with connect (78)
```
require('connect')().use(function(q,s,n){s.writeHead(418);s.end()}).listen(80)
```
You need to run `npm install connect` first. Then start it with `node server.js`
[Answer]
You can do this with minimal effort using a `.htaccess` file and php.
All the accesses to your server will return the status 418.
Below is the code:
# .htaccess (28 bytes)
```
RewriteRule .* index.php [L]
```
# PHP (~~38~~ 19 bytes)
```
<<?header(TE,1,418);
```
---
Thanks to [@primo](https://codegolf.stackexchange.com/users/4098/primo) for saving me a bunch of bytes!
---
I have tested this and confirm it returns the desired result!
![https://i.stack.imgur.com/wLb9p.png](https://i.stack.imgur.com/wLb9p.png)
By the way, "Pedido" means "Request" and "Resposta" means "Answer".
[Answer]
# Java 7, 208 bytes
```
import java.net.*;class R{public static void main(String[]a)throws Exception{for(ServerSocket s=new ServerSocket(80);;){Socket p=s.accept();p.getOutputStream().write("HTTP/1.0 418\n".getBytes());p.close();}}}
```
This question needed a java answer.
```
poke@server ~
$ curl -i localhost:80
HTTP/1.0 418
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E) + [`ncat`](https://nmap.org/ncat), ~~75~~ ~~74~~ ~~69~~ ~~62~~ ~~44~~ 42 bytes
*-12 thanks to [ovs](https://codegolf.stackexchange.com/users/64121/ovs)*.
```
'‚«™’ÿ.¬•"n—Ä -k¯à80<<<'HTTP/1.1 418
'"’.E
```
Your ISP must be AT&T for this to work, as it abuses AT&T's automatic conversion of `\n` to `\r\n` in HTTP requests.
```
'‚«™’...’.E  # trimmed program
         .E  # evaluate...
    ’...’    # "ÿ.shell\"ncat -klp80<<<'HTTP/1.1 418\n'\""...
             # (implicit) with ÿ replaced by...
'‚«          # "system"...
   ™         # in title case...
         .E  # as Elixir code
====================
System.shell"..."  # trimmed program
System.shell       # execute shell command...
            "..."  # literal
====================
ncat -klp80<<<'...'  # trimmed program
ncat                 # host web server...
     - l             # that listens to requests...
     -k              # forever...
     -  p            # on port...
         80          # literal...
ncat                 # that returns response input...
           <<<       # with...
              '...'  # literal...
           <<<       # as input
```
Side note: I do not know Elixir at all so if there are ways to improve the Elixir code please let me know.
# [05AB1E (legacy)](https://github.com/Adriandmen/05AB1E/tree/fb4a2ce2bce6660e1a680a74dd61b72c945e6c3b) + [`ncat`](https://nmap.org/ncat), ~~64~~ ~~54~~ 52 bytes
```
’__£Ø__('ï´').‚«("n—Ä -k¯à80<<<'HTTP/1.1 418\n'")’.E
```
---
```
’...’.E  # trimmed program
     .E  # evaluate...
’...’    # "__import__('os').system(\"ncat -klp80<<<'HTTP/1.1 418\n'\")"...
     .E  # as Python code
====================
__import__('os').system("...")  # trimmed program
__import__('os').system(     )  # execute shell command...
                        "..."   # literal
====================
ncat -klp80<<<'...'  # trimmed program
ncat                 # host web server...
     - l             # that listens to requests...
     -k              # forever...
     -  p            # on port...
         80          # literal...
ncat                 # that returns response input...
           <<<       # with...
              '...'  # literal...
           <<<       # as input
```
[Answer]
# nginx - 35
```
events{}http{server{return 418;}}
```
Throw that in nginx.conf, start nginx.
*Not sure if this uses standard loopholes "Using built-in functions to do the work" or "Interpreting the challenge too literally." [Oops, looks like OP won't like this answer.](https://codegolf.stackexchange.com/questions/41638/418-im-a-teapot#comment97015_41638)*
[Answer]
# [Python 3](https://docs.python.org/3/) + Flask, 64 80 76 75 bytes
```
from flask import*
a=Flask("");a.route("/")(lambda:abort(418));a.run("",80)
```
I don't know much Flask, so I'm not entirely sure if this works. Please let me know in the comments if there's some problem with this. Ungolfed version:
```
from flask import Flask, abort
app = Flask(__name__)
@app.route("/")
def index():
    abort(418)
app.run("127.0.0.1", 80)
```
Simply binds the path "/" to a function that aborts with error code 418.
*Thanks to [Makonede](https://codegolf.stackexchange.com/users/94066/makonede) for pointing out that I didn't start the server and giving a modification to make the solution 80 76 75 bytes. The 80 was from using `app` instead of `a`. Oops! The 76 was from `(a:=Flask(""))` instead of `a=Flask("");a`.*
[Answer]
# [Python 3](https://docs.python.org) + [`ncat`](https://nmap.org/ncat), ~~55~~ 53 bytes
```
import os;os.system("ncat -klp80<<<'HTTP/1.1 418\n'")
```
Your ISP must be AT&T for this to work.
```
import os;os.system("...")  # trimmed program
import                      # import module named...
       os                   # variable
          os.system(     )  # run shell command...
                    "..."   # literal
====================
ncat -klp80<<<'...'  # trimmed program
ncat                 # host web server...
     - l             # that listens to requests...
     -k              # forever...
     -  p            # on port...
         80          # literal...
ncat                 # that returns response input...
           <<<       # with...
              '...'  # literal...
           <<<       # as input
```
]