├── README.md ├── host.json ├── lib └── AzureHTTPHelper.py └── myfunction1 ├── function.json └── run.py /README.md: -------------------------------------------------------------------------------- 1 | ![Azure Functions + Python HTTP](http://mediarealm.com.au/wp-content/uploads/2016/05/Azure-Functions-Python-HTTP.png) 2 | 3 | # Azure Functions + Python HTTP Example Code 4 | *Example code and helper class for Azure Functions (written in Python).* 5 | 6 | This example code is designed to demonstrate how to use Azure Functions with Python for HTTP Triggers. It demonstrates: 7 | 8 | * Receiving GET (Query String) data 9 | * Receiving POST data 10 | * Sending HTTP Response Status Codes 11 | * Sending body response data 12 | * Sending arbitrary HTTP response headers 13 | * Viewing all system environmental variables 14 | 15 | Included is a simple HTTP Helper Class to parse the input data. This should make code changes easier when the Azure Functions team introduce a new interface for Python. 16 | 17 | See also: http://mediarealm.com.au/articles/2016/05/azure-functions-python-http-example-code/ 18 | -------------------------------------------------------------------------------- /host.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": "a2a38496ceee4d11b8bc4e71788ac609" 3 | } 4 | -------------------------------------------------------------------------------- /lib/AzureHTTPHelper.py: -------------------------------------------------------------------------------- 1 | """ 2 | Azure Functions HTTP Helper for Python 3 | 4 | Created by Anthony Eden 5 | http://MediaRealm.com.au/ 6 | """ 7 | 8 | import os 9 | import urlparse 10 | 11 | 12 | class HTTPHelper(object): 13 | 14 | def __init__(self): 15 | self._headers = {} 16 | self._query = {} 17 | self._env = {} 18 | 19 | for x in os.environ: 20 | if x[:12] == "REQ_HEADERS_": 21 | self._headers[x[12:].lower()] = os.environ[x] 22 | 23 | elif x[:10] == "REQ_QUERY_": 24 | self._query[x[10:].lower()] = os.environ[x] 25 | 26 | else: 27 | self._env[x.lower()] = str(os.environ[x]) 28 | 29 | @property 30 | def headers(self): 31 | return self._headers 32 | 33 | @property 34 | def get(self): 35 | return self._query 36 | 37 | @property 38 | def env(self): 39 | return self._env 40 | 41 | @property 42 | def post(self): 43 | postData = open(os.environ['req'], "r").read() 44 | postDataParsed = urlparse.parse_qs(postData) 45 | return postDataParsed -------------------------------------------------------------------------------- /myfunction1/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "bindings": [ 3 | { 4 | "webHookType": "", 5 | "name": "req", 6 | "type": "httpTrigger", 7 | "direction": "in", 8 | "authLevel": "anonymous" 9 | }, 10 | { 11 | "name": "res", 12 | "type": "http", 13 | "direction": "out" 14 | } 15 | ], 16 | "disabled": false 17 | } -------------------------------------------------------------------------------- /myfunction1/run.py: -------------------------------------------------------------------------------- 1 | """ 2 | Azure Functions HTTP Example Code for Python 3 | 4 | Created by Anthony Eden 5 | http://MediaRealm.com.au/ 6 | """ 7 | 8 | import sys, os 9 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'lib'))) 10 | 11 | import json 12 | from AzureHTTPHelper import HTTPHelper 13 | 14 | # This is a little class used to abstract away some basic HTTP functionality 15 | http = HTTPHelper() 16 | 17 | # All these print statements get sent to the Azure Functions live log 18 | print "--- GET ---" 19 | print http.get 20 | print 21 | 22 | print "--- POST ---" 23 | print http.post 24 | print 25 | 26 | print "--- HEADERS ---" 27 | print http.headers 28 | print 29 | 30 | print "--- OTHER ENVIRONMENTAL VARIABLES ---" 31 | for x in http.env: 32 | print x 33 | print 34 | 35 | 36 | # All data to be returned to the client gets put into this dict 37 | returnData = { 38 | #HTTP Status Code: 39 | "status": 200, 40 | 41 | #Response Body: 42 | "body": "

Azure Works :)

", 43 | 44 | # Send any number of HTTP headers 45 | "headers": { 46 | "Content-Type": "text/html", 47 | "X-Awesome-Header": "YesItIs" 48 | } 49 | } 50 | 51 | # Output the response to the client 52 | output = open(os.environ['res'], 'w') 53 | output.write(json.dumps(returnData)) --------------------------------------------------------------------------------