2

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()
4

1 回答 1

1

我修复了它 - 它需要“Access-Control-Allow-Origin”标题添加:

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

——成功了!

于 2013-07-03T08:03:04.083 回答