class _UserSessions(object): def __init__(self): self.cache = SimpleCache() def create(self, user_id): user = User.find(User.id == user_id) if user is None: return None sess = os.urandom(24) self.cache.set(sess, user_id) session['key'] = sess return sess def get(self): if 'key' not in session: return None key = session['key'] user_id = self.cache.get(key) user = User.find(User.id == user_id) session['user'] = user return user def delete(self): if 'key' in session: self.cache.delete(session['key']) session.pop('key', None) session.pop('user', None)
class HybridCache(object): """ A hybrid cache class that provide in-process cache that is backup up by out-of-process cache When setting a key it will set it in both in-process and out-of-process When getting a key it will try to retrieve first from in-process and if not, from out-of-process """ def __init__(self, remote_addresses): self.in_proc_cache = SimpleCache(1000, 3600) #todo - pass these are arguments self.remote_addresses = remote_addresses self.out_proc_cache = MemcachedCache(remote_addresses) def get(self, key): """ get an item from the hybrid cache first in the in-process and then in the out-of-process in case the key does not exist in both return None if the value exist in out of process but not in-process then it is added """ val = self.in_proc_cache.get(key) if val is None: val = MemcachedCache(self.remote_addresses).get(key) if val is not None: self.in_proc_cache.add(key, val, None) #todo: for how long to cache? return val def add(self, key, value, timeout = 300): """ store a key-value in the hybrid cache - both in and out-off process """ #self.out_proc_cache.add(key, value, timeout = timeout) MemcachedCache(self.remote_addresses).add(key, value, timeout) self.in_proc_cache.add(key, value, timeout = timeout)
class Metrics(object): def __init__(self): self._cache = SimpleCache() self._metrics = [] def build_keyspace(self, fields): """ Given: ["one", "two", "three"] Yield: "one", "one.two", "one.two.three" """ current = set() for field in fields: current.add(field) yield ".".join(current) def index(self): if not self._metrics: r = requests.get("%s/metrics/index.json" % graphite_url) self._metrics = json.loads(r.text) def search(self, term): cache_key = term rv = self._cache.get(cache_key) if not rv: rv = [metric for metric in self._metrics if term in metric] self._cache.set(cache_key, rv, timeout=86400) return rv
class PostalBot(TelegramBot): STR_USERNAME = '******' STR_MESSAGE_SENT = "Message sent" SPAM_TIMEOUT = 10*60 def __init__(self, tg_api_key, tg_lounge_id): super(PostalBot, self).__init__(tg_api_key, tg_lounge_id) self.cache = SimpleCache() def handle_stream_publish(self, data): keys = data.keys() if 'watch_url' in keys and 'username' in keys: c_key = self.STR_USERNAME + ':' + data['username'] if not self.cache.get(c_key): message = '{username} went live on Postal\n{url}'.format( username=data['username'], url=data['watch_url'] ) self.send_tg_message(self.tg_lounge_id, message) self.cache.set(c_key, True, timeout=self.SPAM_TIMEOUT) return self.STR_MESSAGE_SENT return '' def handle_postal_new_post(self, data): keys = data.keys() if 'username' in keys and 'title' in keys: message = "New post by {username}\n{title}".format( username=data['username'], title=data['title'] ) if 'image_url' in keys and 'image_size' in keys: message += "\n{url} {size}".format( url=data['image_url'], size=humanize.naturalsize(data['image_size'], gnu=True) ) if 'file_url' in keys and 'file_size' in keys: message += "\n{url} {size}".format( url=data['file_url'], size=humanize.naturalsize(data['file_size'], gnu=True) ) self.send_tg_message(self.tg_lounge_id, message) return self.STR_MESSAGE_SENT return '' def handle_tg_update(self, data): keys = data.keys() if 'message' in keys: self.handle_tg_message(data['message']) return '' def handle_tg_message(self, message): # print "%s %s: %s" % (message['from']['first_name'], message['from']['last_name'], message['text']) pass
def test_simplecache_get_dict(): """SimpleCache.get_dict bug""" cache = SimpleCache() cache.set('a', 'a') cache.set('b', 'b') d = cache.get_dict('a', 'b') assert 'a' in d assert 'a' == d['a'] assert 'b' in d assert 'b' == d['b']
def test_simplecache_get_dict(): """SimpleCache.get_dict bug""" cache = SimpleCache() cache.set("a", "a") cache.set("b", "b") d = cache.get_dict("a", "b") assert "a" in d assert "a" == d["a"] assert "b" in d assert "b" == d["b"]
class UtilityTestCase(unittest.TestCase): def setUp(self): self.c = SimpleCache() def test_werkzeug_cache_get_or_add_missing_key(self): self.assertEquals('bar', werkzeug_cache_get_or_add(self.c, 'foo', 'bar', 10)) def test_werkzeug_cache_get_or_add_existing_key(self): self.c.set('foo', 'bar') self.assertEquals('bar', werkzeug_cache_get_or_add(self.c, 'foo', 'qux', 10))
class ZopeTemplateLoader(jinja2.BaseLoader): def __init__(self, parent_loader, base_path, cache_templates=True, template_list=[]): self.parent_loader = parent_loader self.cache = SimpleCache() self.cache_templates = cache_templates self.path = base_path self.template_list = template_list def get_source(self, environment, template): def passthrough(cachable=True): up = self.parent_loader source, path, uptodate = up.get_source(environment, template) if not cachable: uptodate = lambda: False return source, path, uptodate if not template in self.template_list: return passthrough() path = "%s%s" % (self.path, template) source = self.cache.get(path) if not source: try: response = requests.get(path) except requests.exceptions.ConnectionError: return passthrough(cachable=False) if response.status_code != 200: return passthrough(cachable=False) # escape jinja tags source = response.text source = source.strip() source = source.replace("{%", "{{ '{%' }}").replace("%}", "{{ '%}' }}") source = source.replace("{{", "{{ '{{' }}").replace("}}", "{{ '}}' }}") # template blocks source = source.replace("<!-- block_content -->", "{% block natura2000_content %}{% endblock %}") source = source.replace("<!-- block_head -->", "{% block head %}{% endblock %}") # fix breadcrumb link source = source.replace('"%s"' % self.path, '"%s"' % flask.url_for('naturasites.index')) if self.cache_templates: self.cache.set(path, source) return source, path, lambda: False
def __init__(self): self.sc = SimpleCache() self.databaseService = database_service.DatabaseService() self.devicesControl = devices_control.DevicesControl() self.save_current_office_state(room_state.RoomState(0, self.format_current_time(), [], 0, 0, 0)) self.save_current_bedroom_state(room_state.RoomState(1, self.format_current_time(), [], 0, 0, 0)) print self.sc.get("current_office_state")
def __init__(self, parent_loader, base_path, cache_templates=True, template_list=[]): self.parent_loader = parent_loader self.cache = SimpleCache() self.cache_templates = cache_templates self.path = base_path self.template_list = template_list
def setUp(self): self.recached = False def dispatcher(salt): self.recached = True self.c = SimpleCache() cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher) self.s = Store(self.c, cfg) self.r = Retrieval(self.c, cfg)
class Word(object): def __init__(self): self.cache = SimpleCache(threshold=1000, default_timeout=60*60) def find_word(self, url): """ if any of words in unused, then select one. """ def generator(url): for l in [self.cache.get, self.find_unused_word, self.find_used_word]: yield l(url) for selected_word in generator(url): if bool(selected_word): self.cache.set(url, selected_word) return selected_word def find_url(self, word): if exists_used_word_in_db(word): return get_url_in_db(word) return None def find_unused_word(self, url): # find one from unused for word in split_words(url): if exists_unused_word_in_db(word): return select_unused_word_in_db(word, url) # one random last_word = choose_last_unused_word_in_db() return select_unused_word_in_db(last_word, url) def find_used_word(self, url): words = {} for word in split_words(url): if exists_used_word_in_db(word): words.setdefault(word, get_last_modified_in_db(word)) oldest_word = "" if bool(words): oldest_word = min(words) else: oldest_word = choose_last_modified_used_word_in_db() return select_used_word_in_db(oldest_word, url)
class ImageSimpleCache(ImageCache): """Simple image cache.""" def __init__(self): """Initialize the cache.""" super(ImageSimpleCache, self).__init__() self.cache = SimpleCache() def get(self, key): """Return the key value. :param key: the object's key :return: the stored object :rtype: `BytesIO` object """ return self.cache.get(key) def set(self, key, value, timeout=None): """Cache the object. :param key: the object's key :param value: the stored object :type value: `BytesIO` object :param timeout: the cache timeout in seconds """ timeout = timeout if timeout else self.timeout self.cache.set(key, value, timeout) def delete(self, key): """Delete the specific key.""" self.cache.delete(key) def flush(self): """Flush the cache.""" self.cache.clear()
def test_simplecache_set_many(): """Make sure set_many works with sequences as well as dicts""" cache = SimpleCache() cache.set_many({0: 0, 1: 1, 2: 4}) assert cache.get(2) == 4 cache.set_many((i, i*i) for i in xrange(3)) assert cache.get(2) == 4
class BGAPI(object): # Timeout (in minutes) cache_timeout = 1440 def __init__(self): self.cache = SimpleCache() with open(app.app.config["BG_API_KEY"], "r") as f: self.auth_params = json.load(f) def get(self, url_path, params={}): """Build a simple cache of the requested data""" rv = self.cache.get(url_path) if rv is None: params.update(self.auth_params) url = "https://api.biblegateway.com/3/" + url_path response = requests.get(url, params=params) if response.status_code != 200: request = response.request raise RuntimeError("{} request {} returned {}".format(request.method, request.url, response.status_code)) rv = response.json() self.cache.set(url_path, rv, timeout=self.cache_timeout*60) return rv def list_translations(self): return self.get('bible')['data'] def get_translation(self, xlation): return self.get('bible/{}'.format(xlation))['data'][0] def get_book_info(self, xlation, book_osis): all_books = self.get_translation(xlation)['books'] for book in all_books: if book['osis'] == book_osis: return book raise RuntimeError("Invalid book {} in translation {}".format(book_osis, xlation)) def get_passage(self, xlation, passage_osis): verse_json = self.get("bible/{}/{}".format(passage_osis, xlation))['data'][0] passage_json = verse_json['passages'][0] return {'reference': passage_json['reference'], 'content': passage_json['content']}
class RecacheTestCase(unittest.TestCase): def setUp(self): self.recached = False def dispatcher(salt): self.recached = True self.c = SimpleCache() cfg = Config(preemptive_recache_seconds=10, preemptive_recache_callback=dispatcher) self.s = Store(self.c, cfg) self.r = Retrieval(self.c, cfg) def test_preemptive_recaching_predicate(self): m = Metadata(HeaderSet(('foo', 'bar')), 'qux') def mkretr(**kwargs): return Retrieval(self.c, Config(**kwargs)) with a.test_request_context('/'): self.assertFalse(mkretr(preemptive_recache_seconds=10).should_recache_preemptively(10, m)) self.assertFalse(mkretr(preemptive_recache_callback=lambda x: 0).should_recache_preemptively(10, m)) self.assertFalse(self.r.should_recache_preemptively(11, m)) self.assertTrue(self.r.should_recache_preemptively(10, m)) self.assertFalse(self.r.should_recache_preemptively(10, m)) self.c.clear() self.assertTrue(self.r.should_recache_preemptively(10, m)) def test_preemptive_recaching_cache_bypass(self): fresh = Response('foo') with a.test_request_context('/foo'): self.s.cache_response(fresh) metadata = self.r.fetch_metadata() with a.test_request_context('/foo'): cached = self.r.fetch_response() self.assertEquals(cached.headers[self.r.X_CACHE_HEADER], 'hit') with a.test_request_context('/foo', headers={RECACHE_HEADER: metadata.salt}): self.assertRaises(RecacheRequested, self.r.fetch_response) with a.test_request_context('/foo', headers={RECACHE_HEADER: 'incorrect-salt'}): try: self.r.fetch_response() except RecacheRequested: self.fail('unexpected RecacheRequested for incorrect salt')
class Cache(object): timeout = 604800 #week cache = None def __init__(self, timeout=None): self.timeout = timeout or self.timeout self.cache = SimpleCache() def __call__(self, f): @wraps(f) def decorator(*args, **kwargs): key = request.data + request.path response = self.cache.get(key) if response is None: response = f(*args, **kwargs) self.cache.set(key, response, self.timeout) return response return decorator def get(self, key): return self.cache.get(key) def set(self, key, val): return self.cache.set(key, val, self.timeout)
def find(self, query, index): from flask import g from werkzeug.contrib.cache import SimpleCache cache = SimpleCache() CACHE_TIMEOUT = 86400 index = cache.get('bokbok:metrics_index') if not cache.get('bokbok:metrics_list'): metrics_list = list(g.redis.smembers('bokbok:metrics_cache')) cache.set('bokbok:metrics_list', metrics_list, CACHE_TIMEOUT) if not index: index = self.index(metrics_list) cache.set('bokbok:metrics_index', index, CACHE_TIMEOUT) return [metric for metric in sorted(index) if query in metric]
def increment(self, name, value=1, tags=None, sample_rate=None): statsd.increment( self._metric(name), value=value, tags=self.tags_from_request() + (tags or []), sample_rate=sample_rate, ) metrics = Metrics().initialize() # SENTRY_DSN will be taken from env sentry_sdk.init(integrations=[FlaskIntegration()]) CACHE_TIMEOUT = 3600 cache = SimpleCache(threshold=200, default_timeout=CACHE_TIMEOUT) class InvalidPathComponent(ValueError): pass class RegistryJsonEncoder(json.JSONEncoder): def default(self, o): if hasattr(o, 'to_json'): return o.to_json() return json.JSONEncoder.default(self, o) class RegistryFlask(Flask): json_encoder = RegistryJsonEncoder
""" from assoc_space import AssocSpace global commonsense_assoc if commonsense_assoc: return commonsense_assoc commonsense_assoc = AssocSpace.load_dir(ASSOC_DIR) return commonsense_assoc if len(sys.argv) == 1: root_url = 'http://conceptnet5.media.mit.edu/data/5.2' else: root_url = sys.argv[1] cache_dict = {'limit_timeout': 60, 'limit_amount': 1000} request_cache = SimpleCache(threshold=0, default_timeout=cache_dict['limit_timeout']) def add_slash(uri): """ Ensures that a slash is present in all situations where slashes are absolutely necessary for an address. """ if uri[0] == '/': return uri else: return '/' + uri def request_limit(ip_address, amount=1): """
import requests from flask import current_app as app from hnp.ui import constants from config import hnp_SERVER_HOME import os from werkzeug.contrib.cache import SimpleCache import socket import struct from hnp.api.models import Sensor import geoip2.database flag_cache = SimpleCache(threshold=1000, default_timeout=300) sensor_cache = SimpleCache(threshold=1000, default_timeout=300) geoip2_reader = geoip2.database.Reader(hnp_SERVER_HOME + '/../../GeoLite2-City.mmdb') def is_RFC1918_addr(ip): # 10.0.0.0 = 167772160 # 172.16.0.0 = 2886729728 # 192.168.0.0 = 3232235520 RFC1918_net_bits = ((167772160, 8), (2886729728, 12), (3232235520, 16)) try: # ip to decimal ip = struct.unpack("!L", socket.inet_aton(ip))[0] for net, mask_bits in RFC1918_net_bits: ip_masked = ip & (2**32 - 1 << (32 - mask_bits)) if ip_masked == net:
@author: kmarathe """ import os from flask import Flask import csv import json from flask import jsonify from location import getLatLngFromAddress from datetime import datetime import sys import twilio.twiml datetime.now().strftime('%a') from werkzeug.contrib.cache import SimpleCache cache = SimpleCache() cache.set('counter',100) app = Flask(__name__) import requests # pip install requests import json class Shelter(object): name="" address="" lat="" long="" service_type=1 daysOfWeek=""
def __init__(self): """Initialize the cache.""" super(ImageSimpleCache, self).__init__() self.cache = SimpleCache()
import uuid from config import DEBUG from data import socketio from werkzeug.contrib.cache import SimpleCache cache = SimpleCache() def get_qr_key(): qr_key = cache.get('qr_key') if not qr_key: qr_key = uuid.uuid4().hex[-7:] cache.set('qr_key', qr_key) cache.set('old_qr_key', '') return qr_key def update_key(): old_key = cache.get('qr_key') new_key = uuid.uuid4().hex[-7:] cache.set('qr_key', new_key) cache.set('old_qr_key', old_key) return new_key def verify_key(key): if DEBUG: return True if not key: return False old_key = cache.get('old_qr_key')
def four_oh_four(error): return status(404, message="You've reached an unknown corner of this universe") @app.errorhandler(403) def four_oh_three(error): # Forbidden admin pages should always redirect to the login page if request.path.startswith('/admin/'): return redirect('/admin') return status(403) # Caches # //////////////////////////////////////////////////////////////////////////// vote_cache = SimpleCache() # OAuth providers # //////////////////////////////////////////////////////////////////////////// oauth = OAuth() oauth_providers = {} if FACEBOOK_APP_ID and FACEBOOK_APP_SECRET: oauth_providers['facebook'] = facebook = oauth.remote_app( 'facebook', access_token_url='/oauth/access_token', authorize_url='https://www.facebook.com/dialog/oauth', base_url='https://graph.facebook.com', consumer_key=FACEBOOK_APP_ID, consumer_secret=FACEBOOK_APP_SECRET, request_token_url=None,
def __init__(self, timeout=None): self.timeout = timeout or self.timeout self.cache = SimpleCache()
import test_screenshots parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--port') parser.add_argument('--test-class') args, _ = parser.parse_known_args() port = int(args.port or os.environ.get('MOCK_SERVER_PORT') or 0) if port == 0: print "Port must be set via MOCK_SERVER_PORT or --port" sys.exit() COLLECTIONS = ['entries', 'treatments', 'profile', 'devicestatus'] cache = SimpleCache(default_timeout=999999) for coll in COLLECTIONS: cache.set(coll, '[]') app = Flask(__name__) def _get_post_json(request): return json.loads(request.data or request.form.keys()[0]) @app.route('/api/v1/<coll>.json') def get_collection(coll): elements = _collection_from_test(coll, args.test_class) if args.test_class else _collection_from_cache(coll) if coll == 'treatments': return json.dumps(_filter_treatments(elements, request.args)) elif elements is not None: return json.dumps(elements)
from flask import Flask from werkzeug.contrib.cache import SimpleCache from flask import jsonify, request, send_file import serial cache = SimpleCache() app = Flask(__name__) ser = serial.Serial('/dev/ttyUSB0', 9600) def send_value( value ): char = str( chr( value ) ) ser.write( char ) def rgb_to_6bit( rgb ): return int('00' + bin(int(rgb[0:1], 16))[2:].zfill(4)[0:2] + bin(int(rgb[2:3], 16))[2:].zfill(4)[0:2] + bin(int(rgb[4:5], 16))[2:].zfill(4)[0:2], 2) @app.route('/color', methods=['GET', 'POST']) def color(): if request.method == 'POST': color = request.form['color'] cache.set('color', color) send_value( rgb_to_6bit( color ) ) return jsonify( status = 'ok', color = color ) @app.route('/') def index(): return send_file('templates/index.html') if __name__ == '__main__': cache.set('color', '000000')
import logging import sys from werkzeug.contrib.cache import MemcachedCache from werkzeug.contrib.cache import SimpleCache from settings import CACHE_BACKEND from settings import MEMCACHED_HOST_PORT logger = logging.getLogger(__name__) cache = SimpleCache() # Provides a simple layer of caching for the video list, based on Memcached. if "MEMCACHED" == CACHE_BACKEND: # Try to use memcached try: cache = MemcachedCache([MEMCACHED_HOST_PORT]) # testing the connection dummy_data = "The quick brown fox jumps over the lazy dog abcxyz" cache.set("cs2015_dummy_data", dummy_data) if dummy_data == cache.get("cs2015_dummy_data"): logger.info("MemcachedCache was started successfully.") else: logger.info("MemcachedCache is not working correctly. Using SimpleCache.") cache = SimpleCache() except RuntimeError as e:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('--port') parser.add_argument('--test-class') parser.add_argument('--debug', action='store_true') args, _ = parser.parse_known_args() port = int(args.port or os.environ.get('MOCK_SERVER_PORT') or 0) if port == 0: print "Port must be set via MOCK_SERVER_PORT or --port" sys.exit() COLLECTIONS = ['entries', 'treatments', 'profile', 'devicestatus'] cache = SimpleCache(default_timeout=999999) for coll in COLLECTIONS: cache.set(coll, '[]') app = Flask(__name__) def _get_post_json(request): return json.loads(request.data or request.form.keys()[0]) @app.route('/api/v1/<coll>.json') def get_collection(coll): elements = _collection_from_test( coll, args.test_class) if args.test_class else _collection_from_cache(coll)
""" from assoc_space import AssocSpace global commonsense_assoc if commonsense_assoc: return commonsense_assoc commonsense_assoc = AssocSpace.load_dir(ASSOC_DIR) return commonsense_assoc if len(sys.argv) == 1: root_url = 'http://conceptnet5.media.mit.edu/data/5.2' else: root_url = sys.argv[1] cache_dict = {'limit_timeout': 60, 'limit_amount': 10000} request_cache = SimpleCache(default_timeout=cache_dict['limit_timeout']) def add_slash(uri): """ Ensures that a slash is present in all situations where slashes are absolutely necessary for an address. """ if uri[0] == '/': return uri else: return '/' + uri def request_limit(ip_address, amount=1): """
def simple(app, args, kwargs): kwargs.update(dict(threshold=app.config['CACHE_THRESHOLD'])) return SimpleCache(*args, **kwargs)
def setUp(self): self.c = SimpleCache()
def __init__(self): self.cache=SimpleCache()
class SearchService: # 初始化 def __init__(self): self.cache=SimpleCache() # 判断是否有筛选条件 def findFilter(self,params): if len(params["school"])>0: return True if len(params["field"]) >0: return True if len(params["h_index"]) > 0: return True def findFilter2(self,params): if "school"in params and len(params["school"])>0: return True if "code"in params and len(params["code"]) >0: return True if "name"in params and len(params["name"]) >0: return True return False # 找到搜索缓存的记录 def getKey2(self,params): key = ['keyword'] p={} for k in key: p[k]=params[k] return str(p) def getKey(self,params): key = ['keyword', 'name'] p={} for k in key: p[k]=params[k] return str(p) # 得到查询结果 def getSearchResult(self,params): re={} key=params['keyword'] temp=jeibaUitl.cut(params['keyword']) if len(temp)==0: if len(key)==0: params['keyword']=[] else: temp=[key] params['keyword']=temp else: params['keyword'] = temp temp=paperSearch.searchdao(params) re['num'] = temp["num"] key = self.getKey(params) if not self.findFilter(params): re['filter'] = self.getfilter(temp["filter"]) self.cache.set(key, re['filter'], timeout=5 * 60*60) else: value=self.cache.get(key) if value is None: re['filter'] = self.getfilter(temp["filter"]) self.cache.set(key, re['filter'], timeout=5 * 60 * 60) else: re['filter']=value re['allPage']=int(re['num']/params['pPageNum']) if re['num']%params['pPageNum']!=0: re['allPage']+=1 re['params']=params for r in temp['result']: r["link"]="/main/expert/"+str(r["author_id"]) # re['result']=temp['result'] re['result']=self.setLight(temp['result'],params['keyword']) if len(re['result'])>0 and "light_abstract" not in re['result'][0].keys(): for r in re['result']: r['light_abstract']=r['abstract'] return re def getSearchResult2(self,params): requrl ="http://"+ environment['se']["host"] + ":" + str(environment['se']["port"])+ environment['se']["url"] s = json.dumps(params) r = requests.post(requrl, data=s) res =r.text res=eval(res) t_id=[str(r[0]) for code in res for r in res[code]] if len(t_id)==0: teacher={} else: teacher=expertService.get_infosByIds(t_id) in_value={} t_value={str(r[0]):str(r[1]) for code in res for r in res[code]} for t in teacher: c=teacher[t] k=str(c["school_id"])+"_"+c["institution"] if k not in in_value: in_value[k]=0 in_value[k]+=float(t_value[str(c['id'])]) temp = sorted(in_value.items(), key=lambda item: item[1], reverse=True)[0:3] teacher_temp={} for t in temp: for ta in teacher: c=teacher[ta] k = str(c["school_id"]) + "_" + c["institution"] if k==t[0]: teacher_temp[ta]=c teacher=teacher_temp for id in teacher: if teacher[id]["fields"] is None or len(teacher[id]["fields"])==0: teacher[id]["fields"]=[] else: item=eval(teacher[id]["fields"]) te = sorted(item.items(), key=lambda item: item[1], reverse=True) teacher[id]["fields"]=[t[0] for t in te[0:5]] if teacher[id]["age"] is None: teacher[id]["age"]='' if teacher[id]["eduexp"] is None or len(teacher[id]["eduexp"])==0: teacher[id]["eduexp"]=[] else: teacher[id]["eduexp"] =teacher[id]["eduexp"].split('\n') school=list({str(teacher[t]['school_id']) for t in teacher}) if len(school)==0: school={} else: school = schoolService.get_infosByIds(school) for s in school: school[s]["important"]=schoolService.get_important_discipline_num(s) school[s]["main_lab"] = schoolService.get_main_lab_num(school[s]['name']) if school[s]["characteristic"]=="-": school[s]["characteristic"]=[] elif school[s]["characteristic"]=="-211": school[s]["characteristic"] = [211] elif school[s]["characteristic"]=="985-211": school[s]["characteristic"] = [985,211] result=[] temp2 = sorted(t_value.items(), key=lambda item: item[1], reverse=True) for t in temp: te=t[0].split('_') item={} item['school']=school[te[0]] item['institution']={"name":te[1],"main_lab":schoolService.get_main_lab(te)} teacher_list=[] for t2 in temp2: if str(t2[0]) in teacher: c=teacher[str(t2[0])] k = str(c["school_id"]) + "_" + c["institution"] if k==t[0] and len(teacher_list)<3: teacher_list.append(c) item['teacher']=teacher_list result.append(item) return result # 对查询的结果显示不同的样式 def setLight(self,result,keys): for r in result: for k in keys: r['abstract']=r['abstract'].replace(k,"<span class='light'>"+k+"</span>") return result # 对查询的结构生成筛选条件和数量 def getfilter(self, result): f = {} f['hindexs']={} f['hindexs']['>30']=0 f['hindexs']['20-29'] = 0 f['hindexs']['10-19'] = 0 f['hindexs']['<10'] = 0 f['fields']=[] f['schools'] = [] field=[] schools = [] for r in result: if r['h_index']>=30 : f['hindexs']['>30']+=1 elif r['h_index']>=20: f['hindexs']['20-29'] += 1 elif r['h_index'] >= 10: f['hindexs']['10-19'] += 1 else : f['hindexs']['<10'] += 1 field.extend(r['fields']) schools.append(r['school']) temp=[] temp.append({'value':">30",'num':f['hindexs']['>30']}) temp.append({'value': "20-29", 'num': f['hindexs']['20-29']}) temp.append({'value': "10-19", 'num': f['hindexs']['10-19']}) temp.append({'value': "<10", 'num': f['hindexs']['<10']}) f['hindexs']=temp c2 = Counter(field).items() c1=sorted(c2, key=lambda x: x[1], reverse=True) if len(c1)-5>=0: n=5 else: n=len(c1) for i in range(n): f['fields'].append({'value':c1[i][0],'num':c1[i][1]}) c3 = Counter(schools).items() c0=sorted(c3, key=lambda x: x[1], reverse=True) if len(c0)-5>=0: n=5 else: n=len(c0) for i in range(n): f['schools'].append({'value':c0[i][0],'num':c0[i][1]}) return f # 得到查询结果 # 对查询的结构生成筛选条件和数量 def getfilter2(self, result,teacher): f={} f["codes"]=[] f["schools"] = [] codes={} schools={} for code in result: codes[code]=len(result[code]) for t in result[code]: school_id=teacher[str(t[0])]["school_id"] if school_id in schools: schools[school_id]+=1 else: schools[school_id]= 1 if 0 in schools: del schools[0] school_id=[str(id) for id in schools] if len(school_id)==0: s={} else: s=schoolService.get_infosByIds(school_id) code_id=[c for c in codes] if len(code_id)==0: c={} else: c = schoolService.get_infosByCodes(code_id) c2 = Counter(codes).items() c1=sorted(c2, key=lambda x: x[1], reverse=True) if len(c1)-5>=0: n=5 else: n=len(c1) for i in range(n): f['codes'].append({'value':c1[i][0],'num':c1[i][1],"name":c[c1[i][0]]["name"]}) c2 = Counter(schools).items() c1=sorted(c2, key=lambda x: x[1], reverse=True) if len(c1)-5>=0: n=5 else: n=len(c1) for i in range(n): f['schools'].append({'value':c1[i][0],'num':c1[i][1],"name":s[str(c1[i][0])]["name"]}) return f # 得到查询结果 def getIndexSearchResult(self, params): key = params['keyword'] temp = jeibaUitl.cut(params['keyword']) if len(temp) == 0: if len(key) == 0: params['keyword'] = [] else: temp = [key] params['keyword'] = temp else: params['keyword'] = temp temp = paperSearch.IndexSearchdao(params) for r in temp['result']: r["link"] = "/main/expert/" + str(r["author_id"]) return temp def getHotSearch(self,params): if params["type"] is None: return [] r=taskService.getHotSearch(params) if params["type"]=="专家": ids = [t["value"] for t in r] expers=expertService.get_infosByIds(ids) result=[{"name":expers[t['value']]["name"],"url":"/main/expert/"+t["value"]} for t in r] return result else: result = [{"name":t["value"] , "url": "/main/school/" + t["value"]} for t in r] return result
import os import requests import urllib.parse from bs4 import BeautifulSoup from threading import Thread from werkzeug.contrib.cache import SimpleCache CACHE = SimpleCache() CACHE_TIMEOUT = 60 * 60 REQUEST_TIMEOUT = 2 AFISHA_PAGE_URL = 'https://www.afisha.ru/msk/schedule_cinema/' KINOPOISK_INDEX_PAGE_URL = 'https://kinopoisk.ru/index.php' KINOPOISK_START_PAGE_URL = 'https://www.kinopoisk.ru' HEADERS = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'ru,en-us;q=0.7,en;q=0.3', 'Accept-Encoding': 'UTF-8', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15', 'Accept-Charset': 'windows-1251,utf-8;q=0.7,*;q=0.7', 'Keep-Alive': '300', 'Connection': 'keep-alive', 'Referer': KINOPOISK_START_PAGE_URL } STATIC_FOLDER = '../static/img/'
def __init__(self, ip, domain=None, seed=None): self.ip = ip self.domain = domain if domain != '' else None self.app = Flask(__name__) self.app.config['MAX_CONTENT_LENGTH'] = const.MAX_AVATAR_SIZE * 1024 self.sessions = dict() self.sessions_by_user_name = dict() for obj in DB.get_sessions(): s = Session(obj.name, obj.activated, obj.uid, obj.id, obj.admin) self.sessions[obj.id] = s self.sessions_by_user_name[obj.name] = s self.rooms = { 'PvE': {0: RoomPvE()}, 'PvP': {0: RoomPvP()}, 'Friends': {0: RoomFriend()} } # TODO(debug): При релизе убрать этот костыль для подсветки типов :) del self.rooms['PvE'][0] del self.rooms['PvP'][0] del self.rooms['Friends'][0] self.logger = Logger() self.logger.write_msg('==Server run at %s==' % ip) self.seed = seed self.cache = SimpleCache(default_timeout=60 * 60 * 24 * 30) def update_status(this): while True: delta = timedelta(minutes=5) now = datetime.now() for s in this.sessions.values(): if not s.last_request or (s.last_request + delta < now and s.status == Session.ONLINE): s.status = Session.OFFLINE gevent.sleep(5 * 60 + 1) gevent.spawn(update_status, self) # Error handling def error_handler_generator(e, data_): def func1(e): return render_template( 'error_page.html', text=data_['text'], description=data_['description'] ), int(error) func1.__name__ = "error" + e return func1 http_errors = loads(open('server/configs/http_errors.json').read()) for error, data in http_errors.items(): self.app.error_handler_spec[None][int(error)] = error_handler_generator(error, data) @self.app.route('/500') # static def error_500_generator(): return None # Error handling end @self.app.after_request def after_request(response): session = self.get_session(request) if session: session['ip'] = request.remote_addr session.last_request = datetime.now() if session.status == Session.OFFLINE: session.status = Session.ONLINE if const.FILTRATE_REQUEST_FOR_LOG: for item in self.service_pages: if search(item, request.url): break else: self.logger.write_record(request, response) else: self.logger.write_record(request, response) return response @self.app.route('/') # static def send_root(): session = self.get_session(request) if session: name = session.user return render_template( 'main_menu.html', page_name='Дурак online', page_title='Главная', user_name=name, admin=bool(session.admin)) elif session is None: return redirect(self.app.config["APPLICATION_ROOT"] + '/static/login.html') else: session = self.sessions[request.cookies['sessID']] session.update_activation_status() return redirect(self.app.config["APPLICATION_ROOT"] + '/static/errors/not_activated.html') @self.app.route('/account_settings.html') def account_settings(): session = self.get_session(request) if session: user_data = DB.check_user(session.user) return render_template( 'account_settings.html', header_mini=True, page_name='Настройки аккаунта', page_title='Настройки', u_name=session.user, email=user_data.email) else: return redirect(self.app.config["APPLICATION_ROOT"] + '/') @self.app.route('/static_/svg/<path:path>') # static def send_svg(path): data = self.cache.get(path) if data is None: data = open("./server/static/svg/" + path).read() self.cache.set(path, data) response = make_response(data) response.headers["Content-type"] = "image/svg+xml" response.headers["Cache-Control"] = "max-age=1000000, public" return response @self.app.route('/favicon.ico') # static def send_favicon(): return send_from_directory( os.path.join(self.app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon' ) @self.app.route('/api') # static def send_api_methods(): # TODO: rewrite documentation return redirect(self.app.config["APPLICATION_ROOT"] + '/static/api_methods.html') @self.app.route('/arena') # static; need: get@mode; maybe: get@for(user) def send_arena(): url = self.app.config["APPLICATION_ROOT"] + '/static/arena.html?' for arg in request.args: url += arg + '=' + request.args[arg] + '&' return redirect(url) @self.app.route('/static/server_statistic.html') # static; need: session@admin def send_server_statistic_file(): session = self.get_session(request) if session: if session.admin: file = open(const.SERVER_FOLDER + const.STATIC_FOLDER + '/server_statistic.html', encoding='utf-8').read() response = make_response(file) response.headers["Content-type"] = "text/html" return response else: return 'Permission denied' else: return redirect(self.app.config["APPLICATION_ROOT"] + '/') @self.app.route('/api/activate_account') # need: get@token def activate_account(): token = request.args.get('token') if not search('^[a-zA-Z0-9]+$', token): return 'Bad token' result = DB.activate_account(token) if result is None: return 'Bad token' session = Session(result.name, result.activated, result.uid) self.sessions[session.get_id()] = session session['avatar'] = result.file DB.add_session(session, result.uid) response = redirect(self.app.config["APPLICATION_ROOT"] + '/') session.add_cookie_to_resp(response) return response @self.app.route('/api/resend_email') # need: session def resend_email(): if 'sessID' in request.cookies and request.cookies['sessID'] in self.sessions: session = self.sessions[request.cookies['sessID']] else: return 'Fail', 401 DB.update_email_token(session.user, 'email activation') (email, activation_token) = DB.get_email_adress(session.user) email_.send_email( "Для подтвеждения регистрации пожалуйста перейдите по ссылке " "http://{domain}/api/activate_account?token={token}".format( domain=(self.domain if self.domain is not None else self.ip), token=activation_token ), "Account activation", email ) return 'OK' @self.app.route("/api/subscribe_allow") # need: session def subscribe_allow(): session = self.get_session(request) if not session: return 'Fail', 401 return 'False' if 'cur_room' in session.data else 'True' @self.app.route("/api/subscribe") # need: session def subscribe(): # Create queue for updates from server def gen(sess_id): q = Queue() session = self.sessions[sess_id] session['msg_queue'] = q yield ServerSentEvent('init').encode() while True: # MainLoop for SSE, use threads result = q.get() if str(result) != 'stop': ev = ServerSentEvent(str(result)) yield ev.encode() else: break if q is session['msg_queue']: del session['msg_queue'] del q session = self.get_session(request) if not session: return 'Fail', 401 session.status = Session.PLAY return Response(gen(session.id), mimetype="text/event-stream") @self.app.route("/api/unsubscribe") # need: session@msg_queue def unsubscribe(): session = self.get_session(request) room = session['cur_room'] if not session: response = make_response('Fail', 401) response.headers["Cache-Control"] = "no-store" return response if 'msg_queue' in session.data: session['msg_queue'].put('stop') session.status = Session.ONLINE response = make_response('OK') response.headers["Cache-Control"] = "no-store" if room is None: return response room_id = room.id if room.type == const.MODE_PVE: del self.rooms['PvE'][room_id] del session['cur_room'] del room return response elif room.type == const.MODE_FRIEND: room.send_msg('exit') if room.is_ready(): room.remove_player(session['player_n']) else: del self.rooms['Friends'][room_id] del room del session['cur_room'] return response elif room.type == const.MODE_PVP: if room.is_ready(): room.remove_player(session['player_n']) self.merge_room(room) else: del self.rooms['PvP'][room_id] del room del session['cur_room'] return response @self.app.route("/api/join") # need: session@msg_queue, get@mode; maybe: get@for(user) def join_room(): session = self.get_session(request) if not self.get_session(request): return 'Fail', 401 mode = int(request.args.get('mode')) if mode == const.MODE_PVE: room = RoomPvE(session, seed=self.seed) self.rooms['PvE'][room.id] = session['cur_room'] = room session['player_n'] = const.PLAYER_HAND room.send_player_inf() room.send_changes() elif mode == const.MODE_FRIEND: for_ = request.args.get('for') if for_ is None or session.user == for_ or DB.check_user(for_) is None: return 'Bad request', 400 for id, room in self.rooms['Friends'].items(): if session.user in room.for_ and for_ in room.for_: if session in room.players: return 'Player is already invited' session['player_n'] = room.add_player(session) break else: room = RoomFriend(session, for_=for_, seed=self.seed) session['player_n'] = 0 self.rooms['Friends'][room.id] = room session['cur_room'] = room if room.is_ready(): room.send_player_inf() room.send_changes() room.send_msg(dumps({ 'data': [{ 'type': 'wait', 'player': room.game.turn, 'card': None, 'inf': None }] })) else: room.send_msg('wait') elif mode == const.MODE_PVP: for room in self.rooms['PvP'].values(): if room.type == const.MODE_PVP and not room.is_ready(): break else: room = RoomPvP(seed=self.seed) self.rooms['PvP'][room.id] = room session['player_n'] = room.add_player(session) session['cur_room'] = room if room.is_ready(): room.send_player_inf() room.send_changes() room.send_msg(dumps({ 'data': [{ 'type': 'wait', 'player': room.game.turn, 'card': None, 'inf': None }] })) else: room.send_msg('wait') return 'OK' @self.app.route("/api/attack", methods=['GET']) # need: session@cur_room, get@card def attack(): session = self.get_session(request) if not session: return 'Fail', 401 room = session['cur_room'] card = int(request.args.get('card')) result = room.attack(session['player_n'], card) if result == 'END': room_id = room.id if room.type == const.MODE_PVE: del self.rooms['PvE'][room_id] del session['cur_room'] return 'OK' return result @self.app.route("/api/defense", methods=['GET']) # need: session@cur_room, get@card def defense(): session = self.get_session(request) if not session: return 'Fail', 401 room = session['cur_room'] card = int(request.args.get('card')) result = room.defense(session['player_n'], card) if result == 'END': room_id = room.id if room.type == const.MODE_PVE: del self.rooms['PvE'][room_id] del session['cur_room'] return 'OK' return result @self.app.route("/api/chat", methods=['POST']) # need: session@cur_room, post@msg def send_msg_to_chat(): session = self.get_session(request) if not session: return 'Fail', 401 room = session['cur_room'] msg = request.form.get('msg') room.send_msg(dumps({ 'msg': msg, 'from': session.user, 'hand': session['player_n'] })) return 'OK' @self.app.route("/api/check_user", methods=['POST']) # -> bool or Error # (need: post@user_name; maybe: post@pass) XOR need: post@email def check_user(): password = request.form.get('pass') sha256 = hashlib.sha256(bytes(password, encoding='utf-8')).hexdigest() if password is not None else None email = request.form.get('email') name = request.form.get('name') if name is not None: result = DB.check_user(name, sha256) response = make_response((not result).__str__()) elif email is not None: result = DB.check_email(email) response = make_response((not result).__str__()) else: response = make_response('Bad request', 400) response.headers["Content-type"] = "text/plain" return response @self.app.route("/api/avatar", methods=['GET']) # need: get@user; maybe: get@type := menu | round | round_white any def get_avatar(): user = request.args.get('user') if user == 'AI' or user == 'root': if request.args.get('type') == 'menu' or request.args.get('type') == 'round': response = make_response("/static_/svg/ic_computer_24px.svg") else: response = make_response("/static_/svg/ic_computer_24px_white.svg") else: file_ext = DB.check_user(user).file if file_ext is not None and file_ext != 'None': response = make_response("/static/avatar/{user_name}{file_ext}". format(user_name=user, file_ext=file_ext)) else: if request.args.get('type') == 'menu': response = make_response("/static_/svg/ic_person_24px.svg") elif request.args.get('type') == 'round': response = make_response("/static_/svg/account-circle.svg") elif request.args.get('type') == 'round_white': response = make_response("/static_/svg/account-circle_white.svg") else: response = make_response("/static_/svg/ic_person_24px_white.svg") response.headers["Cache-Control"] = "no-store" return response @self.app.route("/api/add_user", methods=['POST']) # need: post@user_name, post@pass, post@email; maybe: post@file(image) def add_user(): sha256 = hashlib.sha256(bytes(request.form.get('pass'), encoding='utf-8')).hexdigest() name = request.form.get('name') email = request.form.get('email') result = not DB.check_user(name) and not DB.check_email(email) if not (search('^.+@.+\..+$', email) and search('^[a-zA-Z0-9_]+$', name) and result): return make_response('Wrong data', 400) if request.files: file = request.files['file'] if file.mimetype in const.IMAGES: file_ext = const.IMAGES[file.mimetype] file.save("./server/static/avatar/{}{}".format(name, file_ext)) else: return make_response('Wrong data', 400) else: file_ext = None (activation_token, result) = DB.add_user(name, sha256, file_ext, email) if result: response = make_response('OK') result2 = DB.check_user(name, sha256) if result2: session = Session(name, result2.activated, result2.uid) self.sessions[session.get_id()] = session self.sessions_by_user_name[name] = session session['avatar'] = result2.file DB.add_session(session, result2.uid) session.add_cookie_to_resp(response) email_.send_email( "Для подтвеждения регистрации пожалуйста перейдите по ссылке " "http://{domain}/api/activate_account?token={token}".format( domain=(self.domain if self.domain is not None else self.ip), token=activation_token ), "Account activation", email) else: self.logger.write_msg("Something wrong with registration ({})".format(name)) response.headers["Content-type"] = "text/plain" return response else: return 'Error', 500 @self.app.route("/api/change_avatar", methods=['POST']) # need: session, post@file(image) def change_avatar(): session = self.get_session(request) if not session: return 'Fail', 401 if request.files: file = request.files['file'] if file.mimetype in const.IMAGES: file_ext = const.IMAGES[file.mimetype] file.save("./server/static/avatar/{}{}".format(session.user, file_ext)) DB.set_avatar_ext(session.user, file_ext) else: return 'Wrong data', 400 else: return 'Wrong data', 400 return 'OK' @self.app.route("/api/change_pass", methods=['POST']) # need: session, post@old_pass, post@new_pass def change_pass(): session = self.get_session(request) if not session: return 'Fail', 401 sha256 = hashlib.sha256(bytes(request.form.get('old_pass'), encoding='utf-8')).hexdigest() if DB.check_user(session.user, sha256): sha256 = hashlib.sha256(bytes(request.form.get('new_pass'), encoding='utf-8')).hexdigest() DB.set_new_pass(session.user, sha256) return 'OK' else: return 'Wrong password' @self.app.route("/api/send_mail_for_auto_change_pass", methods=['GET']) # get@user def send_mail_for_auto_change_pass(): user = request.args.get('user') DB.update_email_token(user, 'email activation') (email, activation_token) = DB.get_email_adress(user) email_.send_email( "Для подтвеждения смены пароля пожалуйста перейдите по ссылке " "http://{domain}/api/auto_change_pass?user={user}&token={token}".format( domain=(self.domain if self.domain is not None else self.ip), user=user, token=activation_token ), "Password change confirmation", email) return 'OK' @self.app.route("/api/auto_change_pass", methods=['GET']) # get@user, get@token def auto_change_pass(): user = request.args.get('user') token = request.args.get('token') (email, activation_token) = DB.get_email_adress(user) if (token == activation_token): new_pass = DB.auto_set_new_pass(user, 'new password') email_.send_email( "Пользователь: {user}\n" "Новый пароль: {password} " "(Вы сможете изменить пароль на любой другой на странице пользователя)".format( domain=(self.domain if self.domain is not None else self.ip), user=user, password=new_pass ), "Password change", email) return render_template( "error_page.html", title="Password change", text="Парол изменен", description="Письмо с новым паролем отправлено на ваш e-mail" ) else: return redirect(self.app.config["APPLICATION_ROOT"] + '/404') @self.app.route("/api/init_session", methods=['POST']) # need: post@user_name, post@pass def init_session(): if self.get_session(request) is not None: response = make_response('OK') response.headers["Content-type"] = "text/plain" return response user_name = request.form.get('user_name') password = request.form.get('pass') if user_name is None or password is None: return 'Bad request', 400 sha256 = hashlib.sha256(bytes(password, encoding='utf-8')).hexdigest() result = DB.check_user(request.form.get('user_name'), sha256) if result: if user_name in self.sessions_by_user_name: session = self.sessions_by_user_name[user_name] else: session = Session(user_name, result.activated, result.uid, admin=result.admin) self.sessions[session.get_id()] = session self.sessions_by_user_name[user_name] = session session['avatar'] = result.file DB.add_session(session, result.uid) session['ip'] = request.remote_addr response = make_response('True') response.headers["Content-type"] = "text/plain" session.add_cookie_to_resp(response) return response else: response = make_response('False') response.headers["Content-type"] = "text/plain" return response @self.app.route("/api/destroy_session") # need: session def destroy_session(): session = self.get_session(request) if not session: return 'Fail', 401 response = make_response('OK') session.delete_cookie(response) DB.delete_session(session.id) del self.sessions_by_user_name[session.user] del self.sessions[session.id] return response @self.app.route('/api/ping') # maybe: get@data def ping(): data = request.args.get('data') return data if data is not None else 'Pong' @self.app.route('/api/getRequestPerSec') # need: session@admin def get_request_per_sec(): session = self.get_session(request) if session: if self.get_session(request).admin: return self.logger.time_log[-1].__str__() else: return 'Permission denied', 401 else: return 'Fail', 401 @self.app.route('/api/get_table', methods=['GET']) # need: session@admin, get@table def get_table(): session = self.get_session(request) if session: if session.admin: table_s = request.args.get('table') if table_s == 'sessions': table = self.sessions.values() result = list( map(lambda s: dict(s.to_json(), **{'status': self.get_user_status(s.user)}), table)) elif table_s == 'rooms': result = [] for table in [self.rooms['PvE'].values(), self.rooms['PvP'].values(), self.rooms['Friends'].values()]: result += list(map(lambda s: s.to_json(), table)) else: return 'Bad request', 400 return dumps(result) else: return 'Permission denied', 401 else: return 'Fail', 401 @self.app.route('/api/users/check_online', methods=['GET']) # need: session; get@name def check_online(): session = self.get_session(request) if not session: return 'Fail', 401 u_name = request.args.get('name') return self.get_user_status(u_name) @self.app.route('/api/users/get_friend_list') # need: session; maybe: get@invited(bool) def get_friend_list(): session = self.get_session(request) if not session: return 'Fail', 401 invited = 'invited' in request.args return dumps(list(map( lambda x: self.user_to_json(x, session), DB.get_friends(uid=session.uid, accepted=not invited) ))) @self.app.route('/api/users/find', methods=['GET']) # need: session; get@name def find_user(): session = self.get_session(request) if not session: return 'Fail', 401 name = request.args.get('name') if name and len(name) > 3: return dumps(list(map(lambda x: self.user_to_json(x, session), DB.find_user(name)))) else: return 'Bad request', 400 @self.app.route('/api/users/friend_invite', methods=['GET']) # need: session; get@user; maybe get@accept XOR get@reject def friend_invite(): session = self.get_session(request) if not session: return 'Fail', 401 friend = request.args.get('user') accept = 'accept' in request.args reject = 'reject' in request.args if friend: if accept and DB.is_friend(session.user, friend): DB.accept_invite(session.user, friend) return 'OK' elif reject and DB.is_friend(session.user, friend): DB.reject_invite(session.user, friend) return 'OK' elif not (accept or reject or DB.is_friend(session.user, friend)): DB.invite_friend(session.user, friend) return 'OK' return 'Fail' @self.app.route('/api/get_game_invites') # need: session; def get_game_invites(): session = self.get_session(request) if not session: return 'Fail', 401 rooms = self.get_friends_games_for_user(session.user) if rooms: return dumps(list(map( lambda room: room.for_[0] if room.for_[0] != session.user else room.for_[1], rooms ))) else: return '[]'
import os import urllib import requests from flask import Flask, request from werkzeug.contrib.cache import SimpleCache port = int(os.environ['MOCK_SERVER_PORT']) cache = SimpleCache() cache.set('config', '{}') cache.set('sgv', '[]') app = Flask(__name__) def _get_post_data(request): return request.data or request.form.keys()[0] @app.route('/auto-config', methods=['get']) def auto_config(): """Pebble config page which immediately returns config values. Normally, the config page receives a return_to query parameter, to which it must redirect using JavaScript, appending the user's preferences. When this endpoint is requested by the Pebble SDK as if it were a config page, it immediately GETs the return_to url, appending whatever preferences were set in the cache by the most recent POST to /set-config. """ return_to = request.args.get('return_to') requests.get(return_to + urllib.quote(cache.get('config'))) return ''
import json import requests import logging from flask import Flask, request from werkzeug.contrib.cache import SimpleCache app = Flask(__name__) app.debug=True cache = SimpleCache() log = logging.getLogger('werkzeug') log.setLevel(logging.ERROR) @app.route("/") def perf(): collections = cache.get("mtg_coll") cards_name = [] for collection in collections: for card in collection["cards"]: cards_name.append(card["name"]) return json.dumps(cards_name) if __name__ == '__main__': resp = requests.get("http://mtgjson.com/json/AllSetsArray.json") cache.set("mtg_coll", resp.json()) app.run("0.0.0.0", port=8888, processes=4)
def __init__(self): self.cache = SimpleCache(threshold=1000, default_timeout=60*60)
class JWTAuthenticationBackend(AuthenticationBackend): # pylint: disable=too-many-instance-attributes """JWT authentication backend for AuthenticationMiddleware.""" def __init__(self): """Initize app.""" self.algorithm = 'RS256' self.prefix = 'bearer' self.well_known_config = None self.well_known_obj_cache = None self.jwks_uri = None self.issuer = None self.audience = None self.client_secret = None self.cache = None self.caching_enabled = False self.jwt_oidc_test_mode = False self.jwt_oidc_test_public_key_pem = None self.jwt_oidc_test_private_key_pem = None def init_app(self, test_mode: bool = False): """Initize app.""" self.jwt_oidc_test_mode = test_mode # # CHECK IF WE'RE RUNNING IN TEST_MODE!! # if not self.jwt_oidc_test_mode: self.algorithm = get_api_settings().JWT_OIDC_ALGORITHMS # If the WELL_KNOWN_CONFIG is set, then go fetch the JWKS & ISSUER self.well_known_config = get_api_settings( ).JWT_OIDC_WELL_KNOWN_CONFIG if self.well_known_config: # try to get the jwks & issuer from the well known config jurl = urlopen(url=self.well_known_config) self.well_known_obj_cache = json.loads( jurl.read().decode('utf-8')) self.jwks_uri = self.well_known_obj_cache['jwks_uri'] self.issuer = self.well_known_obj_cache['issuer'] else: self.jwks_uri = get_api_settings().JWT_OIDC_JWKS_URI self.issuer = get_api_settings().JWT_OIDC_ISSUER # Setup JWKS caching self.caching_enabled = get_api_settings().JWT_OIDC_CACHING_ENABLED if self.caching_enabled: self.cache = SimpleCache(default_timeout=get_api_settings(). JWT_OIDC_JWKS_CACHE_TIMEOUT) self.audience = get_api_settings().JWT_OIDC_AUDIENCE self.client_secret = get_api_settings().JWT_OIDC_CLIENT_SECRET @classmethod def get_token_from_header(cls, authorization: str, prefix: str): """Get token from header.""" try: scheme, token = authorization.split() except ValueError: raise AuthenticationError( 'Could not separate Authorization scheme and token') if scheme.lower() != prefix.lower(): raise AuthenticationError( f'Authorization scheme {scheme} is not supported') return token async def authenticate(self, request): # pylint: disable=arguments-differ """Authenticate the token.""" if 'Authorization' not in request.headers: return None auth = request.headers['Authorization'] token = self.get_token_from_header(authorization=auth, prefix=self.prefix) if self.jwt_oidc_test_mode: # in test mode, use a publick key to decode token directly. try: payload = jwt.decode(str.encode(token), self.jwt_oidc_test_public_key_pem, algorithms=self.algorithm) except jwt.InvalidTokenError as e: raise AuthenticationError(str(e)) else: # in production mod, get the public key from jwks_url try: unverified_header = jwt.get_unverified_header(token) except jwt.PyJWTError: raise AuthenticationError( 'Invalid header: Use an RS256 signed JWT Access Token') if unverified_header['alg'] == 'HS256': raise AuthenticationError( 'Invalid header: Use an RS256 signed JWT Access Token') if 'kid' not in unverified_header: raise AuthenticationError( 'Invalid header: No KID in token header') rsa_key = self.get_rsa_key(self.get_jwks(), unverified_header['kid']) if not rsa_key and self.caching_enabled: # Could be key rotation, invalidate the cache and try again self.cache.delete('jwks') rsa_key = self.get_rsa_key(self.get_jwks(), unverified_header['kid']) if not rsa_key: raise AuthenticationError( 'invalid_header: Unable to find jwks key referenced in token' ) public_key = RSAAlgorithm.from_jwk(json.dumps(rsa_key)) try: payload = jwt.decode(token, public_key, algorithms=self.algorithm, audience=self.audience) except jwt.InvalidTokenError as e: raise AuthenticationError(str(e)) return AuthCredentials(['authenticated' ]), JWTUser(username=payload['username'], token=token, payload=payload) def get_jwks(self): """Get jwks from well known config endpoint.""" if self.caching_enabled: return self._get_jwks_from_cache() return self._fetch_jwks_from_url() def _get_jwks_from_cache(self): jwks = self.cache.get('jwks') if jwks is None: jwks = self._fetch_jwks_from_url() self.cache.set('jwks', jwks) return jwks def _fetch_jwks_from_url(self): jsonurl = urlopen(self.jwks_uri) return json.loads(jsonurl.read().decode('utf-8')) def get_rsa_key(self, jwks, kid): # pylint: disable=no-self-use """Get a public key.""" rsa_key = {} for key in jwks['keys']: if key['kid'] == kid: rsa_key = { 'kty': key['kty'], 'kid': key['kid'], 'use': key['use'], 'n': key['n'], 'e': key['e'] } return rsa_key def create_testing_jwt(self, claims, header): """Create test jwt token.""" token = jwt.encode(claims, self.jwt_oidc_test_private_key_pem, headers=header, algorithm='RS256') return token.decode('utf-8') def set_testing_keys(self, private_key, public_key): """Set test keys.""" self.jwt_oidc_test_private_key_pem = private_key self.jwt_oidc_test_public_key_pem = public_key
from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy from werkzeug.contrib.cache import SimpleCache from mplayer import Player, CmdPrefix from consoles import Console app = Flask(__name__) app.config.from_object('config') cache = SimpleCache() cache.default_timeout = 86400 player = Player() metaplayer = Player(args=('-af','volume=-200:1')) metaplayer.cmd_prefix = CmdPrefix.PAUSING db = SQLAlchemy(app) console = Console() sleeper = None import views, sockets, models, music, context_processors
from werkzeug.contrib.cache import SimpleCache import psycopg2 from psycopg2.pool import ThreadedConnectionPool app = Flask(__name__) api = Api(app) # dsn = os.environ['POSTGRES_DSN'] dsn = 'host=127.0.0.1 dbname=postgres user=accelerator' pool = ThreadedConnectionPool(1, 5, dsn=dsn) parser = reqparse.RequestParser() parser.add_argument('x') parser.add_argument('y') cache = SimpleCache(default_timeout=600) def distance(p0, p1): """This is actually the distance squared""" x0, y0 = p0 x1, y1 = p1 return (x0 - x1)**2 + (y0 - y1)**2 def load_model(): model = cache.get('model') if model is None: connection = pool.getconn() c = connection.cursor() c.execute("""