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

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

Making a Simple FileServer with Go and Localhost Refused to Connect

问题

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

  1. package main
  2. import (
  3. "net/http"
  4. )
  5. func main() {
  6. h := http.FileServer(http.Dir("."))
  7. http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
  8. }

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

  1. This site cant be reached.
  2. localhost refused to connect.

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

  1. F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
  2. Success: process exited with code 0.
  3. 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]
  4. Success: process exited with code 0.

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

英文:

Now I have the following code extracted from a book.

  1. package main
  2. import (
  3. "net/http"
  4. )
  5. func main() {
  6. h := http.FileServer(http.Dir("."))
  7. http.ListenAndServeTLS(":8001", "rui.crt", "rui.key", h)
  8. }

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:

  1. This site cant be reached.
  2. localhost refused to connect.

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

  1. F:/Go/bin/go.exe build -i [E:/Users/User/Desktop/codespace_v2.6.6/dev/server_side/golang/go_codespace_v2.1/server]
  2. Success: process exited with code 0.
  3. 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]
  4. 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)文件目录中,并运行以下示例代码进行测试:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. )
  7. type server struct {
  8. }
  9. func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  10. fmt.Fprint(w, "*Hello World\n*")
  11. }
  12. func main() {
  13. if err := http.ListenAndServeTLS(":443", "server.pem", "server.key", server{}); err != nil {
  14. log.Fatal(err)
  15. }
  16. }

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

输出结果为:

  1. *Hello World
  2. *
英文:

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):

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;log&quot;
  5. &quot;net/http&quot;
  6. )
  7. type server struct {
  8. }
  9. func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  10. fmt.Fprint(w, &quot;*Hello World\n*&quot;)
  11. }
  12. func main() {
  13. if err := http.ListenAndServeTLS(&quot;:443&quot;, &quot;server.pem&quot;, &quot;server.key&quot;, server{}); err != nil {
  14. log.Fatal(err)
  15. }
  16. }

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:

  1. *Hello World
  2. *

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:

确定