donderdag 19 februari 2015

The Server

The server will be standard Apache. Combined with the WSGI framework this will give a fairly standard python based webserver configuration. I actually already wrote a blog post on how to set this up. This post however stops at serving the 'Hello World' and some additional steps will be required to serve both the web pages as to process the data requests.
First I added the following to the httpd.conf file to configure Apache:

<VirtualHost *:8081>

    ServerAlias example.com
    ServerAdmin cees@nortek-bv.nl

    DocumentRoot c:/DATA/xampp/apache/htdocs

    <Directory c:/DATA/xampp/apache/htdocs>
     AuthType Basic
     AuthName "Scour Server Login"
     AuthUserFile c:/DATA/xampp/apache/passwords/.htpasswd
     AllowOverride all
     Require valid-user
    </Directory>

    WSGIScriptAlias /get_data c:/DATA/xampp/wsgi/get_data.wsgi

    <Directory c:/DATA/xampp/wsgi>
     Require all granted
    </Directory>

</VirtualHost>

# Python WSGI interface module
<IfModule wsgi_module>
  WSGIPythonHome "C:/Python27"
</IfModule>


Putting the rules in a 'VirtualHost' section  allows us to configure differnent setups on other ports if required. The 'AuthType', 'AuthUserFile' and  'Require valid-user' enable a user-name / password security for the site. User-name and Password have to be entered in the '.htpasswd' file using the htpasswd utility. (Check the Authentication page in the Apache docs for details)
By setting the WSGIScriptAlias "/get_data" to the actual program path I can call the program by simply going to the URL www.my_site.com/get_data.
A very nice basic instruction on how to use the WSGI module and to process the requests is given on the WSGI Tutorial pages . What is particularly nice is the example where the program shows all values in the 'environ' dictionary of the WSGI application:

COMSPEC: C:\WINDOWS\system32\cmd.exe
CONTEXT_DOCUMENT_ROOT: C:/DATA/xampp/apache/htdocs
CONTEXT_PREFIX: 
DOCUMENT_ROOT: C:/DATA/xampp/apache/htdocs
GATEWAY_INTERFACE: CGI/1.1
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
HTTP_ACCEPT_ENCODING: gzip, deflate
HTTP_ACCEPT_LANGUAGE: nl,en-US;q=0.7,en;q=0.3
HTTP_CONNECTION: keep-alive
HTTP_HOST: localhost:8081
HTTP_USER_AGENT: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
PATHEXT: .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
PATH_INFO: /
PATH_TRANSLATED: C:\DATA\xampp\apache\htdocs\index.html
QUERY_STRING: age=10&hobbies=software&hobbies=tunning
REMOTE_ADDR: ::1
REMOTE_PORT: 52609
REQUEST_METHOD: GET
REQUEST_SCHEME: http
REQUEST_URI: /myapp/?age=10&hobbies=software&hobbies=tunning
SCRIPT_FILENAME: C:/DATA/xampp/wsgi/hello_world.wsgi
SCRIPT_NAME: /myapp
SERVER_ADDR: ::1
SERVER_ADMIN: cees@nortek-bv.nl
SERVER_NAME: localhost
SERVER_PORT: 8081
SERVER_PROTOCOL: HTTP/1.1
SERVER_SIGNATURE: 
SERVER_SOFTWARE: Apache/2.4.12 (Win64) mod_wsgi/3.4 Python/2.7.9
SystemRoot: C:\WINDOWS
WINDIR: C:\WINDOWS
mod_wsgi.application_group: Lenovo-Cees.nortek.local:8081|/myapp
mod_wsgi.callable_object: application
mod_wsgi.enable_sendfile: 0
mod_wsgi.handler_script: 
mod_wsgi.input_chunked: 0
mod_wsgi.process_group: 
mod_wsgi.request_handler: wsgi-script
mod_wsgi.script_reloading: 1
mod_wsgi.version: (3, 4)
wsgi.errors: <mod_wsgi.Log object at 0x0000000004054DF0>
wsgi.file_wrapper: <built-in method file_wrapper of mod_wsgi.Adapter object at 0x0000000002A4BC60>
wsgi.input: <mod_wsgi.Input object at 0x0000000002A52470>
wsgi.multiprocess: False
wsgi.multithread: True
wsgi.run_once: False
wsgi.url_scheme: http
wsgi.version: (1, 0)

That's a wealth of information ! When you look at this you immediately get a grip on the power of the WSGI framework.

Debugging the wsgi app and Apache

Debugging is harder than normal. Since the wsgi script only runs when called by the server there is no 'real  time' debugging information. Fortunately apache writes all error messages to a file: error.log so it is at least possible to see what goes wrong when you get a server error. What's annoying is that you have to re-open this file to see the changes. Looking for a solution to this I found the Document Monitor plugin for Notepad++. Just go to the 'Plugins->Plugin Manager' and select 'Document Monitor'. When activated on the 'error.log' file it will now update continuously when new lines are added to the file.


Geen opmerkingen:

Een reactie posten