DepthAI v2 has been superseded by DepthAI v3. You are viewing legacy documentation.
DepthAI Tutorials
DepthAI API References

ON THIS PAGE

  • Demo
  • Setup
  • Source code
  • Pipeline

Script HTTP client

This example will send an HTTP request to the http://api.ipify.org and print your public IP.

Demo

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'

Setup

Please run the install script to download all required dependencies. Please note that this script must be ran from git context, so you have to download the depthai-python repository first and then run the script
Command Line
1git clone https://github.com/luxonis/depthai-python.git
2cd depthai-python/examples
3python3 install_requirements.py
For additional information, please follow the installation guide.

Source code

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

Need assistance?

Head over to Discussion Forum for technical support or any other questions you might have.