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

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

tech