Forums

Receiving TCP connections - which port to use?

I have set up Flask to receive TCP connections to receive data. Is there any specific port I need to use when releasing to Python Anywhere? I'm currently using 8081 for my tests in local.

You cannot use Flask to accept TCP connection on an arbitrary port on PythonAnywhere. If you create a web app, it will use port 80 and 443 (the http and https ports)

Thank Glenn I did suspect that. So if I use port 80, would it work?

If you set it up on the "Web" page, yes -- you don't need to run Flask from the command line in a console.

Also, you shouldn't have an app.run in your code -- see this help page.

Thanks Giles. I have the follwing as a thread within if __name__ == "__main__":

if __name__ == "__main__":
     t = threading.Thread(target=launch_socket_server)
     t.daemon = True
     t.start()

But because this won't run, do you have any tutorials on how to launch the TCP server as a thread from the WSGI file?

You cannot run threads on PythonAnywhere and you cannot listen on an arbitrary port, even from a web app. Your socket server will not work.

I think we misunderstood each other. We thought that you were trying to run an ordinary Flask server on a different port, not an entirely separate server on a different port.

I'm a bit confused - bear with me as I'm a beginner. A month back I was told I could stream data to my Python server: https://www.pythonanywhere.com/forums/topic/14771/

Ah. In that case giles was talking about a situation where you had code running on PythonAnywhere that connected out and used a stream and that does work (you could, for instance, run an always on task to stream from Twitter). It's the connection into your code that is not supported and the starting of threads in web apps that's not supported.

Thanks Glenn. So if with an always-on task I set up a socket listening for data coming from another server, would that work? I would still have to specify a port, etc. What I really need is to receive some data, do some processing and save it as JSON. All this is independent from my web app. The web app will just serve the JSON to a specific URL.

Sorry about the confusion in the other thread, I misunderstood what you were asking. Does the connection for the stream come from outside? Or does your code on PythonAnywhere connect outwards to the source of the stream?