4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-04-13
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

4
0
0

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
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?