环境详情
- 操作系统类型和版本:MacOs 10.12.6(也尝试在CentOs上运行)
- Python版本:
3.7.1
- pip版本:
10.0.1
google-cloud-pubsub
版本:2.1.0
- 代理类型:SSL拦截代理(Squid 3.x)
复制步骤
- 运行一个简单的发布/订阅聊天机器人,如:https://github.com/googleworkspace/hangouts-chat-samples/tree/master/python/pub-sub-bot
- 使用
os
设置代理:
os.environ['http_proxy'] = 'http://xx.xx.xx:PORT'
os.environ['https_proxy'] = 'http://xx.xx.xx:PORT'
- 运行:bot.py
代码示例
# !/usr/bin/env python3
# encoding: utf-8
import logging, sys, os, json, time
from google.cloud import pubsub_v1
from google.auth import default
log = logging.getLogger(__name__)
os.environ['http_proxy'] = 'http://xx.xx.xx:PORT'
os.environ['https_proxy'] = 'http://xx.xx.xx:PORT'
SUBSCRIPTION_ID = 'xxx'
os.environ['GOOGLE_APPLICATION_CREDENTIALS']='xxx'
# os.environ['GRPC_DEFAULT_SSL_ROOTS_FILE_PATH'] = '/xxx/cacert.crt'
def init_client():
try:
credentials, project_id = default()
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project_id, SUBSCRIPTION_ID)
except Exception as init_err:
log.error(f'Error in initializing Pub/Sub client: {init_err}')
sys.exit(1)
return subscriber, subscription_path
def receive_messages():
log.info('Subscriber Started')
# Initialize Client
sub, sub_path = init_client()
# Call Back to process pulled messages
def callback(message):
log.info(f'Received message: {message.data}')
event = json.loads(message.data)
ack = True
# process_message(event)
if ack:
message.ack()
log.info('Completed Request')
sub.subscribe(sub_path, callback=callback)
logging.info(f'Listening for messages on {sub_path}')
# Keep main thread from exiting while waiting for messages
while True:
time.sleep(60)
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
style='{',
format='{levelname:.1}{asctime} {filename}:{lineno}] {message}')
receive_messages()
堆栈跟踪
I2020-11-17 21:56:23,136 subscriber-sample.py:27] Subscriber Started
I2020-11-17 21:56:23,142 subscriber-sample.py:44] Listening for messages on projects/xxx/xxx
E1117 21:56:24.377381000 123145466556416 ssl_transport_security.cc:1439] Handshake failed with fatal error SSL_ERROR_SSL: error:10000410:SSL routines:OPENSSL_internal:SSLV3_ALERT_HANDSHAKE_FAILURE.
I2020-11-17 21:56:24,377 streaming_pull_manager.py:696] Observed non-terminating stream error 503 failed to connect to all addresses
I2020-11-17 21:56:24,378 streaming_pull_manager.py:674] Observed recoverable stream error 503 failed to connect to all addresses
I2020-11-17 21:56:24,379 bidi.py:487] Re-established stream
I2020-11-17 21:56:24,379 streaming_pull_manager.py:696] Observed non-terminating stream error 503 failed to connect to all addresses
I2020-11-17 21:56:24,379 streaming_pull_manager.py:674] Observed recoverable stream error 503 failed to connect to all addresses
注:
- 我也尝试过设置证书路径,但没有成功李>
- 如果我在代理之外运行整个代码,它可以正常工作李>
Tags:
目前没有回答
相关问题 更多 >
编程相关推荐