错误描述
如果设置的端口号小于1000就会报错没有权限
2022-03-29T10:23:16.651636+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T10:23:28.994173+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T10:23:30.091143+00:00 app[web.1]: Traceback (most recent call last):
2022-03-29T10:23:30.091204+00:00 app[web.1]: File "/code/server3.py", line 247, in <module>
2022-03-29T10:23:30.091205+00:00 app[web.1]: serverSocket.bind(("127.0.0.1", serverPort))
2022-03-29T10:23:30.091208+00:00 app[web.1]: PermissionError: [Errno 13] Permission denied
2022-03-29T10:23:30.234919+00:00 heroku[web.1]: Process exited with status 1
2022-03-29T10:23:30.279569+00:00 heroku[web.1]: State changed from starting to crashed
如果将端口号设置为8080,就会导致在60秒内连接不上
2022-03-29T10:53:34.400924+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T10:53:49.432473+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T10:53:33.432308+00:00 app[api]: Release v9 created by user ***@icloud.com
2022-03-29T10:53:33.432308+00:00 app[api]: Deployed web (350d1bd5740a) by user ***@icloud.com
2022-03-29T10:54:49.500818+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2022-03-29T10:54:49.587777+00:00 heroku[web.1]: Stopping process with SIGKILL
2022-03-29T10:54:49.800462+00:00 heroku[web.1]: Process exited with status 137
如果动态引用环境变量中的端口号,就会获取失败
2022-03-29T11:12:03.984493+00:00 heroku[web.1]: State changed from crashed to starting
2022-03-29T11:12:19.984320+00:00 heroku[web.1]: Starting process with command `python /code/server3.py`
2022-03-29T11:12:21.706887+00:00 heroku[web.1]: Process exited with status 1
2022-03-29T11:12:21.536737+00:00 app[web.1]: Traceback (most recent call last):
2022-03-29T11:12:21.536756+00:00 app[web.1]: File "/code/server3.py", line 248, in <module>
2022-03-29T11:12:21.536757+00:00 app[web.1]: serverSocket.bind(("0.0.0.0", serverPort))
2022-03-29T11:12:21.536757+00:00 app[web.1]: TypeError: an integer is required (got type NoneType)
2022-03-29T11:12:21.769096+00:00 heroku[web.1]: State changed from starting to crashed
本机地址无论不写 serverSocket.bind(("", serverPort)) 还是写127.0.0.1 serverSocket.bind((“127.0.0.1”, serverPort)) 还是写0.0.0.0 serverSocket.bind((“0.0.0.0”, serverPort)) 都不行
Python项目解决方案
需要动态绑定端口号
Dockerfile中要这样写,传入$POST
FROM python:3.4
ADD ./pythonProject /code
WORKDIR /code
CMD python /code/server3.py $PORT
Python文件书写格式,在server.py中要通过参数获取
serverPort = int(sys.argv[1])
完整结构如下
if __name__ == '__main__':
serverSocket = socket(AF_INET, SOCK_STREAM)
serverPort = int(sys.argv[1])
serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serverSocket.bind(("", serverPort))
serverSocket.listen(5)
print('The server is running')
while True:
connectionSocket, addr = serverSocket.accept()
connectionSocket.settimeout(600)
_thread.start_new_thread(process, (connectionSocket,))
NodeJs项目解决方案
config.port = process.env.PORT
app.listen(config.port, () => {
logger.info('Listening on port %d', config.port);
});
或者
.listen(process.env.PORT || 5000)
或者
production: {
server: {
host: '0.0.0.0',
port: process.env.PORT
}
}
|