简体   繁体   English

如何更改python flask API的本地主机URL

[英]How to change the localhost url of python flask API

I have a python flask api running on my laptop. 我的笔记本电脑上正在运行python flask API。 It starts with a localhost url. 它以localhost url开头。 Base url is below: 基本网址如下:

http://localhost:5555/ http:// localhost:5555 /

I am trying to integrate this api with a c++ project but facing few issues with port number. 我正在尝试将此api与c++项目集成,但是面临端口号的一些问题。 Is it possible to remove the port number and make the base url like below 是否可以删除端口号并使基本网址如下所示

http://localhost/

Below is the code: 下面是代码:

if __name__ == '__main__':
    import os

    HOST = os.environ.get('SERVER_HOST', 'localhost')  
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555

    app.run(HOST, PORT)

If I do app.run(HOST) it still starts with port 5000 . 如果我执行app.run(HOST)它仍然从端口5000开始。 Is it not possible to remove port number from the url. 是否无法从网址中删除端口号。 Please help. 请帮忙。 Thanks 谢谢

Browsers don't show port numbers when you're connecting to the default port for a given protocol (ie, :80 for http, :443 for https). 当您连接到给定协议的默认端口时,浏览器不会显示端口号(即,http :: 80,https :: 443)。 Running a Flask server on port 80 is doable, but depends on what Operating System you're running on. 在端口80上运行Flask服务器是可行的,但这取决于您所运行的操作系统。 It's a convention on many operating systems to reserve "low" port numbers (those under 1024) for privileged software, so you'd need to arrange to run Flask in a privileged way. 在许多操作系统上,为特权软件保留“低”端口号(1024以下的端口号)是一个惯例,因此您需要安排以特权方式运行Flask。

But you note that the issue is integrating with a C++ project, which hints that you might have a different issue. 但是您注意到该问题正在与C ++项目集成,这暗示您可能有其他问题。 If the problem you're running in to is that you're unable to make a request from C++ to a local Flask server on localhost:5000 , then the issue may be that the HTTP request that the C++ program constructs must include the header Host: localhost:5000 . 如果您遇到的问题是无法从C ++向本地主机上的Flask服务器发出请求localhost:5000 ,则问题可能是C ++程序构造的HTTP请求必须包含标头Host: localhost:5000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM