考虑一个使用Python3的简单服务器,该服务器使用BaseHTTPRequestHandler
模块。
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/
以及使用环回地址。
为什么会这样呢?