def setUp(self): """Do before every test""" # Get the Flask test client self.client = app.test_client() app.config['TESTING'] = True self._ctx = app.test_request_context() self._ctx.push() # Connect to test database connect_to_db(app, 'sqlite:////tmp/test.db') # Create tables and add sample data db.create_all() example_data()
def get_vulns_by_class_mined(): r = connect_to_db('redis.json') vuln_class = [ 'misc', 'sde', 'iap', 'ucwkv', 'upapi', 'over', 'smis', 'bac', 'sha1', 'xss', 'pathtrav', 'rl', 'ml', 'auth', 'dos', 'csrf', 'cl', 'injec' ] RESULTS = {'mined': []} Z = 0 for vuln in vuln_class: pip = r.pipeline() pip.keys(pattern='commit:*:*:%s' % vuln) commit = pip.execute() Y = 0 for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') v = pip.execute() if v[0] == '' and v[1] == 'Y': Y += 1 Z += 1 RESULTS['mined'].append({'class': vuln, 'no': Y}) return RESULTS
def get_commits_vulns(): r = connect_to_db('redis.json') pip = r.pipeline() pip.keys(pattern='repo:*:*:n') projs = pip.execute() RESULTS = {'corr1': []} for p in projs[0]: Y = 0 pinf = p.split(':') pip = r.pipeline() pip.hget(p, 'mined') pip.hget(p, 'commits') pip.keys(pattern='commit:%s:%s:*' % (pinf[1], pinf[2])) p_info = pip.execute() if (p_info[0] == 'Y'): for c in p_info[2]: v = r.hget(c, 'vuln?') if (v == 'Y'): Y += 1 RESULTS['corr1'].append({ 'commits': int(p_info[1]), 'vulns': Y, 'all': len(p_info[2]), 'id': ('%s_%s' % (pinf[1], pinf[2])) }) #print(RESULTS) return RESULTS
def get_commits_years_dev(): r = connect_to_db('redis.json') pip = r.pipeline() pip.keys(pattern='repo:*:*:n') projs = pip.execute() RESULTS = {'corr2': []} for p in projs[0]: Y = 0 pinf = p.split(':') pip = r.pipeline() pip.hget(p, 'mined') pip.hget(p, 'dev_time') pip.keys(pattern='commit:%s:%s:*' % (pinf[1], pinf[2])) p_info = pip.execute() if (p_info[0] == 'Y'): year = float(p_info[1]) / 525600 for c in p_info[2]: v = r.hget(c, 'vuln?') if (v == 'Y'): Y += 1 RESULTS['corr2'].append({ 'time': year, 'all': len(p_info[2]), 'id': ('%s_%s' % (pinf[1], pinf[2])) }) return RESULTS
def get_CVES(): r = connect_to_db('redis.json') RESULTS = {'cves': []} pip = r.pipeline() pip.keys(pattern='commit:*:*:*') commit = pip.execute() Y = 0 Z = 0 dic = {} CVE = [] for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget(c, 'lang') pip.hget(c, 'code') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() if (cm[0] == 'Y' and cm[3] == 'Y' and cm[2] is not None): #print(cm[2]) if cm[2] not in CVE and cm[2] != '': CVE.append(cm[2]) Y += 1 if cm[2] != '': Z += 1 print(CVE)
def get_vulns_by_year(): r = connect_to_db('redis.json') RESULTS = {'benchbyyear': []} pip = r.pipeline() pip.keys(pattern='commit:*:*:*') commit = pip.execute() dic = {} Y = 0 for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget(c, 'year') pip.hget(c, 'change_to') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() if (cm[0] == 'Y' and cm[3] == 'Y'): if cm[1] not in dic: dic[cm[1]] = 1 else: dic[cm[1]] += 1 Y += 1 sorted_x = sorted(dic.items(), key=operator.itemgetter(0)) for i in sorted_x: per = round((float(i[1]) / Y) * 100, 1) RESULTS['benchbyyear'].append({'year': i[0], 'no': i[1], 'per': per}) return RESULTS
def get_projs_stats(): r = connect_to_db('redis.json') RESULTS = {'stats': []} pip = r.pipeline() pip.keys(pattern='repo:*:*:n') projs = pip.execute() Y = 0 Z = 0 dic = {} CVE = [] final_found_vulns = 0 final_no_vulns = 0 found_vulns = 0 no_vulns = 0 mined_projs = [] for p in projs[0]: pip = r.pipeline() pip.hget(p, 'mined') cm = pip.execute() if (cm[0] == 'Y'): cm_info = p.split(':') pip = r.pipeline() pip.keys(pattern='commit:%s:%s:*' % (cm_info[1], cm_info[2])) commits = pip.execute() n_vuln = 0 for c in commits[0]: if r.hget(c, 'vuln?') == 'Y': n_vuln += 1 if len(commits[0]) > 0: mined_projs.append(p) print(mined_projs) for p in mined_projs: proj_info = p.split(':') pip = r.pipeline() pip.keys(pattern='commit:%s:%s:*' % (proj_info[1], proj_info[2])) commits = pip.execute() n_vuln = 0 for c in commits[0]: if r.hget(c, 'vuln?') == 'Y': n_vuln += 1 if (n_vuln > 0): final_found_vulns += 1 else: final_no_vulns += 1 print(final_found_vulns) print(final_no_vulns)
def get_vulns_by_class3(): r = connect_to_db('redis.json') pip = r.pipeline() pip.lrange('vulns_class', 0, -1) vulns = pip.execute() RESULTS = {'benchmark': []} rest = {} Y = 0 for vuln in vulns[0]: pip = r.pipeline() pip.keys(pattern='commit:*:*:%s' % vuln) commit = pip.execute() if vuln not in rest.keys(): rest[vuln] = 0 for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget(c, 'change_to') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() # if change_to is not empty and the class belongs to the ones in the db (except CVE) and were already choosen if (cm[1] is not None and cm[1] in vulns[0] and cm[0] == 'Y' and cm[2] == 'Y'): if cm[1] not in rest: rest[cm[1]] = 0 rest[cm[1]] += 1 Y += 1 elif (cm[0] == 'Y' and cm[2] == 'Y'): rest[vuln] += 1 Y += 1 rest['sde'] += rest['sha1'] del rest['sha1'] order = [ 'injec', 'auth', 'xss', 'bac', 'smis', 'sde', 'iap', 'csrf', 'ucwkv', 'upapi', 'ml', 'rl', 'over', 'pathtrav', 'dos', 'misc' ] count = 0 for k in order: per = round((float(rest[k]) / Y) * 100, 1) count += per RESULTS['benchmark'].append({'class': k, 'no': rest[k], 'per': per}) return RESULTS
def get_no_vulns(): r = connect_to_db('redis.json') pip = r.pipeline() pip.lrange('vulns_class', 0, -1) vulns = pip.execute() Z = 0 for vuln in vulns[0]: pip = r.pipeline() pip.keys(pattern='commit:*:*:%s' % vuln) commit = pip.execute() for c in commit[0]: pip = r.pipeline() pip.hget(c, 'vuln?') cm = pip.execute() if (cm[0] == 'Y'): Z += 1 RESULTS = {'no_vulns': Z} return RESULTS
def get_no_vulns_from_mined_repos(): r = connect_to_db('redis.json') pip = r.pipeline() pip.lrange('vulns_class', 0, -1) vulns = pip.execute() Z = 0 for vuln in vulns[0]: pip = r.pipeline() pip.keys(pattern='commit:*:*:%s' % vuln) commit = pip.execute() for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() if (cm[0] == 'Y' and cm[1] == 'Y'): Z += 1 RESULTS = {'no_vulns': Z} return RESULTS
def get_no_mined_projects(): r = connect_to_db('redis.json') pip = r.pipeline() pip.keys(pattern='repo:*:*:n') projs = pip.execute() Y = 0 C = 0 dic = {} for c in projs[0]: pip = r.pipeline() pip.hget(c, 'mined') pip.hget(c, 'commits') cm = pip.execute() if (cm[0] == 'Y' and cm[1] > '1'): Y += 1 C += int(cm[1]) RESULTS = {'mined_proj': Y, 'commits': C} return RESULTS
def get_vulns_by_lang(): r = connect_to_db('redis.json') RESULTS = {'benchbylang': []} pip = r.pipeline() pip.keys(pattern='commit:*:*:*') commit = pip.execute() Y = 0 dic = {} for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget(c, 'lang') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() if (cm[0] == 'Y' and cm[2] == 'Y'): if cm[1] not in dic: dic[cm[1]] = 1 else: dic[cm[1]] += 1 Y += 1 others = 0 sorted_x = sorted(dic.items(), key=operator.itemgetter(1), reverse=True) print(sorted_x) for i in sorted_x: per = round((float(i[1]) / Y) * 100, 1) #if i[0] == 'others': # others = i[1]; #else: RESULTS['benchbylang'].append({'lang': i[0], 'no': i[1], 'per': per}) #RESULTS['benchbylang'].append({ # 'lang': 'others', # 'no': others, # 'per':round((float(others)/Y)*100, 1) #}) return RESULTS
def get_lang(): r = connect_to_db('redis.json') pip = r.pipeline() pip.keys(pattern='repo:*:*:n') projs = pip.execute() RESULTS = {'languages': []} dic = {} Y = 0 for p in projs[0]: pinf = p.split(':') pip = r.pipeline() pip.hget(p, 'mined') pip.keys(pattern='lang:%s:%s' % (pinf[1], pinf[2])) p_info = pip.execute() if (p_info[0] == 'Y'): pip = r.pipeline() pip.hgetall('lang:%s:%s' % (pinf[1], pinf[2])) lang = pip.execute() print(lang) Y += 1 print(p) print(lang) for i, v in lang[0].iteritems(): if i not in dic: dic[i] = 0 dic[i] += int(v) Y += int(v) for key, value in dic.items(): per = round((float(value) / Y) * 100, 2) RESULTS['languages'].append({'lang': key, 'no': value, 'per': per}) print(RESULTS) print(Y) return RESULTS
def get_vulns_by_class(): r = connect_to_db('redis.json') pip = r.pipeline() pip.lrange('vulns_class', 0, -1) vulns = pip.execute() Z = 0 for vuln in vulns[0]: pip = r.pipeline() pip.keys(pattern='commit:*:*:%s' % vuln) commit = pip.execute() Y = 0 for c in commit[0]: cm_info = c.split(':') pip = r.pipeline() pip.hget(c, 'vuln?') pip.hget('repo:%s:%s:n' % (cm_info[1], cm_info[2]), 'mined') cm = pip.execute() if (cm[0] == 'Y' and cm[0] == 'Y'): Y += 1 Z += 1 RESULTS['benchmark'].append({'class': vuln, 'no': Y}) #print(RESULTS) return RESULTS
#!/usr/bin/python # ctc.py # Command-line Twitter Client import os import argparse from ConfigParser import SafeConfigParser, NoOptionError import tweepy from tweepy import Stream from tweepy.streaming import StreamListener import connect ABS_FILE_PATH = os.path.dirname(os.path.abspath(__file__)) connection = connect.connect_to_db() def parse_args(): '''Returns the command-line arguments''' parser = argparse.ArgumentParser(description='Command-line Twitter Client', usage='ctc [options]') parser.add_argument('-a', '--auth', help='Authorise this app with your Twitter account.', action='store_true') parser.add_argument('-u', '--update', nargs='+', help='Update you Twitter status.') parser.add_argument('-s', '--search', help='Search and stream') parser.add_argument('-d', '--dump', help='Dump to mongohq', action='store_true')
except IMDbError as e: print(e) return flag if __name__ == '__main__': # create an instance of the IMDb class ia = IMDb() present = str(date.today()) # Getting Email address from console line email_Add = input("Email Address: ") all_Series = input("TV Series: ") # All series name from console line # Splitting series names by comma separator series_Name = all_Series.split(',') connect.connect_to_db(email_Add, all_Series) # Sending data to database message_to_send = "\n\n" for i in range(len(series_Name)): try: tv_Series = ia.search_movie(series_Name[i]) # searching Series series_Id = tv_Series[0].movieID # Obtaining id of first search result ia.update(tv_Series[0], 'episodes') series_Season = sorted(tv_Series[0]['episodes'].keys()) # No of seasons in a series no_Season = len(series_Season) season_episode = tv_Series[0]['episodes'][no_Season] # episode details in last season no_Episode = len(season_episode) name = 'TV Series Name : ' + str(tv_Series[0]) return_value = check_date() # Calling function to check date res(return_value) # Calling function to generate the final status message_to_send = message_to_send + str(name) + "\n" + str(msg) + "\n\n" except IMDbError as e:
if _ > 4: break # just for checking timezone_list[] # for timezone in timezone_list: # code, lat, coun, utc, dst = timezone # print("code: ", code) # print("lat: ", lat) # print("coun: ", coun) # print("utc: ", utc) # print("dst: ", dst) # print() # print() # connect to database cnx_db, dbcursr = connect_to_db() print() # create table or get table name(from user) tble_name, gettabledbcursr = db.get_table_to_save(dbcursr) # Add or fetch data into/from database check_to_continue_add_fetch = True while check_to_continue_add_fetch: print() print("Want to Add data to table or Fetch data from table or quit?") # check if user import right answer(a/f), not anything else check_answer_add_fetch = True while check_answer_add_fetch:
import psycopg2 import sys from connect import connect_to_db # edit data from arguments in command line # filename counts as first arg args = sys.argv date_to_edit = args[1] # second arg new_lh = args[2] #third arg # adding items to data data = (new_lh, date_to_edit) # connect to database conn = connect_to_db() cur = conn.cursor() # SQL for inserting values into db SQL = "UPDATE BBT_CHART SET LH_TEST = (%s) WHERE DATE = (%s);" # execute SQL command plus data cur.execute(SQL, data) print "LH changed" conn.commit() conn.close()
from flask import Flask from flask_restful import Resource, Api from flask_cors import CORS, cross_origin from mongoengine import * import json app = Flask(__name__) CORS(app, origins='http://*****:*****@api.resource('/') class HelloWorld(Resource): def get(self): return {'hello': 'world'} @api.resource('/patients/<string:id>') class PatientsResource(Resource): def get(self, id): return json.loads(Patient.objects.get(id=id).to_json()) # def put(self, todo_id): # todos[todo_id] = request.form['data'] # return {todo_id: todos[todo_id]} if __name__ == "__main__": connect_to_db() serve(app, host='0.0.0.0', port=8081) # print(Patient.objects.get(id="5e876d0087cd4aaefd6fc1bd"))