The*_*is 13 windows localhost curl windows-10 windows-subsystem-for-linux
我的 WSL 的问题是以下我的 WSL 无法在本地主机下的任何地方发出 cURL 请求。在这个例子中,我创建了一个返回 hello world 的页面。我的 cURL 看起来像这样(请注意,即使使用 http://,响应也是一样的)
curl -vvv localhost:80/hello_world.html
响应如下
* Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 80 failed: Connection refused
* Failed to connect to localhost port 80: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 80: Connection refused
Run Code Online (Sandbox Code Playgroud)
现在,如果我在 Powershell 上运行以下 cURL
curl http://localhost:80/hello_world.html
我得到的回应是
StatusCode : 200
StatusDescription : OK
Content : <h1> Hello World </h1>
RawContent : HTTP/1.1 200 OK
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Accept-Ranges: bytes
Content-Length: 22
Content-Type: text/html
Date: Mon, 24 Feb 2020 09:52:20 GMT
ETag: "16-59f4f46118cab...
Forms : {}
Headers : {[Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Accept-Ranges, bytes], [Content-Length, 22]...} Images : {}
InputFields : {}
Links : {}
ParsedHtml : System.__ComObject
RawContentLength : 22
Run Code Online (Sandbox Code Playgroud)
它返回实际的 hello world 而不是我在 WSL 上收到的错误消息
例如 curl: (7) Failed to connect to localhost port 80: Connection refused
编辑:
服务器在 Windows 10 机器上运行,curl 也在同一台 Windows 10 机器上运行。此外,Windows 是 Insider 版本 19569.100
更新:我查看了@Anaksunaman 在评论中分享的这篇 Microsoft 文章,cat /etc/resolv.conf然后按照从那里说到的步骤进行操作,然后我从 resolv.conf 中获取了我的 IP 并尝试使用我的 IP 进行 curl 操作,但它仍然有效,但仍然无法使用 localhost似乎也适用于它所说的文章
Depending on which version of Windows you're using, you might need to retrieve the IP address of the virtual machine. If your build is 18945 or higher, you can use localhost just like normal.
Run Code Online (Sandbox Code Playgroud)
我的版本高于 18945,因为我是 Windows 内部用户。有什么方法可以将我的 IP(EG 111.222.333.444)映射到本地主机?所以当我对 localhost 执行 CURL 时,curl 实际上使用了 IP 111.222.333.444?
bsp*_*ion 16
我怀疑您实际上正在运行 WSL2,它处理网络的方式几乎与 WSL 处理它的方式完全不同。在 git 上查看此问题,详细了解正在发生的事情以及原因。
简短的回答:localhost在 WSL2 上按照你喜欢的方式运行并不容易,我个人暂时降级到 WSL(第一个版本),直到他们最终提供了一种方法来做到这一点,而两者都没有将您的所有请求映射到任意分配的启动 IP 并打开您的 Windows 防火墙规则以允许您的服务端口上的公共入站连接。
如果您确实想降级并神奇地localhost恢复交互,只需以管理员身份打开 Powershell 控制台并输入以下内容(假设运行 Ubuntu 版本):
wsl --set-version Ubuntu 1
Run Code Online (Sandbox Code Playgroud)
您将失去任何 WSL2 功能,但会恢复理智。请注意,可能需要在 Ubuntu 实例本身上执行一两个迁移任务。
一个快速的解决方法是为 curl 的 cmd 版本设置别名并使用 wsl2 中的别名。
在 ~/.bashrc 中,设置以下内容:
alias curl="cmd.exe /C curl"
Run Code Online (Sandbox Code Playgroud)
重新启动或源 .bashrc 后,您可以像往常一样使用curl。
如果您需要在未设置别名的 bash 脚本中使用它,则可以cmd.exe /C curl在脚本中使用。它很丑,但很有效。
| 归档时间: |
|
| 查看次数: |
16365 次 |
| 最近记录: |