Skip to content

Instantly share code, notes, and snippets.

@wulczer
Created September 22, 2011 10:21
Show Gist options
  • Save wulczer/1234484 to your computer and use it in GitHub Desktop.
Save wulczer/1234484 to your computer and use it in GitHub Desktop.
SelectConnection 503 error
import sys
import time
# Detect if we're running in a git repo
from os.path import exists, normpath
if exists(normpath('../pika')):
sys.path.insert(0, '..')
from pika.adapters import SelectConnection
from pika.connection import ConnectionParameters
from pika import BasicProperties, spec
import pika.log
# We use these to hold our connection & channel
connection = None
channel = None
def on_connected(connection):
print "demo_send: Connected to RabbitMQ"
connection.channel(on_channel_open)
def send_close_ok(code, text):
print "demo_send: Sending a Close-Ok message"
channel.transport.rpc(spec.Channel.CloseOk())
def on_channel_open(channel_):
print "demo_send: Received our Channel"
global channel
channel = channel_
channel.add_on_close_callback(send_close_ok)
channel.basic_publish(exchange='false', body='a', routing_key='b')
print "demo_send: Waiting 1 second before reopening the channel"
connection.add_timeout(1, reopen_channel)
def reopen_channel():
print "demo_send: Reopening the Channel"
connection.channel(on_channel_open_2)
def on_channel_open_2(channel_):
global channel
channel = channel_
channel.add_on_close_callback(send_close_ok)
print "demo_send: Reopened the Channel"
channel.queue_declare(queue="test", durable=True,
exclusive=False, auto_delete=False,
callback=on_queue_declared)
def on_queue_declared(frame):
print "demo_send: Queue Declared"
for x in xrange(0, 3):
message = "Hello World #%i: %.8f" % (x, time.time())
# Create properties with when we sent the message, the app_id
# user we connected with, a content type and non persisted messages
properties = BasicProperties(timestamp=time.time(),
app_id=__file__,
user_id='guest',
content_type="text/plain",
delivery_mode=1)
# Send the message
channel.basic_publish(exchange='',
routing_key="test",
body=message,
properties=properties)
print "demo_send: Sent %s" % message
# Close our connection
print "demo_send: Closing"
connection.close()
if __name__ == '__main__':
pika.log.setup(level=pika.log.INFO)
# Connect to RabbitMQ
host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
connection = SelectConnection(ConnectionParameters(host, 5673),
on_connected)
# Loop until CTRL-C
try:
# Start our blocking loop
connection.ioloop.start()
except KeyboardInterrupt:
# Close the connection
connection.close()
# Loop until the connection is closed
connection.ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment