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
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
1549 | ambient-light-sensor | WHRIA | 2018.09.29 | 280 |
1548 | register | WHRIA | 2018.09.21 | 1930 |
1547 | Deep learning 기반 DEMO | WHRIA | 2018.09.16 | 7121 |
1546 | slide note 삭제 슬라이드 노트 삭제 | WHRIA | 2018.09.12 | 4433 |
1545 | faster rcnn resnet 101 | WHRIA | 2018.09.09 | 175 |
1544 | 나이 40에... | WHRIA | 2018.09.01 | 142 |
1543 | letencrypt - win | WHRIA | 2018.08.12 | 47 |
1542 |
달러 환율
![]() | WHRIA | 2018.06.16 | 1297 |
1541 | Interpretation of the Outputs of Deep Learning Model Trained with Skin Cancer Dataset [1] | WHRIA | 2018.06.02 | 7709 |
1540 | skin cancer [1] | WHRIA | 2018.05.23 | 1469 |
1539 | deep learning framework | WHRIA | 2018.05.17 | 1789 |
1538 | mxnet [2] | WHRIA | 2018.05.17 | 3213 |
1537 | intel 내장 그래픽 / CUDA | WHRIA | 2018.05.12 | 3156 |
1536 | 당좌대월이자율 | WHRIA | 2018.03.06 | 2601 |
1535 | security 0 | WHRIA | 2018.03.02 | 2468 |