James Arems
James Arems

Reputation: 87

How to get server ip (web access ip) as a variable in python flask

I have a Python flask server, its serving me on port 8080. Server can access through NAT . Real IP of server is 192.168.200.10 and after NAT this will be 192.168.100.10.

I have a VNC server running on the same server, i fetched server IP by using ,

def sysip():
    sys_cmd = "ip  -f inet a show enp0s31f6| grep inet| awk '{ print $2}' | cut -d/ -f1"
    sysip = os.popen(sys_cmd).read()        
    return sysip

Web part,

<a class="btn btn-primary btn-sm" href="http://{{sysip}}:6080/vnc.html" target="_blank" role="button">console </a>

But this method is actually not making any sense, because of using NAT.

Is there any way to append whatever 'IP' user accessing to web server, as vnc ip ?

Eg: http://192.168.100.10:8080 -- Main server after using NAT

Now for VNC I am getting http://192.168.200.10:6080/vnc.html (Because of above script)

Instead i need http://192.168.100.10:6080/vnc.html

Is there any method in Flask or javascript ?

Upvotes: 1

Views: 3785

Answers (1)

Selcuk
Selcuk

Reputation: 59425

You can use the Host request header:

from flask import request
hostname = request.headers.get('Host')

This should give you the hostname that the browser used to perform the request, such as 192.168.200.10, or www.example.com.

If you want to strip the port (8080), you can use something like

hostname = request.headers.get('Host').split(':')[0]

Upvotes: 2

Related Questions