Python HTTPServer响应curl但不响应Postman GET请求

问题描述 投票:2回答:1

考虑使用模块BaseHTTPRequestHandler的Python3中的简单服务器。

import json
import urllib.parse
from http.server import BaseHTTPRequestHandler, HTTPServer
import bson.json_util

class GetHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        print("/n=================================")
        json_string = '{"hello":"world"}'
        self.wfile.write(json_string.encode())
        self.send_response(200)
        self.end_headers()
        return

if __name__ == '__main__':
    #from BaseHTTPServer import HTTPServer
    server = HTTPServer(('localhost', 3030), GetHandler)
    print ('Starting server, use <Ctrl-C> to stop')
    server.serve_forever()

这与来自终端的curl正确响应:

curl -i http://localhost:3030/

但是,当尝试从Postman发送请求时,它没有响应。我尝试了URL localhost:3030/http://localhost:3030/以及环回地址。

这是为什么?

python-3.x get postman httpserver
1个回答
1
投票

在所有的例子中,我看到它没有指定内容类型,所以我做了同样的方式,并看到curl工作,我不太担心。

但是应该指定内容类型:在qazxswpo解决问题之前添加这些行:

self.wfile.write(...)

请注意,实际上self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() 已被移动,而不是添加。

© www.soinside.com 2019 - 2024. All rights reserved.