Want to push the mitmdump to azure blob storage in python

NK, SatishKumar 81 Reputation points
2023-05-02T18:10:40.16+00:00
As a requirement, i want to push the capture mitmdump to azure blob storage. I am using the below code. But getting error as "in script .\log_azureblob.py: ('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000256D5BF62D0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlobType
from mitmproxy import  http, flowfilter,ctx 


blob_service_client = BlobServiceClient.from_connection_string(connection_string)


blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)


if not blob_client.exists():
    # if the blob doesn't exist, create it with some initial data
    initial_data = b'Initial data for the blob'
    blob_client.upload_blob(initial_data)

def start():
    # set up the logger
    ctx.log.info("BlobLogger started.")

def request(flow):
    # get the request data and append it to the blob
    request_data = f"{flow.request.method} {flow.request.url} HTTP/1.1\n"
    for name, value in flow.request.headers.items():
        request_data += f"{name}: {value}\n"
    request_data += "\n" # add a blank line between the headers and body
    if flow.request.content:
        request_data += flow.request.content.decode("utf-8") + "\n"

    try:
        blob_client.append_block(request_data.encode("utf-8"))
    except Exception as e:
        ctx.log.error(f"Error appending request data to blob: {e}")

def response(flow):
    # get the response data and append it to the blob
    response_data = f"HTTP/1.1 {flow.response.status_code} {flow.response.reason}\n"
    for name, value in flow.response.headers.items():
        response_data += f"{name}: {value}\n"
    response_data += "\n" # add a blank line between the headers and body
    if flow.response.content:
        response_data += flow.response.content.decode("utf-8") + "\n"

    try:
        blob_client.append_block(response_data.encode("utf-8"))
    except Exception as e:
        ctx.log.error(f"Error appending response data to blob: {e}")

def done():
    # clean up the logger
    ctx.log.info("BlobLogger stopped.")



Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,178 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sedat SALMAN 14,170 Reputation points MVP
    2023-05-03T20:24:33.2433333+00:00

    It seems like the issue you're experiencing is related to a proxy that prevents connecting to Azure Blob Storage. Please check and if you require a proxy server add the relevant code
    If not please give more details about error code

    for example

    from azure.core.pipeline.policies import ProxyPolicy
    from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlobType
    from mitmproxy import http, flowfilter, ctx
    # Add this line to disable proxy:
    proxy_policy = ProxyPolicy(proxies={})
    # Modify this line to include the proxy_policy:
    blob_service_client = BlobServiceClient.from_connection_string(connection_string, policies=[proxy_policy])
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    # ... the rest of your code ...
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.