question

RajGohel-5813 avatar image
0 Votes"
RajGohel-5813 asked RajGohel-5813 commented

Expose python api from VM

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

TravisCragg-MSFT avatar image
1 Vote"
TravisCragg-MSFT answered RajGohel-5813 commented

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.


image.png (209.1 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for your guidance Travis Cragg.

0 Votes 0 ·