Created
January 30, 2026 13:43
-
-
Save pilhuhn/1f3e853ae025e63a2858ac060c9454ab to your computer and use it in GitHub Desktop.
Code of a simple python server. Talk to with a OTEL-enabled client or curl -i http://localhost:8787/payment\?tea=sencha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import argparse | |
| from http.server import BaseHTTPRequestHandler, HTTPServer | |
| import logging | |
| import psycopg2 | |
| import random | |
| import requests | |
| import re | |
| import sys | |
| import time | |
| def get_payment(tea:str) -> bool: | |
| cursor = connection.cursor() | |
| cursor.execute("SELECT * FROM tea WHERE kind=(%s);", (tea, ) ) | |
| # Fetch all rows from database | |
| record = cursor.fetchall() | |
| # print("Data from Database:- ", record) | |
| price = record[0][1] | |
| return price <=5 | |
| class MyRequestHandler(BaseHTTPRequestHandler): | |
| """HttpServer request handler for illustration purposes""" | |
| def do_GET(self): | |
| if random.randint(0, 10) < 2: | |
| self.send_error(500, 'Just a random failure') | |
| return | |
| # simulate a slowness every one and then | |
| if random.randint(0, 10) > 5: | |
| time.sleep(1.5) | |
| # We get a request like 'GET /payment?tea=sencha HTTP/1.1', let's parse it down | |
| req = self.requestline | |
| if not '?' in req: | |
| self.send_response(400) | |
| self.end_headers() | |
| # This must come after sending the headers | |
| return | |
| self.send_response(200) | |
| self.send_header("Content-Type", "text/plain") | |
| self.end_headers() | |
| req = req.split('?')[1] | |
| req = req.split(' ')[0] | |
| req = req.split('=')[1] | |
| the_tea = req.replace('+', ' ') | |
| is_paid = get_payment(the_tea) | |
| # Write http response | |
| self.wfile.write(bytes(str(is_paid).lower(), "utf-8")) | |
| if __name__ == "__main__": | |
| logging.basicConfig(level=logging.INFO) | |
| random.seed(time.time_ns()) | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('-pg_host', default='localhost') | |
| parser.add_argument('-otel_host', default='localhost') | |
| args = parser.parse_args() | |
| logging.info("pg.host: %s, otel.host: %s, ", | |
| args.pg_host, args.otel_host ) | |
| connection = psycopg2.connect(database="replicator", | |
| user="demo", | |
| password="lala7", | |
| host=args.pg_host, | |
| port=5432) | |
| server = HTTPServer(('', 8787), MyRequestHandler) | |
| print("Server started on port 8787") | |
| try: | |
| server.serve_forever() | |
| except KeyboardInterrupt: | |
| pass | |
| server.server_close() | |
| print("Server stopped.") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Indent on line 25 is wrong