Got error "gaierror: [Errno -2] Name or service not known" when run py.test

I am trying to install mitmproxy following the readme.md at master. After the dev.sh finished, I run py.test and got this error “aierror: [Errno -2] Name or service not known”. I am not familiar with python, so could you help me how to solve this?
This is the full terminal output of py.test.

(venv) Vostro-2420 ~/mitmproxy $ py.test
============================= test session starts =====est=========================
platform linux2 -- Python 2.7.12, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/hynguyen/mitmproxy, inifile: setup.cfg
plugins: timeout-1.0.0, cov-2.3.0, xdist-1.14
collected 908 items 

test/mitmproxy/test_addons.py .
test/mitmproxy/test_app.py ..
test/mitmproxy/test_cmdline.py ......
test/mitmproxy/test_contentview.py ...................
test/mitmproxy/test_contrib_tnetstring.py .......
test/mitmproxy/test_controller.py ..........
test/mitmproxy/test_custom_contentview.py .
test/mitmproxy/test_dump.py ...............
test/mitmproxy/test_examples.py ........
test/mitmproxy/test_filt.py ..........................................
test/mitmproxy/test_flow.py .........................................................
test/mitmproxy/test_flow_export.py ..................
test/mitmproxy/test_flow_format_compat.py ..
test/mitmproxy/test_fuzzing.py ...
test/mitmproxy/test_optmanager.py .....
test/mitmproxy/test_platform_pf.py .
test/mitmproxy/test_protocol_http1.py .....
test/mitmproxy/test_protocol_http2.py ............
test/mitmproxy/test_proxy.py ..................
test/mitmproxy/test_proxy_config.py ..
test/mitmproxy/test_server.py ......................................................................................................
test/mitmproxy/test_stateobject.py ...
test/mitmproxy/test_utils.py ..
test/mitmproxy/test_web_app.py ....
test/mitmproxy/test_web_master.py .
test/mitmproxy/builtins/test_anticache.py .
test/mitmproxy/builtins/test_anticomp.py .
test/mitmproxy/builtins/test_dumper.py ..
test/mitmproxy/builtins/test_filestreamer.py .
test/mitmproxy/builtins/test_replace.py ..
test/mitmproxy/builtins/test_script.py ...s.........
test/mitmproxy/builtins/test_setheaders.py ..
test/mitmproxy/builtins/test_stickyauth.py .
test/mitmproxy/builtins/test_stickycookie.py .........
test/mitmproxy/console/test_common.py .
test/mitmproxy/console/test_help.py ..
test/mitmproxy/console/test_master.py ........
test/mitmproxy/console/test_palettes.py .
test/mitmproxy/console/test_pathedit.py ...
test/mitmproxy/script/test_concurrent.py ..
test/netlib/test_basetypes.py .
test/netlib/test_certutils.py ..........
test/netlib/test_debug.py ....
test/netlib/test_encoding.py .....
test/netlib/test_human.py .....
test/netlib/test_multidict.py .........................
test/netlib/test_socks.py ..............
test/netlib/test_strutils.py .........
test/netlib/test_tcp.py ...E...................................................
test/netlib/test_utils.py ..
test/netlib/test_version_check.py ..
test/netlib/test_wsgi.py ......
test/netlib/http/test_authentication.py ...........
test/netlib/http/test_cookies.py .........
test/netlib/http/test_headers.py .......
test/netlib/http/test_message.py ......................
test/netlib/http/test_multipart.py .
test/netlib/http/test_request.py .......s.......................
test/netlib/http/test_response.py ............
test/netlib/http/test_status_codes.py .
test/netlib/http/test_url.py ......
test/netlib/http/test_user_agents.py .
test/netlib/http/http1/test_assemble.py .........
test/netlib/http/http1/test_read.py ..................................
test/netlib/websockets/test_websockets.py .........
test/pathod/test_language_actions.py .................
test/pathod/test_language_base.py ........................
test/pathod/test_language_generators.py ...
test/pathod/test_language_http.py .................................
test/pathod/test_language_http2.py ........................
test/pathod/test_language_websocket.py ..........
test/pathod/test_language_writer.py ......
test/pathod/test_log.py .
test/pathod/test_pathoc.py ....................
test/pathod/test_pathoc_cmdline.py .
test/pathod/test_pathod.py .................................................
test/pathod/test_pathod_cmdline.py ..
test/pathod/test_protocols_http2.py ...........................
test/pathod/test_test.py ...
test/pathod/test_utils.py ..

==================================== ERRORS ====================================
__________________ ERROR at setup of TestServerIPv6.test_echo __________________

cls = <class 'test.netlib.test_tcp.TestServerIPv6'>, kwargs = {}

    @classmethod
    def setup_class(cls, **kwargs):
        cls.q = queue.Queue()
>       s = cls.makeserver(**kwargs)

test/netlib/tservers.py:96: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
test/netlib/tservers.py:103: in makeserver
    return _TServer(cls.ssl, cls.q, cls.handler, cls.addr, **kwargs)
test/netlib/tservers.py:34: in __init__
    tcp.TCPServer.__init__(self, addr)
netlib/tcp.py:898: in __init__
    self.socket.bind(self.address())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

name = 'bind', self = <socket._socketobject object at 0x7fbd146654b0>
args = (('localhost', 0),)

    def meth(name,self,*args):
>       return getattr(self._sock,name)(*args)
E       gaierror: [Errno -2] Name or service not known

/usr/lib/python2.7/socket.py:228: gaierror
====== 905 passed, 2 skipped, 1 pytest-warnings, 1 error in 88.63 seconds ======

When I try to run the py.test in sudo mode.
sudo -H py.test
The terminal stuck for a long time at test_server.py:
========================================== test session starts ===========================================
platform linux2 – Python 2.7.12, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/hynguyen/mitmproxy, inifile: setup.cfg
plugins: timeout-1.0.0, cov-2.3.0, xdist-1.14
collected 908 items

test/mitmproxy/test_addons.py .
test/mitmproxy/test_app.py ..
test/mitmproxy/test_cmdline.py ......
test/mitmproxy/test_contentview.py ...................
test/mitmproxy/test_contrib_tnetstring.py .......
test/mitmproxy/test_controller.py ..........
test/mitmproxy/test_custom_contentview.py .
test/mitmproxy/test_dump.py ...............
test/mitmproxy/test_examples.py ........
test/mitmproxy/test_filt.py ..........................................
test/mitmproxy/test_flow.py .........................................................
test/mitmproxy/test_flow_export.py ..................
test/mitmproxy/test_flow_format_compat.py ..
test/mitmproxy/test_fuzzing.py ...
test/mitmproxy/test_optmanager.py .....
test/mitmproxy/test_platform_pf.py .
test/mitmproxy/test_protocol_http1.py .....
test/mitmproxy/test_protocol_http2.py ............
test/mitmproxy/test_proxy.py ..............F...
test/mitmproxy/test_proxy_config.py ..
test/mitmproxy/test_server.py .

I know this late but I too had the same error today. Check the contents of your /etc/hosts file ‘localhost’ may not be written in front of ::1 . I had ip6-localhost
To fix this just add ‘localhost’ to the line starting with ::1

1 Like