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 | headless PC 를 위한 dummy plug in 을 구입해서 달다. | WHRIA | 2015.11.22 | 30 |
1713 | 모든 연구 자료를 정리. 이것으로 마무리 짓기로. | WHRIA | 2024.03.03 | 30 |
1712 | Dutch - Nederlands | WHRIA | 2024.05.24 | 31 |
1711 | Filipino - Filipino | WHRIA | 2024.05.24 | 35 |
1710 |
USB 에뮬레이션 램디스트 imdisk
![]() | WHRIA | 2016.09.02 | 43 |
1709 | calendar javascript dropdown | WHRIA | 2020.03.01 | 44 |
1708 | imbalanced dataset | WHRIA | 2018.12.26 | 45 |
1707 | onnx broswer | WHRIA | 2020.09.15 | 47 |
1706 | XE 에 tinymce 에디터를 달다. | WHRIA | 2015.11.22 | 49 |
1705 | kaggle leakage | WHRIA | 2019.12.19 | 49 |
1704 | kernel load | WHRIA | 2020.04.03 | 49 |
1703 | lvm [2] | WHRIA | 2020.01.09 | 50 |
1702 | exif remove | WHRIA | 2020.01.28 | 50 |
1701 | sql 중복제거 | WHRIA | 2020.02.10 | 50 |
1700 | 회귀분석 종류 | WHRIA | 2020.04.14 | 50 |