Python 3.x SimpleHTTPRequestHandler未输出任何响应



我正试图在64位Windows 7上运行的python 3.x中制作一个简单的http服务器。我在Mac上的python 2.7上运行得很好,并做了一些更改以升级到Windows上运行的python 3.2。

当被查询时,标头都被正确返回,但浏览器检查器中没有显示任何内容——但是telnet显示响应已被完全接收!

为什么它没有显示在浏览器中?救命!

代码为:

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

我修复了它-它需要添加"Access Control Allow Origin"标头:

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

--成功了!

相关内容

  • 没有找到相关文章

最新更新