1

我在 MS Azure 上托管了一个非常简单的 Ubuntu VM。我在上面运行了这个简单的 python 程序:

import http.server
from prometheus_client import start_http_server
from prometheus_client import Counter

REQUESTS  = Counter('hello_worlds_total','Hello World requested')

class MyHandler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        REQUESTS.inc()
        self.send_response(200)
        self.end_headers()
        self.wfile.write("Hello world")

if __name__ == "__main__":
    start_http_server(8001)
    server = http.server.HTTPServer(('localhost',8000), MyHandler)
    server.serve_forever()
}}}

当我从我的电脑上点击 url http://VM_AZURE_IP:8001 时,它会回复 Promethues 输出。当我尝试 http://VM_AZURE_IP:8000 时,连接被拒绝。newtork 规则没问题,如果我切换start_http_server(8001)start_http_server(8000)http.server.HTTPServer(('localhost',8000), MyHandler)http.server.HTTPServer(('localhost',8001), MyHandler)我会得到 promethues 指标达到 http://VM_AZURE_IP:8000 并且在端口 8001 上连接被拒绝

4

1 回答 1

2

您可以像这样更改代码,并确保已在与 Ubuntu VM 关联的 NSG 的入站规则中添加端口 8001,8000。

import http.server
from prometheus_client import start_http_server
from prometheus_client import Counter

REQUESTS  = Counter('hello_worlds_total','Hello World requested')

class MyHandler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        REQUESTS.inc()
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"Hello world")   # add b

if __name__ == "__main__":
    start_http_server(8000)
    server = http.server.HTTPServer(('0.0.0.0',8001), MyHandler)  # chang to IP address 0.0.0.0
    print("server on!")
    server.serve_forever()

它对我有用。

在此处输入图像描述

于 2020-08-12T06:32:29.107 回答