sigyllly commited on
Commit
c5ddf8d
·
verified ·
1 Parent(s): ad07929

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -56
app.py CHANGED
@@ -1,61 +1,45 @@
1
- from flask import Flask, request, send_from_directory,abort
2
- import os
3
 
4
  app = Flask(__name__)
5
 
6
- UPLOAD_FOLDER = 'uploads'
7
- if not os.path.exists(UPLOAD_FOLDER):
8
- os.makedirs(UPLOAD_FOLDER)
9
-
10
- app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
11
-
 
 
12
 
13
- @app.route('/', methods=['GET', 'POST'])
14
  def home():
15
- abort(500, "Server Error")
16
-
17
-
18
- @app.route('/upload', methods=['GET', 'POST'])
19
- def upload_file():
20
- if request.method == 'POST':
21
- if 'file' not in request.files:
22
- return 'No file part'
23
-
24
- file = request.files['file']
25
-
26
- if file.filename == '':
27
- return 'No selected file'
28
-
29
- if file:
30
- filename = file.filename
31
- file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
32
- return 'File uploaded successfully'
33
-
34
- return '''
35
- <!doctype html>
36
- <title>Upload new File</title>
37
- <h1>Upload new File</h1>
38
- <form method=post enctype=multipart/form-data>
39
- <input type=file name=file>
40
- <input type=submit value=Upload>
41
- </form>
42
- '''
43
-
44
- @app.route('/files')
45
- def list_files():
46
- files = os.listdir(app.config['UPLOAD_FOLDER'])
47
- return '''
48
- <!doctype html>
49
- <title>Uploaded files</title>
50
- <h1>Uploaded files</h1>
51
- <ul>
52
- ''' + ''.join(['<li><a href="/download/{}">{}</a></li>'.format(f, f) for f in files]) + '''
53
- </ul>
54
- '''
55
-
56
- @app.route('/download/<filename>')
57
- def download_file(filename):
58
- return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
59
-
60
- if __name__ == '__main__':
61
- app.run(host='0.0.0.0', port=7860, debug=True)
 
1
+ from flask import Flask, request, Response
2
+ import requests
3
 
4
  app = Flask(__name__)
5
 
6
+ # HTML form template to allow user input
7
+ html_form = '''
8
+ <form action="/proxy" method="get">
9
+ <label for="url">Enter URL:</label>
10
+ <input type="text" id="url" name="url" placeholder="https://example.com" required>
11
+ <button type="submit">Browse</button>
12
+ </form>
13
+ '''
14
 
15
+ @app.route('/')
16
  def home():
17
+ # Display the form for URL input
18
+ return html_form
19
+
20
+ @app.route('/proxy')
21
+ def proxy():
22
+ # Get the URL from the user's input
23
+ url = request.args.get('url')
24
+ if not url:
25
+ return "No URL provided", 400
26
+
27
+ # Validate URL (must start with http:// or https:// for security)
28
+ if not (url.startswith("http://") or url.startswith("https://")):
29
+ return "Invalid URL. Please include http:// or https://", 400
30
+
31
+ try:
32
+ # Fetch the content from the specified URL
33
+ response = requests.get(url)
34
+ response.raise_for_status() # Ensure the request was successful
35
+
36
+ # Return the fetched content as a response to the user
37
+ return Response(response.content, content_type=response.headers['Content-Type'])
38
+
39
+ except requests.RequestException as e:
40
+ # Handle errors in fetching the page
41
+ return f"Error fetching the page: {e}"
42
+
43
+ if __name__ == "__main__":
44
+ # Run the Flask app on port 7860
45
+ app.run(host="0.0.0.0", port=7860)