How to Configure a Proxy for Azure SearchClient in Python?

Brünning, Georg 0 Reputation points
2024-09-30T12:57:14.4666667+00:00

I'm working within a corporate network that uses a proxy server, and I'm trying to connect to Azure Cognitive Search using the SearchClient from the azure-search-documents SDK in Python. I've been struggling to configure the client to route requests through our proxy.

I tried importing RequestsTransport to set up a custom transport layer:

from azure.core.pipeline.transport import RequestsTransport

proxy_settings = {...}

transport = RequestsTransport(proxies=proxy_settings)

search_client = SearchClient(
    endpoint=endpoint,
    index_name=index_name,
    credential=AzureKeyCredential(api_key),
    transport=transport
)

But the import of RequestTransport is not found.

  1. How can I properly configure the SearchClient to use a proxy in a corporate network?
  2. Why am I unable to import RequestsTransport from azure.core.pipeline.transport?
  3. Is there a recommended way to set up the SearchClient with proxy settings and handle SSL verification within a proxy environment?

Any help would be greatly appreciated!

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,339 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. brtrach-MSFT 17,731 Reputation points Microsoft Employee Moderator
    2024-10-01T01:44:48.32+00:00

    @Brünning, Georg To configure the SearchClient to use a proxy in a corporate network, you can use the AiohttpTransport class from the azure.core.pipeline.transport module instead of RequestsTransport. Here's an example of how to set up the SearchClient with proxy settings and handle SSL verification within a proxy environment:

    from azure.core.pipeline.transport import AiohttpTransport
    from azure.search.documents import SearchClient
    from azure.core.credentials import AzureKeyCredential
    
    proxy_settings = {
        "http": "http://<proxy_host>:<proxy_port>",
        "https": "https://<proxy_host>:<proxy_port>"
    }
    
    transport = AiohttpTransport(
        proxies=proxy_settings,
        ssl_verification=True
    )
    
    search_client = SearchClient(
        endpoint=endpoint,
        index_name=index_name,
        credential=AzureKeyCredential(api_key),
        transport=transport
    )
    
    

    In this example, replace &lt;proxy_host&gt; and &lt;proxy_port&gt; with the hostname and port number of your proxy server. The ssl_verification parameter is set to True to enable SSL verification within the proxy environment.

    Regarding your question about why you are unable to import RequestsTransport, it's because RequestsTransport is not included in the azure.core.pipeline.transport module. Instead, you can use AiohttpTransport as shown in the example above


  2. Shuyun 0 Reputation points
    2024-10-10T06:08:56.98+00:00

    I also encountered this issue. And I finally find a way to set the proxy in corporate network.

    proxy_settings = { 
    "http": "http://<proxy_host>:<proxy_port>", 
    "https": "https://<proxy_host>:<proxy_port>" 
    }
    
    proxy_session = requests.Session()
    proxy_session.proxies = proxy_settings
    
    search_client = SearchClient(endpoint=endpoint, 
                                 index_name=index_name,          
                                 session=proxy_session,
                                 connection_verify=False,
                                 )
    
    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.