Skip to content

HowTo | Misc

ML-TANGO edited this page Oct 14, 2022 · 1 revision

In this wiki page, we archive code snippets for simple but common implementaion questions


How to get Host IP address or DNS name in the container code

Front end, Javascript

function DataMain()
{
    const [server_ip, setServer_ip] = useState();

    /* 페이지 로드 완료시 호출 이벤트 */
    useEffect( () =>
    {
        var host = window.location.hostname
        setServer_ip('http://' + host + ':8086')

    }, []);

Back end, Python

*[example code in project_manager/tango/viewsProject.py](https://github.com/ML-TANGO/TANGO/blob/teslasystem/project_manager/tango/viewsProject.py#L36

def get_server_ip(request):

    try:
        # weda_port_num = 8090
        # return Response({'port': weda_port_num})

        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        print(s.getsockname()[0])

        host = s.getsockname()[0]

        return Response({'host': host})

    except Exception as e:
        print(e)