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
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
1714 | MDR - PL | WHRIA | 2023.05.11 | 73 |
1713 | 언어 , 국가 | WHRIA | 2023.04.15 | 139 |
1712 | cloudfront | WHRIA | 2023.04.09 | 59 |
1711 | 원격 진료 | WHRIA | 2023.03.23 | 88 |
1710 | AI papers [6] | WHRIA | 2023.03.02 | 82 |
1709 | gpt2 | WHRIA | 2023.02.21 | 398 |
1708 | rsynccp | WHRIA | 2023.02.20 | 75 |
1707 | disk error | WHRIA | 2023.02.16 | 64 |
1706 | 정량화 논문들 | WHRIA | 2023.02.16 | 71 |
1705 | chat GPT | WHRIA | 2023.02.16 | 73 |
1704 | Google vs Microsoft: The good, bad, and ugly of the AI arms race | WHRIA | 2023.02.14 | 189 |
1703 | insync | WHRIA | 2023.02.09 | 60 |
1702 | indexeddb [2] | WHRIA | 2021.02.07 | 140 |
» | CORS | WHRIA | 2021.01.22 | 167 |
1700 | popup | WHRIA | 2020.12.31 | 124 |