4.4.2. The Topic Publisher

topic_publisher.py publishes messages to the topic exchange, providing multi-part routing keys like usa.news, usa.weather, europe.news, and europe.weather. The publisher has no idea what bindings have been made by subscribers, it simply sends its messages to the topic exchange.
This program defines a function that sends a set of five messages to the topic exchange, using the same routing key for each:
def send_msg(routing_key):
  props = session.delivery_properties(routing_key=routing_key)
  for i in range(5):
     session.message_transfer(destination="amq.topic", message=Message(props,routing_key + " " + str(i)))
In the main body of the program we use this function to send messages with four different routing keys:
# usa.news

# usa.weather

# europe.news

# europe.weather
When we are finished sending these messages, we send a message using the routing key control, indicating that we are done:
# Signal termination
props = session.delivery_properties(routing_key="control")
session.message_transfer(destination="amq.topic", message=Message(props,"That's all, folks!"))