Can I set a header with python's SimpleHTTPServer?

后端 未结 4 509
别跟我提以往
别跟我提以往 2020-12-01 08:44

I\'m using SimpleHTTPServer to test some webpages I\'m working on. It works great, however I need to do some cross-domain requests. That requires setting a

4条回答
  •  隐瞒了意图╮
    2020-12-01 09:26

    While this is an older answer, its the first result in google...

    Basically what @iMon0 suggested..Seems correct?..Example of doPOST

    def do_POST(self):
        self.send_response()
        self.send_header('Content-type','application/json')
        self.send_header('Access-Control-Allow-Origin','*')
        self.end_headers()
        sTest = {}
        sTest['dummyitem'] = "Just an example of JSON"
        self.wfile.write(json.dumps(sTest))
    

    By doing this, the flow feels correct..

    1: You get a request

    2: You apply the headers and response type you want

    3: You post back the data you want, be this what or how ever you want.,

    The above example is working fine for me and can be extended further, its just a bare bone JSON post server. So i'll leave this here on SOF incase someone needs it or i myself come back in a few months for it.

    This does produce a valid JSON file with only the sTest object, Same as a PHP generated page/file.

提交回复
热议问题