Quang Van
Quang Van

Reputation: 12095

Python websocket support on Azure web appservice?

Does Azure appservice has native websockets for Python like they do for node.js/.net?

I'm assuming as of right now, the answer is no, and you'd need to use a VM to achieve this?

(fyi. there's a similar question here but it's been deleted.)

Upvotes: 3

Views: 2735

Answers (2)

Christian Vorhemus
Christian Vorhemus

Reputation: 2663

When using Python, Azure App Service on Linux by default uses Gunicorn as webserver for all incoming requests. WebSocket connections start with a special HTTP GET request containing an "Upgrade" header which must be handled accordingly by the server. There are a few WSGI compatible WebSocket libraries out there, for this example I'm using geventwebsocket

First, create a new Azure App Service Plan + Service:

az appservice plan create -g <ResourceGroupName> -n MyAppPlan --is-linux --number-of-workers 4 --sku S1
az webapp create -g <ResourceGroupName> -p MyAppPlan -n <AppServiceName> --runtime "PYTHON|3.7

Save the following sample to server.py:

from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler

def websocket_app(environ, start_response):
    if environ["PATH_INFO"] == '/echo':
        ws = environ["wsgi.websocket"]
        while not ws.closed:
            message = ws.receive()
            ws.send(message)

Create a file requirements.txt with the following content

gevent
gevent-websocket

Create a file .deployment with the following content

[config]
SCM_DO_BUILD_DURING_DEPLOYMENT = true

Put all three files in a zip folder upload.zip and deploy it to Azure

az webapp deployment source config-zip -g <ResourceGroupName> -n <AppServiceName> --src upload.zip

Set the startup command, we tell Gunicorn here to use a GeventWebSocketWorker for requests and serve the application in the file server.py, function name websocket_app.

az webapp config set -g <ResourceGroupName> -n <AppServiceName> --startup-file "gunicorn --bind=0.0.0.0 -k "geventwebsocket.gunicorn.workers.GeventWebSocketWorker" server:websocket_app"

Enable WebSockets in Azure

az webapp config set -g <ResourceGroupName> -n <AppServiceName> --web-sockets-enabled true

After startup, you should now be able to send requests to the server and get an echo response (assuming the Python websockets package is installed - pip install websockets)

python -m websockets ws://<AppServiceName>.azurewebsites.net/echo

Upvotes: 0

Peter Pan
Peter Pan

Reputation: 24138

The answer if yes, Python websocket support on Azure Web Apps. The necessary steps or guideline as below.

  1. First of all, you need to enable the WEB SOCKETS option of Application settings to ON on Azure portal, as the blog said as below, it's matter with any languages.

enter image description here

  1. Azure IIS supports Python webapp using WSGI, you can refer to the tutorial to know it and follow the tutorial content to build & configure your Python webapp with WSGI.

  2. There is a similar Combining websockets and WSGI in a python app SO thread which had been answered about the feasibility for websocket with WSGI in Python. And as references, there are some packages supported this combination, such as Eventlet, dwebsocket for Django, etc that you can search the words websocket & wsgi to know more.

Hope it helps.

Upvotes: 3

Related Questions