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 |
페이닥터 등록
![]() | WHRIA | 2014.10.21 | 173 |
273 | usb 3.1 + DP | WHRIA | 2020.01.07 | 172 |
272 | concat network [3] | WHRIA | 2020.08.27 | 170 |
271 | resnest50 caffe | WHRIA | 2020.06.21 | 168 |
270 | pytorch pretrained | WHRIA | 2020.10.28 | 168 |
» | CORS | WHRIA | 2021.01.22 | 167 |
268 | pytorch optimize | WHRIA | 2020.08.01 | 164 |
267 | 역시 개원해서는 돈 버는 재미밖에 없다. | WHRIA | 2016.06.17 | 163 |
266 | pROC package [3] | WHRIA | 2019.05.11 | 161 |
265 | Transformer | WHRIA | 2020.10.09 | 160 |
264 | yolo custom | WHRIA | 2019.07.25 | 159 |
263 | fast rcnn cudnn 5.0 | WHRIA | 2017.01.08 | 158 |
262 | SAS controller SAS 9212-4i | WHRIA | 2019.12.08 | 158 |
261 | file lock | WHRIA | 2020.09.22 | 157 |
260 | startup , 미국 | WHRIA | 2020.07.30 | 155 |