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 | 길드 마크 | WHRIA | 2009.11.02 | 7961 |
123 | Medicalphoto 1.08 | WHRIA | 2012.04.04 | 7964 |
122 |
Illustration
![]() | WHRIA | 2007.08.15 | 7974 |
121 | MedicalPhoto 1.0.4 | WHRIA | 2008.05.12 | 7975 |
120 | ^^; 잘 봤음이야... | 민들레 | 2000.06.08 | 7988 |
119 | 싱가폴 다녀옴 | WHRIA | 2008.07.17 | 7989 |
118 | 기로 | WHRIA | 2008.01.24 | 8019 |
117 | MedicalPhoto 1.0.3 Final | WHRIA | 2008.05.04 | 8043 |
116 | OpenVPN 성공 | WHRIA | 2007.10.07 | 8054 |
115 | Valiant | WHRIA | 2008.01.03 | 8067 |
114 | adobe flash 안써도 웹서핑이 쾌적 | WHRIA | 2012.02.09 | 8088 |
113 | 타이 | WHRIA | 2007.11.06 | 8090 |
112 | chunje | chunjae | 2000.06.08 | 8102 |
111 | !!!!`홈페이지 문명의 새 장이 도래`!!!!! | 최은경 | 2000.06.08 | 8116 |
110 | IMF | WHRIA | 2008.10.21 | 8116 |