curl:(7)无法连接到主机我的设置有什么问题?

时间:2018-01-20 03:43:16

标签: linux shell curl

当我运行命令时:

$ curl http://localhost:9201

我收到此错误:

curl: (7) couldn't connect to host

接下来我运行带有详细标志的命令curl,即:

$ curl -v http://localhost:9201

最终导致错误:

* About to connect() to localhost port 9201 (#0)
*   Trying ::1... Connection refused
*   Trying 127.0.0.1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

我真的不明白为什么会发生这种错误。我的设置有什么问题?我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

显然,您的服务器没有侦听端口9201

您需要先验证服务器是否已成功开始侦听指定端口。如yudong shen建议的那样,您可以使用netstat命令来验证这一点。

例如:

列出所有ip:端口对及其状态及其各自的过程:

$ netstat - anp

观察Local addressStatePID/Program name列,以确定您的进程及其指定的ip:端口状态。

检查进程是否使用指定端口,而不管其状态如何:

$ netstat -anp | grep :9201

确保端口已打开以供收听:

$ netstat -anp | grep LISTEN | grep :9201

您还可以使用REST客户端(例如Google Chrome's Postman)从GUI与您的Web服务器进行交互。

答案 1 :(得分:0)

您尝试连接的服务不可用是您应该进行的第一次检查。但是,就我而言,它正在运行并且出现了一个更奇怪的错误:

$ curl -i -X POST http://127.0.0.1:12345/command -d 'some JSON here'
curl: (7) Failed connect to 127.0.0.1:31280; Connection refused

为什么错误的端口?!好man curl确实描述了-v标志,所以让我们使用它:

$ curl -i -v -X POST http://localhost:12345/command -d 'some JSON here'
* About to connect() to proxy 127.0.0.1 port 31280 (#0)
*   Trying 127.0.0.1...
* Connection refused
* Failed connect to 127.0.0.1:31280; Connection refused
* Closing connection 0
curl: (7) Failed connect to 127.0.0.1:31280; Connection refused

现在我们可以为自己没有代理服务。

答案 2 :(得分:0)

另一个原因可能是/ etc / hosts中的配置错误。如果没有类似于

的行
127.0.0.1 localhost

localhost不会解析为127.0.0.1。就我而言,我无法打开apache服务的主机。我的卷发是

root@myserver:~# curl -v -k --compressed -H 'Host: www.mysite.com' http://localhost/
* About to connect() to localhost port 80 (#0)
*   Trying ::1... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

此回复未命中

*   Trying 127.0.0.1... connected

在/ etc / hosts中添加127.0.0.1 localhost解决了该问题。