search
LoginSignup
0

posted at

updated at

python3 -m http.serverをCORSにする【Access-Control-Allow-Origin】

echo "import sys
from http.server import HTTPServer, SimpleHTTPRequestHandler, test

class CORSRequestHandler (SimpleHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        SimpleHTTPRequestHandler.end_headers(self)

if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)" | python3 -- - 8000

or

echo -e "import sys\nfrom http.server import HTTPServer, SimpleHTTPRequestHandler, test\nclass CORSRequestHandler (SimpleHTTPRequestHandler):\n def end_headers (self):\n  self.send_header('Access-Control-Allow-Origin', '*')\n  SimpleHTTPRequestHandler.end_headers(self)\nif __name__ == '__main__':\n test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)" | python3 -- - 8000

毎回、検索してファイル作成して動かすのめんどくさいのでワンライナーにしてみました

const data = await fetch('http://localhost:8000/1.json').then(r=>r.json())

参考

Access to fetch at 'http://localhost:8000/1.json' from origin 'https://qiita.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET http://localhost:8000/1.json net::ERR_FAILED 200
Uncaught (in promise) TypeError: Failed to fetch
at :1:1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0