Heroku、flask 和 python 套接字? [英] Heroku, flask, and python sockets?

查看:24
本文介绍了Heroku、flask 和 python 套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在我的电脑上运行的 python 脚本.它打开一个套接字并打印它接收到的任何内容.这绝对有效 - 我已经设法从其他计算机连接到它并向其发送数据.

I have a python script that runs on my computer. It opens a socket and prints anything it receives. This definitely works -- I've managed to connect to it from other computers and send it data.

问题是我的 heroku 应用程序无法连接到套接字.

The problem is that my heroku app fails to connect to the socket.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    
s.connect((daemon_socket_vars['host'], daemon_socket_vars['port']))
s.send("Hi!")
s.close()

超时后,heroku 应用程序在第二行失败.当我在我的笔记本电脑或朋友的笔记本电脑上运行相同的东西时(在这两种情况下,充当服务器的 python 脚本都在我的笔记本电脑上运行)它可以工作.有谁知道为什么heroku会遇到这个问题?谢谢!

The heroku app fails on the second line after timing out. When I run something identical on either my laptop, or a friend's laptop (while the python script that's acting as the server is running on my laptop in both cases) it works. Does anyone know why heroku would have problems with this? Thanks!

推荐答案

在 Heroku 上运行时,您的服务器应该绑定到环境变量 PORT 中指定的端口(比如 7880,只是为了讨论).不保证是 80、5000、8000、8080 或其他任何值.

When running on Heroku, your server should bind to port specified in the environment variable PORT (say 7880, just for the sake of this discussion). It is not guaranteed to be 80, 5000, 8000, 8080, or anything else.

然而,对于外部世界,这将显示为端口 80 或端口 443.也就是说,如果从 Heroku 外部连接,您的客户端将连接到端口 80.

To the outside world, however, this will appear as port 80 or port 443. That is, if connecting from outside of Heroku, your client will be connecting to port 80.

最后一个警告:当从 Heroku 外部连接时,您的客户端将通过Heroku 路由网格",其中包括 80--> 端口翻译".问题是,路由网格是一个 HTTP 路由网格:它只接受传入的 HTTP 请求,并将它们路由(在有时更改它们之后,例如添加标头等)到您的 dyno.

One final caveat: when connecting from outside Heroku, your client will go thru the "Heroku Routing Mesh", which among other things does the 80-->something port "translation". The thing is, the routing mesh is an HTTP routing mesh: it will only accept incoming HTTP requests, and will route them (after sometimes altering them, like adding headers etc.) to your dyno.

所以你不能只在 Heroku 上编写一个普通的套接字应用程序并直接连接到它,你必须使用 HTTP 作为你的传输.

So you can't just write a plain-sockets app on the Heroku and connect to it directly, you'll have to use HTTP as your transport.

这篇关于Heroku、flask 和 python 套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆