<s> import json <EOL> import random <EOL> import datetime <EOL> from flask import Flask , request , url_for , redirect <EOL> app = Flask ( __name__ ) <EOL> DATA = { <EOL> } <EOL> EXPIRED_DATA = { <EOL> } <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def index ( session_id ) : <EOL> data = DATA . get ( session_id , None ) <EOL> if data is None : <EOL> back_url = EXPIRED_DATA . get ( session_id , None ) <EOL> if back_url is None : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return redirect ( back_url ) <EOL> data [ '<STR_LIT>' ] = datetime . datetime . now ( ) <EOL> return """<STR_LIT>""" % dict ( username = data [ '<STR_LIT:username>' ] , seconds = ( data [ '<STR_LIT>' ] - datetime . datetime . now ( ) ) . seconds ) <EOL> def get_json ( ) : <EOL> if request . data : <EOL> data = request . data <EOL> else : <EOL> keys = request . form . keys ( ) or [ '<STR_LIT>' ] <EOL> data = keys [ <NUM_LIT:0> ] <EOL> return json . loads ( data ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> def start_experiment ( ) : <EOL> request_data = get_json ( ) <EOL> client_initial_data = json . loads ( request_data [ '<STR_LIT>' ] ) <EOL> server_initial_data = json . loads ( request_data [ '<STR_LIT>' ] ) <EOL> print server_initial_data <EOL> start_date_str = server_initial_data [ '<STR_LIT>' ] <EOL> start_date_str , microseconds = start_date_str . split ( '<STR_LIT:.>' ) <EOL> start_date = datetime . datetime . strptime ( start_date_str , "<STR_LIT>" ) + datetime . timedelta ( microseconds = int ( microseconds ) ) <EOL> max_date = start_date + datetime . timedelta ( seconds = float ( server_initial_data [ '<STR_LIT>' ] ) ) <EOL> session_id = str ( random . randint ( <NUM_LIT:0> , <NUM_LIT> ) ) <EOL> DATA [ session_id ] = { <EOL> '<STR_LIT:username>' : server_initial_data [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : max_date , <EOL> '<STR_LIT>' : datetime . datetime . now ( ) , <EOL> '<STR_LIT>' : request_data [ '<STR_LIT>' ] <EOL> } <EOL> link = url_for ( '<STR_LIT:index>' , session_id = session_id , _external = True ) <EOL> print "<STR_LIT>" % session_id <EOL> print "<STR_LIT>" , link <EOL> return json . dumps ( { '<STR_LIT:url>' : link , '<STR_LIT>' : session_id } ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def status ( session_id ) : <EOL> data = DATA . get ( session_id , None ) <EOL> if data is not None : <EOL> print "<STR_LIT>" , datetime . datetime . now ( ) - data [ '<STR_LIT>' ] , "<STR_LIT>" <EOL> print "<STR_LIT>" % ( data [ '<STR_LIT:username>' ] , ( data [ '<STR_LIT>' ] - datetime . datetime . now ( ) ) . seconds ) <EOL> if ( datetime . datetime . now ( ) - data [ '<STR_LIT>' ] ) . seconds > <NUM_LIT:30> : <EOL> print "<STR_LIT>" <EOL> return json . dumps ( { '<STR_LIT>' : - <NUM_LIT:1> } ) <EOL> print "<STR_LIT>" <EOL> return json . dumps ( { '<STR_LIT>' : <NUM_LIT:10> } ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def dispose_experiment ( session_id ) : <EOL> request_data = get_json ( ) <EOL> if '<STR_LIT:action>' in request_data and request_data [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> if session_id in DATA : <EOL> data = DATA . pop ( session_id , None ) <EOL> if data is not None : <EOL> EXPIRED_DATA [ session_id ] = data [ '<STR_LIT>' ] <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> app . run ( debug = True , host = '<STR_LIT>' ) </s>
