连接错误:地址在此上下文中无效
来源:stackoverflow
时间:2024-03-05 19:48:28 266浏览 收藏
怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《连接错误:地址在此上下文中无效》,涉及到,有需要的可以收藏一下
问题内容
我在 golang 上开发了一个机器人。在使用 ubuntu 20.01 操作系统的 vds 上启动它,效果很好,但当我开始调试代码时,这是一个问题。因此,我决定使用我的电脑作为 vds:我打开了 8443 端口等。但是当 main.go 启动时,我收到错误: p>
listen tcp [ip]:8443: bind: the requested address is not valid in its context.
我的代码:
package main import ( "./configuration" "./get_data" "encoding/json" "fmt" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" "io/ioutil" "net/http" "strings" "time" ) var ( newbot, boterr = tgbotapi.newbotapi(configuration.bot_token) ) func setwebhook(bot *tgbotapi.botapi) { webhookinfo := tgbotapi.newwebhookwithcert(fmt.sprintf("https://%s:%s/%s", configuration.bot_host, configuration.bot_port, configuration.bot_token), configuration.cert_file) _, err := bot.setwebhook(webhookinfo) if err != nil { fmt.println(err) } } func main () { fmt.println("ok", time.now().unix(), time.now(), time.now().weekday()) setwebhook(newbot) message := func (w http.responsewriter, r *http.request) { text, _ := ioutil.readall(r.body) var bottext = get_data.botmessage{} _ = json.unmarshal(text, &bottext) chatgroup := int64(bottext.message.chat.id) botcommand := strings.split(bottext.message.text, "@")[0] /*markup := tgbotapi.inlinekeyboardmarkup{ inlinekeyboard: [][]tgbotapi.inlinekeyboardbutton{ []tgbotapi.inlinekeyboardbutton{ tgbotapi.inlinekeyboardbutton{text: "start"}, }, }, }*/ //reply := tgbotapi.neweditmessagereplymarkup(chatgroup, messageid, markup) var msg tgbotapi.messageconfig switch botcommand { case "/start": msg = tgbotapi.newmessage(chatgroup, hellocommandmsg()) msg.replymarkup = tgbotapi.newreplykeyboard( []tgbotapi.keyboardbutton{catalogbtn.btn}, []tgbotapi.keyboardbutton{myprofilebtn.btn, supportbtn.btn}, []tgbotapi.keyboardbutton{getluckybtn.btn, rulesbtn.btn}, []tgbotapi.keyboardbutton{addfundsbtn.btn}, ) break case getluckybtn.btn.text: getluckybtn.action(bottext, newbot) break case myprofilebtn.btn.text: myprofilebtn.action(bottext, newbot) break case addfundsbtn.btn.text: addfundsbtn.action(bottext, newbot) break default: msg = tgbotapi.newmessage(chatgroup, unknowncommandmsg()) } newbot.send(msg) } http.handlefunc("/", message) errlistentls := http.listenandservetls(fmt.sprintf("%s:%s", configuration.bot_host, configuration.bot_port), configuration.cert_file, configuration.cert_key, nil) if errlistentls != nil { fmt.println(errlistentls) } }
配置/main_config.go:
package configuration const ( telegram_url = "https://api.telegram.org/bot" bot_token = mytoken bot_host = "[ip]" bot_port = "8443" )
配置/cert_config.go:
package configuration const ( cert_file = "c:\\users\\user\\desktop\\golocal\\botlocal\\certificates\\mybot.pem" cert_key = "c:\\users\\user\\desktop\\golocal\\botlocal\\certificates\\mybot.key" )
我已经生成了本主题中的证书:为 telegram webhook 创建和使用自签名证书的简单方法是什么?
此外,我尝试设置 0.0.0.0 而不是我的公共 ip,然后出现此错误:
Bad Request: bad webhook: IP address 0.0.0.0 is reserved
解决方案
错误 bind:请求的地址在其上下文中无效。
表示该地址不属于您计算机上的网络接口。可能有一个路由器/负载均衡器/等等...具有实际的公共地址,并且它将流量转发到您的计算机。
您需要拆分您使用的地址:
- 您的本地地址(请参阅
ifconfig
)或所有接口的0.0.0.0
作为传递给http.ListenAndServeTLS
的地址 - 公共地址作为
NewWebhookWithCert
中传入的回调地址
理论要掌握,实操不能落!以上关于《连接错误:地址在此上下文中无效》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习