triflix commited on
Commit
2695b69
·
verified ·
1 Parent(s): 3ac3058

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -10
app.py CHANGED
@@ -1,21 +1,37 @@
1
- import requests
2
  import time
 
3
  from stem import Signal
4
  from stem.control import Controller
 
5
 
 
 
 
6
  def renew_tor_ip():
7
  with Controller.from_port(port=9051) as controller:
8
  controller.authenticate()
9
  controller.signal(Signal.NEWNYM)
10
- time.sleep(5)
11
 
12
  def request_via_tor(url):
13
- proxies = {"http": "socks5h://127.0.0.1:9050", "https": "socks5h://127.0.0.1:9050"}
14
- response = requests.get(url, proxies=proxies)
15
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- if __name__ == "__main__":
18
- for i in range(5):
19
- renew_tor_ip()
20
- r = request_via_tor("https://httpbin.org/ip")
21
- print(r.json())
 
 
1
  import time
2
+ import requests
3
  from stem import Signal
4
  from stem.control import Controller
5
+ import gradio as gr
6
 
7
+ # -------------------------------
8
+ # Tor IP rotation functions
9
+ # -------------------------------
10
  def renew_tor_ip():
11
  with Controller.from_port(port=9051) as controller:
12
  controller.authenticate()
13
  controller.signal(Signal.NEWNYM)
14
+ time.sleep(5) # Give Tor time to switch
15
 
16
  def request_via_tor(url):
17
+ proxies = {"http": "socks5h://127.0.0.1:9050",
18
+ "https": "socks5h://127.0.0.1:9050"}
19
+ response = requests.get(url, proxies=proxies, timeout=30)
20
+ return response.json()
21
+
22
+ # -------------------------------
23
+ # Gradio interface
24
+ # -------------------------------
25
+ def get_ip_via_tor(dummy_input):
26
+ renew_tor_ip() # Rotate IP
27
+ result = request_via_tor("https://httpbin.org/ip")
28
+ return str(result)
29
+
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("### Tor IP Rotation Demo")
32
+ input_text = gr.Textbox(label="Click Run to get new IP", value="Hit Run")
33
+ output_text = gr.Textbox(label="Current IP")
34
+ run_button = gr.Button("Run")
35
+ run_button.click(get_ip_via_tor, inputs=input_text, outputs=output_text)
36
 
37
+ demo.launch(server_name="0.0.0.0", server_port=7860)