给我一段mq的代码,再用http长连接的代码替换,附加代码
时间: 2024-04-14 10:07:50 浏览: 10
以下是使用MQ的示例代码:
```python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
```
接下来是使用HTTP长连接的示例代码:
```python
import requests
# 创建长连接
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=3)
session.mount('http://', adapter)
# 发送请求
response = session.get('http://httpbin.org/get')
# 输出响应内容
print(response.text)
```
附加代码:
```python
import requests
# 创建长连接
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(pool_connections=10, pool_maxsize=10, max_retries=3)
session.mount('http://', adapter)
# 发送请求
response = session.get('http://httpbin.org/get')
# 输出响应内容
print(response.text)
# 关闭连接
session.close()
```