[Azure Congnitive QnA rest] Python 使用 HTTPSConnection 调 API 遇到 <class ‘socket.gaierror‘> 错误, 中文乱码
Github sample地址:https://github.com/Azure-Samples/cognitive-services-qnamaker-python/blob/master/documentation-samples/quickstarts/get-answer/get-answer-3x.py
VS Code,anaconda的Python3.7,运行sample遇到错误:
Unexpected error: <class 'socket.gaierror'>
Unexpected error: [Errno 8] nodename nor servname provided, or not known
百度下,很多说host文件等等,仔细查看下,错误其实很简单,是Host地址前面不能带有 "https://"
# Represents the various elements used to create HTTP request URIs# for QnA Maker operations.# From Publish Page# Example: YOUR-RESOURCE-NAME.azurewebsites.net# CAUTION: This is not the exact value of HOST field# HOST trimmed to work with http libraryhost = "YOUR-RESOURCE-NAME.azurewebsites.net";
这个sample,直接输入文中问题,后台服务会不识别:
发送前:encodedQuestion = question.encode("utf-8")
拿到结果后解析:print(json.dumps(json.loads(answer, encoding="utf-8"), ensure_ascii=False, indent=4))
代码:
# -*- coding:utf-8 -*-import http.client, urllib.parse, json, time, sys# Represents the various elements used to create HTTP request URIs
# for QnA Maker operations.
# From Publish Page
# Example: YOUR-RESOURCE-NAME.azurewebsites.net
# CAUTION: This is not the exact value of HOST field
# HOST trimmed to work with http library
host = "YOUR-RESOURCE-NAME.azurewebsites.net";# Authorization endpoint key
# From Publish Page
endpoint_key = "xxx";# Management APIs postpend the version to the route
# From Publish Page
# Example: /knowledgebases/ZZZ15f8c-d01b-4698-a2de-85b0dbf3358c/generateAnswer
# CAUTION: This is not the exact value after POST
# Part of HOST is prepended to route to work with http library
route = "";# JSON format for passing question to service
question = "{'question': '天空为什么是蓝色的','top': 3}"
encodedQuestion = question.encode("utf-8")
print(encodedQuestion)headers = {'Authorization': 'EndpointKey ' + endpoint_key,'Content-Type': 'application/json'
}try:conn = http.client.HTTPSConnection(host, port=443)conn.request("POST", route, encodedQuestion, headers)response = conn.getresponse ()answer = response.read()print(json.dumps(json.loads(answer, encoding="utf-8"), ensure_ascii=False, indent=4))except:print ("Unexpected error:", sys.exc_info()[0])print ("Unexpected error:", sys.exc_info()[1])
[Azure Congnitive QnA rest] Python 使用 HTTPSConnection 调 API 遇到 <class ‘socket.gaierror‘> 错误, 中文乱码相关推荐
- [转]python新手必碰到的问题---encode与decode,中文乱码--转载
edu.codepub.com/2009/1029/17037.php 这个问题在python3.0里已经解决了. 这有篇很好的文章,可以明白这个问题: 为什么会报错"UnicodeEnco ...
- mark python新手必碰到的问题---encode与decode,中文乱码[转]
转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错"UnicodeEncodeError:'ascii' c ...
- python写入txt文件正常,但是写入csv文件中文乱码问题
with open('/yourpath/test.csv','a',newline='', encoding='utf-8') as f:这是一开始用的语法,怎么都是乱的,但是修改了下encodin ...
- python写入文件中文乱码_记一次python写入txt文件正常,但是写入csv文件中文乱码问题...
今天在将已经爬取完存成txt文件批量导入保存csv格式时,文本中的汉字能够正常正常显示,但是用正常的方法保存到csv中就成了乱码. 最开始的写法:with open(city+'.csv','a+') ...
- python中画图时x,y轴名称出现中文乱码的解决方法
#-*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt from matplotlib.font_manage ...
- Python解决中文乱码问题
在学Python的过程中,一直困扰我的就是中文乱码,输上英文之后看上去不得劲,所以只能解决这个乱码问题,为了这个问题,百度查了谷歌查,还有包括自己总结的,终于解决了,下面看个案例吧(continue) ...
- python向数据库插入中文乱码问题
碰到编码问题就头疼啊!.... 在想数据库中插入中文时候,会出现如下错误提示:Warning: Incorrect string value: '\xE5\x8C\x97\xE4\xBA\xAC... ...
- python中plot不能显示标签_python 2: 解决python中的plot函数的图例legend不能显示中文问题...
问题: 图像标题.横纵坐标轴的标签都能显示中文名字,但是图例就是不能显示中文,怎么解决呢? 解决: plt.figure() plt.title(u'训练性能', fontproperties=fon ...
- [转载] python zip 文件解压中文乱码问题解决
参考链接: Python zip() 今天突然碰到zip文件解压后中文乱码问题.百度了下觉得这篇文章不错.保存保存. 来源:http://blog.sina.com.cn/s/blog_5805e98 ...
最新文章
- 运行nltk示例 Resource u'tokenizers punkt english.pickle' not found解决
- mac怎么合并两个容器_PDF怎样合并?在Mac上合并PDF文件的最佳方法
- 大数据资产管理在腾讯游戏的实践
- WEB前端 屏蔽移动端浏览器页面顶部提示 此网页由网站提供
- 配置hibernate根据实体类自动建表功能
- instagram架构_如何创建像Instagram这样的照片共享应用程序:基本知识。
- 计算机内部总线和外部总线,科学网-怎样将计算机内部总线扩展为外部网络?-姜咏江的博文...
- java删_JAVA删除文件夹
- 服务器内网怎么更新微软补丁,架WSUS服务器 内网自动打补丁
- 修改了DNS服务器网速慢,网络速度缓慢怎么办?轻松一键修改DNS设置让网速提升五倍...
- HTML 中划线 、 下划线 、上划线
- Excel IRR函数告诉你信用卡分期、贷款的实际利率
- 数字信号常用典型序列(1)
- iOS开发 手势密码解锁和指纹TouchID解锁
- 为什么要数据归一化和归一化方法
- 微信小程序云开发CMS中WebHook功能的使用方法
- GitLab介绍与使用
- 操作系统——文件管理
- 在Mac OS X苹果lion系统上制作USB启动盘
- ICC Scenario Difinition(什么是Scenario?)
热门文章
- 用C++实现RGB转256色
- 禅道-软件测试缺陷管理工具
- JavaScript continue
- Linux virsh查看/管理虚拟机
- only whitespace content allowed before start tag and not
- 问题:Android NDK location should not contain whitespace...解决方法
- python imread的用法_Python imread、newaxis用法详解
- Thumbnails使用方法(图片处理工具类)
- 服务器网站泛域名设置,Nginx服务器泛域名配置实例教程
- 【克罗地亚】LAGNO