Step 1: Windows IIS Installation - Nicholas Propes



Python – Flask – WSGI / FastCGI – Windows IISSetup InstructionsNicholas Propes, 2016Contents TOC \o "1-3" \h \z \u Step 1: Windows IIS Installation PAGEREF _Toc467009822 \h 3Step 2: Python Configuration PAGEREF _Toc467009823 \h 4Step 3: IIS Configuration PAGEREF _Toc467009824 \h 6Step 4: Add Website Bindings PAGEREF _Toc467009825 \h 7Step 5: Set responseBufferLimit to 0 (Optional) PAGEREF _Toc467009826 \h 8Step 6: Directory Access PAGEREF _Toc467009827 \h 9Step 7: Testing the Result PAGEREF _Toc467009828 \h 10Step 8: Flask Integration PAGEREF _Toc467009829 \h 11Step 9: FastCGI Activity Timeout PAGEREF _Toc467009830 \h 12Step 1: Windows IIS Installation In Windows 7, go to “Control Panels” “Programs and Features” “Turn Windows features on or off”Make sure “Internet Information Services” is selected and that “CGI” is also selected under “World Wide Web Services” “Application Development Features” “CGI”Click “OK” button at the bottom right.Step 2: Python ConfigurationDownload and install the version of Python (32-bit or 64-bit) you need from . Recommended: Install Python 2.7.x instead of Python 3.x since many older libraries are not yet supported by Python 3.x.Open a command window, and install the wfastcgi tool using the command “pip install wfastcgi”. wfastcgi provides a bridge between IIS and Python using WSGI and FastCGI.“pip install <package name>” other packages you require or use binaries located at this link: To install the wheel file downloaded from the website use “pip install <wheel file path>” Primarily, you will use wheel files for numpy and scipy.Open a command window as an administrator, and then type “wfastcgi-enable” Let us suppose we have the following python function we wish to call when a website is loaded. Save this file to “test.py” under your website directory (e.g. “C://inetpub/wwwroot”) Note the signature of the function is for WSGI / FASTCGI.def app(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) s = 0 for i in range(1,101): s = s + i HTMLmessage = 'Hello World<br> The sum of all numbers between 1 and 100 is ' + str(s) return [HTMLmessage]Now create a “web.config” such as the following. This provides some configuration information for IIS. Put this file in the same directory as “test.py”<configuration> <system.webServer> <handlers> <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\python27-64\python.exe|C:\python27-64\lib\site-packages\wfastcgi.pyc" resourceType="Unspecified" requireAccess="Script" responseBufferLimit="0" /> </handlers> </system.webServer> <appSettings> <add key="WSGI_HANDLER" value="test.app" /> <add key="PYTHONPATH" value="C:\inetpub\wwwroot" /> </appSettings></configuration>The items in this file you might change are:scriptProcessor = <path to python.exe> | <path to wfastcgi.pyc>PYTHONPATH = <path to the location of your .py files>WSGI_HANDLER = <filename>.<function_name>.wsgi_appStep 3: IIS ConfigurationLaunch the Internet Information Services (IIS) Manager.Navigate to the website under “Connections” on the left side of IIS Manager and click on “Add Module Mapping…” on the right side of the IIS Manager. Enter the information as shown below. Alter the Executable (optional): text box if the python.exe and the wfastcgi.pyc are located in a different place (<path to python.exe> | <path to wfastcgi.pyc>)Step 4: Add Website BindingsTo allow others to access your website you can add your website to the binding. Open the Internet Information Services (IIS) Manager and then navigate under connections to the website. On the right side, click on “Bindings…” Add the binding IP for your website (usually the computer’s IP will already be provided by the selection).Step 5: Set responseBufferLimit to 0 (Optional)The IIS server buffers the information for a website page before actually sending it to a client’s webbrowser. You have to set this buffer to 0 if you want the client’s webbrowser to immediately see any updates to the webpage being sent to it. In the IIS Manger, under “Management”, select “Configuration Editor”;Under “Section”, enter ‘system.webServer/handlers’;Next to “(Collection)” click “…” OR mark the element “(Collection)” and, under “Actions” und ‘(Collection)’ Element, click “Edit Items”;Scroll down until you find “Python FastCGI” version under “Name”;At the bottom, the Properties are shown an can be edited manually, including responseBufferLimit, which should be set to 0.Step 6: Directory Access1. Select Feature Delegation2. Set CGI and Handler Mappings to Read/WriteStep 7: Testing the ResultOpen a webbrowser to the IP you binded in step 5. You should see the result below: Step 8: Flask Integration1. To integrate with Flask. You simply have to point the WSGI_HANDLE in the web.config file to the flask app object reference’s wsgi_app function. Simple example code is demonstrated below.Change the code for test.py in Step 2, #4 to the following:from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world(): result = 0 for i in range(1,101): result = result + i s = '' s += '<html><head><h1>Hello World!</h1></head><body>' s += 'The sum of numbers between 1 and 100 is ' + str(result) s += '</body></html>' return sif __name__ == '__main__': app.run(debug=False)Change the web.config file information to the following:<configuration> <system.webServer> <handlers> <add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="C:\python27-64\python.exe|C:\python27-64\lib\site-packages\wfastcgi.pyc" resourceType="Unspecified" requireAccess="Script" responseBufferLimit="0" /> </handlers> </system.webServer> <appSettings> <add key="WSGI_HANDLER" value="test.app.wsgi_app" /> <add key="PYTHONPATH" value="C:\inetpub\wwwroot" /> </appSettings></configuration>Now test your webpage (e.g. )Step 9: FastCGI Activity TimeoutSometimes you want to have a longer timeout especially for long processing times or else the data will stop being sent to the clients browser prematurely.1. Select FastCGI settings.2. Double click on the Python link.3. Set the Activity Timeout to a preferred value in seconds.Step 10: Request FilteringSecurity settings on IIS have something called Request Filtering that limits how much data you can upload, length of URLs, etc. This may prevent your application from loading all the data you need. To change these settings follow the steps below:1. Launch IIS Manager and double click on Request Filtering for your website.2. Click on “Edit Feature Settings…” on the far right.3. Edit the settings as required. There is a max limit on these allowed. I believe the maximum upload is around 4 GB. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download