Reading JSON from SimpleHTTPServer Post data

Thanks matthewatabet for the klein idea. I figured a way to implement it using BaseHTTPHandler. The code below. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer import SocketServer import simplejson import random class S(BaseHTTPRequestHandler): def _set_headers(self): self.send_response(200) self.send_header(‘Content-type’, ‘text/html’) self.end_headers() def do_GET(self): self._set_headers() f = open(“index.html”, “r”) self.wfile.write(f.read()) def do_HEAD(self): self._set_headers() def do_POST(self): self._set_headers() print “in post method” … Read more

Is it possible to run python SimpleHTTPServer on localhost only?

In Python versions 3.4 and higher, the http.server module accepts a bind parameter. According to the docs: python -m http.server 8000 By default, server binds itself to all interfaces. The option -b/–bind specifies a specific address to which it should bind. For example, the following command causes the server to bind to localhost only: python … Read more

Can I set a header with python’s SimpleHTTPServer?

This is a bit of a hack because it changes end_headers() behavior, but I think it’s slightly better than copying and pasting the entire SimpleHTTPServer.py file. My approach overrides end_headers() in a subclass and in it calls send_my_headers() followed by calling the superclass’s end_headers(). It’s not 1 – 2 lines either, less than 20 though; … Read more

How to run a http server which serves a specific path?

In Python 3.7 SimpleHTTPRequestHandler can take a directory argument: import http.server import socketserver PORT = 8000 DIRECTORY = “web” class Handler(http.server.SimpleHTTPRequestHandler): def __init__(self, *args, **kwargs): super().__init__(*args, directory=DIRECTORY, **kwargs) with socketserver.TCPServer((“”, PORT), Handler) as httpd: print(“serving at port”, PORT) httpd.serve_forever() and from the command line: python -m http.server –directory web To get a little crazy… you … Read more

What is a faster alternative to Python’s http.server (or SimpleHTTPServer)?

http-server for node.js is very convenient, and is a lot faster than Python’s SimpleHTTPServer. This is primarily because it uses asynchronous IO for concurrent handling of requests, instead of serialising requests. Installation Install node.js if you haven’t already. Then use the node package manager (npm) to install the package, using the -g option to install … Read more