weixin_44067186 2022-04-23 19:25 采纳率: 33.3%
浏览 46

Django调用百度api,页面跳转时出现错误,该怎么解决?HTTPSConnectionPool

img

错误:ConnectionError at /trait
HTTPSConnectionPool(host='aip.baidubcse.com', port=443): Max retries exceeded with url: /rest/2.0/face/v3/detect?acces_token=24.5f998d7e9a328101007dbdab6af23051.2592000.1653304808.282335-26040449 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002A6B02743C8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Request Method: GET
Request URL: http://127.0.0.1:8000/trait
Django Version: 3.2.13
Exception Type: ConnectionError
Exception Value:
HTTPSConnectionPool(host='aip.baidubcse.com', port=443): Max retries exceeded with url: /rest/2.0/face/v3/detect?acces_token=24.5f998d7e9a328101007dbdab6af23051.2592000.1653304808.282335-26040449 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002A6B02743C8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Exception Location: D:\Python\daima\face_recognize\face_recognize_django\venv\lib\site-packages\requests\adapters.py, line 519, in send
Python Executable: D:\Python\daima\face_recognize\face_recognize_django\venv\Scripts\python.exe
Python Version: 3.7.0

部分代码

  if not request.session.get('is_login', None):
        # 没登陆,回到首页,未登陆
        return redirect("/home")
    # now_personId = request.session['name']
    ''' 获取access token  '''
    """ host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SK"""
    '''填充自己申请的AK和SK'''
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Xli5UAihCH93gj3hT0MUXywG&client_secret=Gnc8gqLswWvqmIGEGdC6KLcLXGb5CzEE'
    request1 = urllib2.Request(host)  # 此处不能取名为request
    request1.add_header('Content-Type', 'application/json; charset=UTF-8')
    response = urllib2.urlopen(request1)
    content = response.read()
    if (content):
        token = json.loads(content)['access_token']
    response.close()  # 可无

    ''' 图片信息获取处理成base64编码格式  '''
    imgPath = 'static/image/login/this_guy.jpg'
    with open(imgPath, "rb") as f:  # 转为二进制格式
        base64_data = base64.b64encode(f.read())  # 使用base64进行加密
        f.close()
    base64_data = str(base64_data, 'utf-8')

    ''' 人脸检测与属性分析  '''
    request_url = "https://aip.baidubcse.com/rest/2.0/face/v3/detect"
    request_url = request_url + "?acces_token=" + token
    header = {'Content-Type': 'application/json'}
    params = {"image": base64_data, "image_type": "BASE64",
              "face_field": "age,beauty,expression,face_shape,gender,glasses,face_shape,race"}
    response1 = requests.post(url=request_url, data=params, headers=header)
    # response = urllib2.urlopen(request, urllib.parse.urlencode(params).encode(encoding='utf-8'))
    content = response1.json()
    face_list = content['result']['face_list'][0]
    # location = face_list['location']
    age = face_list['age']
    beauty = face_list['beauty']
    expression = face_list['expression']['type']
    gender = face_list['gender']['type']
    glasses = face_list['glasses']['type']
    face_shape = face_list['face_shape']['type']
    # emotione=face_list['emotione']['type']
    race = face_list['race']['type']
    trait = {'age': age, 'beauty': beauty, 'expression': expression, gender: 'gender', glasses: 'glasses',
             face_shape: 'face_shape', race: 'race'}
    return render(request, "trait.html", trait)


  • 写回答

1条回答 默认 最新

  • ~白+黑 Python领域新星创作者 2022-04-24 10:42
    关注

    HTTPSConnectionPool(host='aip.baidubcse.com', port=443): Max retries exceeded 超过最大重试次数

    评论

报告相同问题?

问题事件

  • 创建了问题 4月23日

悬赏问题

  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率