R
RunPod10mo ago
akx

TCP Port Not Working

I started a runpod instance and specifed TCP port, I got something like this Public IP: <iphere> Internal: 22 External: 35292 Internal: 35293 External: 35293 Internal: 35294 External: 35294 I create a simple app to test out
from flask import Flask

app = Flask(__name__)

@app.route('/hello', methods=['GET'])
def hello_world():
return {"text": "hello"}

if __name__ == '__main__':
app.run(debug=True, port=35293)
from flask import Flask

app = Flask(__name__)

@app.route('/hello', methods=['GET'])
def hello_world():
return {"text": "hello"}

if __name__ == '__main__':
app.run(debug=True, port=35293)
Now curl http://127.0.0.1:35293/hello is working but if I change local host with public ip and same port 35293 it gets connection refused
Solution:
The issue here is that you need to bind to 0.0.0.0 and not the default of 127.0.0.1 to access the public IP.
Jump to solution
12 Replies
justin
justin10mo ago
@akx I was able to get it working with gunicorn just went through it now
justin
justin10mo ago
No description
justin
justin10mo ago
No description
justin
justin10mo ago
No description
justin
justin10mo ago
No description
No description
justin
justin10mo ago
Also had trouble getting it to work with flask just not, even setting the host to 0.0.0.0 If you want my requirements.txt:
blinker==1.7.0
click==8.1.7
Flask==3.0.2
gunicorn==21.2.0
h11==0.14.0
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.5
packaging==23.2
typing_extensions==4.9.0
uvicorn==0.27.1
Werkzeug==3.0.1
blinker==1.7.0
click==8.1.7
Flask==3.0.2
gunicorn==21.2.0
h11==0.14.0
itsdangerous==2.1.2
Jinja2==3.1.3
MarkupSafe==2.1.5
packaging==23.2
typing_extensions==4.9.0
uvicorn==0.27.1
Werkzeug==3.0.1
Commands ran:
... made my virtual env
source myenv/bin/activate
gunicorn -w 4 -b 0.0.0.0:8000 main:app
... made my virtual env
source myenv/bin/activate
gunicorn -w 4 -b 0.0.0.0:8000 main:app
And then just visited:
http://194.68.245.26:22118/hello
http://194.68.245.26:22118/hello
justin
justin10mo ago
https://docs.runpod.io/pods/configuration/expose-ports Was able to do the HTTP part for flask, but not for TCP, which made me confused, so I tried gunicorn wondering if that was maybe the issue
Expose ports | RunPod Documentation
Learn to expose your ports.
justin
justin10mo ago
since they used something called univorn, was thinking could be idk why, I thought that something like: app.run(debug=True, host='0.0.0.0', port ... would work, but it didn't.) Which is why i used gunicorn. also i switched it to an 8000 port, didn't test it with 35XXX as u did my second time around just to follow the documentation tutorial, but i dont see why that would make a difference. tbh theoretically in my head, flask and gunicorn should have both worked, but i guess just to let you know what i tried I think.. the code i ended up running the flask server with was a: main.py:
from flask import Flask

app = Flask(__name__)

@app.route('/hello', methods=['GET'])
def hello_world():
return {"text": "hello"}

if __name__ == '__main__':
app.run(debug=True, port=8000)
from flask import Flask

app = Flask(__name__)

@app.route('/hello', methods=['GET'])
def hello_world():
return {"text": "hello"}

if __name__ == '__main__':
app.run(debug=True, port=8000)
But i just turned off my server before i forgot to grab it. but i basically just copied from u and kept bouncing back and forth with chatgpt, which is how i got to gunicorn.
akx
akxOP10mo ago
Thanks I'll try this. I have previously used fastapi on runpod with tcp and it did worked. Don't remember if I also tried flask earlier. This time, both didn't worked. I'll try gunicorn
Solution
ashleyk
ashleyk10mo ago
The issue here is that you need to bind to 0.0.0.0 and not the default of 127.0.0.1 to access the public IP.
ashleyk
ashleyk10mo ago
Changing the application is not a proper solution. Flask, FastAPI etc should all work but most of these applications bind to 127.0.0.1 by default and you specifically have to specify to bind to 0.0.0.0.
akx
akxOP10mo ago
You are right. Thanks
Want results from more Discord servers?
Add your server