Ramesh-vani commited on
Commit
e5d281d
·
1 Parent(s): 7cb2b9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -52
app.py CHANGED
@@ -1,52 +1,12 @@
1
- from http.server import HTTPServer, BaseHTTPRequestHandler
2
- import json
3
- from socketserver import ThreadingMixIn
4
- class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
5
- pass
6
- class MyHandler(BaseHTTPRequestHandler):
7
- API_KEY = "123" # Replace with your actual API key
8
-
9
- def do_GET(self):
10
- # Extract the API key from the request headers
11
- api_key = self.headers.get('API-Key')
12
-
13
- if api_key == self.API_KEY:
14
- # API key is valid, process the request
15
- if self.path == '/api/data':
16
- response_data = {'message': 'Hello, this is your API response!'}
17
- self.send_response(200)
18
- self.send_header('Content-type', 'application/json')
19
- self.end_headers()
20
- self.wfile.write(bytes(json.dumps(response_data), 'utf8'))
21
- else:
22
- # Serve HTML file for other requests
23
- try:
24
- with open('templates/index.html', 'r') as f:
25
- content = f.read()
26
- self.send_response(200)
27
- self.send_header('Content-type', 'text/html')
28
- self.end_headers()
29
- self.wfile.write(bytes(content, 'utf8'))
30
- except FileNotFoundError:
31
- self.send_response(404)
32
- self.send_header('Content-type', 'text/html')
33
- self.end_headers()
34
- self.wfile.write(b'404: File not found')
35
- except Exception as e:
36
- self.send_response(500)
37
- self.send_header('Content-type', 'text/html')
38
- self.end_headers()
39
- self.wfile.write(f'500: Internal Server Error - {str(e)}'.encode('utf-8'))
40
- else:
41
- # Invalid API key
42
- self.send_response(401)
43
- self.send_header('Content-type', 'text/html')
44
- self.end_headers()
45
- self.wfile.write(b'401: Unauthorized - Invalid API Key')
46
-
47
- # Create an HTTP server and bind it to a specific host and port
48
- # Create a threaded HTTP server
49
- httpd = ThreadedHTTPServer(('0.0.0.0', 7860), MyHandler)
50
-
51
- # Start serving indefinitely
52
- httpd.serve_forever()
 
1
+ import subprocess
2
+ def run_command(command):
3
+ process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
4
+ output, error = process.communicate()
5
+ if process.returncode == 0:
6
+ result = output.decode('utf-8')
7
+ return result
8
+ else:
9
+ result = error.decode('utf-8')
10
+ return result
11
+ cmd="gunicorn -w 4 -b 0.0.0.0:7860 app:server"
12
+ run_command(cmd)