| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- #!/usr/bin/env python3
- import requests
- from requests.exceptions import HTTPError
- from http.server import BaseHTTPRequestHandler, HTTPServer
- import time
- class MyServer(BaseHTTPRequestHandler):
- def do_GET(self):
- try:
- 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')
- response.raise_for_status()
- # access JSOn content
- jsonResponse = response.json()
- print("Entire JSON response")
- print(jsonResponse)
- print(jsonResponse["ethereum"]["usd"])
- except HTTPError as http_err:
- print('HTTP error occurred:', http_err)
- except Exception as err:
- print('Other error occurred:', err)
- self.send_response(200)
- self.send_header("Content-type", "text/html")
- self.end_headers()
- self.wfile.write(bytes("<html><head><title>Price Check</title></head>", "utf-8"))
- self.wfile.write(bytes("<h1 style='font-size:10em;' >ETH = $ %s" % jsonResponse["ethereum"]["usd"], "utf-8"))
- self.wfile.write(bytes("</body></html>", "utf-8"))
-
- webServer = HTTPServer(("127.0.0.1", 8080), MyServer)
- print("Server started ")
- try:
- webServer.serve_forever()
- except KeyboardInterrupt:
- pass
- webServer.server_close()
- print("Server stopped.")
|