main.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. import requests
  3. from requests.exceptions import HTTPError
  4. from http.server import BaseHTTPRequestHandler, HTTPServer
  5. import time
  6. class MyServer(BaseHTTPRequestHandler):
  7. def do_GET(self):
  8. try:
  9. response = requests.get('https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd&include_market_cap=false&include_24hr_vol=true&include_24hr_change=true&include_last_updated_at=true&precision=2')
  10. response.raise_for_status()
  11. # access JSOn content
  12. jsonResponse = response.json()
  13. print("Entire JSON response")
  14. print(jsonResponse)
  15. print(jsonResponse["ethereum"]["usd"])
  16. except HTTPError as http_err:
  17. print('HTTP error occurred:', http_err)
  18. except Exception as err:
  19. print('Other error occurred:', err)
  20. self.send_response(200)
  21. self.send_header("Content-type", "text/html")
  22. self.end_headers()
  23. self.wfile.write(bytes("<html><head><title>Price Check</title></head>", "utf-8"))
  24. self.wfile.write(bytes("<h1 style='font-size:10em;' >ETH = $ %s" % jsonResponse["ethereum"]["usd"], "utf-8"))
  25. self.wfile.write(bytes("</body></html>", "utf-8"))
  26. webServer = HTTPServer(("127.0.0.1", 8080), MyServer)
  27. print("Server started ")
  28. try:
  29. webServer.serve_forever()
  30. except KeyboardInterrupt:
  31. pass
  32. webServer.server_close()
  33. print("Server stopped.")