

終止支援通知：2026 年 5 月 31 日， AWS 將終止對 的支援 AWS Panorama。2026 年 5 月 31 日之後，您將無法再存取 AWS Panorama 主控台或 AWS Panorama 資源。如需詳細資訊，請參閱[AWS Panorama 終止支援](https://docs.aws.amazon.com/panorama/latest/dev/panorama-end-of-support.html)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 提供傳入流量
<a name="applications-ports"></a>

您可以搭配應用程式程式碼執行 HTTP 伺服器，在本機監控或偵錯應用程式。若要提供外部流量，請將 AWS Panorama 設備上的連接埠映射至應用程式容器上的連接埠。

**重要**  
根據預設，AWS Panorama 設備不接受任何連接埠上的傳入流量。在設備上開啟連接埠會有隱含的安全風險。使用此功能時，您必須採取其他步驟[來保護您的設備免受外部流量](appliance-network.md)影響，並保護授權用戶端與設備之間的通訊。  
本指南中包含的範例程式碼僅供示範之用，不會實作身分驗證、授權或加密。

您可以在設備上開啟範圍介於 8000–9000 的連接埠。這些連接埠開啟時，可以接收來自任何可路由用戶端的流量。部署應用程式時，您可以指定要開啟的連接埠，並將設備上的連接埠映射到應用程式容器上的連接埠。設備軟體會將流量轉送至容器，並將回應傳回給請求者。請求會在您指定的設備連接埠上收到，而回應則會在隨機暫時性連接埠上傳出。

## 設定傳入連接埠
<a name="applications-ports-configuration"></a>

您可以在應用程式組態的三個位置指定連接埠映射。程式碼套件的 `package.json`，您可以指定程式碼節點在`network`區塊中接聽的連接埠。下列範例宣告節點接聽連接埠 80。

**Example [package/123456789012-DEBUG\_SERVER-1.0/package.json](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/packages/123456789012-DEBUG_SERVER-1.0/package.json)**  

```
                "outputs": [
                    {
                        "description": "Video stream output",
                        "name": "video_out",
                        "type": "media"
                    }
                ],
                "network": {
                    "inboundPorts": [
                        {
                            "port": 80,
                            "description": "http"
                        }
                    ]
                }
```

在應用程式資訊清單中，您會宣告路由規則，將設備上的連接埠映射到應用程式的程式碼容器上的連接埠。下列範例新增規則，將裝置上的連接埠 8080 映射至`code_node`容器上的連接埠 80。

**Example [graphs/my-app/graph.json](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/graphs/my-app/graph.json)**  

```
            {
                "producer": "model_input_width",
                "consumer": "code_node.model_input_width"
            },
            {
                "producer": "model_input_order",
                "consumer": "code_node.model_input_order"
            }
        ],
        "networkRoutingRules": [
            {
                "node": "code_node",
                "containerPort": 80,
                "hostPort": 8080,
                "decorator": {
                    "title": "Listener port 8080",
                    "description": "Container monitoring and debug."
                }
            }
        ]
```

部署應用程式時，您可以在 AWS Panorama 主控台中指定相同的規則，或將覆寫文件傳遞至 [CreateApplicationInstance](https://docs.aws.amazon.com/panorama/latest/api/API_CreateApplicationInstance.html) API。您必須在部署時間提供此組態，以確認您想要開啟設備上的連接埠。

**Example [graphs/my-app/override.json](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/graphs/my-app/override.json)**  

```
            {
                "replace": "camera_node",
                "with": [
                    {
                        "name": "exterior-north"
                    }
                ]
            }
        ],
        "networkRoutingRules":[
            {
                "node": "code_node",
                "containerPort": 80,
                "hostPort": 8080
            }
        ],
        "envelopeVersion": "2021-01-01"
    }
}
```

如果應用程式資訊清單中指定的裝置連接埠正由另一個應用程式使用，您可以使用覆寫文件來選擇不同的連接埠。

## 服務流量
<a name="applications-ports-serverthread"></a>

在容器上開啟連接埠時，您可以開啟通訊端或執行伺服器來處理傳入的請求。此`debug-server`範例顯示搭配電腦視覺應用程式程式碼執行的 HTTP 伺服器的基本實作。

**重要**  
範例實作對於生產使用並不安全。為了避免讓您的設備容易遭受攻擊，您必須在程式碼和網路組態中實作適當的安全控制。

**Example [package/123456789012-DEBUG\_SERVER-1.0/application.py](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/packages/123456789012-DEBUG_SERVER-1.0/application.py) – HTTP 伺服器**  

```
    # HTTP debug server
    def run_debugger(self):
        """Process debug commands from local network."""
        class ServerHandler(SimpleHTTPRequestHandler):
            # Store reference to application
            application = self
            # Get status
            def do_GET(self):
                """Process GET requests."""
                logger.info('Get request to {}'.format(self.path))
                if self.path == '/status':
                    self.send_200('OK')
                else:
                    self.send_error(400)
            # Restart application
            def do_POST(self):
                """Process POST requests."""
                logger.info('Post request to {}'.format(self.path))
                if self.path == '/restart':
                    self.send_200('OK')
                    ServerHandler.application.stop()
                else:
                    self.send_error(400)
            # Send response
            def send_200(self, msg):
                """Send 200 (success) response with message."""
                self.send_response(200)
                self.send_header('Content-Type', 'text/plain')
                self.end_headers()
                self.wfile.write(msg.encode('utf-8'))
        try:
            # Run HTTP server
            self.server = HTTPServer(("", self.CONTAINER_PORT), ServerHandler)
            self.server.serve_forever(1)
            # Server shut down by run_cv loop
            logger.info("EXITING SERVER THREAD")
        except:
            logger.exception('Exception on server thread.')
```

伺服器接受`/status`路徑上的 GET 請求，以擷取應用程式的一些資訊。它也接受 的 POST 請求`/restart`，以重新啟動應用程式。

為了示範此功能，範例應用程式會在單獨的執行緒上執行 HTTP 用戶端。用戶端會在啟動後立即透過本機網路呼叫`/status`路徑，並在幾分鐘後重新啟動應用程式。

**Example [package/123456789012-DEBUG\_SERVER-1.0/application.py](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/packages/123456789012-DEBUG_SERVER-1.0/application.py) – HTTP 用戶端**  

```
    # HTTP test client
    def run_client(self):
        """Send HTTP requests to device port to demnostrate debug server functions."""
        def client_get():
            """Get container status"""
            r = requests.get('http://{}:{}/status'.format(self.device_ip, self.DEVICE_PORT))
            logger.info('Response: {}'.format(r.text))
            return
        def client_post():
            """Restart application"""
            r = requests.post('http://{}:{}/restart'.format(self.device_ip, self.DEVICE_PORT))
            logger.info('Response: {}'.format(r.text))
            return
        # Call debug server
        while not self.terminate:
            try:
                time.sleep(30)
                client_get()
                time.sleep(300)
                client_post()
            except:
                logger.exception('Exception on client thread.')
        # stop signal received
        logger.info("EXITING CLIENT THREAD")
```

主迴圈會管理執行緒，並在結束時重新啟動應用程式。

**Example [package/123456789012-DEBUG\_SERVER-1.0/application.py](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/packages/123456789012-DEBUG_SERVER-1.0/application.py) – 主迴圈**  

```
def main():
    panorama = panoramasdk.node()
    while True:
        try:
            # Instantiate application
            logger.info('INITIALIZING APPLICATION')
            app = Application(panorama)
            # Create threads for stream processing, debugger, and client
            app.run_thread = threading.Thread(target=app.run_cv)
            app.server_thread = threading.Thread(target=app.run_debugger)
            app.client_thread = threading.Thread(target=app.run_client)
            # Start threads
            logger.info('RUNNING APPLICATION')
            app.run_thread.start()
            logger.info('RUNNING SERVER')
            app.server_thread.start()
            logger.info('RUNNING CLIENT')
            app.client_thread.start()
            # Wait for threads to exit
            app.run_thread.join()
            app.server_thread.join()
            app.client_thread.join()
            logger.info('RESTARTING APPLICATION')
        except:
            logger.exception('Exception during processing loop.')
```

若要部署範例應用程式，請參閱[本指南的 GitHub 儲存庫中的指示。](https://github.com/awsdocs/aws-panorama-developer-guide/blob/main/sample-apps/debug-server/README.md)