简体   繁体   中英

How to change the localhost url of python flask API

I have a python flask api running on my laptop. It starts with a localhost url. Base url is below:

http://localhost:5555/

I am trying to integrate this api with a c++ project but facing few issues with port number. 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 . 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). Running a Flask server on port 80 is doable, but depends on what Operating System you're running on. 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.

But you note that the issue is integrating with a C++ project, which hints that you might have a different issue. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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