File size: 1,435 Bytes
077aa2e
bd628f6
077aa2e
5b625be
 
 
 
 
 
bd628f6
5b625be
 
077aa2e
5b625be
 
bd628f6
5b625be
bd628f6
 
 
 
 
c2ae28c
 
 
 
bd628f6
5b625be
bd628f6
 
5b625be
 
 
bd628f6
 
 
5b625be
 
bd628f6
5b625be
bd628f6
 
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
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
import socket

resp_message = {0: 'Opened',
                10061: 'Opened but nothing',
                10060: 'Closed'}

def check(ip: str, port: int, timeout: int) :
    print(ip, port, timeout)
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s :
        s.settimeout(timeout)
        resp = s.connect_ex((ip, port))

    if resp_message.get(resp, False) :
        return [resp, resp_message[resp]]
    else :
        return [resp, "Unknown"]

with gr.Blocks() as demo :
    gr.Markdown("""
                # Port Checker
                ### Output will be returned one of the 4 states.
                ① Opened (0)  
                ② Opened but nothing (10061)  
                ③ Closed (10060)  
                ④ Unknown (others)  
                """)
    
    with gr.Row() :
        with gr.Column() :
            input_ip = gr.Textbox(label='ip', value="127.0.0.1")
            input_port = gr.Number(label='port', value=80, precision=0)
            input_timeout = gr.Number(label='timeout', value=10, precision=0)
            btn = gr.Button(value="Check", variant='primary')
        
        with gr.Column() :
            output_errno = gr.Textbox(label='errno', interactive=False)
            output_state = gr.Textbox(label='state', interactive=False)

    btn.click(fn=check, inputs=[input_ip, input_port, input_timeout], outputs=[output_errno, output_state])

demo.launch()