Cannot send message to azure bus service

Sutradhar, Debjyoti 0 Reputation points
2025-04-15T12:32:25.21+00:00

I am not able to send message to service bus.
The error is:

[CPF-API] 2025-04-15:17:53:36,801 INFO [_transport.py:connect:207] Transport connection failed: TimeoutError('timed out') [CPF-API] 2025-04-15:17:53:36,801 INFO [_pyamqp_transport.py:_handle_amqp_exception_with_condition:940] AMQP error occurred: (AMQPConnectionError('Error condition: ErrorCondition.SocketError\n Error Description: Failed to initiate the connection due to exception: timed out')), condition: (<ErrorCondition.SocketError: b'amqp:socket-error'>), description: ('Failed to initiate the connection due to exception: timed out'). [CPF-API] 2025-04-15:17:53:38,402 INFO [_base_handler.py:_backoff:438] 'servicebus.pysdk-91a7ece8' has an exception (ServiceBusConnectionError('Failed to initiate the connection due to exception: timed out Error condition: amqp:socket-error.')). Retrying... [CPF-API] 2025-04-15:17:53:39,406 INFO [_transport.py:connect:207] Transport connection failed: TimeoutError('timed out') [CPF-API] 2025-04-15:17:53:39,406 INFO [_pyamqp_transport.py:_handle_amqp_exception_with_condition:940] AMQP error occurred: (AMQPConnectionError('Error condition: ErrorCondition.SocketError\n Error Description: Failed to initiate the connection due to exception: timed out')), condition: (<ErrorCondition.SocketError: b'amqp:socket-error'>), description: ('Failed to initiate the connection due to exception: timed out'). [CPF-API] 2025-04-15:17:53:42,607 INFO [_base_handler.py:_backoff:438] 'servicebus.pysdk-91a7ece8' has an exception (ServiceBusConnectionError('Failed to initiate the connection due to exception: timed out Error condition: amqp:socket-error.')). Retrying... [CPF-API] 2025-04-15:17:53:43,616 INFO [_transport.py:connect:207] Transport connection failed: TimeoutError('timed out') [CPF-API] 2025-04-15:17:53:43,616 INFO [_pyamqp_transport.py:_handle_amqp_exception_with_condition:940] AMQP error occurred: (AMQPConnectionError('Error condition: ErrorCondition.SocketError\n Error Description: Failed to initiate the connection due to exception: timed out')), condition: (<ErrorCondition.SocketError: b'amqp:socket-error'>), description: ('Failed to initiate the connection due to exception: timed out'). [CPF-API] 2025-04-15:17:53:50,17 INFO [_base_handler.py:_backoff:438] 'servicebus.pysdk-91a7ece8' has an exception (ServiceBusConnectionError('Failed to initiate the connection due to exception: timed out Error condition: amqp:socket-error.')). Retrying... [CPF-API] 2025-04-15:17:53:51,174 INFO [_transport.py:connect:207] Transport connection failed: TimeoutError('timed out') [CPF-API] 2025-04-15:17:53:51,174 INFO [_pyamqp_transport.py:_handle_amqp_exception_with_condition:940] AMQP error occurred: (AMQPConnectionError('Error condition: ErrorCondition.SocketError\n Error Description: Failed to initiate the connection due to exception: timed out')), condition: (<ErrorCondition.SocketError: b'amqp:socket-error'>), description: ('Failed to initiate the connection due to exception: timed out'). [CPF-API] 2025-04-15:17:53:51,174 INFO [_base_handler.py:_do_retryable_operation:398] 'servicebus.pysdk-91a7ece8' operation has exhausted retry. Last exception: ServiceBusConnectionError('Failed to initiate the connection due to exception: timed out Error condition: amqp:socket-error.').

My code is:


def send_message_to_service_bus(result): # function to send message to service bus
    service_bus_client = ServiceBusClient.from_connection_string(STR)
    try:
        with service_bus_client:
            sender = service_bus_client.get_queue_sender(queue_name=QUEUE)
            message = ServiceBusMessage(result)
            sender.send_messages(message)
            logger.info(f"Query sent to Azure Service Bus")
    except Exception as e:
        logger.error(f"Failed to send message to Service Bus: {e}")

Where Am I wrong??

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
700 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shireesha Eeraboina 3,435 Reputation points Microsoft External Staff Moderator
    2025-04-16T07:21:47.9733333+00:00

    Hi Sutradhar, Debjyoti,

    Thanks for sharing the details. The error message indicates that your application is unable to connect to Azure Service Bus, most likely due to a network timeout. This can happen for a few common reasons:

    • The connection string might be incorrect or missing required permissions.
    • Network restrictions (like firewalls or NSGs) may be blocking the AMQP port (usually port 5671).
    • If you're running this in a private network or VM, it might not have internet access.

    As a quick workaround, you can try switching to HTTPS transport by modifying your code like this:

    from azure.servicebus import TransportType
    service_bus_client = ServiceBusClient.from_connection_string(STR, transport_type=TransportType.Http)
    
    

    Also, please double-check the connection string and ensure the correct queue name is being used.

    For more help, you can refer to Send messages to and receive messages from Azure Service Bus queues (Python)

    Let us know if you're still facing the issue after trying these steps!

    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.