title: rabbitmq, python - ack consumer program example
body: <p>I'm looking for a basic Rabbitmq ack consumer program in python. So far, I have the basic ack producer program. But I don't know whether it is correct.</p>
  
  <p><strong>producer.py</strong>   </p>
  
  <pre class="lang-py prettyprint-override"><code>import pika, socket
  
  credentials = pika.PlainCredentials('xxxx', '1234')
  hostname = socket.gethostname()
  parameters = 
  pika.ConnectionParameters(host=socket.gethostbyname(hostname), 
  port=5672, virtual_host='/', credentials=credentials)
  
  connection = pika.BlockingConnection(parameters)
  channel = connection.channel()
  
  msg_props = pika.BasicProperties()
  msg_props.content_type = "text/plain"
  
  channel.queue_declare(queue='hello')
  if channel.basic_publish(exchange='', routing_key='hello', body='Hello 
  World!', properties=msg_props):
       print ("Message Acknowledged")
  else:
       print ("Message Lost")
  
  print("[x] Sent 'Hello World!'")
  connection.close()
  </code></pre>
  
  <p><strong>consumer.py</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>import pika, socket
  
  credentials = pika.PlainCredentials('xxxx', '1234')
  hostname = socket.gethostname()
  parameters = 
  pika.ConnectionParameters(host=socket.gethostbyname(hostname), 
  port=5672, virtual_host='/', credentials=credentials)
  
  connection = pika.BlockingConnection(parameters)
  channel = connection.channel()
  
  channel.queue_declare(queue='hello')
  
  def callback(ch, method, properties, body):
      print(" [x] Received %r" % body)
      channel.basic_ack(method.delivery_tag)
  
  channel.basic_consume(queue='hello', on_message_callback=callback, 
  auto_ack=False)
  
  print(' [*] Waiting for messages. To exit press CTRL+C')
  channel.start_consuming()
  </code></pre>
  
  <pre><code>&gt;python consumer.py
  
   [*] Waiting for messages. To exit press CTRL+C
   [x] Received b'Hello World!'
  
  
  &gt;python producer.py
  
   Message Lost
   [x] Sent 'Hello World!'
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>Though the message is received at the consumer.py program, the delivery 
  note at producer.py says, "Message Lost", but the note should be "Message Acknowledged". 
  </code></pre>
  
  <p>What is the correct consumer and producer ack program in python for rabbitmq?</p>