Sometimes we need just a microserver to simulate or do some tests using our APIS so python can be used to create this micro httpserver in a simple way! 🙂
Case 1
We just need to serve some html files so, go the path that you need and just:
python -m http.server
Case 2
We need to perform something else like CORS, CONTENT TYPE…
#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, test
import sys
class CORSRequestHandler (SimpleHTTPRequestHandler):
def end_headers (self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header("Content-type", "application/json")
SimpleHTTPRequestHandler.end_headers(self)
if __name__ == '__main__':
test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)