

终止支持通知：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 [packages/123456789012-DEBUG\$1SERVER-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 [packages/123456789012-DEBUG\$1SERVER-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 请求，以检索有关应用程序的某些信息。它还接受对 `/restart` 的 POST 请求以重新启动应用程序。

为了演示此功能，示例应用程序在单独的线程上运行 HTTP 客户端。客户端在启动后不久通过本地网络调用 `/status` 路径，并在几分钟后重新启动应用程序。

**Example [packages/123456789012-DEBUG\$1SERVER-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 [packages/123456789012-DEBUG\$1SERVER-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)