DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
此页面由 AI 自动翻译。查看英文原版
DepthAI 教程
DepthAI API 参考

本页目录

  • 演示
  • 设置
  • 源代码
  • Pipeline

Script HTTP 客户端

此示例将向 http://api.ipify.org 发送一个 HTTP 请求,并打印您的公网 IP。

演示

Command Line
1~/depthai-python/examples/Script$ python3 script_http_client.py
2Sending http GET request...
3200 OK
4Public IP: b'123.123.123.123'

设置

请运行 安装脚本 以下载所有必需的依赖项。请注意,此脚本必须在 git 上下文中运行,因此您必须先下载 depthai-python 存储库,然后运行脚本
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
有关更多信息,请遵循 安装指南

源代码

Python

Python
GitHub
1#!/usr/bin/env python3
2import depthai as dai
3
4# Start defining a pipeline
5pipeline = dai.Pipeline()
6
7# Script node
8script = pipeline.create(dai.node.Script)
9script.setProcessor(dai.ProcessorType.LEON_CSS)
10script.setScript("""
11    import http.client
12    import time
13
14    node.warn('Sending http GET request...')
15    h1 = http.client.HTTPConnection('api.ipify.org', 80)
16    h1.request("GET", "/")
17    r1 = h1.getresponse()
18    node.warn(f'{r1.status} {r1.reason}')
19    data1 = r1.read()
20    node.warn(f'Public IP: {data1}')
21
22    node.io['end'].send(Buffer(32))
23""")
24
25xout = pipeline.create(dai.node.XLinkOut)
26xout.setStreamName('end')
27script.outputs['end'].link(xout.input)
28
29# Connect to device with pipeline
30with dai.Device(pipeline) as device:
31    device.getOutputQueue("end").get() # Wait for the "end" msg

C++

1#include <iostream>
2
3// Includes common necessary includes for development using depthai library
4#include "depthai/depthai.hpp"
5
6int main() {
7    using namespace std;
8
9    // Start defining a pipeline
10    dai::Pipeline pipeline;
11
12    // Script node
13    auto script = pipeline.create<dai::node::Script>();
14    script->setProcessor(dai::ProcessorType::LEON_CSS);
15    script->setScript(R"(
16    import http.client
17    import time
18
19    node.warn('Sending http GET request...')
20    h1 = http.client.HTTPConnection('api.ipify.org', 80)
21    h1.request("GET", "/")
22    r1 = h1.getresponse()
23    node.warn(f'{r1.status} {r1.reason}')
24    data1 = r1.read()
25    node.warn(f'Public IP: {data1}')
26
27    node.io['end'].send(Buffer(32))
28    )");
29
30    // XLinkOut
31    auto xout = pipeline.create<dai::node::XLinkOut>();
32    xout->setStreamName("end");
33    script->outputs["end"].link(xout->input);
34
35    // Connect to device with pipeline
36    dai::Device device(pipeline);
37    device.getOutputQueue("end")->get<dai::Buffer>();
38    return 0;
39}

Pipeline

需要帮助?

请前往 Discussion Forum 获取技术支持或提出您可能有的任何其他问题。