Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Created March 22, 2021 04:32
Show Gist options
  • Save thanakijwanavit/17b7384237a80e598fe5eba952df0c11 to your computer and use it in GitHub Desktop.
Save thanakijwanavit/17b7384237a80e598fe5eba952df0c11 to your computer and use it in GitHub Desktop.
getRemoteSize
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "getRemoteSize",
"private_outputs": true,
"provenance": [],
"authorship_tag": "ABX9TyOKcYmpN/LdJAoHdlpoflEL",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/thanakijwanavit/17b7384237a80e598fe5eba952df0c11/getremotesize.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "qRO_caTQM2Bw"
},
"source": [
"!pip install beartype"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "DM-IE4ItKKmQ"
},
"source": [
"import pandas as pd\n",
"from beartype import beartype\n",
"import requests"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "5b_tddlfKNMs"
},
"source": [
"url = 'https://inventory-bucket-dev-manual.s3-accelerate.amazonaws.com/pp8pcAY7E%2BSxHbchQ0zDh7gmgpg%3D?AWSAccessKeyId=ASIAVX4Z5TKDSNHIJOSR&Signature=WIniD29GK7nW97IW51%2BEg725pPA%3D&x-amz-security-token=IQoJb3JpZ2luX2VjEGQaDmFwLXNvdXRoZWFzdC0xIkcwRQIhAMAcEWMVtu37Q86MJsfq6xst3KmwdgE098UmmRhN%2FWprAiBIhVe%2Bzz9ZOeYwDEiGz33lnq14SN6kNlbz4SaTyH%2Fo8yrpAQid%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F8BEAEaDDM5NDkyMjkyNDY3OSIMFLjmkMbNep3%2BfCcJKr0BMAaiZ9DOkVD26AoUHxgE6zQi1gz49jQ3I98iVdYRQ08SLZrPfJlM6xRzmaCO4KOpltYRGrqhjtiNHHiSRYb2TiL1cVF5BDJBBA5BoelLWeEdLZgcijV%2Bm4Mg3d05PVHP%2BumCxxpLwfDyY%2FADvs14GXbtaXhgrSNpNwI7%2BWht%2FgO%2B4%2FIwpgvVWsPpm%2Bi0wOOwCBy30t3Nc71I1%2BYxx0KR8hejmedOB3BsS%2BoniUqPHD24JcQzNrKqMWy2PhUqMOyy4IIGOuAByn30YJx3ReKi2Ni%2BNAhR4wB7kUUKW9ZEa0%2BIWw6hg1OPxsGkKViQDJQhJtwyK%2FhdgsGKzSjrVRwoqYgsOGmF%2Bt81tUsZlPuz94ZYcsz7zqitAv25ksRh3NjkYXZQNXvjX%2FzRY20bu3DK%2Bfxf2t6zZo0lRG7bMNPWx5LwXDZt%2BCcdaxuBOL1jvhZbRcn7yTbxjUMTrLzZKP37A6605%2BQpzUGd7ZgymVAH8ulPRNMtQXenvoA1jIDCXNOfTeSr%2BOPiWfqAZ69ghDoXyaxMDngqYMeO66bbwKuSKb6gC4YHSDo%3D&Expires=1616387910'\n",
"r = requests.get(url)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "saaSMlo8Mg92"
},
"source": [
"# Get decoded size"
]
},
{
"cell_type": "code",
"metadata": {
"id": "myqLC48iKW8H"
},
"source": [
"import sys\n",
"sys.getsizeof(r.content)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "fC7F9972KbGd"
},
"source": [
"# Get raw response size"
]
},
{
"cell_type": "code",
"metadata": {
"id": "4m-PvPmYKqA9"
},
"source": [
"def requestsSize(r:requests.Response):\n",
" def header_size(headers):\n",
" return sum(len(key) + len(value) + 4 for key, value in headers.items()) + 2\n",
" request_line_size = len(r.request.method) + len(r.request.path_url) + 12\n",
" request_size = request_line_size + header_size(r.request.headers) + int(r.request.headers.get('content-length', 0))\n",
" response_line_size = len(r.reason) + 15\n",
" response_size = response_line_size + header_size(r.headers) + int(r.headers.get('content-length', 0))\n",
" total_size = request_size + response_size\n",
" return total_size"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "NFJQYSVSLaHr"
},
"source": [
"total_size"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "P9kMg5PML_EE"
},
"source": [
"# get size from url"
]
},
{
"cell_type": "code",
"metadata": {
"id": "nThey5iEMv0d"
},
"source": [
"@beartype\n",
"def sizeFromUrl(url:str):\n",
" r = requests.get(url)\n",
" return requestsSize(r)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "0M9D0zJBM_oG"
},
"source": [
"sizeFromUrl(url)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "4duKz7wiNDJD"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment