1

Using Python's SimpleHTTPServer, how can I make it so that the server only responds to requests that come from the local machine?

Is there a language-agnostic way to do this? Maybe by using a specific port that is not open to the public, or by telling the firewall not to allow access to that port from outside?

This question is similar to this one, but specific to a server written with Python's SimpleHTTPServer.

Florian Dietz
  • 877
  • 9
  • 20

1 Answers1

2

You could use the loopback interface. localhost or 127.0.0.1 refers to the local address, bypasses the full networking stack, and is not accessible via the network. See wikipedia

Take the SimpleHTTPServer example.

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

We can bind to a specific address, you could use the address assigned to your machine/interface, or in your case, the loopback device.

httpd = SocketServer.TCPServer(("127.0.0.1", PORT), Handler)

You can then access the server at http://127.0.0.1:8000/ or http://localhost:8000/. The server will not be accessible using your machines assigned IP address, both from your local machine and via the network.

The following may provide additional information

Community
  • 1
  • 1