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 | sql replication 성공 [1] | WHRIA | 2020.02.20 | 50 |
123 | android / capture and crop [5] | WHRIA | 2020.05.10 | 50 |
122 | melanoma awareness [1] | WHRIA | 2019.12.27 | 49 |
121 | lvm [2] | WHRIA | 2020.01.09 | 49 |
120 | sql 중복제거 | WHRIA | 2020.02.10 | 49 |
119 | XE 에 tinymce 에디터를 달다. | WHRIA | 2015.11.22 | 48 |
118 | exif remove | WHRIA | 2020.01.28 | 48 |
117 | letencrypt - win | WHRIA | 2018.08.12 | 47 |
116 | kaggle leakage | WHRIA | 2019.12.19 | 47 |
115 | kernel load | WHRIA | 2020.04.03 | 47 |
114 | onnx broswer | WHRIA | 2020.09.15 | 46 |
113 | 회귀분석 종류 | WHRIA | 2020.04.14 | 45 |
112 | imbalanced dataset | WHRIA | 2018.12.26 | 44 |
111 | calendar javascript dropdown | WHRIA | 2020.03.01 | 41 |
110 |
USB 에뮬레이션 램디스트 imdisk
![]() | WHRIA | 2016.09.02 | 39 |