【发布时间】:2017-04-15 23:36:30
【问题描述】:
使用 Python 的 SimpleHTTPServer,我怎样才能使服务器只响应来自本地机器的请求?
是否有与语言无关的方法来做到这一点?可能是通过使用不向公众开放的特定端口,或者通过告诉防火墙不允许从外部访问该端口?
这个问题类似于this one,但特定于使用 Python 的 SimpleHTTPServer 编写的服务器。
【问题讨论】:
标签: python security localhost port
使用 Python 的 SimpleHTTPServer,我怎样才能使服务器只响应来自本地机器的请求?
是否有与语言无关的方法来做到这一点?可能是通过使用不向公众开放的特定端口,或者通过告诉防火墙不允许从外部访问该端口?
这个问题类似于this one,但特定于使用 Python 的 SimpleHTTPServer 编写的服务器。
【问题讨论】:
标签: python security localhost port
您可以使用环回接口。 localhost 或 127.0.0.1 指的是本地地址,绕过完整的网络堆栈,并且无法通过网络访问。见wikipedia
以 SimpleHTTPServer example.
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
我们可以绑定到特定地址,您可以使用分配给您的机器/接口的地址,或者在您的情况下,使用环回设备。
httpd = SocketServer.TCPServer(("127.0.0.1", PORT), Handler)
然后您可以通过http://127.0.0.1:8000/ 或http://localhost:8000/ 访问服务器。使用您的计算机分配的 IP 地址将无法从您的本地计算机和通过网络访问服务器。
以下内容可能会提供更多信息
【讨论】: