如何监听特定端口并返回200状态

Nov*_*ser 6 networking

如何在我的服务器上侦听特定端口并在有人尝试连接时仅返回 HTTP 200 状态?

可以nc用来返回 HTTP 状态吗?

小智 2

python -m SimpleHTTPServer 8000 将在端口 8000(默认端口;低于 1024 的端口将要求您以 root(例如)权限运行)上将当前目录作为网页提供服务sudo。如果从该端口获取,将返回“200 OK”:

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

$ wget localhost:8000
--2014-03-12 00:42:30--  http://localhost:8000/
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8000... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:8000... connected.
HTTP request sent, awaiting response... 127.0.0.1 - - [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>
<body>
<h2>Directory listing for /</h2>
<hr>
<ul>
</ul>
<hr>
</body>
</html>
$
Run Code Online (Sandbox Code Playgroud)