CORS
2021.01.23 00: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
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
1714 | MDR - PL | WHRIA | 2023.05.12 | 104 |
1713 | 언어 , 국가 | WHRIA | 2023.04.15 | 231 |
1712 | cloudfront | WHRIA | 2023.04.09 | 78 |
1711 | 원격 진료 | WHRIA | 2023.03.23 | 106 |
1710 | AI papers [6] | WHRIA | 2023.03.02 | 110 |
1709 | gpt2 | WHRIA | 2023.02.21 | 415 |
1708 | rsynccp | WHRIA | 2023.02.20 | 100 |
1707 | disk error | WHRIA | 2023.02.16 | 82 |
1706 | 정량화 논문들 | WHRIA | 2023.02.16 | 107 |
1705 | chat GPT | WHRIA | 2023.02.16 | 107 |
1704 | Google vs Microsoft: The good, bad, and ugly of the AI arms race | WHRIA | 2023.02.14 | 464 |
1703 | insync | WHRIA | 2023.02.09 | 83 |
1702 | indexeddb [2] | WHRIA | 2021.02.07 | 171 |
» | CORS | WHRIA | 2021.01.23 | 194 |
1700 | popup | WHRIA | 2020.12.31 | 154 |