3

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!

bee
  • 216
  • 1
  • 13
  • 1
    IIRC, Heroku only allows external connections from ports 80 and 443. – Blender Apr 20 '13 at 20:11
  • When testing on another machine, did you use the same script? – timss Apr 20 '13 at 20:24
  • Should limits on which port it can connect from affect which ports it can connect _to_? The socket on my laptop is listening on port 50000. I don't specify which port the heroku app should use. The python script on all machines is the same. – bee Apr 20 '13 at 20:53

1 Answers1

3

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.

Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54