You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Ubuntu Anaconda环境下socket.gaierror: [Errno -5]错误

问题描述

参考Colab上的钓鱼网站检测代码,在Ubuntu系统的Anaconda环境中运行时触发socket.gaierror: [Errno -5] No address associated with hostname错误,怀疑由Anaconda导致,附上测试代码及报错堆栈:

测试代码

# -*- coding: utf-8 -*-

# importing required packages for this section
from urllib.parse import urlparse,urlencode
import ipaddress
import re
import pandas as pd
import ssl

"""#### **3.1.1. Domain of the URL**
Here, we are just extracting the domain present in the URL. This feature doesn't have much significance in the training. May even be dropped while training the model.
"""

#!pip install python-whois

# importing required packages for this section
import re
from bs4 import BeautifulSoup
#import whois
import urllib
import urllib.request
from datetime import datetime



# 12.Web traffic (Web_Traffic)
def web_traffic(url):
  try:
    #Filling the whitespaces in the URL if any
    url = urllib.parse.quote(url)
    rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find(
        "REACH")['RANK']
    rank = int(rank)
  except TypeError:
        return 1
  if rank <100000:
    return 1
  else:
    return 0


#converting the list to dataframe
url = "http://www.phishtank.com/phish_detail.php?phish_id=8127675"
try:
    response = requests.get(url)
except:
    response = ""

print(web_traffic(url))

报错信息

hactl@hactl-ThinkPad-X201:~/Downloads/Phishing-Website-Detection-by-Machine-Learning-Techniques-master$ python testing.py 
Traceback (most recent call last):
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 1348, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 1282, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 1328, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 1277, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 1037, in _send_output
    self.send(msg)
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 975, in send
    self.connect()
  File "/home/hactl/anaconda3/lib/python3.10/http/client.py", line 941, in connect
    self.sock = self._create_connection(
  File "/home/hactl/anaconda3/lib/python3.10/socket.py", line 824, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/home/hactl/anaconda3/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -5] No address associated with hostname

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hactl/Downloads/Phishing-Website-Detection-by-Machine-Learning-Techniques-master/testing.py", line 49, in <module>
    print(web_traffic(url))
  File "/home/hactl/Downloads/Phishing-Website-Detection-by-Machine-Learning-Techniques-master/testing.py", line 31, in web_traffic
    rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find(
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 519, in open
    response = self._open(req, data)
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 536, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 496, in _call_chain
    result = func(*args)
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 1377, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/home/hactl/anaconda3/lib/python3.10/urllib/request.py", line 1351, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -5] No address associated with hostname>
解决方法

这个错误和Anaconda无关,核心原因是代码中调用的data.alexa.com域名已经失效,Alexa的公开数据接口早已停止服务,导致DNS无法解析该主机名,触发socket.gaierror。

你可以通过以下两种方式解决:

1. 替换Web流量特征的数据源

选择仍在维护的网站排名/流量API替代,修改web_traffic函数的实现逻辑即可。

2. 临时调整代码,跳过该接口调用

如果这个特征不是模型必须的,可以修改函数,捕获URLError并返回默认值,同时保留原有逻辑的兼容性:

# 修改后的web_traffic函数
def web_traffic(url):
  try:
    #Filling the whitespaces in the URL if any
    url = urllib.parse.quote(url)
    rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find(
        "REACH")['RANK']
    rank = int(rank)
  except TypeError:
        return 1
  except urllib.error.URLError:
        # 处理Alexa接口失效的情况,返回默认值
        return 0  # 可根据需求返回1
  if rank <100000:
    return 1
  else:
    return 0

另外注意代码中存在一个小问题:调用requests.get(url)但未导入requests库,需要在代码开头添加import requests,否则会触发NameError。

内容的提问来源于stack exchange,提问作者dragontim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 14:32:12