-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
59 lines (47 loc) · 1.64 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from dotenv import load_dotenv
import requests
from http.server import HTTPServer, BaseHTTPRequestHandler
import psycopg
from bs4 import BeautifulSoup
load_dotenv() # set environment variables from .env
database_name = os.getenv("DB_NAME")
database_user = os.getenv("DB_USER")
database_password = os.getenv("DB_PASSWORD")
database_host = os.getenv("DB_HOST")
target_url = os.getenv("TARGET_URL") # no slash at the end
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(b"Flemme ok")
def run_server(port):
server_address = ("", port)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print(f"Server running on port {port}")
httpd.serve_forever()
run_server(5001)
# Connect to an existing database
with psycopg.connect(
f"host={database_host} port=5432 dbname={database_name} user={database_user} password={database_password}"
) as conn:
# Open a cursor to perform database operations
with conn.cursor() as cur:
cur.execute("select * from information_schema.tables where table_name=%s", ("things",))
val = cur.fetchone()
if val == False:
cur.execute(
"""
CREATE TABLE things (
id serial PRIMARY KEY,
name text,
power int
data text)
"""
)
cur.execute("SELECT * FROM things")
cur.fetchone()
for record in cur:
print(record)
conn.commit()