使用Go创建一个简单的文件服务器,但是本地主机拒绝连接。

huangapple go评论90阅读模式
英文:

Making a Simple FileServer with Go and Localhost Refused to Connect

问题

现在我有从一本书中提取的以下代码。

package main

import (
	"net/http"
)

func main() {
	h := http.FileServer(http.Dir("."))
	http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
}

我期望它可以列出main.go文件夹中的所有文件,但是当我浏览到 https://localhost:8001 时,我只能看到:

This site can’t be reached.
localhost refused to connect.

我使用LiteIDE构建和运行程序。点击BuildAndRun后,显示以下消息。

F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.
E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server/server.exe  [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.

为什么会这样,我该如何修复它?

英文:

Now I have the following code extracted from a book.

package main

import (
	"net/http"
)

func main() {
	h := http.FileServer(http.Dir("."))
	http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
}

I expect it can list all the file in the folder of main.go but when I browse to:
https://localhost:8001
I can only see:

This site can’t be reached.  
localhost refused to connect.

I use LiteIDE to build and run the program. After <kbd>BuildAndRun</kbd> clicked, the following messages are shown.

F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.
E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server/server.exe  [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
Success: process exited with code 0.

Why and how can I fix it?

答案1

得分: 1

你的系统找不到证书文件。这个错误意味着你需要在主二进制文件旁边有一个名为"rui.crt"的文件。如果你没有证书,请参考:https://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl

然后将"server.pem"和"server.key"文件复制到你的二进制(.exe)文件目录中,并运行以下示例代码进行测试:

package main

import (
	"fmt"
	"log"
	"net/http"
)

type server struct {
}

func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "*Hello World\n*")
}

func main() {
	if err := http.ListenAndServeTLS(":443", "server.pem", "server.key", server{}); err != nil {
		log.Fatal(err)
	}
}

然后打开网页:https://127.0.0.1/,如果防火墙弹出提示,请选择允许,如果看到"There is a problem with this website’s security certificate.",请点击继续(高级)。

输出结果为:

*Hello World
*
英文:

your system cannot find the certificate file.:
this error means you need "rui.crt" file alongside with your main binary file.
if you do not have certificate see: https://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl

then copy "server.pem", "server.key" files to your binary(.exe) file directory

and run this sample code (for test):

package main

import (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;net/http&quot;
)

type server struct {
}

func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, &quot;*Hello World\n*&quot;)
}

func main() {
	if err := http.ListenAndServeTLS(&quot;:443&quot;, &quot;server.pem&quot;, &quot;server.key&quot;, server{}); err != nil {
		log.Fatal(err)
	}
}

then open web page: https://127.0.0.1/
if firewall poped up say yes,
if you see There is a problem with this website’s security certificate. say continue (advance).

output:

*Hello World
*

huangapple
  • 本文由 发表于 2016年7月20日 01:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/38464730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定