response= urllib.request.urlopen("http://www.fishc.com")错误
python 3.12换成别的网址比如百度就是可以的,课程里的鱼C网站就不行,换成论坛地址和https://ilovefishc.com/这个地址都是不行,搜索了相关问题,import了ssl然后ssl._create_default_https_context = ssl._create_unverified_context了也不行
错误信息:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 1344, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1319, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1365, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1314, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1074, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1018, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/http/client.py", line 1460, in connect
self.sock = self._context.wrap_socket(self.sock,
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 455, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 1046, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/ssl.py", line 1317, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#69>", line 1, in <module>
response= urllib.request.urlopen("http://www.fishc.com")
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 215, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 521, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 630, in http_response
response = self.parent.error(
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 553, in error
result = self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 492, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 745, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 521, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 630, in http_response
response = self.parent.error(
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 553, in error
result = self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 492, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 745, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 515, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 532, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 492, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 1392, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/urllib/request.py", line 1347, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)>
这个错误是由于SSL证书验证失败导致的。在Python 3.12中,urllib库默认会验证HTTPS请求的SSL证书,如果证书无效或者是自签名证书,就会抛出这个错误。
解决这个问题的方法是,可以通过设置urllib的全局SSL上下文来禁用证书验证。具体做法是,在发送请求之前,调用 ssl._create_default_https_context() 方法创建默认的SSL上下文,并将其设置为不验证证书,然后再发送请求。
以下是修改后的代码:
import urllib.request
import ssl
# 创建不验证证书的SSL上下文
ssl._create_default_https_context = ssl._create_unverified_context
# 发送请求
response = urllib.request.urlopen("http://www.fishc.com")
这样就可以绕过证书验证,发送HTTP请求并获取响应了。请注意,禁用证书验证可能会带来安全风险,所以在实际应用中,建议使用有效的证书来确保通信的安全性。 这个错误是由于网站的证书验证失败导致的。在Python中,当使用urllib.request.urlopen()打开一个网址时,会尝试验证服务器的证书链是否有效。如果网站使用自签名证书或存在其他证书问题,就会引发SSLCertVerificationError错误。
解决此问题的一种方法是禁用证书验证,但这并不是安全的做法,因为它会使你的连接变得容易受到攻击。然而,在测试或特定情况下使用时,可以采用以下方法:
import ssl
import urllib.request
# 禁用证书验证
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen("http://www.fishc.com")
请注意,禁用证书验证可能会导致安全风险,因此建议仅在必要时使用,并在生产环境中避免使用此方法。应该尽量使用具有有效证书的安全网址(以https开头)进行访问。
请注意,本回答来自GPT-3.5
如果问题已经解决,请设置最佳答案 问题分析:
根据错误信息提示,这个问题是由于SSL证书验证失败导致的。对于某些网站,Python默认会进行SSL证书验证,而对于自签名证书或无效证书,会抛出SSLCertVerificationError异常。
解决方案:
可以通过禁用SSL验证来解决这个问题。以下是一个可能的解决方法:
import ssl
import urllib.request
# 禁用SSL验证
ssl._create_default_https_context = ssl._create_unverified_context
response = urllib.request.urlopen("http://www.fishc.com")
print(response.read())
这样就可以绕过SSL证书验证,正常访问网站。注意,禁用SSL验证可能存在一定的安全风险,请谨慎使用。
请尝试以上方法,并确保你的网络连接正常。如果问题仍然存在,请提供详细的错误信息以便进一步分析和解决。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:} isdkz 发表于 2023-10-21 20:12
这个错误是由于SSL证书验证失败导致的。在Python 3.12中,urllib库默认会验证HTTPS请求的SSL证书,如果证书 ...
我说了,import了ssl按照这个操作的不行,而且按照您说的我又试了一遍还是不行呀{:10_266:} Mike_python小 发表于 2023-10-21 20:12
问题分析:
根据错误信息提示,这个问题是由于SSL证书验证失败导致的。对于某些网站,Python默认会进行SSL ...
restart shell 又走了一遍可以了 谢谢
请问这个禁用验证了,如何再次重新开启验证呢?另外每次都需要import sll,然后ssl._create_default_https_context = ssl._create_unverified_context么?岂不是很麻烦 课程里的鱼C网站就不行
这个好像没办法,论坛以前用的是 http 域名(如果你是指这个的话)
http://bbs.fishc.com -> https://fishc.com.cn
http://www.fishc.com -> https://ilovefishc.com
http://demo.fishc.com -> https://man.ilovefishc.com/html5
http://man.fishc.com -> https://man.ilovefishc.com 貌似没问题啊
歌者文明清理员 发表于 2023-10-23 18:34
貌似没问题啊
我这里确实每次都需要ssl._create_default_https_context = ssl._create_unverified_context这个一下{:10_266:}
页:
[1]