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
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
274 | slide note 삭제 슬라이드 노트 삭제 | WHRIA | 2018.09.12 | 4435 |
273 | Deep learning 기반 DEMO | WHRIA | 2018.09.16 | 7121 |
272 | register | WHRIA | 2018.09.21 | 1931 |
271 | ambient-light-sensor | WHRIA | 2018.09.29 | 280 |
270 | Intro | WHRIA | 2018.10.01 | 4081 |
269 | faster rcnn cuda 10 | WHRIA | 2018.10.06 | 2107 |
268 | delayed network drive connect | WHRIA | 2018.10.07 | 152 |
267 | mixed precision training [1] | WHRIA | 2018.11.06 | 1780 |
266 | excel 에서 소수점 표시 함수 | WHRIA | 2018.11.08 | 200 |
265 | ac-68u dd-wrt 에서 정펌으로 복구 | WHRIA | 2018.11.22 | 547 |
264 | fatal error: turbojpeg.h: No such file or directory | WHRIA | 2018.11.24 | 645 |
263 | CNN Models | WHRIA | 2018.12.23 | 353 |
262 | imbalanced dataset | WHRIA | 2018.12.26 | 44 |
261 | center % crop | WHRIA | 2018.12.27 | 70 |
260 | Swap file add | WHRIA | 2019.01.02 | 2132 |