CORS
2021.01.22 23:02
https://royportas.com/posts/2019-03-02-cors-python/
Full Code
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, HTTPServer
from json import dumps
""" The HTTP request handler """
class RequestHandler(BaseHTTPRequestHandler):
def _send_cors_headers(self):
""" Sets headers required for CORS """
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
self.send_header("Access-Control-Allow-Headers", "x-api-key,Content-Type")
def send_dict_response(self, d):
""" Sends a dictionary (JSON) back to the client """
self.wfile.write(bytes(dumps(d), "utf8"))
def do_OPTIONS(self):
self.send_response(200)
self._send_cors_headers()
self.end_headers()
def do_GET(self):
self.send_response(200)
self._send_cors_headers()
self.end_headers()
response = {}
response["status"] = "OK"
self.send_dict_response(response)
def do_POST(self):
self.send_response(200)
self._send_cors_headers()
self.send_header("Content-Type", "application/json")
self.end_headers()
dataLength = int(self.headers["Content-Length"])
data = self.rfile.read(dataLength)
print(data)
response = {}
response["status"] = "OK"
self.send_dict_response(response)
print("Starting server")
httpd = HTTPServer(("127.0.0.1", 8000), RequestHandler)
print("Hosting server on port 8000")
httpd.serve_forever()
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
124 | test | WHRIA | 2020.12.03 | 64 |
123 | sroc [1] | WHRIA | 2020.12.10 | 84 |
122 | 32 bit UEFI [6] | WHRIA | 2020.12.24 | 237 |
121 | bay trail ubuntu 20.04 [1] | WHRIA | 2020.12.24 | 68 |
120 | popup | WHRIA | 2020.12.31 | 125 |
» | CORS | WHRIA | 2021.01.22 | 167 |
118 | indexeddb [2] | WHRIA | 2021.02.07 | 140 |
117 | insync | WHRIA | 2023.02.09 | 61 |
116 | Google vs Microsoft: The good, bad, and ugly of the AI arms race | WHRIA | 2023.02.14 | 189 |
115 | chat GPT | WHRIA | 2023.02.16 | 74 |
114 | 정량화 논문들 | WHRIA | 2023.02.16 | 71 |
113 | disk error | WHRIA | 2023.02.16 | 64 |
112 | rsynccp | WHRIA | 2023.02.20 | 75 |
111 | gpt2 | WHRIA | 2023.02.21 | 399 |
110 | AI papers [6] | WHRIA | 2023.03.02 | 84 |