我想扩展SimpleHTTPRequestHandler并覆盖do_GET()的默认行为。我从我的自定义处理程序返回一个字符串,但是客户端没有收到响应。
下面是我的处理程序类:
DUMMY_RESPONSE = """Content-type: text/html
<html>
<head>
<title>Python Test</title>
</head>
<body>
Test page...success.
</body>
</html>
"""
class MyHandler(CGIHTTPRequestHandler):
def __init__(self,req,client_addr,server):
CGIHTTPRequestHandler.__init__(self,req,client_addr,server)
def do_GET(self):
return DUMMY_RESPONSE我必须进行哪些更改才能使其正常工作?
发布于 2011-06-18 04:28:04
类似于(未测试的代码):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.send_header("Content-length", len(DUMMY_RESPONSE))
self.end_headers()
self.wfile.write(DUMMY_RESPONSE)发布于 2017-09-05 18:07:04
上面的答案是有效的,但是你可能会在下面这一行得到TypeError: a bytes-like object is required, not 'str':self.wfile.write(DUMMY_RESPONSE)。您需要这样做:self.wfile.write(str.encode(DUMMY_RESPONSE))
https://stackoverflow.com/questions/6391280
复制相似问题