Skip to content

Instantly share code, notes, and snippets.

@pbandreddy
Created April 26, 2023 11:11
Show Gist options
  • Save pbandreddy/e85ea9abbc923229368d477dc2fc7dd5 to your computer and use it in GitHub Desktop.
Save pbandreddy/e85ea9abbc923229368d477dc2fc7dd5 to your computer and use it in GitHub Desktop.
from flask import Flask, render_template, request, send_file
import haralyzer
import csv
import io
import json
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 160 * 1024 * 1024 # 160 MB max file size
@app.route('/')
def index():
return render_template('index.html')
@app.route('/convert', methods=['POST'])
def convert():
if request.method == 'POST':
# Get the uploaded file from the form
file = request.files['file']
# Read the file data
har_data = file.read()
# Parse the HAR data using haralyzer
har_parser = haralyzer.HarParser(json.loads(har_data))
# Get the domain parameter from the form
domain = request.form.get('domain')
api_data = [['API', 'Status', 'Method', 'Response Time (ms)']]
for page in har_parser.pages:
for entry in page.entries:
if domain in entry['request']['url']:
api = entry['request']['url']
status = entry['response']['status']
method = entry['request']['method']
response_time = entry['time']
api_data.append([api, status, method, response_time])
# Write the data to a CSV file
with open(f'{domain}_api_data.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(api_data)
# Return the CSV file as an attachment
return send_file(f'{domain}_api_data.csv', as_attachment=True)
# If the request method is not POST, return an error
return 'Invalid request method'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment