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

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

问题描述

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

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

$ p $ s $ socket $ socket $ {$ s $ socket $ _ $ s $ ()
s.close()

超时后,heroku应用程序在第二行失败。当我在我的笔记本电脑上或者朋友的笔记本电脑上运行相同的东西时(虽然在这两种情况下,作为服务器的python脚本都在我的笔记本电脑上运行)。有谁知道为什么heroku会有这个问题?在Heroku上运行时,你的服务器应该绑定到环境变量PORT中指定的端口(比如说7880,只是为了这个讨论的缘故)。它不能保证是80,5000,8000,8080或其他任何东西。

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



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



所以你不能直接在Heroku上编写一个简单的套接字应用程序,直接连接它,你必须使用HTTP作为你的传输。


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.

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

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!

解决方案

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.

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.

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.

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,烧瓶和python套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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