首页 > 编程知识 正文

valueerror是什么异常,python3

时间:2023-05-05 05:42:15 阅读:46295 作者:1435

文章目录中遇到的问题解决方法参考

面临的问题

value error : check _ hostnamerequiresserver _ hostname

具体的错误报告内容如下

跟踪后退(mostrecentcalllast ) :文件' python repos.py ',line 6,inmoduler=requests.get ) URL,headers=headers site-packagesrequestsAPI.py ',line 76,ingetreturnrequest (get ' usersm 1521appdataroaming python inrequestreers * kwargs (文件(c : (用户(m 1521 ) appdata (roaming (python (python 38 (site-packages ) requests ) ) send _ kwargs (文件' c : (users (m 1521 ) appdata (roaming (python 38 (site-packages (请求hon ) site inges ) ) **kwargs ) file ' c : (用户(m 1521 ) appdata (路由) python 38 insendresp=conn.urlopen (文件' c : ) 站点包nurlopenself._ prepare _ proxy (conn ) file(c:(users(m1521 ) appdata(roaming ) python ) python38 ) sing in appdataroamingpythonpython 38site-python inconnectconn=self._ connect _ TLS _ proxy (hostname, conn ) file ' c : (用户(m 1521 ) appdata (roaming ) ) m connection.py ',line 500, in _ connect _ TLS _ proxyreturnssl _ wrap _ socket (文件' c : _ SERS _ m 1521 _ aper urllib3_ u util _ SSL _.) in SSL _ wrap _ socket SSL _ sock=_ SSL _ wrap _ socket _ impl (sock,cock roamingpythonpython 38 site-packet ) in _ SSL _ wrap _ socket _ program filespython 38libSSL.py ',line 500,in wrap _ socketreturnself.SSL socker in _ createraisevalueerror (check _ hostnamerequiresserver _ hostname ) ) value error 3360 check _ hostnamerequirer ) ) hos

导入请求#执行API调用并保存响应

url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'headers = {'Accept': 'application/vnd.github.v3+json'}r = requests.get(url, headers=headers)print(f"Status code: {r.status_code}")# 将API响应赋给一个变量response_dict = r.json()# 处理结果print(response_dict.keys()) 解决方法

这个问题的原因是urllib3版本太高,新版的urllib3修改了一些东西,所以会报错。下面的解决办法是降低urlib3的版本。

打开命令行,运行:

pip install urllib3==1.25.11

如果成功,则完成;如果不成功,报错如下,则继续阅读。

Defaulting to user installation because normal site-packages is not writeableWARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/urllib3/WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/urllib3/WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/urllib3/WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/urllib3/WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))': /simple/urllib3/Could not fetch URL https://pypi.org/simple/urllib3/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/urllib3/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1125)'))) - skippingERROR: Could not find a version that satisfies the requirement urllib3==1.25.11ERROR: No matching distribution found for urllib3==1.25.11

可以使用

pip install urllib3==1.25.11 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

意思是通过国内的源来安装,速度快并且不会出现连接问题。

显示安装成功

PS D:user文档pythonpython_workdata_visualizationdownload_data> pip install urllib3==1.25.11 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com>>Defaulting to user installation because normal site-packages is not writeableLooking in indexes: http://pypi.douban.com/simpleCollecting urllib3==1.25.11 Downloading http://pypi.doubanio.com/packages/56/aa/4ef5aa67a9a62505db124a5cb5262332d1d4153462eb8fd89c9fa41e5d92/urllib3-1.25.11-py2.py3-none-any.whl (127 kB) |████████████████████████████████| 127 kB 3.3 MB/sInstalling collected packages: urllib3 Attempting uninstall: urllib3 Found existing installation: urllib3 1.26.4 Uninstalling urllib3-1.26.4: Successfully uninstalled urllib3-1.26.4Successfully installed urllib3-1.25.11PS D:user文档pythonpython_workdata_visualizationdownload_data>

然后运行自己的程序,成功!

Status code: 200dict_keys(['total_count', 'incomplete_results', 'items']) 参考

[1]https://stackoverflow.com/questions/66642705/why-requests-raise-this-exception-check-hostname-requires-server-hostname

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。