python grpc_Python grpc 模块,Channel() 实例源码 - 编程字典

def secure_authorized_channel(

credentials, request, target, ssl_credentials=None, **kwargs):

"""Creates a secure authorized gRPC channel.

This creates a channel with SSL and :class:`AuthMetadataPlugin`. This

channel can be used to create a stub that can make authorized requests.

Example::

import google.auth

import google.auth.transport.grpc

import google.auth.transport.requests

from google.cloud.speech.v1 import cloud_speech_pb2

# Get credentials.

credentials, _ = google.auth.default()

# Get an HTTP request function to refresh credentials.

request = google.auth.transport.requests.Request()

# Create a channel.

channel = google.auth.transport.grpc.secure_authorized_channel(

credentials, 'speech.googleapis.com:443', request)

# Use the channel to create a stub.

cloud_speech.create_Speech_stub(channel)

Args:

credentials (google.auth.credentials.Credentials): The credentials to

add to requests.

request (google.auth.transport.Request): A HTTP transport request

object used to refresh credentials as needed. Even though gRPC

is a separate transport, there's no way to refresh the credentials

without using a standard http transport.

target (str): The host and port of the service.

ssl_credentials (grpc.ChannelCredentials): Optional SSL channel

credentials. This can be used to specify different certificates.

kwargs: Additional arguments to pass to :func:`grpc.secure_channel`.

Returns:

grpc.Channel: The created gRPC channel.

"""

# Create the metadata plugin for inserting the authorization header.

metadata_plugin = AuthMetadataPlugin(credentials, request)

# Create a set of grpc.CallCredentials using the metadata plugin.

google_auth_credentials = grpc.metadata_call_credentials(metadata_plugin)

if ssl_credentials is None:

ssl_credentials = grpc.ssl_channel_credentials()

# Combine the ssl credentials and the authorization credentials.

composite_credentials = grpc.composite_channel_credentials(

ssl_credentials, google_auth_credentials)

return grpc.secure_channel(target, composite_credentials, **kwargs)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值