Update app.py
Browse files
app.py
CHANGED
@@ -3,23 +3,28 @@ import socket
|
|
3 |
|
4 |
def predict(text, request: gr.Request):
|
5 |
client_ip = request.client.host
|
6 |
-
local_ip = socket.gethostbyname(socket.
|
|
|
7 |
headers = request.headers
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
return text, {"real_client_ip":real_client_ip,
|
12 |
-
"client_ip": client_ip,
|
13 |
-
"local_ip": local_ip,
|
14 |
"headers": headers}
|
15 |
|
|
|
16 |
with gr.Blocks() as block:
|
17 |
gr.Markdown("## Gradio get client IP")
|
18 |
text = gr.Textbox(label="dummy input")
|
19 |
output = gr.JSON({})
|
20 |
btn = gr.Button("Test")
|
21 |
|
22 |
-
btn.click(predict, inputs=[text],outputs=[text, output])
|
23 |
|
24 |
|
25 |
-
block.launch(share=False, server_name='0.0.0.0',show_api=True)
|
|
|
3 |
|
4 |
def predict(text, request: gr.Request):
|
5 |
client_ip = request.client.host
|
6 |
+
local_ip = socket.gethostbyname(socket.gethostbyname(""))
|
7 |
+
|
8 |
headers = request.headers
|
9 |
+
real_client_ip = ""
|
10 |
+
if 'x-forwarded-for' in headers:
|
11 |
+
x_forwarded_for = headers['x-forwarded-for']
|
12 |
+
real_client_ip = x_forwarded_for.split(
|
13 |
+
' ')[0] if x_forwarded_for else ""
|
14 |
|
15 |
+
return text, {"real_client_ip": real_client_ip,
|
16 |
+
"client_ip": client_ip,
|
17 |
+
"local_ip": local_ip,
|
18 |
"headers": headers}
|
19 |
|
20 |
+
|
21 |
with gr.Blocks() as block:
|
22 |
gr.Markdown("## Gradio get client IP")
|
23 |
text = gr.Textbox(label="dummy input")
|
24 |
output = gr.JSON({})
|
25 |
btn = gr.Button("Test")
|
26 |
|
27 |
+
btn.click(predict, inputs=[text], outputs=[text, output])
|
28 |
|
29 |
|
30 |
+
block.launch(share=False, server_name='0.0.0.0', show_api=True)
|