Skip to main content
 首页 » 编程设计

python之在生产服务器上工作的端口值应该是多少

2024年11月01日11Free-Thinker

在本地机器上开发时,我使用标准代码来授权和使用 Google 磁盘

creds = None 
cred = os.path.join(settings.BASE_DIR, 'credentials.json') 
# The file token.pickle stores the user's access and refresh tokens, and is 
# created automatically when the authorization flow completes for the first 
# time. 
if os.path.exists('token.pickle'): 
    with open('token.pickle', 'rb') as token: 
        creds = pickle.load(token) 
# If there are no (valid) credentials available, let the user log in. 
if not creds or not creds.valid: 
    if creds and creds.expired and creds.refresh_token: 
        creds.refresh(Request()) 
    else: 
        flow = InstalledAppFlow.from_client_secrets_file( 
            cred, SCOPES) 
        creds = flow.run_local_server(port=8080) 
    # Save the credentials for the next run 
    with open('token.pickle', 'wb') as token: 
        pickle.dump(creds, token) 

我使用 8080 作为端口参数值。这行得通。在生产服务器上部署项目时,我收到一条消息 port is already in use。

服务器:Ubuntu 18.04、Nginx、uwsgi

我应该使用什么值?

UPD

我已将端口更改为 5000,在本地计算机上一切正常。我试图在生产服务器上运行它,但出现了 504 错误。我检查了 uwsgi 日志,看到文件末尾包含授权链接。 在本地机器上,此链接会自动在新窗口中打开以登录该帐户。如果尝试再次运行生产服务器,我将得到 [Errno 98] 地址已在使用错误 并且此错误将被保存直到重新启动 uwsgi。重启后,一切又重演了。

请您参考如下方法:

尝试使用 netstat 查看正在使用的端口。

netstat -an | grep LISTEN

Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

返回的任何端口都会给您上面的错误“端口已在使用中”。 任何其他端口都应该工作。 出于安全原因,最好使用 1024 以上的端口和非特权用户。 django 经常使用8000 或8888 端口,但可以使用任何端口。

您也可以使用套接字。

端口 8078、8079 和 8080 通常由 tomcat/jetty 等应用程序服务器使用。