Expose python api from VM

Raj Gohel 21 Reputation points
2021-04-21T13:53:12.577+00:00

I have azure VM (ubuntu 18.04) and i need to expose python api. My python code as shown below

from flask import Flask, request
import AskQuestions
import json
app = Flask(__name__)
@app.route('/answer', methods=['POST'])
def getAnswer():
    requestData = request.form
    print(requestData)
    return AskQuestions.getAnswer(requestData)
if __name__ == '__main__':
    app.run(host='localhost', port=8080)

Currently python server is running on http://localhost:8080.Can anyone guide me how can i give host to my VM url? Or I need a url which run my python api code for example from postman i can hit url and it runs my python code and send back a response.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,013 questions
0 comments No comments
{count} votes

Accepted answer
  1. TravisCragg-MSFT 5,696 Reputation points Microsoft Employee Moderator
    2021-04-21T21:51:24.223+00:00

    To expose your outbound ports, you will need to allow the traffic through any NSGs that could be affecting the traffic.

    Doing this is quite easy in the portal. Navigate to your VM and select the 'Networking' tab from the left menu. This will bring up networking information and your NSG rules that effect your VM. In the example you gave above, you are hosting the server on port 8080, so you need to allow port 8080 inbound. An outbound rule is not needed, NSG rules for TCP are only needed for the establishment of a connection.

    90087-image.png

    select the 'Add Inbound Port Rule', and add a rule with a high priority (a higher number than any existing rules that might block traffic to port 8080) that allows any TCP inbound traffic on port 8080.

    Once that is done, you should be able to connect via the public IP Address to port 8080 and access your API server.

    If you would like to also include a DNS Name, you can do that via the Public IP Address (#3 on the image). Select the Public IP, and then go to 'Configuration' on the left menu. You can set a custom DNS prefix there, and then you will be able to get a DNS name to your VM that you will be able to give to others to connect to your VM on port 8080. An example of this once set would be

    yourdnsprefix.centralus.cloudapp.azure.com:8080

    You might also need to allow port 8080 on your OS firewall. Please let me know if you have any additional questions.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.