AEUPH commited on
Commit
ab31bb5
·
verified ·
1 Parent(s): 3b7b2a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -2,27 +2,23 @@ import streamlit as st
2
  import requests
3
  from urllib.parse import urlencode, quote_plus, urlparse
4
 
5
- def is_valid_url(url):
6
- """
7
- Checks if the URL has a valid scheme.
8
- """
9
- parsed_url = urlparse(url)
10
- return parsed_url.scheme in ['http', 'https']
11
-
12
  def forward_parameters(target_url, extra_params):
13
  """
14
  Forward parameters to the target URL and return the response.
15
-
16
  :param target_url: The URL to which the parameters should be forwarded.
17
  :param extra_params: A dictionary of additional parameters to forward.
18
  :return: The response from the target URL.
19
  """
20
- if not is_valid_url(target_url):
21
- return "Invalid URL: No scheme supplied. URLs must start with http:// or https://"
22
-
23
  try:
 
 
 
 
 
24
  # Construct the full URL with extra parameters
25
- full_url = f"{target_url}?{urlencode(extra_params, quote_via=quote_plus)}"
 
26
  response = requests.get(full_url) # Using GET request for simplicity
27
  return response.text
28
  except requests.exceptions.RequestException as e:
@@ -35,14 +31,16 @@ st.title('HTTPS Streamlit Parameters Forwarder')
35
  # Correctly accessing the query parameters
36
  params = st.query_params
37
 
38
- target_url = params.get('param1', [None])[0]
 
 
 
39
 
40
  if target_url:
41
  # Remove 'param1' to forward the remaining parameters
42
- if 'param1' in params:
43
- del params['param1']
44
- response = forward_parameters(target_url, params)
45
- st.text_area("Response from Target URL", value=response, height=300)
46
  else:
47
  st.error("No target URL provided.")
48
  st.markdown("""
 
2
  import requests
3
  from urllib.parse import urlencode, quote_plus, urlparse
4
 
 
 
 
 
 
 
 
5
  def forward_parameters(target_url, extra_params):
6
  """
7
  Forward parameters to the target URL and return the response.
8
+
9
  :param target_url: The URL to which the parameters should be forwarded.
10
  :param extra_params: A dictionary of additional parameters to forward.
11
  :return: The response from the target URL.
12
  """
 
 
 
13
  try:
14
+ # Validate the target URL
15
+ parsed_url = urlparse(target_url)
16
+ if not parsed_url.scheme in ['http', 'https']:
17
+ return "Invalid URL: URLs must start with http:// or https://"
18
+
19
  # Construct the full URL with extra parameters
20
+ encoded_params = urlencode(extra_params, quote_via=quote_plus)
21
+ full_url = f"{target_url}?{encoded_params}" if encoded_params else target_url
22
  response = requests.get(full_url) # Using GET request for simplicity
23
  return response.text
24
  except requests.exceptions.RequestException as e:
 
31
  # Correctly accessing the query parameters
32
  params = st.query_params
33
 
34
+ # Debug: Log received parameters
35
+ st.write("Received parameters:", params)
36
+
37
+ target_url = params.get('param1', [None])[0] # Extract the target URL
38
 
39
  if target_url:
40
  # Remove 'param1' to forward the remaining parameters
41
+ forwarded_params = {k: v for k, v in params.items() if k != 'param1'}
42
+ response = forward_parameters(target_url, forwarded_params)
43
+ st.text_area("Response from Target URL", value=response, height=300, help="The response from the forwarded URL.")
 
44
  else:
45
  st.error("No target URL provided.")
46
  st.markdown("""