def handle(self): # handle query data, connection = self.request packet = DNSRecord.parse(data) packet_id = packet.header.id content_id = str(packet.q.qname).strip('.') rtype = packet.q.qtype value = Redis("127.0.0.1", 6379).get(content_id) # if redis can't find the queried key, then the value sets 'None' # create response payload if value: # value = [obj, RR, Record Value, TTL] ans = value.decode() ttl = '60' # ans, ttl = value[-2], value[-1] response = DNSRecord( DNSHeader(qr=1, aa=1, ra=1, id=packet_id, rcode=RCODE["NoError"])) response.add_question(DNSQuestion(content_id, rtype)) print(response) response.add_answer(*RR.fromZone("{} {} IN A {}".format(content_id, ttl, ans))) else: response = DNSRecord( DNSHeader(qr=1, aa=1, ra=1, id=packet_id, rcode=RCODE["NXDomain"])) response.add_question(DNSQuestion(content_id, rtype)) payload = response.pack() connection.sendto(payload, self.client_address)
def timestamp_query(url, target_date): """queries for nearest timestamp to target date from archive.org""" parsed_url = urlparse(url) # check timestamp cache ts_cached = Redis().get( f"{parsed_url.netloc}{parsed_url.path}:{target_date}") if ts_cached: return ts_cached.decode('utf-8') # cache miss, ask archive.org base_url = 'https://web.archive.org/wayback/available?url=' req_url = f"{base_url}{parsed_url.netloc}{parsed_url.path}×tamp={target_date}" try: req = urlopen(req_url) except (HTTPError, URLError): return 'NONE!' result = req.read() match = json.loads(result) try: timestamp = match['archived_snapshots']['closest']['timestamp'] except KeyError: print('no snapshot available', match) timestamp = 'NONE!' # populate cache Redis().mset( {f"{parsed_url.netloc}{parsed_url.path}:{target_date}": timestamp}) return timestamp
def stock(symbol: str) -> str: try: if init_error: return jsonify(error=init_error), 500 symbol = symbol.upper() closing_price = Redis(host=GlobalConfig.redis_server, port=GlobalConfig.redis_port).get(symbol) if not closing_price: print( "Api=GetStock Action=UsingAlphaVantage Symbol={0}".format(symbol)) data, meta_data = ts.get_intraday(symbol) last_refresh = meta_data['3. Last Refreshed'] last_data = data[last_refresh] closing_price = last_data['4. close'] Redis(host=GlobalConfig.redis_server, port=GlobalConfig.redis_port).set( symbol, closing_price, ex=GlobalConfig.cache_duration_in_minutes * 60) else: closing_price = closing_price.decode("utf-8") print( "Api=GetStock Action=UsingRedisCache Symbol={0}".format(symbol)) return jsonify( symbol=symbol, closing_price=closing_price ) except Exception as e: print(e) return jsonify(error=str(e)), 500
def proxy_date(addr): """query redis for client's proxy date or set it to default""" date = Redis().get(f"wayback_date:{addr}") if not date: date = DEFAULT_DATE Redis().mset({f"wayback_date:{addr}": date}) else: date = date.decode('utf-8') return date
def proxy(): http = Redis().rpop('proxies') if http: return 'http://'+http.decode('utf-8') return http