-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.py
More file actions
26 lines (20 loc) · 859 Bytes
/
hello.py
File metadata and controls
26 lines (20 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
import psycopg2
class HttpHandler(BaseHTTPRequestHandler):
def do_GET(self):
message = f'Grettings from {os.uname()[1]}.'
print(message)
dbname = os.environ['POSTGRES_DB']
dbuser = os.environ['POSTGRES_USER']
dbpass = os.environ['POSTGRES_PASSWORD']
conn_str = f"host='db' dbname='{dbname}' user='{dbuser}' password='{dbpass}'"
with psycopg2.connect(conn_str) as connection:
cursor = connection.cursor()
cursor.execute('insert into greetings (text) values (%s);', (message,))
self.send_response(200)
self.end_headers()
self.wfile.write(str.encode(message))
print('Serving hello server on 0.0.0.0:8000')
httpd = HTTPServer(('0.0.0.0', 8000), HttpHandler)
httpd.serve_forever()