URLError:<urlopen错误[Errno 61]连接被拒绝

时间:2019-09-29 16:39:25

标签: python-3.x http urllib

import urllib.request
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
fhand = urllib.request.urlopen('http://www.dr-chuck.com/page1.htm',context=ctx)
for line in fhand:
    print(line.decode().strip())

使用http时出现错误。

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
 ...
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "urllib2.py", line 8, in <module>
    fhand = urllib.request.urlopen('http://www.dr-chuck.com/page1.htm',context=ctx)
...
    return self.do_open(http.client.HTTPConnection, req)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1319, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

在网址中使用'http://www.dr-chuck.com/page1.htm'代替http时,它可以正常工作。

<h1>The First Page</h1>
<p>
If you like, you can switch to the
<a href="http://www.dr-chuck.com/page2.htm">
Second Page</a>.
</p>

如何处理此问题? 谢谢!!

0 个答案:

没有答案