Question

Im trying to make a simple http server in python 3.x running on Windows 7 64-bit. I had this working fine on python 2.7 on my Mac, and made a few changes to upgrade to python 3.2 running on Windows.

When queried, the headers are all returned correctly, but there is no content showing in browser inspector - however telnet reveals that the response is being fully received!

Why does it not show up in the browser? HELP!

Code is:

import sys

import http.server
from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import usb.core

class MyHandler(SimpleHTTPRequestHandler):

    def __init__(self,req,client_addr,server):
        SimpleHTTPRequestHandler.__init__(self,req,client_addr,server)      

    def do_GET(self):
        r="<h1>Hello World</h1>"
        self.send_response(200)
        self.send_header("Content-type", "application/json;charset=utf-8")

        self.send_header("Content-length", len(r))
        self.end_headers()
        self.wfile.write(r.encode("utf-8"))
        self.wfile.flush()
        print(r)

HandlerClass = MyHandler
Protocol     = "HTTP/1.1"
port = 80

server_address = ('localhost', port)

HandlerClass.protocol_version = Protocol

try:
    httpd = HTTPServer(server_address, MyHandler)
    print ("Server Started")
    httpd.serve_forever()
except KeyboardInterrupt:
    print('Shutting down server')
    httpd.socket.close()
Was it helpful?

Solution

I fixed it - it needs "Access-Control-Allow-Origin" header adding:

self.send_header("Access-Control-Allow-Origin","*")

-- did the trick!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top