Nic*_*ack 30 localhost curl hosts-file osx-yosemite
我刚刚从小牛队升级到优胜美地,现在curl看不到环回主机名。
设置一个简单的 http 服务器来测试:
$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...
Run Code Online (Sandbox Code Playgroud)
现在我可以在 chrome 中点击 localhost:8000。我什至可以得到它。但是在 curl 中,会发生这种情况:
$ curl localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused
Run Code Online (Sandbox Code Playgroud)
但是,这有效:
$ curl 127.0.0.1:8000
Run Code Online (Sandbox Code Playgroud)
我阅读了有关 wget proxy settings 的答案,但没有帮助,因为它有效:
$ wget --proxy=off localhost:8000
Run Code Online (Sandbox Code Playgroud)
这真的很令人沮丧,因为我的/etc/hosts文件中列出了几个不同的环回主机名,因此我可以在本地开发应用程序,而且我习惯于使用 curl 调试它们。
我已经尝试过 osx 附带的 curl 版本:
$ curl --version
curl 7.37.1 (x86_64-apple-darwin14.0) libcurl/7.37.1 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IPv6 Largefile NTLM NTLM_WB SSL libz
$ curl localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused
$ curl 127.0.0.1 # works
Run Code Online (Sandbox Code Playgroud)
我试过用 brew 编译 curl:
$ /usr/local/Cellar/curl/7.38.0/bin/curl --version
curl 7.38.0 (x86_64-apple-darwin14.0.0) libcurl/7.38.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp
Features: IPv6 Largefile NTLM NTLM_WB SSL libz
$ /usr/local/Cellar/curl/7.38.0/bin/curl localhost:8000
curl: (7) Failed to connect to localhost port 8000: Connection refused
$ /usr/local/Cellar/curl/7.38.0/bin/curl 127.0.0.1:8000 # works
Run Code Online (Sandbox Code Playgroud)
Nic*_*ack 36
我只是通过注释掉我的 /etc/hosts 文件中的 IPv6 环回行之一来让它工作:
#fe80::1%lo0 localhost
Run Code Online (Sandbox Code Playgroud)
现在我所有的环回主机名都可以工作,而不仅仅是本地主机。我想知道这是怎么回事?
小智 24
替代方案(不需要 sudo 或修改/etc/hosts) - 始终使用 ipv4,直到 curl 变得更聪明。
$ echo '--ipv4' >> ~/.curlrc
Run Code Online (Sandbox Code Playgroud)
(然后一切都会按预期工作)