本文介绍了Python的BaseHTTPServer返回垃圾响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python的BaseHTTPServer并实现以下非常简单的BaseHTTPRequestHandler:

I use Python's BaseHTTPServer and implement the following very simple BaseHTTPRequestHandler:

class WorkerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.wfile.write('{"status" : "ready"}')
        self.send_response(200)

当我从Web浏览器运行GET查询时,只需转到localhost:port,我将得到以下响应:

When I run a GET query from the web browser, by simply going to localhost:port, I get the following response:

{"status" : "ready"}HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.12
Date: Mon, 09 Jan 2017 12:45:13 GMT

我只想要JSON.如何使服务器不发送这些垃圾数据?

I only want the JSON. How can I make the server not sending this junky data?

HTTP/1.0 200 OK
Server: BaseHTTP/0.3 Python/2.7.12
Date: Mon, 09 Jan 2017 12:45:13 GMT

推荐答案

最后,我自己成功修复了该问题.与您分享:

Finally succeeded fixing it myself. Sharing with you:

class WorkerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write('{"status" : "ready"}')

交换send_responsewfile.write.在send_response

这篇关于Python的BaseHTTPServer返回垃圾响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43