File size: 883 Bytes
71f7514
 
 
73e407b
71f7514
14f899b
 
715d16c
14f899b
 
 
 
 
2ff48a3
14f899b
 
 
2ff48a3
71f7514
14f899b
bd56722
b76b5f8
71f7514
c49c5a9
71f7514
 
14f899b
71f7514
 
14f899b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import gradio as gr
import socket

def predict(text, request: gr.Request):
    client_ip = request.client.host
    local_ip = socket.gethostbyname(socket.gethostbyname(""))

    headers = request.headers
    real_client_ip = ""
    if 'x-forwarded-for' in headers:
        x_forwarded_for = headers['x-forwarded-for']
        real_client_ip = x_forwarded_for.split(
            ' ')[0] if x_forwarded_for else ""

    return text, {"real_client_ip": real_client_ip,
                  "client_ip": client_ip,
                  "local_ip": local_ip,
                  "headers": headers}


with gr.Blocks() as block:
    gr.Markdown("## Gradio get client IP")
    text = gr.Textbox(label="dummy input")
    output = gr.JSON({})
    btn = gr.Button("Test")

    btn.click(predict, inputs=[text], outputs=[text, output])


block.launch(share=False, server_name='0.0.0.0', show_api=True)